First NIT release and new clean mercurial repository
[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 CKSUM="cksum" # Tool that perfors checksum. cksum seems to be very portable
27
28 usage()
29 {
30         e=`basename "$0"`
31         cat<<END
32 Usage: $e [options] modulename [options for module execution]
33 -O          Compile with optimizations
34 -i          Use the intel compiler instead of gcc
35 -I path     Add a include directory
36 -o name     Call name the executable
37 -d          Create temporary files in a specific directory
38 -h          This help
39 END
40 }
41
42 stop=false
43 while [ $stop = false ]; do
44         case $1 in 
45                 -O) CC="gcc --ansi --pedantic -O2" ext="_savo"; shift;;
46                 -i) CC="/opt/intel/cc/10.1.015/bin/icc -O2" ext="_savi"; shift;;
47                 -I) OPTS="$OPTS -I $2"; shift; shift;;
48                 -o) out="$2"; shift; shift;;
49                 -d) dir="$2/"; shift; shift;;
50                 -x) OPTS="$OPTS $2"; shift; shift;;
51                 -h|"") usage; exit;;
52                 *) stop=true
53         esac
54 done
55
56 for i in "$@"; do
57         j=`basename "$i" .c`
58         found="false"
59         cksum=`gcc -E $i 2> /dev/null | $CKSUM`
60         for e in $ext; do
61                 o="$dir$j.$e.o"
62                 cksumfile="$dir$j.$e.cksum"
63                 if [ -f "$cksumfile" -a -f "$o" ]; then
64                         cksumtry=`cat $cksumfile`
65                         if [ "x$cksum" = "x$cksumtry" ]; then
66                                 found=true
67                                 break
68                         fi
69                 fi
70         done
71         if [ $found = false ]; then
72                 echo "* $CC $OPTS -c $i -o $o" >&2
73                 if $CC $OPTS -c $i -o $o; then
74                         echo "$cksum" > "$cksumfile"
75                 else
76                         exit 1
77                 fi
78         fi
79         objs="$objs $o"
80 done
81
82 echo "* $CC $OPTS -lm $objs -o $out" >&2
83 $CC $OPTS -lm $objs -o "$out"