Automatically enable Bohem's GC if available.
[nit.git] / bin / gccx
1 #!/bin/sh
2 # This file is part of NIT ( http://www.nitlanguage.org ).
3 #
4 # Copyright 2008 Jean Privat <jean@pryen.org>
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #     http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 # Separately recompile a bunch of c files then link them
19
20 OPTS="-g" # option for compiler call
21 objs="" # List of .o files
22 CC="gcc --ansi --pedantic" # Default compiler call
23 ext="_savo _sav" # Default flavor to reuse
24 out="a.out"  # Default output binary filename
25 dir="" # Default tmp dir
26 nolibgc="false" # Try to use libgc ?
27 CKSUM="cksum" # Tool that perfors checksum. cksum seems to be very portable
28
29 usage()
30 {
31         e=`basename "$0"`
32         cat<<END
33 Usage: $e [options] modulename [options for module execution]
34 -O          Compile with optimizations
35 -i          Use the intel compiler instead of gcc
36 -I path     Add a include directory
37 -o name     Call name the executable
38 -d          Create temporary files in a specific directory
39 -nolibgc    Do not include libgc
40 -h          This help
41 END
42 }
43
44 test_libgc() {
45 cat > .tmp.c <<END
46 #include <stdlib.h>
47 #include <gc/gc.h>
48 int main(void) {
49         void *r = GC_malloc(1);
50                 return r == NULL;
51         }
52 END
53 gcc .tmp.c -lgc -o .tmp.bin 2> /dev/null
54 res=$?
55 rm .tmp.c
56 if [ $res = 0 ]; then
57         ./.tmp.bin
58         res=$?
59         rm .tmp.bin
60 fi
61 return $res
62 }
63
64 stop=false
65 while [ $stop = false ]; do
66         case $1 in 
67                 -O) CC="gcc --ansi --pedantic -O2" ext="_savo"; shift;;
68                 -i) CC="/opt/intel/cc/10.1.015/bin/icc -O2" ext="_savi"; shift;;
69                 -I) OPTS="$OPTS -I $2"; shift; shift;;
70                 -o) out="$2"; shift; shift;;
71                 -d) dir="$2/"; shift; shift;;
72                 --nolibgc) nolibgc=true; shift;;
73                 -x) OPTS="$OPTS $2"; shift; shift;;
74                 -h|"") usage; exit;;
75                 *) stop=true
76         esac
77 done
78
79 if [ $nolibgc != true ] && test_libgc; then
80         OPTS="$OPTS -DWITH_LIBGC -lgc"
81 fi
82
83 for i in "$@"; do
84         j=`basename "$i" .c`
85         found="false"
86         cksum=`gcc -E $OPTS $i 2> /dev/null | $CKSUM`
87         for e in $ext; do
88                 o="$dir$j.$e.o"
89                 cksumfile="$dir$j.$e.cksum"
90                 if [ -f "$cksumfile" -a -f "$o" ]; then
91                         cksumtry=`cat $cksumfile`
92                         if [ "x$cksum" = "x$cksumtry" ]; then
93                                 found=true
94                                 break
95                         fi
96                 fi
97         done
98         if [ $found = false ]; then
99                 echo "* $CC $OPTS -c $i -o $o" >&2
100                 if $CC $OPTS -c $i -o $o; then
101                         echo "$cksum" > "$cksumfile"
102                 else
103                         exit 1
104                 fi
105         fi
106         objs="$objs $o"
107 done
108
109 echo "* $CC $OPTS -lm $objs -o $out" >&2
110 $CC $OPTS -lm $objs -o "$out"