C+probe revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg### this script contains archaic constructs that work with all sh variants ###
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes# Glenn Fowler
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes# AT&T Research
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes# @(#)C probe (AT&T Research) 2012-02-29
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes# probe [ -d ] c-compiler-path [ attributes ]
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes# common C probe preamble for the tool specific probes
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes# NOTE: some cc -E's do syntax analysis!
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes# probe_* are first eval'd and then attempted from left to right
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesprobe_binding="-dy -dn -Bdynamic -Bstatic '-Wl,-ashared -Wl,+s' -Wl,-aarchive -call_shared -non_shared -dynamic -static -bshared -bstatic '' -static"
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesprobe_env="CC_OPTIONS CCOPTS LD_OPTIONS LDOPTS LIBPATH LPATH"
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesprobe_include="stdio.h iostream.h complex.h ctype.h plot.h stdarg.h varargs.h ranlib.h hash.h sys/types.h stab.h cmath cstdio iostream string"
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesprobe_longlong="long 'long long'"
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesprobe_longlong_t="__int64_t _int64_t __int64 _int64 int64"
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesprobe_l="l yyreject m sin mopt sin"
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesprobe_lxx="C exit ++ exit g++ exit"
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesprobe_ppprefix="a n"
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesprobe_size="size"
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesprobe_src="cxx C cc c"
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesprobe_sa=".sa"
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesprobe_sd=".dll .lib .dll .x"
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesprobe_sdb=".pdb"
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesprobe_so=".dylib .so .sl"
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesprobe_symprefix="_"
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesprobe_verbose="'-v -v' '-# -#' '-d -d' -dryrun '-V -V'"
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesprobe_version="--version -V -version -v"
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes# the following are set by the preamble for the tool specific probe
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesexecutable="test -x"
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholessuffix_command=
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesif test "" != "$TMPDIR" -a -d "$TMPDIR"
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesthen tmpdir=$TMPDIR
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholeselse tmpdir=/tmp
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesundef="define defined elif else endif error if ifdef ifndef include line pragma undef __STDC__ __STDPP__ __ARGC__ __BASE__ __BASE_FILE__ __DATE__ __FILE__ __FUNCTION__ __INCLUDE_LEVEL__ __LINE__ __PATH__ __TIME__ __TIMESTAMP__ __VERSION__"
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesversion_flags=
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesversion_stamp=
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesversion_string=
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes# constrain the environment
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesexport DISPLAY LC_ALL
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes# now the common probes
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesdo case $1 in
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes -d) debug=1 ;;
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes -*) set ''; break ;;
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes[\\/]*|[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]:\\*)
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes*) echo "Usage: $0 [ -d ] c-compiler-path [ attributes ]" >&2
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes_probe_PATH=$PATH
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesPATH=/usr/bin:/bin:$PATH
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes*[\\/]*) dir=`echo $0 | sed -e 's,[\\/][\\/]*[^\\/]*\$,,'` ;;
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes$executable . 2>/dev/null || executable='test -r'
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholescase $SHELL in
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes[\\/]*|[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]:\\*)
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholestrap 'code=$?; cd ..; rm -rf $tmpdir; exit $code' 0 1 2 3
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesmkdir $tmpdir
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesexec 3>&1 4>&2 </dev/null
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholescase $debug in
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes"") exec >/dev/null 2>&1
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes (ulimit -c 0) >/dev/null 2>&1 && ulimit -c 0
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes*) PS4='+$LINENO+ '
ac7985784d08a3655291f24f711812b4d8b1cbcffuankgif (xxx=xxx; unset xxx)
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholeseval set x $probe_env
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes eval x='$'$1
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes '') continue ;;
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes *PATH) _probe_export="$_probe_export $1='$x'" ;;
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes case $UNSET in
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes '') eval $1=
ac7985784d08a3655291f24f711812b4d8b1cbcffuankgfor src in $probe_src
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesdo echo "int main(){return 0;}" > ../test.$src
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes if $cc -c ../test.$src
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes then set test.*
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes if test -f "$1"
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes do if $cc -o test.exe ../$i
0b2a6b63977ab27352a0b525bdad1e1982a1c0b1bnicholes then obj=`echo "$i" | sed -e 's,test.,,'`
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes $executable test.exe || executable="test -r"
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes if $cc -o test ../$i
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes if $executable "$1"
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes then exe=`echo "$1" | sed -e 's,test.,,'`
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes suffix_command=.$exe
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes*) echo '// (
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes class { public: int i; } j;
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes int k = j.i + 1;
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes}' > dialect.$src
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes if $cc -c dialect.$src && $cc -o dialect.$exe dialect.$obj && $executable dialect.$exe
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes then mv dialect.$src dialect.c
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes rm -f dialect.$obj dialect.$exe
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes if $cc -c dialect.c && $cc -o dialect.$exe dialect.$obj && $executable dialect.$exe
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes else set x $cc
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes *=*) continue ;;
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes case `echo $1 | sed -e 's,.*/,,'` in
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes *CC*|*++*|*[xX][xX]*|*[pP][lL][uU][sS]*) ;;
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesset x x '(' 1 'int x;' 0
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes [01]) break ;;
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes rm -f test.$obj
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg echo "$1" > test.$src
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes $cc -c test.$src
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes 0) test -f test.$obj || r=1 ;;
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes case $2:$r in
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes 0:1) echo "$cc: not a C compiler: failed to compile \`\`$1''" >&4
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes 1:0) echo "$cc: not a C compiler: successfully compiled \`\`$1''" >&4
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholeshosttype=`package CC="$cc" || $SHELL -c "package CC='$cc'"`
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholescase $hosttype in
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes*[Uu][Ss][Aa][Gg][Ee]:*)
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes hosttype=`PATH=$_probe_PATH; export PATH; package CC="$cc" || $SHELL -c "package CC='$cc'"`
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesecho '#include <stdio.h>
ac7985784d08a3655291f24f711812b4d8b1cbcffuankgint main(){printf("hello");return 0;}' > dynamic.$src
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesecho 'extern int sfclose() { return 0; }' > fun.$src
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesif $cc -c dynamic.$src && $cc -c fun.$src
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholesthen eval set x $probe_so
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes for i in foo junk
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes do rm -f dynamic.$exe
0b2a6b63977ab27352a0b525bdad1e1982a1c0b1bnicholes if $cc -L. -o dynamic.$exe dynamic.$obj -l$i
0b2a6b63977ab27352a0b525bdad1e1982a1c0b1bnicholes then : "there's really a -l$i"?
0b2a6b63977ab27352a0b525bdad1e1982a1c0b1bnicholes else rm -f dynamic.$exe
0b2a6b63977ab27352a0b525bdad1e1982a1c0b1bnicholes cat fun.$obj > lib$i$1
0b2a6b63977ab27352a0b525bdad1e1982a1c0b1bnicholes $cc -L. -o dynamic.$exe dynamic.$obj -l$i && $executable dynamic.$exe
0b2a6b63977ab27352a0b525bdad1e1982a1c0b1bnicholes rm -f dynamic.$exe > lib$i$1.1
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes $cc -L. -o dynamic.$exe dynamic.$obj -l$i && $executable dynamic.$exe
e76fdcdfb8994ad70776526f50fa013b3e9a6033bnicholes rm lib$i$1.1
(cd ..; rm $tmpdir/libc$1$i)
do if $cc $o > version.out 2>&1
then version_string=`sed -e '/ is /d' -e 's/;/ /g' version.out | sed -e 1q`
''|*[Ee][Rr][Rr][Oo][Rr]*|*[Ff][Aa][Tt][Aa][Ll]*|*[Ww][Aa][Rr][Nn][Ii][Nn][Gg]*|*[Oo][Pp][Tt][Ii][Oo][Nn]*)
echo 'int main() { return 0; }' > version.i
then version_string=`sed -e '/ is /d' -e 's/;/ /g' version.out | sed -e 1q`
''|*[Ee][Rr][Rr][Oo][Rr]*|*[Ff][Aa][Tt][Aa][Ll]*|*[Ww][Aa][Rr][Nn][Ii][Nn][Gg]*|*[Oo][Pp][Tt][Ii][Oo][Nn]*)
*) cp /bin/echo cpp
if test `realcppC=./cpp $cc -Dprobe -E flags.$src | tee cpp.out | grep -c '[-]Dprobe'` -eq 1
elif test `cppC=./cpp $cc -Dprobe -E flags.$src | tee cpp.out | grep -c '[-]Dprobe'` -eq 1
elif test `_CPPNAME=./cpp $cc -Dprobe -E flags.$src | tee cpp.out | grep -c '[-]Dprobe'` -eq 1
elif test `_CPP=./cpp $cc -Dprobe -E flags.$src | tee cpp.out | grep -c '[-]Dprobe'` -eq 1
elif test `$cc -Dprobe -E -%p+. flags.$src | tee cpp.out | grep -c '[-]Dprobe'` -eq 1 && test `$cc -Dprobe -E -%p+. flags.$src | wc -l` -eq 1
elif test `$cc -Dprobe -E -Yp,. flags.$src | tee cpp.out | grep -c '[-]Dprobe'` -eq 1
elif test `$cc -Dprobe -E -Qpath $tmpdir flags.$src | tee cpp.out | grep -c '[-]Dprobe'` -eq 1
elif test `$cc -Dprobe -E -tp -B./ flags.$src 2>err.out | tee cpp.out | grep -c '[-]Dprobe'` -eq 1 -a ! -s err.out
elif test `$cc -Dprobe -E -B./ flags.$src | tee cpp.out | grep -c '[-]Dprobe'` -eq 1
elif test `$cc -Dprobe -E -tp -h./ -B flags.$src | tee cpp.out | grep -c '[-]Dprobe'` -eq 1
elif test `$cc -Dprobe -E -t p,./cpp flags.$src | tee cpp.out | grep -c '[-]Dprobe'` -eq 1
} 2>&1 | sed -e "s/['\"]//g" > cpp.out
set x `sed -e 's,[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]:\\\\,/,g' -e 's,\\\\,/,g' cpp.out`
-I/*) f=`echo X$1 | sed 's/X-I//'`
*\ /usr/include\ *)
*) usrinclude=/usr/include
tstinclude=`$cc -v -E flags.$src 2>&1 | sed -e '1,/[iI][nN][cC][lL][uU][dD][eE][ ]*<[.][.][.]>/d' -e '/^[eE][nN][dD] [oO][fF] [sS][eE][aA][rR][cC][hH]/,\$d'`
C_hdr="libc.h"
usrinclude=/usr/include
set '' $tstinclude /usr/include
echo "#include <sys/types.h>" > include.$src
$cc -E include.$src | sed -e '/# 1 "[\\/]/!d' -e 's,[^"]*",,' -e 's,[\\/][^\\/]*".*,,' -e 's,[\\/]sys,,' > include.out
for f in `cat include.out`
/usr/include) usrinclude=$f ;;
?*) tstinclude=$x/usr/include
cp /bin/echo cpp
done > include.out
for f in $x $tstinclude `sed -e 's,\\\\,/,g' -e 's,///*,/,g' -e 's,"[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]:/,"/,g' -e '/^#[line ]*[0123456789][0123456789]*[ ][ ]*"[\\/]/!d' -e 's/^#[line ]*[0123456789][0123456789]*[ ][ ]*"\(.*\)[\\/].*".*/\1/' include.out | sort -u`
rm include.out
if $cc -E include.$src > /dev/null
$cc -E include.$src > include.out
I=`grep -c $i/$k < include.out`
J=`grep -c $j/$k < include.out`
echo "#include <$1>" > t.c
for j in `$cc -E t.c | grep "$1" | sed -e 's,\\\\,/,g' -e 's,"[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]:/,"/,g' -e '/^#[line ]*1[ ][ ]*"[\\/]/!d' -e 's/^#[line ]*1[ ][ ]*"\(.*\)[\\/].*".*/\1/'`
} | tsort > tmp.tmp
tstinclude=`cat tmp.tmp`
/usr/include) usrinclude= ;;