]> git.tdb.fi Git - builder.git/blob - bootstrap.sh
Refactor transitive dependencies to work on all targets
[builder.git] / bootstrap.sh
1 #!/bin/sh
2
3 set -e
4
5 INCLUDEDIR=temp/bootstrap/include
6 REQUIRED="core datafile"
7 CFLAGS="-I$INCLUDEDIR `pkg-config --cflags sigc++-2.0`"
8 LIBS="`pkg-config --libs sigc++-2.0` -lpthread"
9 if pkg-config --exists "sigc++-2.0 >= 2.5.1"; then
10         CFLAGS="$CFLAGS -std=c++11"
11 fi
12 MACHINE="`uname -m`"
13 SYSTEM="`uname -s`"
14 if [ "$MACHINE" = "x86_64" ]; then
15         MULTIARCH="x86_64-linux-gnu"
16 else
17         MULTIARCH="i386-linux-gnu"
18 fi
19 if [ "$SYSTEM" = "Darwin" -o "$SYSTEM" = "FreeBSD" ]; then
20         if which clang++ >/dev/null; then
21                 COMPILER="clang++"
22         else
23                 COMPILER="g++"
24         fi
25 else
26         COMPILER="g++"
27 fi
28 if [ -e /usr/lib/libdl.so -o -e /usr/lib/$MULTIARCH/libdl.so ]; then
29         LIBS="$LIBS -ldl"
30 fi
31
32 while [ "$1" ]; do
33         case $1 in
34         --libpath)
35                 shift
36                 LIBPATH="$1"
37                 ;;
38         --libpath=*)
39                 LIBPATH="${1#--libpath=}"
40                 ;;
41         --prefix)
42                 shift
43                 PREFIX="$1"
44                 ;;
45         --prefix=*)
46                 PREFIX="${1#--prefix=}"
47                 ;;
48         --keep-temp)
49                 KEEP_TEMP=yes
50                 ;;
51         --compiler)
52                 shift
53                 COMPILER="$1"
54                 CUSTOM_COMPILER=yes
55                 ;;
56         --compiler=*)
57                 COMPILER="${1#--compiler=}"
58                 CUSTOM_COMPILER=yes
59                 ;;
60         --debug)
61                 DEBUG=-g
62                 ;;
63         *)
64                 echo "Unknown option $1"
65                 exit 1
66                 ;;
67         esac
68         shift
69 done
70
71 if [ -z "$LIBPATH" -o "${LIBPATH%%/*}" ]; then
72         LIBPATH=`pwd`/${LIBPATH:-..}
73 fi
74
75 rm -rf "$INCLUDEDIR"
76 mkdir -p "$INCLUDEDIR/msp"
77
78 sources=source/*.cpp
79
80 use_overlays="unix generic"
81 missing=""
82 for i in $REQUIRED; do
83         path=`ls -1d $LIBPATH/*$i* 2>/dev/null | head -n1`
84         if [ -z "$path"  -o ! -d "$path/source" ]; then
85                 missing="$missing msp$i"
86         else
87                 if [ "`echo $path/source/*.cpp`" = "$path/source/*.cpp" ]; then
88                         subdirs=`find "$path/source" -mindepth 1 -maxdepth 1 -type d`
89                 else
90                         subdirs="$path/source"
91                 fi
92
93                 check_overlay=""
94                 for j in $use_overlays; do
95                         [ -z "$check_overlay" ] || check_overlay="$check_overlay -o "
96                         check_overlay="${check_overlay}-name $j"
97                 done
98                 findargs="$subdirs"
99                 if [ "$check_overlay" ]; then
100                         findargs="$findargs -mindepth 1 ( -type d ! ( $check_overlay ) -prune -false ) -o "
101                 fi
102
103                 headers=`find $findargs -name '*.h'`
104                 for j in $headers; do
105                         rel=${j#$path/source/}
106                         comp=${rel%%/*}
107                         if [ "$comp" = "$rel" ]; then
108                                 comp=$i
109                         fi
110                         mkdir -p "$INCLUDEDIR/msp/$comp"
111                         ln -sf $j "$INCLUDEDIR/msp/$comp/"
112                 done
113                 sources="$sources `find $findargs -name '*.cpp'`"
114         fi
115 done
116
117 if [ ! -z "$missing" ]; then
118         echo "The following libraries were not found:$missing"
119         echo "I looked for them in $LIBPATH"
120         echo "If they are somewhere else, please add --libpath=PATH to the command line."
121         exit 1
122 fi
123
124 echo "Compiling builder-stage1.  This may take several minutes."
125 objects=""
126 for i in $sources; do
127         obj=`mktemp temp/bootstrap/XXXXXX`
128         mv $obj $obj.o
129         obj=$obj.o
130         dir=${i%/*}
131         flags="$CFLAGS"
132         for j in $use_overlays; do
133                 dir=${dir%/$j}
134                 if [ -d "$dir/$j" ]; then
135                         flags="$flags -iquote $dir -iquote $dir/$j"
136                         break
137                 fi
138         done
139         $COMPILER -c $DEBUG $i -o $obj $flags
140         objects="$objects $obj"
141 done
142 $COMPILER $objects -o builder-stage1 $LIBS
143
144 if [ "$KEEP_TEMP" != "yes" ]; then
145         echo "Cleaning up"
146         rm -r temp/bootstrap
147 fi
148
149 echo "Using builder-stage1 to compile builder."
150 ARGS=
151 if [ "$PREFIX" ]; then
152         ARGS="$ARGS --prefix='$PREFIX'"
153 fi
154 if [ "$CUSTOM_COMPILER" = "yes" ]; then
155         ARGS="$ARGS CXX=$COMPILER"
156 fi
157 eval "./builder-stage1 $ARGS"