compile: traces know REG variables
[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 -Wall -Wextra -Wformat-security -Wcast-align -Wno-uninitialized -Wno-unused-variable -Wno-unused-label -Wno-unused-parameter -Wno-missing-field-initializers" # 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 -v          Verbose (show compilation steps)
41 -vv         Verbose+ (show gcc calls)
42 -h          This help
43 END
44 }
45
46 test_libgc() {
47 cat > .tmp.c <<END
48 #include <stdlib.h>
49 #include <gc/gc.h>
50 int main(void) {
51         void *r = GC_malloc(1);
52                 return r == NULL;
53         }
54 END
55 gcc .tmp.c -lgc -o .tmp.bin 2> /dev/null
56 res=$?
57 rm .tmp.c
58 if [ $res = 0 ]; then
59         ./.tmp.bin
60         res=$?
61         rm .tmp.bin
62 fi
63 return $res
64 }
65
66 stop=false
67 verbose=false
68 vverbose=false
69 while [ $stop = false ]; do
70         case $1 in 
71                 -O) OPTS="$OPTS -O2" ext="_savo"; shift;;
72                 -i) CC="/opt/intel/cc/10.1.015/bin/icc -O2" ext="_savi"; shift;;
73                 -I) OPTS="$OPTS -I $2"; shift; shift;;
74                 -o) out="$2"; shift; shift;;
75                 -d) dir="$2/"; shift; shift;;
76                 --nolibgc) nolibgc=true; shift;;
77                 -x) OPTS="$OPTS $2"; shift; shift;;
78                 -vv) vverbose=true; verbose=true; shift;;
79                 -v) verbose=true; shift;;
80                 -h|"") usage; exit;;
81                 *) stop=true
82         esac
83 done
84
85 if [ $nolibgc != true ] && test_libgc; then
86         OPTS="$OPTS -DWITH_LIBGC -lgc"
87 fi
88
89 for i in "$@"; do
90         j=`basename "$i" .c`
91         transformed=`echo "$i" | sed "
92                 /\.nit_compile\/.*_sep\.c/s/\.nit_compile\/\(.*\)\._sep\.c/Module \1/
93                 /\/nit_main\.c/s/.*\/nit_main\.c/Main/
94                 /\.nit_compile\/.*\._tables\.c/s/.*/Tables/
95                 /\/\/.*_nit\.c/s/.*\/\/\(.*\)_nit.c/Native \1/
96         "`
97
98         found="false"
99         # We remove starting # to be path independent (after preprocess, there are the only # remainings)
100         cksum=`gcc -E $OPTS $i 2> /dev/null | grep -v "^#" | $CKSUM`
101         for e in $ext; do
102                 o="$dir$j.$e.o"
103                 cksumfile="$dir$j.$e.cksum"
104                 if [ -f "$cksumfile" -a -f "$o" ]; then
105                         cksumtry=`cat $cksumfile`
106                         if [ "x$cksum" = "x$cksumtry" ]; then
107                                 if [ $vverbose = true  ] ; then
108                                         echo "* $transformed up-to-date"
109                                 fi
110                                 found=true
111                                 break
112                         fi
113                 fi
114         done
115         if [ $found = false ]; then
116                 if [ $verbose = true  ] ; then
117                         if [ $vverbose = true  ] ; then
118                                 echo "* $CC $OPTS -c $i -o $o" >&2
119                         else
120                                 echo "* $transformed"
121                         fi
122                 fi
123                 if $CC $OPTS -c $i -o $o; then
124                         echo "$cksum" > "$cksumfile"
125                 else
126                         exit 1
127                 fi
128         fi
129         objs="$objs $o"
130 done
131
132 if [ $vverbose = true  ] ; then
133         echo "* $CC $OPTS -lm $objs -o $out" >&2
134 fi
135 $CC $OPTS -lm $objs -o "$out"