#!/bin/sh
-REQUIRED="misc core parser path regex++ getopt++"
-CFLAGS="-Iinclude `pkg-config --cflags sigc++-2.0`"
+set -e
+
+INCLUDEDIR=temp/bootstrap/include
+REQUIRED="core datafile"
+CFLAGS="-I$INCLUDEDIR `pkg-config --cflags sigc++-2.0`"
LIBS="`pkg-config --libs sigc++-2.0` -lpthread"
-if [ -z "$LIBPATH" ]; then
- LIBPATH=`pwd`/..
+if pkg-config --exists "sigc++-2.0 >= 2.5.1"; then
+ CFLAGS="$CFLAGS -std=c++11"
+fi
+MACHINE="`uname -m`"
+SYSTEM="`uname -s`"
+if [ "$MACHINE" = "x86_64" ]; then
+ MULTIARCH="x86_64-linux-gnu"
+else
+ MULTIARCH="i386-linux-gnu"
+fi
+if [ "$SYSTEM" = "Darwin" -o "$SYSTEM" = "FreeBSD" ]; then
+ if which clang++ >/dev/null; then
+ COMPILER="clang++"
+ else
+ COMPILER="g++"
+ fi
+else
+ COMPILER="g++"
+fi
+if [ -e /usr/lib/libdl.so -o -e /usr/lib/$MULTIARCH/libdl.so ]; then
+ LIBS="$LIBS -ldl"
fi
-mkdir -p include/msp
+while [ "$1" ]; do
+ case $1 in
+ --libpath)
+ shift
+ LIBPATH="$1"
+ ;;
+ --libpath=*)
+ LIBPATH="${1#--libpath=}"
+ ;;
+ --prefix)
+ shift
+ PREFIX="$1"
+ ;;
+ --prefix=*)
+ PREFIX="${1#--prefix=}"
+ ;;
+ --keep-temp)
+ KEEP_TEMP=yes
+ ;;
+ --compiler)
+ shift
+ COMPILER="$1"
+ CUSTOM_COMPILER=yes
+ ;;
+ --compiler=*)
+ COMPILER="${1#--compiler=}"
+ CUSTOM_COMPILER=yes
+ ;;
+ --debug)
+ DEBUG=-g
+ ;;
+ *)
+ echo "Unknown option $1"
+ exit 1
+ ;;
+ esac
+ shift
+done
+
+if [ -z "$LIBPATH" -o "${LIBPATH%%/*}" ]; then
+ LIBPATH=`pwd`/${LIBPATH:-..}
+fi
+
+rm -rf "$INCLUDEDIR"
+mkdir -p "$INCLUDEDIR/msp"
sources=source/*.cpp
+use_overlays="unix generic"
+missing=""
for i in $REQUIRED; do
- path=$LIBPATH/$i
- if [ ! -e $path ]; then
- echo $i missing
- exit 1
- fi
- if [ $i = "core" ]; then
- ln -sf $path/source/core include/msp/core
- ln -sf $path/source/time include/msp/time
- elif [ $i = "misc" ]; then
- ln -sf $path/source/*.h include/msp
+ path=`ls -1d $LIBPATH/*$i* 2>/dev/null | head -n1`
+ if [ -z "$path" -o ! -d "$path/source" ]; then
+ missing="$missing msp$i"
else
- ln -sf $path/source include/msp/$i
+ if [ "`echo $path/source/*.cpp`" = "$path/source/*.cpp" ]; then
+ subdirs=`find "$path/source" -mindepth 1 -maxdepth 1 -type d`
+ else
+ subdirs="$path/source"
+ fi
+
+ check_overlay=""
+ for j in $use_overlays; do
+ [ -z "$check_overlay" ] || check_overlay="$check_overlay -o "
+ check_overlay="${check_overlay}-name $j"
+ done
+ findargs="$subdirs"
+ if [ "$check_overlay" ]; then
+ findargs="$findargs -mindepth 1 ( -type d ! ( $check_overlay ) -prune -false ) -o "
+ fi
+
+ headers=`find $findargs -name '*.h'`
+ for j in $headers; do
+ rel=${j#$path/source/}
+ comp=${rel%%/*}
+ if [ "$comp" = "$rel" ]; then
+ comp=$i
+ fi
+ mkdir -p "$INCLUDEDIR/msp/$comp"
+ ln -sf $j "$INCLUDEDIR/msp/$comp/"
+ done
+ sources="$sources `find $findargs -name '*.cpp'`"
fi
- sources="$sources `find $path/source -name '*.cpp'`"
done
-g++ $sources -o builder-stage1 $CFLAGS $LIBS
+if [ ! -z "$missing" ]; then
+ echo "The following libraries were not found:$missing"
+ echo "I looked for them in $LIBPATH"
+ echo "If they are somewhere else, please add --libpath=PATH to the command line."
+ exit 1
+fi
+
+echo "Compiling builder-stage1. This may take several minutes."
+objects=""
+for i in $sources; do
+ obj=`mktemp temp/bootstrap/XXXXXX`
+ mv $obj $obj.o
+ obj=$obj.o
+ dir=${i%/*}
+ flags="$CFLAGS"
+ for j in $use_overlays; do
+ dir=${dir%/$j}
+ if [ -d "$dir/$j" ]; then
+ flags="$flags -iquote $dir -iquote $dir/$j"
+ break
+ fi
+ done
+ $COMPILER -c $DEBUG $i -o $obj $flags
+ objects="$objects $obj"
+done
+$COMPILER $objects -o builder-stage1 $LIBS
+
+if [ "$KEEP_TEMP" != "yes" ]; then
+ echo "Cleaning up"
+ rm -r temp/bootstrap
+fi
+
+echo "Using builder-stage1 to compile builder."
+ARGS=
+if [ "$PREFIX" ]; then
+ ARGS="$ARGS --prefix='$PREFIX'"
+fi
+if [ "$CUSTOM_COMPILER" = "yes" ]; then
+ ARGS="$ARGS CXX=$COMPILER"
+fi
+eval "./builder-stage1 $ARGS"