build-scripts/iosbuild.sh
author Gabriel Jacobo <gabomdq@gmail.com>
Tue, 19 Jun 2012 18:25:04 -0300
changeset 6333 8d541aa4e1da
child 6334 85b8778a05c2
permissions -rwxr-xr-x
Adds a simple command line utility (iosbuild.sh) to create iOS binaries for armv6, armv7 and i386
     1 #!/bin/sh
     2 #
     3 # Build a fat binary for iOS
     4 # Based on fatbuild.sh and code from the Ignifuga Game Engine
     5 
     6 # Number of CPUs (for make -j)
     7 NCPU=`sysctl -n hw.ncpu`
     8 if test x$NJOB = x; then
     9     NJOB=$NCPU
    10 fi
    11 
    12 # SDK path
    13 XCODE_PATH=`xcode-select --print-path`
    14 if [ -z "$XCODE_PATH" ]; then
    15     echo "Could not find XCode location"
    16     exit 1
    17 fi
    18 
    19 prepare_environment() {
    20     ARCH=$1
    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 
    49     if [ ! -d "$SDK_PATH" ]; then
    50         echo "Could not find iOS SDK at $SDK_PATH"
    51         exit 1
    52     fi
    53 
    54     if test x$MIN_OS_VERSION = x; then
    55         export MIN_OS_VERSION="3.0"
    56     fi
    57     
    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 
   101 prepare_environment "armv6"
   102 echo "Building with iOS SDK v$SDK_VERSION for iOS >= $MIN_OS_VERSION"
   103 
   104 #
   105 # Find the configure script
   106 #
   107 srcdir=`dirname $0`/..
   108 srcdir=`cd $srcdir && pwd`
   109 auxdir=$srcdir/build-scripts
   110 cd $srcdir
   111 
   112 #
   113 # Figure out which phase to build:
   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 
   185 #
   186 # Create the build directories
   187 #
   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 
   196 #
   197 # Build the armv6 binary
   198 #
   199 prepare_environment "armv6"
   200 if test x$configure_armv6 = xyes; then
   201     (cd build/armv6 && \
   202      sh ../../configure $CONFIG_FLAGS CC="$CC" CXX="$CXX" CFLAGS="$CFLAGS" LDFLAGS="$LDFLAGS") || exit 2
   203      # configure is not yet fully ready for iOS, some manual patching is required
   204      cp include/* build/armv6/include
   205      cp include/SDL_config_iphoneos.h build/armv6/include/SDL_config.h || exit 2
   206      sed -i "" -e "s|^EXTRA_CFLAGS.*|EXTRA_CFLAGS=-I./include|g" build/armv6/Makefile || exit 2
   207      sed -i "" -e "s|^EXTRA_LDFLAGS.*|EXTRA_LDFLAGS=-lm|g" build/armv6/Makefile || exit 2
   208 fi
   209 if test x$make_armv6 = xyes; then
   210     (cd build/armv6 && make -j$NJOB) || exit 3
   211 fi
   212 #
   213 # Build the armv7 binary
   214 #
   215 prepare_environment "armv7"
   216 if test x$configure_armv7 = xyes; then
   217     (cd build/armv7 && \
   218      sh ../../configure $CONFIG_FLAGS CC="$CC" CXX="$CXX" CFLAGS="$CFLAGS" LDFLAGS="$LDFLAGS") || exit 2
   219      # configure is not yet fully ready for iOS, some manual patching is required
   220      cp include/* build/armv7/include
   221      cp include/SDL_config_iphoneos.h build/armv7/include/SDL_config.h || exit 2
   222      sed -i "" -e "s|^EXTRA_CFLAGS.*|EXTRA_CFLAGS=-I./include|g" build/armv7/Makefile || exit 2
   223      sed -i "" -e "s|^EXTRA_LDFLAGS.*|EXTRA_LDFLAGS=-lm|g" build/armv7/Makefile || exit 2
   224 fi
   225 if test x$make_armv7 = xyes; then
   226     (cd build/armv7 && make -j$NJOB) || exit 3
   227 fi
   228 #
   229 # Build the i386 binary
   230 #
   231 prepare_environment "i386"
   232 if test x$configure_i386 = xyes; then
   233     (cd build/i386 && \
   234      sh ../../configure $CONFIG_FLAGS CC="$CC" CXX="$CXX" CFLAGS="$CFLAGS" LDFLAGS="$LDFLAGS") || exit 2
   235      # configure is not yet fully ready for iOS, some manual patching is required
   236      cp include/* build/i386/include
   237      cp include/SDL_config_iphoneos.h build/i386/include/SDL_config.h || exit 2
   238      sed -i "" -e "s|^EXTRA_CFLAGS.*|EXTRA_CFLAGS=-I./include|g" build/i386/Makefile || exit 2
   239      sed -i "" -e "s|^EXTRA_LDFLAGS.*|EXTRA_LDFLAGS=-lm|g" build/i386/Makefile || exit 2
   240 fi
   241 if test x$make_i386 = xyes; then
   242     (cd build/i386 && make -j$NJOB) || exit 3
   243 fi
   244 
   245 #
   246 # Combine into fat binary
   247 #
   248 if test x$merge = xyes; then
   249     output=ios/lib
   250     sh $auxdir/mkinstalldirs build/$output
   251     cd build
   252     target=`find . -mindepth 4 -maxdepth 4 -type f -name '*.dylib' | head -1 | sed 's|.*/||'`
   253     (lipo -create -o $output/libSDL2.a */build/.libs/libSDL2.a &&
   254      lipo -create -o $output/libSDL2main.a */build/libSDL2main.a &&
   255      cp -r armv6/include ios
   256      echo "Build complete!" &&
   257      echo "Files can be found under the build/ios directory.") || exit 4
   258     cd ..
   259 fi
   260 
   261 #
   262 # Clean up
   263 #
   264 do_clean()
   265 {
   266     echo $*
   267     $* || exit 6
   268 }
   269 if test x$clean_armv6 = xyes; then
   270     do_clean rm -r build/armv6
   271 fi
   272 if test x$clean_armv7 = xyes; then
   273     do_clean rm -r build/armv7
   274 fi
   275 if test x$clean_i386 = xyes; then
   276     do_clean rm -r build/i386
   277 fi