]> 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 SOURCEDIRS="source/lib source/bootstrap plugins/base plugins/gnu"
6 INCLUDEDIR=temp/bootstrap/include
7 REQUIRED="core datafile"
8 CFLAGS="-std=c++11 -iquote . -I$INCLUDEDIR `pkg-config --cflags sigc++-2.0`"
9 LIBS="`pkg-config --libs sigc++-2.0` -lpthread"
10 MACHINE="`uname -m`"
11 SYSTEM="`uname -s`"
12 if [ "$MACHINE" = "x86_64" ]; then
13         MULTIARCH="x86_64-linux-gnu"
14 else
15         MULTIARCH="i386-linux-gnu"
16 fi
17 if [ "$SYSTEM" = "Darwin" -o "$SYSTEM" = "FreeBSD" ]; then
18         if which clang++ >/dev/null; then
19                 COMPILER="clang++"
20         else
21                 COMPILER="g++"
22         fi
23 else
24         COMPILER="g++"
25 fi
26 if [ -e /usr/lib/libdl.so -o -e /usr/lib/$MULTIARCH/libdl.so ]; then
27         LIBS="$LIBS -ldl"
28 fi
29
30 while [ "$1" ]; do
31         case $1 in
32         --libpath)
33                 shift
34                 LIBPATH="$1"
35                 ;;
36         --libpath=*)
37                 LIBPATH="${1#--libpath=}"
38                 ;;
39         --prefix)
40                 shift
41                 PREFIX="$1"
42                 ;;
43         --prefix=*)
44                 PREFIX="${1#--prefix=}"
45                 ;;
46         --keep-temp)
47                 KEEP_TEMP=yes
48                 ;;
49         --compiler)
50                 shift
51                 COMPILER="$1"
52                 CUSTOM_COMPILER=yes
53                 ;;
54         --compiler=*)
55                 COMPILER="${1#--compiler=}"
56                 CUSTOM_COMPILER=yes
57                 ;;
58         --debug)
59                 DEBUG=-g
60                 ;;
61         *)
62                 echo "Unknown option $1"
63                 exit 1
64                 ;;
65         esac
66         shift
67 done
68
69 if [ -z "$LIBPATH" -o "${LIBPATH%%/*}" ]; then
70         LIBPATH="$PWD/${LIBPATH:-..}"
71 fi
72
73 rm -rf "$INCLUDEDIR"
74 mkdir -p "$INCLUDEDIR/msp"
75
76 sources=`find $SOURCEDIRS -name '*.cpp'`
77
78 mkdir -p "$INCLUDEDIR/msp/builder"
79 ln -sf "$PWD/source/lib"/*.h "$INCLUDEDIR/msp/builder/"
80
81 use_overlays="unix generic"
82 missing=""
83 for i in $REQUIRED; do
84         path=`ls -1d $LIBPATH/*$i* 2>/dev/null | head -n1`
85         if [ -z "$path"  -o ! -d "$path/source" ]; then
86                 missing="$missing msp$i"
87         else
88                 if [ "`echo $path/source/*.cpp`" = "$path/source/*.cpp" ]; then
89                         subdirs=`find "$path/source" -mindepth 1 -maxdepth 1 -type d`
90                 else
91                         subdirs="$path/source"
92                 fi
93
94                 check_overlay=""
95                 for j in $use_overlays; do
96                         [ -z "$check_overlay" ] || check_overlay="$check_overlay -o "
97                         check_overlay="${check_overlay}-name $j"
98                 done
99                 findargs="$subdirs"
100                 if [ "$check_overlay" ]; then
101                         findargs="$findargs -mindepth 1 ( -type d ! ( $check_overlay ) -prune -false ) -o "
102                 fi
103
104                 for j in `find $findargs -name '*.h'`; 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         echo "Compiling $i"
140         $COMPILER -c $DEBUG $i -o $obj $flags
141         objects="$objects $obj"
142 done
143 echo "Linking builder-stage1"
144 $COMPILER $objects -o builder-stage1 $LIBS
145
146 if [ "$KEEP_TEMP" != "yes" ]; then
147         echo "Cleaning up"
148         rm -r temp/bootstrap
149 fi
150
151 echo "Using builder-stage1 to compile builder."
152 ARGS=
153 if [ "$PREFIX" ]; then
154         ARGS="$ARGS --prefix='$PREFIX'"
155 fi
156 if [ "$CUSTOM_COMPILER" = "yes" ]; then
157         ARGS="$ARGS --compiler=$COMPILER"
158 fi
159 eval "./builder-stage1 $ARGS"