update NOTICE and LICENSE
[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="cc" # 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 cache=true
70 if ccache -V 2>/dev/null >/dev/null; then
71         cache=ccache
72 fi
73
74 stop=false
75 verbose=false
76 vverbose=false
77 while [ $stop = false ]; do
78         case $1 in 
79                 -R) recompile=true; shift;;
80                 -O) OPTS="$OPTS -O2" ext="_savo"; shift;;
81                 -i) CC="/opt/intel/cc/10.1.015/bin/icc -O2" ext="_savi"; shift;;
82                 -ll) CC="clang --ansi --pedantic -O3"; ext="_savll"; shift;;
83                 -I) OPTS="$OPTS -I $2"; shift; shift;;
84                 -o) out="$2"; shift; shift;;
85                 -d) dir="$2/"; shift; shift;;
86                 --nolibgc) nolibgc=true; shift;;
87                 -x) OPTS="$OPTS $2"; shift; shift;;
88                 -vv) vverbose=true; verbose=true; shift;;
89                 -v) verbose=true; shift;;
90                 -h|"") usage; exit;;
91                 *) stop=true
92         esac
93 done
94
95 if [ $nolibgc != true ] && test_libgc; then
96         OPTS="$OPTS -DWITH_LIBGC -lgc"
97 fi
98
99 if [ $cache = "ccache" ]; then
100         CC="ccache $CC"
101 fi
102
103 for i in "$@"; do
104         j=`basename "$i" .c`
105         transformed=`echo "$i" | sed "
106                 /\.nit_compile\/.*_sep\.c/s/\.nit_compile\/\(.*\)\._sep\.c/Module \1/
107                 /\.nit_compile\/.*_glob\.c/s/\.nit_compile\/\(.*\)\._glob\.c/Module \1/
108                 /\.nit_compile[1-9]\/.*_sep\.c/s/\.nit_compile[1-9]\/\(.*\)\._sep\.c/Module \1/
109                 /\.nit_compile[1-9]\/.*_glob\.c/s/\.nit_compile[1-9]\/\(.*\)\._glob\.c/Module \1/
110                 /\/nit_main\.c/s/.*\/nit_main\.c/Main/
111                 /\.nit_compile\/.*\._tables\.c/s/.*/Tables/
112                 /\.nit_compile[1-9]\/.*\._tables\.c/s/.*/Tables/
113                 /\/\/.*_nit\.c/s/.*\/\/\(.*\)_nit.c/Native \1/
114                 s/.*\/gc.c/Garbage Collector/
115                 s/.*\/gc_static_objects_list.c/Garbage Collector object list/
116         "`
117
118         found="false"
119         if [ $cache != true ]; then
120                 o="$dir$j.o"
121                 if [ $vverbose = true  ] ; then
122                         echo "* $CC $OPTS -c $i -o $o" >&2
123                 fi
124                 $CC $OPTS -c $i -o $o || exit 1
125         else
126         # We remove starting # to be path independent (after preprocess, there are the only # remainings)
127         cksum=`gcc -E $OPTS $i 2> /dev/null | grep -v "^#" | $CKSUM`
128         for e in $ext; do
129                 o="$dir$j.$e.o"
130                 cksumfile="$dir$j.$e.cksum"
131                 if [ -f "$cksumfile" -a -f "$o" -a "x$recompile" != "xtrue" ]; then
132                         cksumtry=`cat $cksumfile`
133                         if [ "x$cksum" = "x$cksumtry" ]; then
134                                 if [ $vverbose = true  ] ; then
135                                         echo "* $transformed up-to-date"
136                                 fi
137                                 found=true
138                                 break
139                         fi
140                 fi
141         done
142         if [ $found = false ]; then
143                 if [ $verbose = true  ] ; then
144                         if [ $vverbose = true  ] ; then
145                                 echo "* $CC $OPTS -c $i -o $o" >&2
146                         else
147                                 echo "* $transformed"
148                         fi
149                 fi
150                 if $CC $OPTS -c $i -o $o; then
151                         echo "$cksum" > "$cksumfile"
152                 else
153                         exit 1
154                 fi
155         fi
156 fi
157         objs="$objs $o"
158 done
159
160 if [ $vverbose = true  ] ; then
161         echo "* $CC $OPTS -lm $objs -o $out" >&2
162 fi
163 $CC $OPTS -lm $objs -o "$out"