iosbuild.sh 7.7 KB

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