Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
gtest-1.7.0 build script

Build Script for using Google Test 1.7.0 on iOS

#!/bin/bash
echo "$(tput setaf 2)"
echo "###################################################################"
echo "# Preparing to build Google Test for iOS"
echo "###################################################################"
echo "$(tput sgr0)"
# The results will be stored relative to the location
# where you stored this script, **not** relative to
# the location of the protobuf git repo.
PREFIX=`pwd`/gtest
if [ -d ${PREFIX} ]
then
rm -rf "${PREFIX}"
fi
mkdir -p "${PREFIX}/platform"
GTEST_VERSION=1.7.0
GTEST_RELEASE_URL=https://googletest.googlecode.com/files/gtest-${GTEST_VERSION}.zip
GTEST_RELEASE_DIRNAME=gtest-${GTEST_VERSION}
BUILD_MACOSX_X86_64=YES
BUILD_I386_IOSSIM=YES
BUILD_X86_64_IOSSIM=YES
BUILD_IOS_ARMV7=YES
BUILD_IOS_ARMV7S=YES
BUILD_IOS_ARM64=YES
GTEST_SRC_DIR=/tmp/gtest
DARWIN=darwin13.4.0
XCODEDIR=`xcode-select --print-path`
IOS_SDK_VERSION=`xcrun --sdk iphoneos --show-sdk-version`
MIN_SDK_VERSION=7.1
MACOSX_PLATFORM=${XCODEDIR}/Platforms/MacOSX.platform
MACOSX_SYSROOT=${MACOSX_PLATFORM}/Developer/MacOSX10.9.sdk
IPHONEOS_PLATFORM=`xcrun --sdk iphoneos --show-sdk-platform-path`
IPHONEOS_SYSROOT=`xcrun --sdk iphoneos --show-sdk-path`
IPHONESIMULATOR_PLATFORM=`xcrun --sdk iphonesimulator --show-sdk-platform-path`
IPHONESIMULATOR_SYSROOT=`xcrun --sdk iphonesimulator --show-sdk-path`
# Uncomment if you want to see more information about each invocation
# of clang as the builds proceed.
CLANG_VERBOSE="--verbose"
CC=clang
CXX=clang
CFLAGS="${CLANG_VERBOSE} -DNDEBUG -g -O0 -pipe -fPIC -fcxx-exceptions"
CXXFLAGS="${CLANG_VERBOSE} ${CFLAGS} -std=c++11 -stdlib=libc++"
LDFLAGS="-stdlib=libc++"
LIBS="-lc++ -lc++abi"
echo "PREFIX ..................... ${PREFIX}"
echo "GTEST_VERSION .............. ${GTEST_VERSION}"
echo "GTEST_RELEASE_URL .......... ${GTEST_RELEASE_URL}"
echo "GTEST_RELEASE_DIRNAME ...... ${GTEST_RELEASE_DIRNAME}"
echo "BUILD_MACOSX_X86_64 ........ ${BUILD_MACOSX_X86_64}"
echo "BUILD_I386_IOSSIM .......... ${BUILD_I386_IOSSIM}"
echo "BUILD_X86_64_IOSSIM ........ ${BUILD_X86_64_IOSSIM}"
echo "BUILD_IOS_ARMV7 ............ ${BUILD_IOS_ARMV7}"
echo "BUILD_IOS_ARMV7S ........... ${BUILD_IOS_ARMV7S}"
echo "BUILD_IOS_ARM64 ............ ${BUILD_IOS_ARM64}"
echo "GTEST_SRC_DIR .............. ${GTEST_SRC_DIR}"
echo "DARWIN ..................... ${DARWIN}"
echo "XCODEDIR ................... ${XCODEDIR}"
echo "IOS_SDK_VERSION ............ ${IOS_SDK_VERSION}"
echo "MIN_SDK_VERSION ............ ${MIN_SDK_VERSION}"
echo "MACOSX_PLATFORM ............ ${MACOSX_PLATFORM}"
echo "MACOSX_SYSROOT ............. ${MACOSX_SYSROOT}"
echo "IPHONEOS_PLATFORM .......... ${IPHONEOS_PLATFORM}"
echo "IPHONEOS_SYSROOT ........... ${IPHONEOS_SYSROOT}"
echo "IPHONESIMULATOR_PLATFORM ... ${IPHONESIMULATOR_PLATFORM}"
echo "IPHONESIMULATOR_SYSROOT .... ${IPHONESIMULATOR_SYSROOT}"
echo "CC ......................... ${CC}"
echo "CFLAGS ..................... ${CFLAGS}"
echo "CXX ........................ ${CXX}"
echo "CXXFLAGS ................... ${CXXFLAGS}"
echo "LDFLAGS .................... ${LDFLAGS}"
echo "LIBS ....................... ${LIBS}"
while true; do
read -p "Proceed with build? (y/n) " yn
case $yn in
[Yy]* ) break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
echo "$(tput setaf 2)"
echo "###################################################################"
echo "# Fetch Google Test"
echo "###################################################################"
echo "$(tput sgr0)"
(
if [ -d ${GTEST_SRC_DIR} ]
then
rm -rf ${GTEST_SRC_DIR}
fi
cd `dirname $GTEST_SRC_DIR}`
if [ -d ${GTEST_RELEASE_DIRNAME} ]
then
rm -rf "${GTEST_RELEASE_DIRNAME}"
fi
curl --location ${GTEST_RELEASE_URL} --output ${GTEST_RELEASE_DIRNAME}.tar.gz
tar xvf ${GTEST_RELEASE_DIRNAME}.tar.gz
mv "${GTEST_RELEASE_DIRNAME}" "${GTEST_SRC_DIR}"
rm ${GTEST_RELEASE_DIRNAME}.tar.gz
)
###################################################################
# This section contains the build commands to create the native
# gtest library for Mac OS X.
###################################################################
echo "$(tput setaf 2)"
echo "###################################################################"
echo "# x86_64 for Mac OS X"
echo "###################################################################"
echo "$(tput sgr0)"
if [ "${BUILD_MACOSX_X86_64}" == "YES" ]
then
(
cd ${GTEST_SRC_DIR}
make distclean
./configure --disable-shared --prefix=${PREFIX} --exec-prefix=${PREFIX}/platform/x86_64-mac "CC=${CC}" "CFLAGS=${CFLAGS} -arch x86_64" "CXX=${CXX}" "CXXFLAGS=${CXXFLAGS} -arch x86_64" "LDFLAGS=${LDFLAGS}" "LIBS=${LIBS}"
make
make check
cp -R lib "${PREFIX}/platform/x86_64-mac"
)
fi
###################################################################
# This section contains the build commands for each of the
# architectures that will be included in the universal binaries.
###################################################################
echo "$(tput setaf 2)"
echo "###########################"
echo "# i386 for iPhone Simulator"
echo "###########################"
echo "$(tput sgr0)"
if [ "${BUILD_I386_IOSSIM}" == "YES" ]
then
(
cd ${GTEST_SRC_DIR}
make distclean
./configure --build=x86_64-apple-${DARWIN} --host=i386-apple-${DARWIN} --disable-shared --prefix=${PREFIX} --exec-prefix=${PREFIX}/platform/i386-sim "CC=${CC}" "CFLAGS=${CFLAGS} -miphoneos-version-min=${MIN_SDK_VERSION} -arch i386 -isysroot ${IPHONESIMULATOR_SYSROOT}" "CXX=${CXX}" "CXXFLAGS=${CXXFLAGS} -arch i386 -isysroot ${IPHONESIMULATOR_SYSROOT}" LDFLAGS="-arch i386 -miphoneos-version-min=${MIN_SDK_VERSION} ${LDFLAGS}" "LIBS=${LIBS}"
make
cp -R lib "${PREFIX}/platform/i386-sim"
)
fi
echo "$(tput setaf 2)"
echo "#############################"
echo "# x86_64 for iPhone Simulator"
echo "#############################"
echo "$(tput sgr0)"
if [ "${BUILD_X86_64_IOSSIM}" == "YES" ]
then
(
cd ${GTEST_SRC_DIR}
make distclean
./configure --build=x86_64-apple-${DARWIN} --host=x86_64-apple-${DARWIN} --disable-shared --prefix=${PREFIX} --exec-prefix=${PREFIX}/platform/x86_64-sim "CC=${CC}" "CFLAGS=${CFLAGS} -miphoneos-version-min=${MIN_SDK_VERSION} -arch x86_64 -isysroot ${IPHONESIMULATOR_SYSROOT}" "CXX=${CXX}" "CXXFLAGS=${CXXFLAGS} -arch x86_64 -isysroot ${IPHONESIMULATOR_SYSROOT}" LDFLAGS="-arch x86_64 -miphoneos-version-min=${MIN_SDK_VERSION} ${LDFLAGS}" "LIBS=${LIBS}"
make
cp -R lib "${PREFIX}/platform/x86_64-sim"
)
fi
echo "$(tput setaf 2)"
echo "##################"
echo "# armv7 for iPhone"
echo "##################"
echo "$(tput sgr0)"
if [ "${BUILD_IOS_ARMV7}" == "YES" ]
then
(
cd ${GTEST_SRC_DIR}
make distclean
./configure --build=x86_64-apple-${DARWIN} --host=armv7-apple-${DARWIN} --disable-shared --prefix=${PREFIX} --exec-prefix=${PREFIX}/platform/armv7-ios "CC=${CC}" "CFLAGS=${CFLAGS} -miphoneos-version-min=${MIN_SDK_VERSION} -arch armv7 -isysroot ${IPHONEOS_SYSROOT}" "CXX=${CXX}" "CXXFLAGS=${CXXFLAGS} -arch armv7 -isysroot ${IPHONEOS_SYSROOT}" LDFLAGS="-arch armv7 -miphoneos-version-min=${MIN_SDK_VERSION} ${LDFLAGS}" "LIBS=${LIBS}"
make
cp -R lib "${PREFIX}/platform/armv7-ios"
)
fi
echo "$(tput setaf 2)"
echo "###################"
echo "# armv7s for iPhone"
echo "###################"
echo "$(tput sgr0)"
if [ "${BUILD_IOS_ARMV7S}" == "YES" ]
then
(
cd ${GTEST_SRC_DIR}
make distclean
./configure --build=x86_64-apple-${DARWIN} --host=armv7s-apple-${DARWIN} --disable-shared --prefix=${PREFIX} --exec-prefix=${PREFIX}/platform/armv7s-ios "CC=${CC}" "CFLAGS=${CFLAGS} -miphoneos-version-min=${MIN_SDK_VERSION} -arch armv7s -isysroot ${IPHONEOS_SYSROOT}" "CXX=${CXX}" "CXXFLAGS=${CXXFLAGS} -arch armv7s -isysroot ${IPHONEOS_SYSROOT}" LDFLAGS="-arch armv7s -miphoneos-version-min=${MIN_SDK_VERSION} ${LDFLAGS}" "LIBS=${LIBS}"
make
cp -R lib "${PREFIX}/platform/armv7s-ios"
)
fi
echo "$(tput setaf 2)"
echo "##################"
echo "# arm64 for iPhone"
echo "##################"
echo "$(tput sgr0)"
if [ "${BUILD_IOS_ARM64}" == "YES" ]
then
(
cd ${GTEST_SRC_DIR}
make distclean
./configure --build=x86_64-apple-${DARWIN} --host=arm --disable-shared --prefix=${PREFIX} --exec-prefix=${PREFIX}/platform/arm64-ios "CC=${CC}" "CFLAGS=${CFLAGS} -miphoneos-version-min=${MIN_SDK_VERSION} -arch arm64 -isysroot ${IPHONEOS_SYSROOT}" "CXX=${CXX}" "CXXFLAGS=${CXXFLAGS} -arch arm64 -isysroot ${IPHONEOS_SYSROOT}" LDFLAGS="-arch arm64 -miphoneos-version-min=${MIN_SDK_VERSION} ${LDFLAGS}" "LIBS=${LIBS}"
make
cp -R lib "${PREFIX}/platform/arm64-ios"
)
fi
echo "$(tput setaf 2)"
echo "###################################################################"
echo "# Create Universal Libraries and Finalize the packaging"
echo "###################################################################"
echo "$(tput sgr0)"
(
cd ${PREFIX}/platform
mkdir universal
lipo x86_64-sim/.libs/libgtest.a i386-sim/.libs/libgtest.a -create -output universal/libgtest-simulator.a
lipo arm64-ios/.libs/libgtest.a armv7s-ios/.libs/libgtest.a armv7-ios/.libs/libgtest.a -create -output universal/libgtest-device.a
lipo x86_64-sim/.libs/libgtest_main.a i386-sim/.libs/libgtest_main.a -create -output universal/libgtest_main-simulator.a
lipo arm64-ios/.libs/libgtest_main.a armv7s-ios/.libs/libgtest_main.a armv7-ios/.libs/libgtest_main.a -create -output universal/libgtest_main-device.a
lipo universal/libgtest-simulator.a universal/libgtest-device.a -create -output universal/libgtest.a
lipo universal/libgtest_main-simulator.a universal/libgtest_main-device.a -create -output universal/libgtest_main.a
)
(
cd ${PREFIX}
mkdir lib
mkdir include
cp -R platform/universal/* lib
cp -R ${GTEST_SRC_DIR}/include/* include
rm -rf platform
lipo -info lib/libgtest.a
lipo -info lib/libgtest_main.a
)
echo Done!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment