Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Shell script for building the Boost Framework for iOS (armv7, armv7s, arm64), the iOS Simulator (i386, x86_64), and Mac OSX (i386, x86_64). Creates a set of universal libraries and a pseudo-framework to facilitate using Boost C++ in Xcode. Tested with Boost C++ 1.57.0.
# Shell script for building Boost C++ for iOS (armv7, armv7s, arm64), the iOS Simulator
# (i386, x86_64), and Mac OSX (i386, x86_64).
#
# Creates a set of universal libraries and a pseudo-framework to facilitate using
# Boost C++ in Xcode.
#
# To configure the script, define:
#
# BOOST_VERSION Boost C++ version (e.g. 1.57.0)
# BOOST_LIBS: which Boost C++ libraries to build
#
# This script will automatically select the newest available iOS and OSX SDK.
# To select a specific SDK version, edit IOS_SDKVERSION and/or OSX_SDKVERSION as
# indicated in the comments below.
#
# This script will automatically attempt to download the source tar.bz archive of the
# Boost C++ version you want to build. Alternatively, you can download the source tar.bz
# archive yourself and place it in the same directory as this script.
#===============================================================================
: ${BOOST_VERSION:=1.57.0}
: ${BOOST_VERSION2:=1_57_0}
: ${BOOST_LIBS:="thread signals filesystem regex program_options system serialization wave iostreams"}
# Uncomment the following line to automatically select the newest available iOS SDK version:
: ${IOS_SDKVERSION:=`xcodebuild -showsdks | grep iphoneos | egrep "[[:digit:]]+\.[[:digit:]]+" -o | tail -1`}
# Uncomment the following line to select a specific iOS SDK version (e.g. 8.1):
# : ${IOS_SDKVERSION:=8.1}
# Uncomment the following line to automatically select the newest available OSX SDK version:
: ${OSX_SDKVERSION:=`xcodebuild -showsdks | grep macosx | egrep "[[:digit:]]+\.[[:digit:]]+" -o | tail -1`}
# Uncomment the following line to select a specific OSX SDK version (e.g. 10.10):
# : ${OSX_SDKVERSION:=10.10}
: ${XCODE_ROOT:=`xcode-select -print-path`}
: ${EXTRA_CPPFLAGS:="-DBOOST_AC_USE_PTHREADS -DBOOST_SP_USE_PTHREADS -std=c++11 -stdlib=libc++"}
# The EXTRA_CPPFLAGS definition works around a thread race issue in
# shared_ptr. I encountered this historically and have not verified that
# the fix is no longer required. Without using the posix thread primitives
# an invalid compare-and-swap ARM instruction (non-thread-safe) was used for the
# shared_ptr use count causing nasty and subtle bugs.
#
# Should perhaps also consider/use instead: -BOOST_SP_USE_PTHREADS
: ${TARBALLDIR:=`pwd`}
: ${SRCDIR:=`pwd`/src}
: ${IOSBUILDDIR:=`pwd`/ios/build}
: ${OSXBUILDDIR:=`pwd`/osx/build}
: ${PREFIXDIR:=`pwd`/ios/prefix}
: ${IOSFRAMEWORKDIR:=`pwd`/ios/framework}
: ${OSXFRAMEWORKDIR:=`pwd`/osx/framework}
BOOST_TARBALL=$TARBALLDIR/boost_$BOOST_VERSION2.tar.bz2
BOOST_SRC=$SRCDIR/boost_${BOOST_VERSION2}
#===============================================================================
IOS_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 $OSXBUILDDIR
rm -rf $PREFIXDIR
rm -rf $IOSFRAMEWORKDIR/$FRAMEWORK_NAME.framework
rm -rf $OSXFRAMEWORKDIR/$FRAMEWORK_NAME.framework
doneSection
}
#===============================================================================
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()
{
cp $BOOST_SRC/tools/build/src/user-config.jam-bk $BOOST_SRC/tools/build/src/user-config.jam
}
#===============================================================================
updateBoost()
{
echo Updating boost into $BOOST_SRC...
cp $BOOST_SRC/tools/build/src/user-config.jam $BOOST_SRC/tools/build/src/user-config.jam-bk
cat >> $BOOST_SRC/tools/build/src/user-config.jam <<EOF
using darwin : ${IOS_SDKVERSION}~iphone
: $XCODE_ROOT/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch armv7 -arch armv7s -arch arm64 -fvisibility=hidden -fvisibility-inlines-hidden $EXTRA_CPPFLAGS
: <striper> <root>$XCODE_ROOT/Platforms/iPhoneOS.platform/Developer
: <architecture>arm <target-os>iphone
;
using darwin : ${IOS_SDKVERSION}~iphonesim
: g++ -arch i386 -arch x86_64 -fvisibility=hidden -fvisibility-inlines-hidden $EXTRA_CPPFLAGS
: <striper> <root>$XCODE_ROOT/Platforms/iPhoneSimulator.platform/Developer
: <architecture>x86 <target-os>iphone
;
using darwin : ${OSX_SDKVERSION}
: g++ -arch i386 -arch x86_64 -fvisibility=hidden -fvisibility-inlines-hidden $EXTRA_CPPFLAGS
: <striper> <root>$XCODE_ROOT/Platforms/MacOSX.platform/Developer
: <architecture>x86 <target-os>darwin
;
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${IOS_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
}
#===============================================================================
buildBoostForIOS()
{
cd $BOOST_SRC
echo Building Boost for iPhone
# Install this one so we can copy the includes for the frameworks...
./b2 -j16 --build-dir=iphone-build --stagedir=iphone-build/stage --prefix=$PREFIXDIR toolset=darwin architecture=arm target-os=iphone macosx-version=iphone-${IOS_SDKVERSION} define=_LITTLE_ENDIAN link=static stage
./b2 -j16 --build-dir=iphone-build --stagedir=iphone-build/stage --prefix=$PREFIXDIR toolset=darwin architecture=arm target-os=iphone macosx-version=iphone-${IOS_SDKVERSION} define=_LITTLE_ENDIAN link=static install
doneSection
echo Building Boost for iPhoneSimulator
./b2 -j16 --build-dir=iphonesim-build --stagedir=iphonesim-build/stage toolset=darwin-${IOS_SDKVERSION}~iphonesim architecture=x86 target-os=iphone macosx-version=iphonesim-${IOS_SDKVERSION} link=static stage
doneSection
echo building Boost for OSX
./b2 -j16 --build-dir=osx-build --stagedir=osx-build/stage toolset=clang cxxflags="-std=c++11 -stdlib=libc++ -arch i386 -arch x86_64" linkflags="-stdlib=libc++" link=static threading=multi macosx-version=${OSX_SDKVERSION} stage
doneSection
}
#===============================================================================
unpackArchive()
{
BUILDDIR=$1
LIBNAME=$2
echo "Unpacking $LIBNAME"
mkdir -p $BUILDDIR/$LIBNAME
rm $BUILDDIR/$LIBNAME/*.o
rm $BUILDDIR/$LIBNAME/*.SYMDEF*
(
cd $BUILDDIR/$NAME; ar -x ../../libboost_$NAME.a;
for FILE in *.o; do
NEW_FILE="${NAME}_${FILE}"
mv $FILE $NEW_FILE
done
)
}
scrunchAllLibsTogetherInOneLibPerPlatform()
{
cd $BOOST_SRC
# iOS Device
mkdir -p $IOSBUILDDIR/armv7/obj
mkdir -p $IOSBUILDDIR/armv7s/obj
mkdir -p $IOSBUILDDIR/arm64/obj
# iOS Simulator
mkdir -p $IOSBUILDDIR/i386/obj
mkdir -p $IOSBUILDDIR/x86_64/obj
# OSX
mkdir -p $OSXBUILDDIR/i386/obj
mkdir -p $OSXBUILDDIR/x86_64/obj
ALL_LIBS=""
echo Splitting all existing fat binaries...
for NAME in $BOOST_LIBS; do
if [ "$NAME" == "test" ]; then
NAME="unit_test_framework"
fi
ALL_LIBS="$ALL_LIBS libboost_$NAME.a"
$IOS_DEV_CMD lipo "iphone-build/stage/lib/libboost_$NAME.a" -thin armv7 -o $IOSBUILDDIR/armv7/libboost_$NAME.a
$IOS_DEV_CMD lipo "iphone-build/stage/lib/libboost_$NAME.a" -thin armv7s -o $IOSBUILDDIR/armv7s/libboost_$NAME.a
$IOS_DEV_CMD lipo "iphone-build/stage/lib/libboost_$NAME.a" -thin arm64 -o $IOSBUILDDIR/arm64/libboost_$NAME.a
$SIM_DEV_CMD lipo "iphonesim-build/stage/lib/libboost_$NAME.a" -thin i386 -o $IOSBUILDDIR/i386/libboost_$NAME.a
$SIM_DEV_CMD lipo "iphonesim-build/stage/lib/libboost_$NAME.a" -thin x86_64 -o $IOSBUILDDIR/x86_64/libboost_$NAME.a
$IOS_DEV_CMD lipo "osx-build/stage/lib/libboost_$NAME.a" -thin i386 -o $OSXBUILDDIR/i386/libboost_$NAME.a
$IOS_DEV_CMD lipo "osx-build/stage/lib/libboost_$NAME.a" -thin x86_64 -o $OSXBUILDDIR/x86_64/libboost_$NAME.a
done
echo "Decomposing each architecture's .a files"
for NAME in $BOOST_LIBS; do
if [ "$NAME" == "test" ]; then
NAME="unit_test_framework"
fi
echo "Decomposing libboost_${NAME}.a"
unpackArchive "$IOSBUILDDIR/armv7/obj" $NAME
unpackArchive "$IOSBUILDDIR/armv7s/obj" $NAME
unpackArchive "$IOSBUILDDIR/arm64/obj" $NAME
unpackArchive "$IOSBUILDDIR/i386/obj" $NAME
unpackArchive "$IOSBUILDDIR/x86_64/obj" $NAME
unpackArchive "$OSXBUILDDIR/i386/obj" $NAME
unpackArchive "$OSXBUILDDIR/x86_64/obj" $NAME
done
echo "Linking each architecture into an uberlib ($ALL_LIBS => libboost.a )"
rm $IOSBUILDDIR/*/libboost.a
rm $OSXBUILDDIR/*/libboost.a
for NAME in $BOOST_LIBS; do
if [ "$NAME" == "test" ]; then
NAME="unit_test_framework"
fi
echo $NAME
echo ...armv7
(cd $IOSBUILDDIR/armv7; $IOS_DEV_CMD ar crus libboost.a obj/$NAME/*.o; )
echo ...armv7s
(cd $IOSBUILDDIR/armv7s; $IOS_DEV_CMD ar crus libboost.a obj/$NAME/*.o; )
echo ...arm64
(cd $IOSBUILDDIR/arm64; $IOS_DEV_CMD ar crus libboost.a obj/$NAME/*.o; )
echo ...i386
(cd $IOSBUILDDIR/i386; $SIM_DEV_CMD ar crus libboost.a obj/$NAME/*.o; )
echo ...x86_64
(cd $IOSBUILDDIR/x86_64; $SIM_DEV_CMD ar crus libboost.a obj/$NAME/*.o; )
echo ...osx-i386
(cd $OSXBUILDDIR/i386; $OSX_DEV_CMD ar crus libboost.a obj/$NAME/*.o; )
echo ...osx-x86_64
(cd $OSXBUILDDIR/x86_64; $OSX_DEV_CMD ar crus libboost.a obj/$NAME/*.o; )
done
}
#===============================================================================
buildFramework()
{
: ${1:?}
FRAMEWORKDIR=$1
BUILDDIR=$2
VERSION_TYPE=Alpha
FRAMEWORK_NAME=boost
FRAMEWORK_VERSION=A
FRAMEWORK_CURRENT_VERSION=$BOOST_VERSION
FRAMEWORK_COMPATIBILITY_VERSION=$BOOST_VERSION
FRAMEWORK_BUNDLE=$FRAMEWORKDIR/$FRAMEWORK_NAME.framework
echo "Framework: Building $FRAMEWORK_BUNDLE from $BUILDDIR..."
rm -rf $FRAMEWORK_BUNDLE
echo "Framework: Setting up directories..."
mkdir -p $FRAMEWORK_BUNDLE
mkdir -p $FRAMEWORK_BUNDLE/Versions
mkdir -p $FRAMEWORK_BUNDLE/Versions/$FRAMEWORK_VERSION
mkdir -p $FRAMEWORK_BUNDLE/Versions/$FRAMEWORK_VERSION/Resources
mkdir -p $FRAMEWORK_BUNDLE/Versions/$FRAMEWORK_VERSION/Headers
mkdir -p $FRAMEWORK_BUNDLE/Versions/$FRAMEWORK_VERSION/Documentation
echo "Framework: Creating symlinks..."
ln -s $FRAMEWORK_VERSION $FRAMEWORK_BUNDLE/Versions/Current
ln -s Versions/Current/Headers $FRAMEWORK_BUNDLE/Headers
ln -s Versions/Current/Resources $FRAMEWORK_BUNDLE/Resources
ln -s Versions/Current/Documentation $FRAMEWORK_BUNDLE/Documentation
ln -s Versions/Current/$FRAMEWORK_NAME $FRAMEWORK_BUNDLE/$FRAMEWORK_NAME
FRAMEWORK_INSTALL_NAME=$FRAMEWORK_BUNDLE/Versions/$FRAMEWORK_VERSION/$FRAMEWORK_NAME
echo "Lipoing library into $FRAMEWORK_INSTALL_NAME..."
$IOS_DEV_CMD lipo -create $BUILDDIR/*/libboost.a -o "$FRAMEWORK_INSTALL_NAME" || abort "Lipo $1 failed"
echo "Framework: Copying includes..."
cp -r $PREFIXDIR/include/boost/* $FRAMEWORK_BUNDLE/Headers/
echo "Framework: Creating plist..."
cat > $FRAMEWORK_BUNDLE/Resources/Info.plist <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleExecutable</key>
<string>${FRAMEWORK_NAME}</string>
<key>CFBundleIdentifier</key>
<string>org.boost</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>${FRAMEWORK_CURRENT_VERSION}</string>
</dict>
</plist>
EOF
doneSection
}
#===============================================================================
# Execution starts here
#===============================================================================
mkdir -p $IOSBUILDDIR
cleanEverythingReadyToStart #may want to comment if repeatedly running during dev
restoreBoost
echo "BOOST_VERSION: $BOOST_VERSION"
echo "BOOST_LIBS: $BOOST_LIBS"
echo "BOOST_SRC: $BOOST_SRC"
echo "IOSBUILDDIR: $IOSBUILDDIR"
echo "OSXBUILDDIR: $OSXBUILDDIR"
echo "PREFIXDIR: $PREFIXDIR"
echo "IOSFRAMEWORKDIR: $IOSFRAMEWORKDIR"
echo "OSXFRAMEWORKDIR: $OSXFRAMEWORKDIR"
echo "IOS_SDKVERSION: $IOS_SDKVERSION"
echo "OSX_SDKVERSION: $OSX_SDKVERSION"
echo "XCODE_ROOT: $XCODE_ROOT"
echo
downloadBoost
unpackBoost
inventMissingHeaders
bootstrapBoost
updateBoost
buildBoostForIOS
scrunchAllLibsTogetherInOneLibPerPlatform
buildFramework $IOSFRAMEWORKDIR $IOSBUILDDIR
buildFramework $OSXFRAMEWORKDIR $OSXBUILDDIR
restoreBoost
echo "Completed successfully"
#===============================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.