build-scripts/iosbuild.sh
changeset 10904 4dbd025c4eb9
parent 6334 85b8778a05c2
equal deleted inserted replaced
10903:875e0bb9646c 10904:4dbd025c4eb9
     1 #!/bin/sh
     1 #!/bin/sh
     2 #
     2 #
     3 # Build a fat binary for iOS
     3 # Build a fat binary for iOS
     4 # Based on fatbuild.sh and code from the Ignifuga Game Engine
       
     5 
     4 
     6 # Number of CPUs (for make -j)
     5 # Number of CPUs (for make -j)
     7 NCPU=`sysctl -n hw.ncpu`
     6 NCPU=`sysctl -n hw.ncpu`
     8 if test x$NJOB = x; then
     7 if test x$NJOB = x; then
     9     NJOB=$NCPU
     8     NJOB=$NCPU
    10 fi
     9 fi
    11 
    10 
    12 # SDK path
    11 SRC_DIR=$(cd `dirname $0`/..; pwd)
    13 XCODE_PATH=`xcode-select --print-path`
    12 if [ "$PWD" = "$SRC_DIR" ]; then
    14 if [ -z "$XCODE_PATH" ]; then
    13     PREFIX=$SRC_DIR/ios-build
    15     echo "Could not find XCode location (use xcode-select -switch to set the correct path)"
    14     mkdir $PREFIX
    16     exit 1
    15 else
       
    16     PREFIX=$PWD
    17 fi
    17 fi
    18 
    18 
    19 prepare_environment() {
    19 BUILD_I386_IOSSIM=YES
    20     ARCH=$1
    20 BUILD_X86_64_IOSSIM=YES
    21     
       
    22     if test x$SDK_VERSION = x; then
       
    23       export SDK_VERSION=`xcodebuild -showsdks | grep iphoneos | sed "s|.*iphoneos||"`
       
    24       if [ -z "$XCODE_PATH" ]; then
       
    25           echo "Could not find a valid iOS SDK"
       
    26           exit 1
       
    27       fi  
       
    28     fi
       
    29     
       
    30     case $ARCH in
       
    31         armv6)
       
    32             DEV_PATH="$XCODE_PATH/Platforms/iPhoneOS.platform/Developer"
       
    33             SDK_PATH="$DEV_PATH/SDKs/iPhoneOS$SDK_VERSION.sdk"
       
    34             ;;
       
    35         armv7)
       
    36             DEV_PATH="$XCODE_PATH/Platforms/iPhoneOS.platform/Developer"
       
    37             SDK_PATH="$DEV_PATH/SDKs/iPhoneOS$SDK_VERSION.sdk"
       
    38             ;;
       
    39         i386)
       
    40             DEV_PATH="$XCODE_PATH/Platforms/iPhoneSimulator.platform/Developer"
       
    41             SDK_PATH="$DEV_PATH/SDKs/iPhoneSimulator$SDK_VERSION.sdk"
       
    42             ;;
       
    43         *)
       
    44             echo "Unknown Architecture $ARCH"
       
    45             exit 1
       
    46             ;;
       
    47     esac
       
    48 
    21 
    49     if [ ! -d "$SDK_PATH" ]; then
    22 BUILD_IOS_ARMV7=YES
    50         echo "Could not find iOS SDK at $SDK_PATH"
    23 BUILD_IOS_ARMV7S=YES
    51         exit 1
    24 BUILD_IOS_ARM64=YES
    52     fi
       
    53 
    25 
    54     if test x$MIN_OS_VERSION = x; then
    26 # 13.4.0 - Mavericks
    55         export MIN_OS_VERSION="3.0"
    27 # 14.0.0 - Yosemite
    56     fi
    28 # 15.0.0 - El Capitan
    57     
    29 DARWIN=darwin15.0.0
    58     # Environment flags
       
    59     CFLAGS="-g -O2 -pipe -no-cpp-precomp -isysroot $SDK_PATH \
       
    60             -miphoneos-version-min=$MIN_OS_VERSION -I$SDK_PATH/usr/include/"
       
    61     LDFLAGS="-L$SDK_PATH/usr/lib/ -isysroot $SDK_PATH \
       
    62              -miphoneos-version-min=$MIN_OS_VERSION -static-libgcc"
       
    63     export CXXFLAGS="$CFLAGS"
       
    64     export CXXCPP="$DEV_PATH/usr/bin/llvm-cpp-4.2"
       
    65     export CPP="$CXXCPP"
       
    66     export CXX="$DEV_PATH/usr/bin/llvm-g++-4.2"
       
    67     export CC="$DEV_PATH/usr/bin/llvm-gcc-4.2"
       
    68     export LD="$DEV_PATH/usr/bin/ld"
       
    69     export AR="$DEV_PATH/usr/bin/ar"
       
    70     export AS="$DEV_PATH/usr/bin/ls"
       
    71     export NM="$DEV_PATH/usr/bin/nm"
       
    72     export RANLIB="$DEV_PATH/usr/bin/ranlib"
       
    73     export STRIP="$DEV_PATH/usr/bin/strip"
       
    74     
       
    75     # We dynamically load X11, so using the system X11 headers is fine.
       
    76     CONFIG_FLAGS="--disable-shared --enable-static"
       
    77     
       
    78     case $ARCH in
       
    79         armv6)
       
    80             export CONFIG_FLAGS="$CONFIG_FLAGS --host=armv6-apple-darwin"
       
    81             export CFLAGS="$CFLAGS -arch armv6"
       
    82             export LDFLAGS="$LDFLAGS -arch armv6"
       
    83             ;;
       
    84         armv7)
       
    85             export CONFIG_FLAGS="$CONFIG_FLAGS --host=armv7-apple-darwin"
       
    86             export CFLAGS="$CFLAGS -arch armv7"
       
    87             export LDFLAGS="$LDFLAGS -arch armv7"
       
    88             ;;
       
    89         i386)
       
    90             export CONFIG_FLAGS="$CONFIG_FLAGS --host=i386-apple-darwin"
       
    91             export CFLAGS="$CFLAGS -arch i386"
       
    92             export LDFLAGS="$LDFLAGS -arch i386"
       
    93             ;;
       
    94         *)
       
    95             echo "Unknown Architecture $ARCH"
       
    96             exit 1
       
    97             ;;
       
    98     esac
       
    99 }
       
   100 
    30 
   101 prepare_environment "armv6"
    31 XCODEDIR=`xcode-select --print-path`
   102 echo "Building with iOS SDK v$SDK_VERSION for iOS >= $MIN_OS_VERSION"
    32 IOS_SDK_VERSION=`xcrun --sdk iphoneos --show-sdk-version`
       
    33 MIN_SDK_VERSION=6.0
   103 
    34 
   104 #
    35 IPHONEOS_PLATFORM=`xcrun --sdk iphoneos --show-sdk-platform-path`
   105 # Find the configure script
    36 IPHONEOS_SYSROOT=`xcrun --sdk iphoneos --show-sdk-path`
   106 #
       
   107 srcdir=`dirname $0`/..
       
   108 srcdir=`cd $srcdir && pwd`
       
   109 auxdir=$srcdir/build-scripts
       
   110 cd $srcdir
       
   111 
    37 
   112 #
    38 IPHONESIMULATOR_PLATFORM=`xcrun --sdk iphonesimulator --show-sdk-platform-path`
   113 # Figure out which phase to build:
    39 IPHONESIMULATOR_SYSROOT=`xcrun --sdk iphonesimulator --show-sdk-path`
   114 # all,
       
   115 # configure, configure-armv6, configure-armv7, configure-i386
       
   116 # make, make-armv6, make-armv7, make-i386, merge
       
   117 # clean
       
   118 if test x"$1" = x; then
       
   119     phase=all
       
   120 else
       
   121     phase="$1"
       
   122 fi
       
   123 case $phase in
       
   124     all)
       
   125         configure_armv6="yes"
       
   126         configure_armv7="yes"
       
   127         configure_i386="yes"
       
   128         make_armv6="yes"
       
   129         make_armv7="yes"
       
   130         make_i386="yes"
       
   131         merge="yes"
       
   132         ;;
       
   133     configure)
       
   134         configure_armv6="yes"
       
   135         configure_armv7="yes"
       
   136         configure_i386="yes"
       
   137         ;;
       
   138     configure-armv6)
       
   139         configure_armv6="yes"
       
   140         ;;
       
   141     configure-armv7)
       
   142         configure_armv7="yes"
       
   143         ;;
       
   144     configure-i386)
       
   145         configure_i386="yes"
       
   146         ;;
       
   147     make)
       
   148         make_armv6="yes"
       
   149         make_armv7="yes"
       
   150         make_i386="yes"
       
   151         merge="yes"
       
   152         ;;
       
   153     make-armv6)
       
   154         make_armv6="yes"
       
   155         ;;
       
   156     make-armv7)
       
   157         make_armv7="yes"
       
   158         ;;
       
   159     make-i386)
       
   160         make_i386="yes"
       
   161         ;;
       
   162     merge)
       
   163         merge="yes"
       
   164         ;;
       
   165     clean)
       
   166         clean_armv6="yes"
       
   167         clean_armv7="yes"
       
   168         clean_i386="yes"
       
   169         ;;
       
   170     clean-armv6)
       
   171         clean_armv6="yes"
       
   172         ;;
       
   173     clean-armv7)
       
   174         clean_armv7="yes"
       
   175         ;;
       
   176     clean-i386)
       
   177         clean_i386="yes"
       
   178         ;;
       
   179     *)
       
   180         echo "Usage: $0 [all|configure[-armv6|-armv7|-i386]|make[-armv6|-armv7|-i386]|merge|clean[-armv6|-armv7|-i386]]"
       
   181         exit 1
       
   182         ;;
       
   183 esac
       
   184 
    40 
   185 #
    41 # Uncomment if you want to see more information about each invocation
   186 # Create the build directories
    42 # of clang as the builds proceed.
   187 #
    43 # CLANG_VERBOSE="--verbose"
   188 for dir in build build/armv6 build/armv7 build/i386; do
       
   189     if test -d $dir; then
       
   190         :
       
   191     else
       
   192         mkdir $dir || exit 1
       
   193     fi
       
   194 done
       
   195 
    44 
   196 #
    45 CC=clang
   197 # Build the armv6 binary
    46 CXX=clang
   198 #
    47 
   199 prepare_environment "armv6"
    48 SILENCED_WARNINGS="-Wno-unused-local-typedef -Wno-unused-function"
   200 if test x$configure_armv6 = xyes; then
    49 
   201     (cd build/armv6 && \
    50 CFLAGS="${CLANG_VERBOSE} ${SILENCED_WARNINGS} -DNDEBUG -g -O0 -pipe -fPIC -fobjc-arc"
   202      sh ../../configure $CONFIG_FLAGS CC="$CC" CXX="$CXX" CFLAGS="$CFLAGS" LDFLAGS="$LDFLAGS") || exit 2
    51 
   203      # configure is not yet fully ready for iOS, some manual patching is required
    52 echo "PREFIX ..................... ${PREFIX}"
   204      cp include/* build/armv6/include
    53 echo "BUILD_MACOSX_X86_64 ........ ${BUILD_MACOSX_X86_64}"
   205      cp include/SDL_config_iphoneos.h build/armv6/include/SDL_config.h || exit 2
    54 echo "BUILD_I386_IOSSIM .......... ${BUILD_I386_IOSSIM}"
   206      sed -i "" -e "s|^EXTRA_CFLAGS.*|EXTRA_CFLAGS=-I./include|g" build/armv6/Makefile || exit 2
    55 echo "BUILD_X86_64_IOSSIM ........ ${BUILD_X86_64_IOSSIM}"
   207      sed -i "" -e "s|^EXTRA_LDFLAGS.*|EXTRA_LDFLAGS=-lm|g" build/armv6/Makefile || exit 2
    56 echo "BUILD_IOS_ARMV7 ............ ${BUILD_IOS_ARMV7}"
   208 fi
    57 echo "BUILD_IOS_ARMV7S ........... ${BUILD_IOS_ARMV7S}"
   209 if test x$make_armv6 = xyes; then
    58 echo "BUILD_IOS_ARM64 ............ ${BUILD_IOS_ARM64}"
   210     (cd build/armv6 && make -j$NJOB) || exit 3
    59 echo "DARWIN ..................... ${DARWIN}"
   211 fi
    60 echo "XCODEDIR ................... ${XCODEDIR}"
   212 #
    61 echo "IOS_SDK_VERSION ............ ${IOS_SDK_VERSION}"
   213 # Build the armv7 binary
    62 echo "MIN_SDK_VERSION ............ ${MIN_SDK_VERSION}"
   214 #
    63 echo "IPHONEOS_PLATFORM .......... ${IPHONEOS_PLATFORM}"
   215 prepare_environment "armv7"
    64 echo "IPHONEOS_SYSROOT ........... ${IPHONEOS_SYSROOT}"
   216 if test x$configure_armv7 = xyes; then
    65 echo "IPHONESIMULATOR_PLATFORM ... ${IPHONESIMULATOR_PLATFORM}"
   217     (cd build/armv7 && \
    66 echo "IPHONESIMULATOR_SYSROOT .... ${IPHONESIMULATOR_SYSROOT}"
   218      sh ../../configure $CONFIG_FLAGS CC="$CC" CXX="$CXX" CFLAGS="$CFLAGS" LDFLAGS="$LDFLAGS") || exit 2
    67 echo "CC ......................... ${CC}"
   219      # configure is not yet fully ready for iOS, some manual patching is required
    68 echo "CFLAGS ..................... ${CFLAGS}"
   220      cp include/* build/armv7/include
    69 echo "CXX ........................ ${CXX}"
   221      cp include/SDL_config_iphoneos.h build/armv7/include/SDL_config.h || exit 2
    70 echo "CXXFLAGS ................... ${CXXFLAGS}"
   222      sed -i "" -e "s|^EXTRA_CFLAGS.*|EXTRA_CFLAGS=-I./include|g" build/armv7/Makefile || exit 2
    71 echo "LDFLAGS .................... ${LDFLAGS}"
   223      sed -i "" -e "s|^EXTRA_LDFLAGS.*|EXTRA_LDFLAGS=-lm|g" build/armv7/Makefile || exit 2
    72 
   224 fi
    73 ###################################################################
   225 if test x$make_armv7 = xyes; then
    74 # This section contains the build commands for each of the 
   226     (cd build/armv7 && make -j$NJOB) || exit 3
    75 # architectures that will be included in the universal binaries.
   227 fi
    76 ###################################################################
   228 #
    77 
   229 # Build the i386 binary
    78 echo "$(tput setaf 2)"
   230 #
    79 echo "###########################"
   231 prepare_environment "i386"
    80 echo "# i386 for iPhone Simulator"
   232 if test x$configure_i386 = xyes; then
    81 echo "###########################"
   233     (cd build/i386 && \
    82 echo "$(tput sgr0)"
   234      sh ../../configure $CONFIG_FLAGS CC="$CC" CXX="$CXX" CFLAGS="$CFLAGS" LDFLAGS="$LDFLAGS") || exit 2
    83 
   235      # configure is not yet fully ready for iOS, some manual patching is required
    84 if [ "${BUILD_I386_IOSSIM}" == "YES" ]
   236      cp include/* build/i386/include
    85 then
   237      cp include/SDL_config_iphoneos.h build/i386/include/SDL_config.h || exit 2
    86     (
   238      sed -i "" -e "s|^EXTRA_CFLAGS.*|EXTRA_CFLAGS=-I./include|g" build/i386/Makefile || exit 2
    87         cd ${PREFIX}
   239      sed -i "" -e "s|^EXTRA_LDFLAGS.*|EXTRA_LDFLAGS=-lm|g" build/i386/Makefile || exit 2
    88         make clean
   240 fi
    89         ../configure --build=x86_64-apple-${DARWIN} --host=i386-ios-${DARWIN} --disable-shared --prefix=${PREFIX}/platform/i386-sim "CC=${CC}" "CFLAGS=${CFLAGS} -mios-simulator-version-min=${MIN_SDK_VERSION} -arch i386 -isysroot ${IPHONESIMULATOR_SYSROOT}" "CXX=${CXX}" "CXXFLAGS=${CXXFLAGS} -mios-simulator-version-min=${MIN_SDK_VERSION} -arch i386 -isysroot ${IPHONESIMULATOR_SYSROOT}" LDFLAGS="-arch i386 -mios-simulator-version-min=${MIN_SDK_VERSION} ${LDFLAGS} -L${IPHONESIMULATOR_SYSROOT}/usr/lib/ -L${IPHONESIMULATOR_SYSROOT}/usr/lib/system" || exit 2
   241 if test x$make_i386 = xyes; then
    90 	cp $SRC_DIR/include/SDL_config_iphoneos.h include/SDL_config.h
   242     (cd build/i386 && make -j$NJOB) || exit 3
    91         make -j10 || exit 3
       
    92         make install
       
    93     ) || exit $?
   243 fi
    94 fi
   244 
    95 
   245 #
    96 echo "$(tput setaf 2)"
   246 # Combine into fat binary
    97 echo "#############################"
   247 #
    98 echo "# x86_64 for iPhone Simulator"
   248 if test x$merge = xyes; then
    99 echo "#############################"
   249     output=ios/lib
   100 echo "$(tput sgr0)"
   250     sh $auxdir/mkinstalldirs build/$output
   101 
   251     cd build
   102 if [ "${BUILD_X86_64_IOSSIM}" == "YES" ]
   252     target=`find . -mindepth 4 -maxdepth 4 -type f -name '*.dylib' | head -1 | sed 's|.*/||'`
   103 then
   253     (lipo -create -o $output/libSDL2.a armv6/build/.libs/libSDL2.a armv7/build/.libs/libSDL2.a i386/build/.libs/libSDL2.a &&
   104     (
   254      lipo -create -o $output/libSDL2main.a armv6/build/libSDL2main.a armv7/build/libSDL2main.a i386/build/libSDL2main.a &&
   105         cd ${PREFIX}
   255      cp -r armv6/include ios
   106         make clean
   256      echo "Build complete!" &&
   107         ../configure --build=x86_64-apple-${DARWIN} --host=x86_64-ios-${DARWIN} --disable-shared --prefix=${PREFIX}/platform/x86_64-sim "CC=${CC}" "CFLAGS=${CFLAGS} -mios-simulator-version-min=${MIN_SDK_VERSION} -arch x86_64 -isysroot ${IPHONESIMULATOR_SYSROOT}" "CXX=${CXX}" "CXXFLAGS=${CXXFLAGS} -mios-simulator-version-min=${MIN_SDK_VERSION} -arch x86_64 -isysroot ${IPHONESIMULATOR_SYSROOT}" LDFLAGS="-arch x86_64 -mios-simulator-version-min=${MIN_SDK_VERSION} ${LDFLAGS} -L${IPHONESIMULATOR_SYSROOT}/usr/lib/ -L${IPHONESIMULATOR_SYSROOT}/usr/lib/system" || exit 2
   257      echo "Files can be found under the build/ios directory.") || exit 4
   108 	cp $SRC_DIR/include/SDL_config_iphoneos.h include/SDL_config.h
   258     cd ..
   109         make -j$NJOB || exit 3
       
   110         make install
       
   111     ) || exit $?
   259 fi
   112 fi
   260 
   113 
   261 #
   114 echo "$(tput setaf 2)"
   262 # Clean up
   115 echo "##################"
   263 #
   116 echo "# armv7 for iPhone"
   264 do_clean()
   117 echo "##################"
   265 {
   118 echo "$(tput sgr0)"
   266     echo $*
   119 
   267     $* || exit 6
   120 if [ "${BUILD_IOS_ARMV7}" == "YES" ]
   268 }
   121 then
   269 if test x$clean_armv6 = xyes; then
   122     (
   270     do_clean rm -r build/armv6
   123         cd ${PREFIX}
       
   124         make clean
       
   125         ../configure --build=x86_64-apple-${DARWIN} --host=armv7-ios-${DARWIN} --disable-shared --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}" || exit 2
       
   126 	cp $SRC_DIR/include/SDL_config_iphoneos.h include/SDL_config.h
       
   127         make -j$NJOB || exit 3
       
   128         make install
       
   129     ) || exit $?
   271 fi
   130 fi
   272 if test x$clean_armv7 = xyes; then
   131 
   273     do_clean rm -r build/armv7
   132 echo "$(tput setaf 2)"
       
   133 echo "###################"
       
   134 echo "# armv7s for iPhone"
       
   135 echo "###################"
       
   136 echo "$(tput sgr0)"
       
   137 
       
   138 if [ "${BUILD_IOS_ARMV7S}" == "YES" ]
       
   139 then
       
   140     (
       
   141         cd ${PREFIX}
       
   142         make clean
       
   143         ../configure --build=x86_64-apple-${DARWIN} --host=armv7s-ios-${DARWIN} --disable-shared --prefix=${PREFIX}/platform/armv7s-ios "CC=${CC}" "CFLAGS=${CFLAGS} -miphoneos-version-min=${MIN_SDK_VERSION} -arch armv7s -isysroot ${IPHONEOS_SYSROOT}" "CXX=${CXX}" "CXXFLAGS=${CXXFLAGS} -miphoneos-version-min=${MIN_SDK_VERSION} -arch armv7s -isysroot ${IPHONEOS_SYSROOT}" LDFLAGS="-arch armv7s -miphoneos-version-min=${MIN_SDK_VERSION} ${LDFLAGS}" || exit 2
       
   144 	cp $SRC_DIR/include/SDL_config_iphoneos.h include/SDL_config.h
       
   145         make -j$NJOB || exit 3
       
   146         make install
       
   147     ) || exit $?
   274 fi
   148 fi
   275 if test x$clean_i386 = xyes; then
   149 
   276     do_clean rm -r build/i386
   150 echo "$(tput setaf 2)"
       
   151 echo "##################"
       
   152 echo "# arm64 for iPhone"
       
   153 echo "##################"
       
   154 echo "$(tput sgr0)"
       
   155 
       
   156 if [ "${BUILD_IOS_ARM64}" == "YES" ]
       
   157 then
       
   158     (
       
   159         cd ${PREFIX}
       
   160         make clean
       
   161         ../configure --build=x86_64-apple-${DARWIN} --host=arm-ios-${DARWIN} --disable-shared --prefix=${PREFIX}/platform/arm64-ios "CC=${CC}" "CFLAGS=${CFLAGS} -miphoneos-version-min=${MIN_SDK_VERSION} -arch arm64 -isysroot ${IPHONEOS_SYSROOT}" "CXX=${CXX}" "CXXFLAGS=${CXXFLAGS} -miphoneos-version-min=${MIN_SDK_VERSION} -arch arm64 -isysroot ${IPHONEOS_SYSROOT}" LDFLAGS="-arch arm64 -miphoneos-version-min=${MIN_SDK_VERSION} ${LDFLAGS}" || exit 2
       
   162 	cp $SRC_DIR/include/SDL_config_iphoneos.h include/SDL_config.h
       
   163         make -j$NJOB || exit 3
       
   164         make install
       
   165     ) || exit $?
   277 fi
   166 fi
       
   167 
       
   168 echo "$(tput setaf 2)"
       
   169 echo "###################################################################"
       
   170 echo "# Create Universal Libraries and Finalize the packaging"
       
   171 echo "###################################################################"
       
   172 echo "$(tput sgr0)"
       
   173 
       
   174 (
       
   175     cd ${PREFIX}/platform
       
   176     mkdir -p universal
       
   177     lipo x86_64-sim/lib/libSDL2.a i386-sim/lib/libSDL2.a arm64-ios/lib/libSDL2.a armv7s-ios/lib/libSDL2.a armv7-ios/lib/libSDL2.a -create -output universal/libSDL2.a
       
   178 )
       
   179 
       
   180 (
       
   181     cd ${PREFIX}
       
   182     mkdir -p lib
       
   183     cp -r platform/universal/* lib
       
   184     #rm -rf platform
       
   185     lipo -info lib/libSDL2.a
       
   186 )
       
   187 
       
   188 echo Done!