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