From e6868b874c7d3176d9e9941ac7858d2a9b5dcdc3 Mon Sep 17 00:00:00 2001
From: Romain Bertozzi <romain.bertozzi@savoirfairelinux.com>
Date: Mon, 28 Nov 2016 15:30:55 -0500
Subject: [PATCH] add build boost for iOS script

---
 build-boost-iOS-libc++ | 463 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 463 insertions(+)
 create mode 100755 build-boost-iOS-libc++

diff --git a/build-boost-iOS-libc++ b/build-boost-iOS-libc++
new file mode 100755
index 0000000..cca155e
--- /dev/null
+++ b/build-boost-iOS-libc++
@@ -0,0 +1,463 @@
+#! /bin/bash
+#
+#===============================================================================
+# Filename:  build-boost-libc++.sh
+# Author:    Pete Goodliffe, Daniel Rosser
+# Copyright: (c) Copyright 2009 Pete Goodliffe, 2013-2015 Daniel Rosser
+# Licence:   Please feel free to use this, with attribution
+# Modified version ## for ofxiOSBoost
+#===============================================================================
+#
+# Builds a Boost framework for the iPhone.
+# Creates a set of universal libraries that can be used on an iPhone and in the
+# iPhone simulator. Then creates a pseudo-framework to make using boost in Xcode
+# less painful.
+#
+# To configure the script, define:
+#    BOOST_LIBS:        which libraries to build
+#    IPHONE_SDKVERSION: iPhone SDK version (e.g. 8.0)
+#
+# Then go get the source tar.bz of the boost you want to build, shove it in the
+# same directory as this script, and run "./boost.sh". Grab a cuppa. And voila.
+#===============================================================================
+here="`dirname \"$0\"`"
+echo "cd-ing to $here"
+cd "$here" || exit 1
+
+CPPSTD=c++11    #c++89, c++99, c++14
+STDLIB=libc++   # libstdc++
+COMPILER=clang++
+PARALLEL_MAKE=16   # how many threads to make boost with
+
+BOOST_V1=1.60.0
+BOOST_V2=1_60_0
+
+OUTPUT_DIR_LIB=""
+OUTPUT_DIR_SRC=""
+
+MIN_IOS_VERSION=8.0
+
+BITCODE=""
+
+for i in "$@"
+do
+  case $i in
+    --includedir=*)
+    OUTPUT_DIR_SRC="${i#*=}"
+    shift # past argument=value
+    ;;
+    --libdir=*)
+    OUTPUT_DIR_LIB="${i#*=}"
+    shift # past argument=value
+    ;;
+    --boostVersion1=*)
+    BOOST_V1="${i#*=}"
+    shift # past argument=value
+    ;;
+    --boostVersion2=*)
+    BOOST_V2="${i#*=}"
+    shift # past argument=value
+    ;;
+    CXXFLAGS=*)
+    FLAGS="${i#*=}"
+    for flag in $FLAGS; do
+      case "$flag" in
+        -stdlib=*)
+          STDLIB="${flag#*=}"
+          continue
+        ;;
+        -std=*)
+          CPPSTD="${flag#*=}"
+          continue
+        ;;
+        -fembed-bitcode*)
+          BITCODE="-fembed-bitcode"
+          continue
+        ;;
+        -miphoneos-version-min*)
+          MIN_IOS_VERSION="${flag#*=}"
+          continue
+        ;;
+      esac
+    done
+    shift
+    ;;
+    *)
+            # unknown option
+    ;;
+  esac
+done
+
+CURRENTPATH=`pwd`
+LOGDIR="$CURRENTPATH/build/logs/"
+SDKVERSION=`xcrun -sdk iphoneos --show-sdk-version`
+OSX_SDKVERSION=`xcrun -sdk macosx --show-sdk-version`
+DEVELOPER=`xcode-select -print-path`
+XCODE_ROOT=`xcode-select -print-path`
+
+if [ ! -d "$DEVELOPER" ]; then
+  echo "xcode path is not set correctly $DEVELOPER does not exist (most likely because of xcode > 4.3)"
+  echo "run"
+  echo "sudo xcode-select -switch <xcode path>"
+  echo "for default installation:"
+  echo "sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer"
+  exit 1
+fi
+
+case $DEVELOPER in
+     *\ * )
+           echo "Your Xcode path contains whitespaces, which is not supported."
+           exit 1
+          ;;
+esac
+
+case $CURRENTPATH in
+     *\ * )
+           echo "Your path contains whitespaces, which is not supported by 'make install'."
+           exit 1
+          ;;
+esac
+
+: ${BOOST_LIBS:="random system"}
+: ${IPHONE_SDKVERSION:=`xcodebuild -showsdks | grep iphoneos | egrep "[[:digit:]]+\.[[:digit:]]+" -o | tail -1`}
+: ${EXTRA_CPPFLAGS:="-fPIC -DBOOST_SP_USE_SPINLOCK -std=$CPPSTD -stdlib=$STDLIB -miphoneos-version-min=$MIN_IOS_VERSION $BITCODE -fvisibility=hidden -fvisibility-inlines-hidden"}
+
+: ${TARBALLDIR:=`pwd`}
+: ${SRCDIR:=`pwd`}
+: ${IOSBUILDDIR:=`pwd`/build/libs/boost/lib}
+: ${IOSINCLUDEDIR:=`pwd`/build/libs/boost/include/boost}
+: ${PREFIXDIR:=`pwd`/build/ios/prefix}
+: ${COMPILER:="clang++"}
+#: ${OUTPUT_DIR:=`pwd`/libs/boost/}
+#: ${OUTPUT_DIR_LIB:=`pwd`/libs/boost/ios/}
+OUTPUT_DIR_SRC+=/boost
+
+: ${BOOST_VERSION:=$BOOST_V1}
+: ${BOOST_VERSION2:=$BOOST_V2}
+
+BOOST_TARBALL=$TARBALLDIR/boost_$BOOST_VERSION2.tar.bz2
+BOOST_SRC=$SRCDIR
+BOOST_INCLUDE=$BOOST_SRC/boost
+
+
+#===============================================================================
+ARM_DEV_CMD="xcrun --sdk iphoneos"
+SIM_DEV_CMD="xcrun --sdk iphonesimulator"
+OSX_DEV_CMD="xcrun --sdk macosx"
+
+#===============================================================================
+
+
+#===============================================================================
+# Functions
+#===============================================================================
+
+abort()
+{
+    echo
+    echo "Aborted: $@"
+    exit 1
+}
+
+doneSection()
+{
+    echo
+    echo "================================================================="
+    echo "Done"
+    echo
+}
+
+#===============================================================================
+
+cleanEverythingReadyToStart()
+{
+    echo Cleaning everything before we start to build...
+
+    rm -rf iphone-build iphonesim-build osx-build
+    rm -rf $IOSBUILDDIR
+    rm -rf $PREFIXDIR
+    rm -rf $IOSINCLUDEDIR
+    rm -rf $TARBALLDIR/build
+    rm -rf $LOGDIR
+
+    doneSection
+}
+
+postcleanEverything()
+{
+	echo Cleaning everything after the build...
+
+	rm -rf iphone-build iphonesim-build osx-build
+	rm -rf $PREFIXDIR
+	rm -rf $IOSBUILDDIR/armv6/obj
+    rm -rf $IOSBUILDDIR/armv7/obj
+    #rm -rf $IOSBUILDDIR/armv7s/obj
+	rm -rf $IOSBUILDDIR/arm64/obj
+    rm -rf $IOSBUILDDIR/i386/obj
+	rm -rf $IOSBUILDDIR/x86_64/obj
+    rm -rf $TARBALLDIR/build
+    rm -rf $LOGDIR
+	doneSection
+}
+
+prepare()
+{
+
+    mkdir -p $LOGDIR
+#    mkdir -p $OUTPUT_DIR
+    mkdir -p $OUTPUT_DIR_SRC
+    mkdir -p $OUTPUT_DIR_LIB
+
+}
+
+#===============================================================================
+
+downloadBoost()
+{
+    if [ ! -s $TARBALLDIR/boost_${BOOST_VERSION2}.tar.bz2 ]; then
+        echo "Downloading boost ${BOOST_VERSION}"
+        curl -L -o $TARBALLDIR/boost_${BOOST_VERSION2}.tar.bz2 http://sourceforge.net/projects/boost/files/boost/${BOOST_VERSION}/boost_${BOOST_VERSION2}.tar.bz2/download
+    fi
+
+    doneSection
+}
+
+#===============================================================================
+
+unpackBoost()
+{
+    [ -f "$BOOST_TARBALL" ] || abort "Source tarball missing."
+
+    echo Unpacking boost into $SRCDIR...
+
+    [ -d $SRCDIR ]    || mkdir -p $SRCDIR
+    [ -d $BOOST_SRC ] || ( cd $SRCDIR; tar xfj $BOOST_TARBALL )
+    [ -d $BOOST_SRC ] && echo "    ...unpacked as $BOOST_SRC"
+
+    doneSection
+}
+
+#===============================================================================
+
+restoreBoost()
+{
+    mv $BOOST_SRC/tools/build/example/user-config.jam.bk $BOOST_SRC/tools/build/example/user-config.jam
+}
+
+#===============================================================================
+
+updateBoost()
+{
+    echo Updating boost into $BOOST_SRC...
+    local CROSS_TOP_IOS="${DEVELOPER}/Platforms/iPhoneOS.platform/Developer"
+    local CROSS_SDK_IOS="iPhoneOS${SDKVERSION}.sdk"
+    local CROSS_TOP_SIM="${DEVELOPER}/Platforms/iPhoneSimulator.platform/Developer"
+    local CROSS_SDK_SIM="iPhoneSimulator${SDKVERSION}.sdk"
+    local BUILD_TOOLS="${DEVELOPER}"
+
+    cp $BOOST_SRC/tools/build/example/user-config.jam $BOOST_SRC/tools/build/example/user-config.jam.bk
+
+    cat >> $BOOST_SRC/tools/build/example/user-config.jam <<EOF
+using darwin : ${IPHONE_SDKVERSION}~iphone
+: $XCODE_ROOT/Toolchains/XcodeDefault.xctoolchain/usr/bin/$COMPILER -arch armv7 -arch arm64 $EXTRA_CPPFLAGS "-isysroot ${CROSS_TOP_IOS}/SDKs/${CROSS_SDK_IOS}" -I${CROSS_TOP_IOS}/SDKs/${CROSS_SDK_IOS}/usr/include/
+: <striper> <root>$XCODE_ROOT/Platforms/iPhoneOS.platform/Developer
+: <architecture>arm <target-os>iphone
+;
+using darwin : ${IPHONE_SDKVERSION}~iphonesim
+: $XCODE_ROOT/Toolchains/XcodeDefault.xctoolchain/usr/bin/$COMPILER -arch i386 -arch x86_64 $EXTRA_CPPFLAGS "-isysroot ${CROSS_TOP_SIM}/SDKs/${CROSS_SDK_SIM}" -I${CROSS_TOP_SIM}/SDKs/${CROSS_SDK_SIM}/usr/include/
+: <striper> <root>$XCODE_ROOT/Platforms/iPhoneSimulator.platform/Developer
+: <architecture>x86 <target-os>iphone
+;
+EOF
+
+    doneSection
+}
+
+#===============================================================================
+
+inventMissingHeaders()
+{
+    # These files are missing in the ARM iPhoneOS SDK, but they are in the simulator.
+    # They are supported on the device, so we copy them from x86 SDK to a staging area
+    # to use them on ARM, too.
+    echo Invent missing headers
+
+    cp $XCODE_ROOT/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator${IPHONE_SDKVERSION}.sdk/usr/include/{crt_externs,bzlib}.h $BOOST_SRC
+}
+
+#===============================================================================
+
+bootstrapBoost()
+{
+    cd $BOOST_SRC
+
+    BOOST_LIBS_COMMA=$(echo $BOOST_LIBS | sed -e "s/ /,/g")
+    echo "Bootstrapping (with libs $BOOST_LIBS_COMMA)"
+    ./bootstrap.sh --with-libraries=$BOOST_LIBS_COMMA
+
+    doneSection
+}
+
+#===============================================================================
+
+buildBoostForIPhoneOS()
+{
+    cd $BOOST_SRC
+
+    # Install this one so we can copy the includes for the frameworks...
+
+
+    set +e
+    echo "------------------"
+    LOG="$LOGDIR/build-iphone-stage.log"
+    echo "Running bjam for iphone-build stage"
+    echo "To see status in realtime check:"
+    echo " ${LOG}"
+    echo "Please stand by..."
+    ./bjam -j${PARALLEL_MAKE} --build-dir=iphone-build -sBOOST_BUILD_USER_CONFIG=$BOOST_SRC/tools/build/example/user-config.jam --stagedir=iphone-build/stage --prefix=$PREFIXDIR --toolset=darwin-${IPHONE_SDKVERSION}~iphone cxxflags="-miphoneos-version-min=$MIN_IOS_VERSION -stdlib=$STDLIB $BITCODE" variant=release linkflags="-stdlib=$STDLIB" architecture=arm target-os=iphone macosx-version=iphone-${IPHONE_SDKVERSION} define=_LITTLE_ENDIAN link=static stage > "${LOG}" 2>&1
+    if [ $? != 0 ]; then
+        tail -n 100 "${LOG}"
+        echo "Problem while Building iphone-build stage - Please check ${LOG}"
+        exit 1
+    else
+        echo "iphone-build stage successful"
+    fi
+
+    echo "------------------"
+    LOG="$LOGDIR/build-iphone-install.log"
+    echo "Running bjam for iphone-build install"
+    echo "To see status in realtime check:"
+    echo " ${LOG}"
+    echo "Please stand by..."
+    ./bjam -j${PARALLEL_MAKE} --build-dir=iphone-build -sBOOST_BUILD_USER_CONFIG=$BOOST_SRC/tools/build/example/user-config.jam --stagedir=iphone-build/stage --prefix=$PREFIXDIR --toolset=darwin-${IPHONE_SDKVERSION}~iphone cxxflags="-miphoneos-version-min=$MIN_IOS_VERSION -stdlib=$STDLIB $BITCODE" variant=release linkflags="-stdlib=$STDLIB" architecture=arm target-os=iphone macosx-version=iphone-${IPHONE_SDKVERSION} define=_LITTLE_ENDIAN link=static install > "${LOG}" 2>&1
+    if [ $? != 0 ]; then
+        tail -n 100 "${LOG}"
+        echo "Problem while Building iphone-build install - Please check ${LOG}"
+        exit 1
+    else
+        echo "iphone-build install successful"
+    fi
+    doneSection
+
+    echo "------------------"
+    LOG="$LOGDIR/build-iphone-simulator-build.log"
+    echo "Running bjam for iphone-sim-build "
+    echo "To see status in realtime check:"
+    echo " ${LOG}"
+    echo "Please stand by..."
+    ./bjam -j${PARALLEL_MAKE} --build-dir=iphonesim-build -sBOOST_BUILD_USER_CONFIG=$BOOST_SRC/tools/build/example/user-config.jam --stagedir=iphonesim-build/stage --toolset=darwin-${IPHONE_SDKVERSION}~iphonesim architecture=x86 target-os=iphone variant=release cxxflags="-miphoneos-version-min=$MIN_IOS_VERSION -stdlib=$STDLIB $BITCODE" macosx-version=iphonesim-${IPHONE_SDKVERSION} link=static stage > "${LOG}" 2>&1
+    if [ $? != 0 ]; then
+        tail -n 100 "${LOG}"
+        echo "Problem while Building iphone-simulator build - Please check ${LOG}"
+        exit 1
+    else
+        echo "iphone-simulator build successful"
+    fi
+
+    doneSection
+}
+
+#===============================================================================
+
+scrunchAllLibsTogether()
+{
+    cd $BOOST_SRC
+
+    ALL_LIBS=$(find iphone-build/stage/lib -name "libboost_*.a" | sed -n 's/.*\(libboost_.*.a\)/\1/p' | paste -sd " " -)
+
+    for NAME in $ALL_LIBS; do
+      lipo -c "iphone-build/stage/lib/$NAME" "iphonesim-build/stage/lib/$NAME" -o "$PREFIXDIR/lib/$NAME"
+    done
+
+    echo "Completed Fat Lib"
+    echo "------------------"
+}
+
+#===============================================================================
+buildLibs()
+{
+  mkdir -p $IOSINCLUDEDIR
+  echo "------------------"
+  echo "Copying Libs to Final Dir $OUTPUT_DIR_LIB"
+  LOG="$LOGDIR/buildIncludes.log"
+  set +e
+
+  cp -r $PREFIXDIR/lib/*  $OUTPUT_DIR_LIB/ > "${LOG}" 2>&1
+  if [ $? != 0 ]; then
+      tail -n 100 "${LOG}"
+      echo "Problem while copying directories - Please check ${LOG}"
+      exit 1
+  else
+      echo "Copy of libs successful"
+  fi
+  echo "------------------"
+
+  doneSection
+}
+
+buildIncludes()
+{
+
+    mkdir -p $IOSINCLUDEDIR
+    echo "------------------"
+    echo "Copying Includes to Final Dir $OUTPUT_DIR_SRC"
+    LOG="$LOGDIR/buildIncludes.log"
+    set +e
+
+    cp -r $PREFIXDIR/include/boost/*  $OUTPUT_DIR_SRC/ > "${LOG}" 2>&1
+    if [ $? != 0 ]; then
+        tail -n 100 "${LOG}"
+        echo "Problem while copying includes - Please check ${LOG}"
+        exit 1
+    else
+        echo "Copy of Includes successful"
+    fi
+    echo "------------------"
+
+    doneSection
+}
+
+#===============================================================================
+# Execution starts here
+#===============================================================================
+
+mkdir -p $IOSBUILDDIR
+
+cleanEverythingReadyToStart
+restoreBoost
+
+echo "BOOST_VERSION:     $BOOST_VERSION"
+echo "BOOST_VERSION_1:   $BOOST_V1"
+echo "BOOST_VERSION_2:   $BOOST_V2"
+echo "BOOST_LIBS:        $BOOST_LIBS"
+echo "BOOST_SRC:         $BOOST_SRC"
+echo "IOSBUILDDIR:       $IOSBUILDDIR"
+echo "PREFIXDIR:         $PREFIXDIR"
+echo "IPHONE_SDKVERSION: $IPHONE_SDKVERSION"
+echo "XCODE_ROOT:        $XCODE_ROOT"
+echo "COMPILER:          $COMPILER"
+echo "STDLIB:            $STDLIB"
+if [ -z ${BITCODE} ]; then
+    echo "BITCODE EMBEDDED: NO $BITCODE"
+else
+    echo "BITCODE EMBEDDED: YES with: $BITCODE"
+fi
+
+#downloadBoost
+#unpackBoost
+#inventMissingHeaders
+prepare
+bootstrapBoost
+updateBoost
+buildBoostForIPhoneOS
+scrunchAllLibsTogether
+buildIncludes
+buildLibs
+
+#restoreBoost
+
+#postcleanEverything
+
+echo "Completed successfully"
+
+#===============================================================================
-- 
2.9.3 (Apple Git-75)

