bdeb3bb8bab5d1bc1127a01b76fecd7042cc3427
[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="true" # Disable boehm libgc?
27 CKSUM="cksum" # Tool that perfors checksum. cksum seems to be very portable
28 recompile="false"
29
30 usage()
31 {
32         e=`basename "$0"`
33         cat<<END
34 Usage: $e [options] modulename [options for module execution]
35 -R          Force full recompilation
36 -O          Compile with optimizations
37 -i          Use the intel compiler instead of gcc
38 -ll         Use the clang compiler (llvm) instead of gcc
39 -I path     Add a include directory
40 -o name     Call name the executable
41 -d          Create temporary files in a specific directory
42 -nolibgc    Do not include libgc
43 -v          Verbose (show compilation steps)
44 -vv         Verbose+ (show gcc calls)
45 -h          This help
46 END
47 }
48
49 test_libgc() {
50 cat > .tmp.c <<END
51 #include <stdlib.h>
52 #include <gc/gc.h>
53 int main(void) {
54         void *r = GC_malloc(1);
55                 return r == NULL;
56         }
57 END
58 gcc .tmp.c -lgc -o .tmp.bin 2> /dev/null
59 res=$?
60 rm .tmp.c
61 if [ $res = 0 ]; then
62         ./.tmp.bin
63         res=$?
64         rm .tmp.bin
65 fi
66 return $res
67 }
68
69 stop=false
70 verbose=false
71 vverbose=false
72 while [ $stop = false ]; do
73         case $1 in 
74                 -R) recompile=true; shift;;
75                 -O) OPTS="$OPTS -O2" ext="_savo"; shift;;
76                 -i) CC="/opt/intel/cc/10.1.015/bin/icc -O2" ext="_savi"; shift;;
77                 -ll) CC="clang --ansi --pedantic -O3"; ext="_savll"; shift;;
78                 -I) OPTS="$OPTS -I $2"; shift; shift;;
79                 -o) out="$2"; shift; shift;;
80                 -d) dir="$2/"; shift; shift;;
81                 --nolibgc) nolibgc=true; shift;;
82                 -x) OPTS="$OPTS $2"; shift; shift;;
83                 -vv) vverbose=true; verbose=true; shift;;
84                 -v) verbose=true; shift;;
85                 -h|"") usage; exit;;
86                 *) stop=true
87         esac
88 done
89
90 if [ $nolibgc != true ] && test_libgc; then
91         OPTS="$OPTS -DWITH_LIBGC -lgc"
92 fi
93
94 for i in "$@"; do
95         j=`basename "$i" .c`
96         transformed=`echo "$i" | sed "
97                 /\.nit_compile\/.*_sep\.c/s/\.nit_compile\/\(.*\)\._sep\.c/Module \1/
98                 /\.nit_compile\/.*_glob\.c/s/\.nit_compile\/\(.*\)\._glob\.c/Module \1/
99                 /\.nit_compile[1-9]\/.*_sep\.c/s/\.nit_compile[1-9]\/\(.*\)\._sep\.c/Module \1/
100                 /\.nit_compile[1-9]\/.*_glob\.c/s/\.nit_compile[1-9]\/\(.*\)\._glob\.c/Module \1/
101                 /\/nit_main\.c/s/.*\/nit_main\.c/Main/
102                 /\.nit_compile\/.*\._tables\.c/s/.*/Tables/
103                 /\.nit_compile[1-9]\/.*\._tables\.c/s/.*/Tables/
104                 /\/\/.*_nit\.c/s/.*\/\/\(.*\)_nit.c/Native \1/
105                 s/.*\/gc.c/Garbage Collector/
106                 s/.*\/gc_static_objects_list.c/Garbage Collector object list/
107         "`
108
109         found="false"
110         # We remove starting # to be path independent (after preprocess, there are the only # remainings)
111         cksum=`gcc -E $OPTS $i 2> /dev/null | grep -v "^#" | $CKSUM`
112         for e in $ext; do
113                 o="$dir$j.$e.o"
114                 cksumfile="$dir$j.$e.cksum"
115                 if [ -f "$cksumfile" -a -f "$o" -a "x$recompile" != "xtrue" ]; then
116                         cksumtry=`cat $cksumfile`
117                         if [ "x$cksum" = "x$cksumtry" ]; then
118                                 if [ $vverbose = true  ] ; then
119                                         echo "* $transformed up-to-date"
120                                 fi
121                                 found=true
122                                 break
123                         fi
124                 fi
125         done
126         if [ $found = false ]; then
127                 if [ $verbose = true  ] ; then
128                         if [ $vverbose = true  ] ; then
129                                 echo "* $CC $OPTS -c $i -o $o" >&2
130                         else
131                                 echo "* $transformed"
132                         fi
133                 fi
134                 if $CC $OPTS -c $i -o $o; then
135                         echo "$cksum" > "$cksumfile"
136                 else
137                         exit 1
138                 fi
139         fi
140         objs="$objs $o"
141 done
142
143 if [ $vverbose = true  ] ; then
144         echo "* $CC $OPTS -lm $objs -o $out" >&2
145 fi
146 $CC $OPTS -lm $objs -o "$out"