1 # $XConsortium: shipfeature.sh /main/2 1995/07/19 17:09:46 drk $
3 # Glenn Fowler & Phong Vo
4 # AT&T Bell Laboratories
6 # C feature test support
7 # @(#)feature (AT&T Bell Laboratories) 03/01/94
9 # NOTE: .exe a.out suffix and [\\/] in path patterns for dos/nt
14 binding="-Bdynamic -Bstatic -Wl,-ashared -Wl,-aarchive -call_shared -non_shared"
28 $executable . 2>/dev/null || executable='test -r'
37 ""|?|??|???|????|????)
41 ksh) tmp=${HOSTNAME%${HOSTNAME#????}} ;;
42 *) eval `echo $HOSTNAME | sed 's/\(....\).*/tmp=\1/'` ;;
54 ksh) tmp=${tmp%${tmp#?????????}} ;;
55 *) eval `echo $tmp | sed 's/\(.........\).*/tmp=\1/'` ;;
66 # standard error to /dev/null unless debugging
67 # standard output to the current output file
69 # stdout original standard output
70 # stderr original standard error
71 # nullin /dev/null input
72 # nullout /dev/null output
74 stdout=5 stderr=6 nullin=7 nullout=8
75 eval "exec $nullin>/dev/null $nullout>/dev/null $stdout>&1 $stderr>&2 2>&$nullout"
77 # options -- `-' for output to stdout otherwise usage
88 -*) echo "Usage: $command [-] [op [arg ... [-l* *.a *.sh * ...]] [: op ...]" >&$stderr
97 # tmp files cleaned up on exit
98 # status: 0:success 1:failure 2:interrupt
101 trap "rm -f $tmp*.*; exit \$status" 0 1 2
103 # loop on op [ arg [ ... ] ] [ : op [ arg [ ... ] ] ]
112 *) read lin || lin="set out +"
118 # check if "run xxx" is equivalent to "set in xxx"
125 */*) x=`echo $2 | sed -e 's,.*[\\\\/],,'` ;;
137 # set drops out early
180 export `echo $x | sed -e 's/=.*//'`
199 ""|1) exec 2>&$stderr
205 ksh) PS4="${PS4%+*([ ])}+\$LINENO+ "
210 *) echo "$command: $arg: debug levels are 0, 1, 2" >&$stderr
220 then echo "$command: $in: not found" >&$stderr
228 ksh) out=${in##*[\\/]}
231 *) eval `echo $in | sed -e 's,.*[\\\\/],,' -e 's/\.[^.]*//' -e 's/^/out=/'`
244 nodebug)exec 2>&$nullout
254 prototyped|noprototyped)
257 pragma) pragma="$pragma $arg"
265 *) echo "$command: $op: unkown option" >&$stderr
321 "") echo "$command: missing }end" >&$stderr
343 else echo "$command: missing }end" >&$stderr
372 hdr) hdr="$hdr $1" ;;
373 lib) lib="$lib $1" ;;
380 ref) occ="$occ $1" ;;
381 run) opt="$opt $1" ;;
402 # check the candidate headers
417 *.h) echo "#include \"$x\"" > $tmp.c
418 $cc -E $tmp.c <&$nullin >&$nullout || continue
420 *) test -r $x || continue
432 # check the candidate libraries
438 echo "main(){return(0);}" > $tmp.c
449 if $cc -o $tmp.exe $tmp.c $e $x <&$nullin >&$nullout
450 then lib="$lib $e $x"
466 # precheck some more ops
469 ref) deflib="$deflib $lib"
470 defhdr="$defhdr $hdr"
480 # check for op aliases
485 def|default) x="$x cmd hdr key lib mth sys typ" ;;
490 # loop on the ops o and args a
507 *) eval `echo $a | sed -e "s/\\(.*\\)\\\\${x}\\(.*\\)/p=\\1 v=\\2/"`
517 *) eval `echo $a | sed -e 's,.*[\\\\/],,' -e 's/\(.*\)\.\(.*\)/p=\1 v=\2/'`
541 # check output redirection
554 *[\\/]*|*.h) x=$cur ;;
558 sed 's,/\*[^/]*\*/, ,g' $tmp.h > $tmp.c
559 sed 's,/\*[^/]*\*/, ,g' $x > $tmp.t
560 if cmp -s $tmp.c $tmp.t
563 ${dir}[\\/]$cur) test -d $dir || mkdir $dir || exit 1 ;;
576 -) eval "exec >&$stdout"
583 *[\\/]*|???????????????*) cur=$v ;;
599 echo "/* : : generated$x by $command version $version : : */"
620 *) eval `echo $in | sed -e 's,\.,_,g' -e 's/^/x=/'`
633 ksh) c=${PWD##*[\\/]}
638 *) x=${x}_`echo $x | sed -e 's,.*[\\\\/],,' -e 's,^lib,,' -e 's,\..*,,'`
643 x=`echo $x | sed -e 's/[^a-zA-Z0-9_]/_/g'`
644 echo "#ifndef _def${x}"
645 echo "#define _def${x} 1"
653 # set up the candidate include list
656 for x in $defhdr $hdr
662 */*) c=${x%%[\\/]*}_${c} ;;
665 *) eval `echo $x | sed -e 's,^\([^\\\\/]*\).*[\\\\/]\([^\\\\/]*\)\$,\1_\2,' -e 's/\..*//' -e 's/^/c=/'`
671 *) gothdr="$gothdr $x"
672 echo "#define _inc_${c} 1 /* candidate header <$x> found */"
681 # set up the candidate lib list
683 for x in $lib $deflib
684 do case " $gotlib " in
687 *) gotlib="$gotlib $x"
688 echo "/* candidate library $x found */"
693 # src overrides builtin test
710 (eval "$src") <&$nullin || e=1
713 if $cc -E $tmp.c <&$nullin >$tmp.i
714 then sed -e '/<<[ ]*".*"[ ]*>>/!d' -e 's/<<[ ]*"//g' -e 's/"[ ]*>>//g' $tmp.i
718 p*|nop*)$cc -DTEST=$p -DID=$v -E $tmp.c <&$nullin >&$nullout || e=1
720 c*|noc*)$cc -DTEST=$p -DID=$v -c $tmp.c <&$nullin >&$nullout || e=1
723 if $cc -DTEST=$p -DID=$v -o $tmp.exe $tmp.c $lib $deflib <&$nullin >&$nullout && $executable $tmp.exe
726 o*|noo*)$tmp.exe <&$nullin || e=1 ;;
727 *) $tmp.exe <&$nullin >&$nullout || e=1 ;;
743 *) echo "#define $m 1 /* ${run}} passed */" ;;
745 (eval "$pass") <&$nullin
747 *) (eval "$fail") <&$nullin
754 # initialize common builtin state
759 "") statictest=FoobaR
760 echo "#if __cplusplus
763 extern int $statictest();
767 static int ((*i)())=$statictest;main(){return(i==0);}" > $tmp.c
769 if $cc -o $tmp.exe $tmp.c <&$nullin >&$nullout && $executable $tmp.exe
772 echo '#include <stdio.h>
773 main(){printf("hello");return(0);}' > $tmp.c
775 if $cc -c $tmp.c <&$nullin >&$nullout && $cc -o $tmp.exe $tmp.o <&$nullin >&$nullout 2>$tmp.e && $executable $tmp.exe
776 then e=`wc -l $tmp.e`
785 $cc -o $tmp.exe $1 $tmp.o <&$nullin >&$nullout 2>$tmp.e && $executable $tmp.exe || continue
786 case `wc -l $tmp.e` in
792 $cc -o $tmp.exe $2 $tmp.o <&$nullin >&$nullout 2>$tmp.e && $executable $tmp.exe || continue
793 case `wc -l $tmp.e` in
797 case `ls -s $tmp.exe` in
829 do if test -f $d/$i/$a
832 echo "#define $m 1 /* $a in ?(/usr)/(bin|etc|ucb) */"
835 echo "#define ${s}_${i}_${v} 1 /* $d/$i/$a found */"
848 *) echo "#if __cplusplus
857 echo "main(){char* i = (char*)&$v; return i!=0;}"
859 $cc -c $tmp.c <&$nullin >&$nullout &&
861 if $cc $static -o $tmp.exe $tmp.o $lib $deflib <&$nullin >&$nullout && $executable $tmp.exe
862 then echo "#define $m 1 /* $v in default lib(s) */"
867 lcl) eval p='$'_lcl_$v
876 hdr) echo "#include <$f.h>" > $tmp.c
877 $cc -E $tmp.c <&$nullin >&$nullout &&
878 echo "#define $m 1 /* #include <$f.h> ok */"
880 sys) echo "#include <sys/$f.h>" > $tmp.c
881 $cc -E $tmp.c <&$nullin >&$nullout &&
882 echo "#define $m 1 /* #include <sys/$f.h> ok */"
899 #include <$f.h>" > $tmp.c
900 if $cc -E $tmp.c <&$nullin >$tmp.i
901 then i=`sed -e '/^#[line ]*1[ ][ ]*"[\\\\/].*[\\\\/]'$x'\.h"/!d' -e '/[\\\\/]sys[\\\\/]'$x'\.h"/d' -e s'/.*"\(.*\)".*/\1/' $tmp.i`
908 [\\/]*) echo "#if defined(__STDPP__directive)"
909 echo "__STDPP__directive pragma pp:hosted"
911 echo "#include \"$i\" /* untrusted local <$f.h> */"
916 *) echo "/* no local <$f.h> */"
925 int f(){int $v = 1;return($v);}" > $tmp.c
926 $cc -c $tmp.c <&$nullin >&$nullout ||
927 echo "#define $m 1 /* $v is a reserved keyword */"
941 static int ((*i)())=$v;main(){return(i==0);}" > $tmp.c
942 $cc -c $tmp.c <&$nullin >&$nullout &&
944 if $cc $static -o $tmp.exe $tmp.o $lib $deflib <&$nullin >&$nullout && $executable $tmp.exe
946 lib) echo "#define $m 1 /* $v() in default lib(s) */" ;;
950 $cc $static -o $tmp.exe $tmp.o -lm <&$nullin >&$nullout &&
951 $executable $tmp.exe &&
952 echo "#define $m 1 /* $v() in math lib */"
963 '#define $m 1 /* $v is a macro */'
965 $cc -E $tmp.c <&$nullin | sed -e "/^'#define/!d" -e "s/'//g"
968 ?*) for i in "" "struct "
972 int n = sizeof(i.$v);" > $tmp.c
973 $cc -c $tmp.c <&$nullin >&$nullout &&
974 echo "#define $m 1 /* $v is member of $i$p */" &&
978 *) echo "$command: $o: <struct>.<member> expected" >&$stderr
984 one) for i in $a $hdr
987 if $cc -E $tmp.c <&$nullin >&$nullout
995 then echo "$command: $a: not found" >&$stderr
1001 $cc -o $tmp.exe $tmp.c $lib $deflib <&$nullin >&$nullout &&
1002 $executable $tmp.exe &&
1003 $tmp.exe $opt <&$nullin
1005 *.sh) ${SHELL-sh} $debug $a "cc='$cc' executable='$executable' id='$m' static='$static' tmp='$tmp'" $opt $hdr <&$nullin
1012 *) echo "$command: $a: cannot run" >&$stderr
1023 '=' $x '='" > $tmp.c
1024 $cc -E $tmp.c <&$nullin \
1029 -e 's/((([^()]*)))->/->/g' \
1030 -e 's/(([^()]*))->/->/g' \
1031 -e 's/([^()]*)->/->/g' \
1032 -e 's/\([a-zA-Z_][a-zA-Z_0-9]*\)\[/\
1034 -e 's/\([a-zA-Z_][a-zA-Z_0-9]*\)(/\
1036 -e 's/\*->\([a-zA-Z_]\)/->\
1038 -e 's/->\([a-zA-Z_]\)/->\
1041 -e 's/^[a-zA-Z_][a-zA-Z_0-9]*$/\
1048 -e 's/\(...\) \([a-zA-Z_][a-zA-Z_0-9]*\).*/#ifndef _\1_'$v'\
1049 #define _\1_'$v' \2\
1050 #define _\1_'$v'_str "\2"\
1059 #include <sys/types.h>"
1062 for i in time "" times
1074 if $cc -c $tmp.c <&$nullin >&$nullout
1076 echo "#define _inc_${c} 1 /* candidate header <$h> found */"
1082 for i in stddef stdlib
1085 echo "$u$typ" > $tmp.c
1086 if $cc -c $tmp.c <&$nullin >&$nullout
1088 echo "#define _inc_$i 1 /* candidate header <$i.h> found */"
1102 echo "v <<= 4; i = v >> 2; i = v * i; i = i / v; v = v + i; i = i - v; i = 10; i = v % i; i |= v; v ^= i; i = 123; v &= i;
1107 struct xxx { $x$v mem; };
1108 static struct xxx v;
1109 struct xxx* f() { return &v; }"
1112 echo "main() { f(); return 0; }"
1115 if $cc -o $tmp.exe $tmp.c <&$nullin >&$nullout &&
1116 $executable $tmp.exe
1117 then echo "#define $m 1 /* $x$v is a type */"
1122 *) echo "$command: $o: unknown feature test" >&$stderr
1127 ?*) (eval "$user") <&$nullin ;;