|
1 | 1 | #!/bin/sh
|
2 |
| -set -e |
3 |
| -set -x |
4 | 2 |
|
5 |
| -PLATFORM=OS |
6 |
| -VERBOSE=no |
7 |
| -SDK_VERSION=$(xcodebuild -showsdks | grep iphoneos | sort | tail -n 1 | awk '{print substr($NF,9)}') |
8 |
| -# FIXME: why min deploy target can't use 5.1.1 |
9 |
| -SDK_MIN=6.0 |
10 |
| -ARCH=armv7 |
| 3 | +# |
| 4 | +# A script to build static library for iOS |
| 5 | +# |
11 | 6 |
|
12 |
| -# TODO: configure to compile speficy 3rd party libraries |
13 |
| -OPTIONS="" |
| 7 | +build_arches="all" |
| 8 | +build_mode="release" |
| 9 | +build_library="all" |
14 | 10 |
|
15 |
| - |
16 |
| -usage() |
| 11 | +function usage() |
17 | 12 | {
|
18 |
| -cat << EOF |
19 |
| -usage: $0 [-s] [-k sdk] [-a arch] [-l libname] |
20 |
| -
|
21 |
| -OPTIONS |
22 |
| - -k <sdk version> Specify which sdk to use ('xcodebuild -showsdks', current: ${SDK_VERSION}) |
23 |
| - -s Build for simulator |
24 |
| - -a <arch> Specify which arch to use (current: ${ARCH}) |
25 |
| - -l <libname> Specify which static library to build |
26 |
| -EOF |
| 13 | + echo "You should follow the instructions here to build static library for iOS" |
| 14 | + echo "If you want to build a fat, you should pass 'all' to --arch parameter." |
| 15 | + echo "" |
| 16 | + echo "Sample:" |
| 17 | + echo "./bulid.sh --arch=armv7,arm64 --mode=debug" |
| 18 | + echo "You must seperate each arch with comma, otherwise it won't parse correctly. No whitespace is allowed between two arch types." |
| 19 | + echo "" |
| 20 | + echo "./build_png.sh" |
| 21 | + echo "\t-h --help" |
| 22 | + echo "\t--libs=[all | png,lua,tiff,jpeg,webp,zlib,websockets,luajit,freetype2,curl,chipmunk,box2d]" |
| 23 | + echo "\t--arch=[all | armv7,arm64,i386,x86_64]" |
| 24 | + echo "\t--mode=[release | debug]" |
| 25 | + echo "" |
27 | 26 | }
|
28 | 27 |
|
29 |
| -spushd() |
30 |
| -{ |
31 |
| - pushd "$1" 2>&1> /dev/null |
| 28 | + |
| 29 | +while [ "$1" != "" ]; do |
| 30 | + PARAM=`echo $1 | awk -F= '{print $1}'` |
| 31 | + VALUE=`echo $1 | awk -F= '{print $2}'` |
| 32 | + case $PARAM in |
| 33 | + -h | --help) |
| 34 | + usage |
| 35 | + exit |
| 36 | + ;; |
| 37 | + --libs) |
| 38 | + build_library=$VALUE |
| 39 | + ;; |
| 40 | + --arch) |
| 41 | + build_arches=$VALUE |
| 42 | + ;; |
| 43 | + --mode) |
| 44 | + build_mode=$VALUE |
| 45 | + ;; |
| 46 | + *) |
| 47 | + echo "ERROR: unknown parameter \"$PARAM\"" |
| 48 | + usage |
| 49 | + exit 1 |
| 50 | + ;; |
| 51 | + esac |
| 52 | + shift |
| 53 | +done |
| 54 | + |
| 55 | +function contains() { |
| 56 | + local n=$# |
| 57 | + local value=${!n} |
| 58 | + for ((i=1;i < $#;i++)) { |
| 59 | + if [ "${!i}" == "${value}" ]; then |
| 60 | + echo "y" |
| 61 | + return 0 |
| 62 | + fi |
| 63 | + } |
| 64 | + echo "n" |
| 65 | + return 1 |
32 | 66 | }
|
33 | 67 |
|
34 |
| -spopd() |
| 68 | +all_arches=("armv7" "arm64" "i386" "x86_64") |
| 69 | +all_libraries=("png" "zlib") |
| 70 | + |
| 71 | +# TODO: we only build a fat library with armv7, arm64, i386 and x86_64 arch. If you want to build armv7s into the fat lib, please add it into the following array. |
| 72 | +if [ $build_arches = "all" ]; then |
| 73 | + declare -a build_arches=("armv7" "arm64" "i386" "x86_64") |
| 74 | +else |
| 75 | + build_arches=(${build_arches//,/ }) |
| 76 | +fi |
| 77 | + |
| 78 | +if [ $build_library = "all" ]; then |
| 79 | + # TODO: more libraries need to be added here |
| 80 | + declare -a build_library=("png" "zlib") |
| 81 | +else |
| 82 | + build_library=(${build_library//,/ }) |
| 83 | +fi |
| 84 | + |
| 85 | +#check invalid arch type |
| 86 | +function check_invalid_arch_type() |
35 | 87 | {
|
36 |
| - popd 2>&1> /dev/null |
| 88 | + for arch in "${build_arches[@]}" |
| 89 | + do |
| 90 | + echo "checking ${arch} is in ${all_arches[@]}" |
| 91 | + if [ $(contains "${all_arches[@]}" $arch) == "n" ]; then |
| 92 | + echo "Invalid arch! Only ${all_arches[@]} is acceptable." |
| 93 | + exit 1 |
| 94 | + fi |
| 95 | + done |
37 | 96 | }
|
38 | 97 |
|
39 |
| -info() |
| 98 | +check_invalid_arch_type |
| 99 | + |
| 100 | +#check invalid library name |
| 101 | +function check_invalid_library_name() |
40 | 102 | {
|
41 |
| - local blue="\033[1;34m" |
42 |
| - local normal="\033[0m" |
43 |
| - echo "[${blue}info${normal}] $1" |
| 103 | + for lib in "${build_library[@]}" |
| 104 | + do |
| 105 | + echo "checking ${lib} is in ${all_libraries[@]}" |
| 106 | + if [ $(contains "${all_libraries[@]}" $lib) == "n" ]; then |
| 107 | + echo "Invalid library names! Only ${all_libraries[@]} is acceptable!" |
| 108 | + exit 1 |
| 109 | + fi |
| 110 | + done |
44 | 111 | }
|
45 | 112 |
|
| 113 | +check_invalid_library_name |
46 | 114 |
|
47 |
| -while getopts "hvsk:a:l:" OPTION |
48 |
| -do |
49 |
| - case $OPTION in |
50 |
| - h) |
51 |
| - usage |
52 |
| - exit 1 |
53 |
| - ;; |
54 |
| - v) |
55 |
| - VERBOSE=yes |
56 |
| - ;; |
57 |
| - s) |
58 |
| - PLATFORM=Simulator |
59 |
| - ;; |
60 |
| - k) |
61 |
| - SDK_VERSION=$OPTARG |
62 |
| - ;; |
63 |
| - a) |
64 |
| - ARCH=$OPTARG |
65 |
| - ;; |
66 |
| - l) |
67 |
| - OPTIONS=--enable-$OPTARG |
68 |
| - ;; |
69 |
| - ?) |
70 |
| - usage |
71 |
| - exit 1 |
72 |
| - ;; |
73 |
| - esac |
74 |
| -done |
75 |
| - |
76 |
| -if test -z "$OPTIONS" |
77 |
| -then |
78 |
| - echo "You must specify a OPTIONS parameter." |
| 115 | +#check invalid build mode, only debug and release is acceptable |
| 116 | +if [ $build_mode != "release" ] && [ $build_mode != "debug" ]; then |
| 117 | + echo "invalid build mode, only: debug and release is acceptabl" |
79 | 118 | usage
|
80 |
| - exit 1 |
| 119 | + exit |
81 | 120 | fi
|
82 | 121 |
|
83 |
| -shift $(($OPTIND - 1)) |
84 |
| - |
85 | 122 |
|
86 |
| -if [ "x$1" != "x" ]; then |
87 |
| - usage |
88 |
| - exit 1 |
89 |
| -fi |
| 123 | +for arch in "${build_arches[@]}" |
| 124 | +do |
| 125 | + echo $arch |
| 126 | +done |
90 | 127 |
|
| 128 | +for lib in "${build_library[@]}" |
| 129 | +do |
| 130 | + echo $lib |
| 131 | +done |
91 | 132 |
|
92 |
| -out="/dev/null" |
93 |
| -if [ "$VERBOSE" = "yes" ]; then |
94 |
| - out="/dev/stdout" |
95 |
| -fi |
| 133 | +echo "build mode is $build_mode" |
96 | 134 |
|
97 |
| -info "Building cocos2d-x third party libraries for iOS" |
| 135 | +exit |
98 | 136 |
|
99 |
| -if [ "$PLATFORM" = "Simulator" ]; then |
100 |
| - TARGET="${ARCH}-apple-darwin" |
101 |
| - OPTIM="-O3 -g" |
102 |
| -else |
103 |
| - TARGET="arm-apple-darwin" |
104 |
| - OPTIM="-O3 -g" |
105 |
| -fi |
106 | 137 |
|
107 |
| -info "Using ${ARCH} with SDK version ${SDK_VERSION}" |
| 138 | +current_dir=`pwd` |
| 139 | +library_name=png |
| 140 | +rm -rf $library_name |
| 141 | +# build for armv7 |
| 142 | +arch=armv7 |
| 143 | +./build.sh -a $arch -l $library_name |
| 144 | +top_dir=$current_dir/../.. |
108 | 145 |
|
109 |
| -THIS_SCRIPT_PATH=`pwd` |
| 146 | +cd $current_dir |
| 147 | +mkdir -p $library_name/prebuilt/ |
| 148 | +mkdir -p $library_name/include/ |
110 | 149 |
|
111 |
| -COCOSROOT=`pwd`/../.. |
| 150 | +cp $top_dir/contrib/install-ios-OS/$arch/lib/lib$library_name.a $library_name/prebuilt/lib$library_name-$arch.a |
| 151 | +cp -r $top_dir/contrib/install-ios-Os/$arch/include/png*.h $library_name/include/ |
112 | 152 |
|
113 |
| -if test -z "$SDKROOT" |
114 |
| -then |
115 |
| - SDKROOT=`xcode-select -print-path`/Platforms/iPhone${PLATFORM}.platform/Developer/SDKs/iPhone${PLATFORM}${SDK_VERSION}.sdk |
116 |
| - echo "SDKROOT not specified, assuming $SDKROOT" |
117 |
| -fi |
| 153 | +echo "cleaning up" |
| 154 | +rm -rf $top_dir/contrib/install-ios-OS |
| 155 | +rm -rf $top_dir/contrib/iPhoneOS-$arch |
118 | 156 |
|
119 |
| -if [ ! -d "${SDKROOT}" ] |
120 |
| -then |
121 |
| - echo "*** ${SDKROOT} does not exist, please install required SDK, or set SDKROOT manually. ***" |
122 |
| - exit 1 |
123 |
| -fi |
| 157 | +# build for i386 |
| 158 | +arch=i386 |
| 159 | +./build.sh -s -a $arch -l $library_name |
| 160 | +top_dir=$current_dir/../.. |
124 | 161 |
|
125 |
| -BUILDDIR="${COCOSROOT}/build-ios-${PLATFORM}/${ARCH}" |
| 162 | +cd $current_dir |
| 163 | +mkdir -p $library_name/prebuilt/ |
| 164 | +mkdir -p $library_name/include/ |
126 | 165 |
|
127 |
| -PREFIX="${COCOSROOT}/contrib/install-ios-${PLATFORM}/${ARCH}" |
| 166 | +cp $top_dir/contrib/install-ios-Simulator/$arch/lib/lib${library_name}.a $library_name/prebuilt/lib$library_name-$arch.a |
128 | 167 |
|
129 |
| -export PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin" |
| 168 | +echo "cleaning up" |
| 169 | +rm -rf $top_dir/contrib/install-ios-Simulator |
| 170 | +rm -rf $top_dir/contrib/iPhoneSimulator-$arch |
130 | 171 |
|
| 172 | +#build for x86_64 |
| 173 | +arch=x86_64 |
| 174 | +./build.sh -s -a $arch -l $library_name |
| 175 | +top_dir=$current_dir/../.. |
131 | 176 |
|
132 |
| -info "Building contrib for iOS in '${COCOSROOT}/contrib/iPhone${PLATFORM}-${ARCH}'" |
| 177 | +cd $current_dir |
| 178 | +mkdir -p $library_name/prebuilt/ |
| 179 | +mkdir -p $library_name/include/ |
133 | 180 |
|
134 |
| -# The contrib will read the following |
135 |
| -export AR="xcrun ar" |
| 181 | +cp $top_dir/contrib/install-ios-Simulator/$arch/lib/lib${library_name}.a $library_name/prebuilt/lib$library_name-$arch.a |
136 | 182 |
|
137 |
| -export RANLIB="xcrun ranlib" |
138 |
| -export CC="xcrun clang" |
139 |
| -export OBJC="xcrun clang" |
140 |
| -export CXX="xcrun clang++" |
141 |
| -export LD="xcrun ld" |
142 |
| -export STRIP="xcrun strip" |
| 183 | +echo "cleaning up" |
| 184 | +rm -rf $top_dir/contrib/install-ios-Simulator/ |
| 185 | +rm -rf $top_dir/contrib/iPhoneSimulator-$arch |
143 | 186 |
|
144 |
| -export PLATFORM=$PLATFORM |
145 |
| -export SDK_VERSION=$SDK_VERSION |
| 187 | +#build for arm64 |
| 188 | +arch=arm64 |
| 189 | +./build.sh -a $arch -l $library_name |
| 190 | +top_dir=$current_dir/../.. |
146 | 191 |
|
147 |
| -if [ "$PLATFORM" = "OS" ]; then |
148 |
| -export CFLAGS="-isysroot ${SDKROOT} -arch ${ARCH} -miphoneos-version-min=${SDK_MIN} ${OPTIM}" |
149 |
| -if [ "$ARCH" != "arm64" ]; then |
150 |
| -export CFLAGS="${CFLAGS} -mcpu=cortex-a8" |
151 |
| -fi |
152 |
| -else |
153 |
| -export CFLAGS="-isysroot ${SDKROOT} -arch ${ARCH} -miphoneos-version-min=${SDK_MIN} ${OPTIM}" |
154 |
| -fi |
| 192 | +cd $current_dir |
| 193 | +mkdir -p $library_name/prebuilt/ |
| 194 | +mkdir -p $library_name/include/ |
155 | 195 |
|
156 |
| -export CPP="xcrun cc -E" |
157 |
| -export CXXCPP="xcrun c++ -E" |
| 196 | +cp $top_dir/contrib/install-ios-OS/$arch/lib/lib${library_name}.a $library_name/prebuilt/lib$library_name-$arch.a |
158 | 197 |
|
159 |
| -export BUILDFORIOS="yes" |
| 198 | +echo "cleaning up" |
| 199 | +rm -rf $top_dir/contrib/install-ios-OS |
| 200 | +rm -rf $top_dir/contrib/iPhoneOS-$arch |
160 | 201 |
|
161 |
| -if [ "$PLATFORM" = "Simulator" ]; then |
162 |
| - # Use the new ABI on simulator, else we can't build |
163 |
| - export OBJCFLAGS="-fobjc-abi-version=2 -fobjc-legacy-dispatch ${OBJCFLAGS}" |
164 |
| -fi |
165 | 202 |
|
166 |
| -export LDFLAGS="-L${SDKROOT}/usr/lib -arch ${ARCH} -isysroot ${SDKROOT} -miphoneos-version-min=${SDK_MIN}" |
167 |
| - |
168 |
| -EXTRA_CFLAGS="" |
169 |
| -# if [ "$PLATFORM" = "OS" ]; then |
170 |
| -# EXTRA_CFLAGS="-arch ${ARCH}" |
171 |
| -# if [ "$ARCH" != "arm64" ]; then |
172 |
| -# EXTRA_CFLAGS+=" -mcpu=cortex-a8" |
173 |
| -# fi |
174 |
| -# EXTRA_LDFLAGS="-arch ${ARCH}" |
175 |
| -# else |
176 |
| -# EXTRA_CFLAGS="-arch ${ARCH}" |
177 |
| -# EXTRA_LDFLAGS="-arch ${ARCH}" |
178 |
| -# fi |
179 |
| - |
180 |
| -# EXTRA_CFLAGS+=" -miphoneos-version-min=${SDK_MIN}" |
181 |
| -# EXTRA_LDFLAGS+=" -miphoneos-version-min=${SDK_MIN}" |
182 |
| - |
183 |
| -info "LD FLAGS SELECTED = '${LDFLAGS}'" |
184 |
| - |
185 |
| -spushd ${COCOSROOT} |
186 |
| - |
187 |
| -echo ${COCOSROOT} |
188 |
| -mkdir -p "${COCOSROOT}/contrib/iPhone${PLATFORM}-${ARCH}" |
189 |
| -spushd "${COCOSROOT}/contrib/iPhone${PLATFORM}-${ARCH}" |
190 |
| - |
191 |
| -## FIXME: do we need to replace Apple's gas? |
192 |
| -if [ "$PLATFORM" = "OS" ]; then |
193 |
| - export AS="gas-preprocessor.pl ${CC}" |
194 |
| - export ASCPP="gas-preprocessor.pl ${CC}" |
195 |
| - export CCAS="gas-preprocessor.pl ${CC}" |
196 |
| - if [ "$ARCH" = "arm64" ]; then |
197 |
| - export GASPP_FIX_XCODE5=1 |
198 |
| - fi |
199 |
| -else |
200 |
| - export ASCPP="xcrun as" |
201 |
| -fi |
| 203 | +#strip & create fat library |
| 204 | +LIPO="xcrun -sdk iphoneos lipo" |
| 205 | +STRIP="xcrun -sdk iphoneos strip" |
202 | 206 |
|
| 207 | +$LIPO -create $library_name/prebuilt/lib$library_name-armv7.a \ |
| 208 | + $library_name/prebuilt/lib$library_name-i386.a \ |
| 209 | + $library_name/prebuilt/lib$library_name-arm64.a \ |
| 210 | + $library_name/prebuilt/lib$library_name-x86_64.a \ |
| 211 | + -output $library_name/prebuilt/lib$library_name.a |
203 | 212 |
|
| 213 | +rm $library_name/prebuilt/lib$library_name-armv7.a |
| 214 | +rm $library_name/prebuilt/lib$library_name-i386.a |
| 215 | +rm $library_name/prebuilt/lib$library_name-arm64.a |
| 216 | +rm $library_name/prebuilt/lib$library_name-x86_64.a |
204 | 217 |
|
205 |
| -../bootstrap ${OPTIONS} \ |
206 |
| - --build=x86_64-apple-darwin14 \ |
207 |
| - --host=${TARGET} \ |
208 |
| - --prefix=${PREFIX} > ${out} |
209 | 218 |
|
210 |
| -echo "EXTRA_CFLAGS = ${EXTRA_CFLAGS}" >> config.mak |
211 |
| -echo "EXTRA_LDFLAGS = ${EXTRA_LDFLAGS}" >> config.mak |
212 |
| -echo "IOS_ARCH := ${ARCH}" >> config.mak |
213 |
| -make fetch |
214 |
| -make list |
215 |
| -make |
216 |
| -spopd |
| 219 | +#remove debugging info |
| 220 | +$STRIP -S $library_name/prebuilt/lib$library_name.a |
| 221 | +$LIPO -info $library_name/prebuilt/lib$library_name.a |
0 commit comments