-
Notifications
You must be signed in to change notification settings - Fork 5
/
cmake_ios_wrapper.sh
174 lines (154 loc) · 5.62 KB
/
cmake_ios_wrapper.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/bash
###########################################################################
# Change values here
# Use xcode-select --install to install command line tools
SDKVERSION=$(xcrun -sdk iphoneos --show-sdk-version)
#
###########################################################################
#
# Don't change anything here
WORKING_DIR="$(pwd)"
ARCHS="arm64" # i386 x86_64 armv7 armv7s arm64
DEVELOPER_ROOT=$(xcode-select -print-path)
BUILD_WITH_EMBED_BITCODE="off"
SOURCE_DIR="$PWD"
BUILD_TYPE="RelWithDebInfo"
OTHER_CFLAGS="-fPIC"
DEPLOYMENT_TARGET=$(xcrun -sdk iphoneos --show-sdk-platform-version)
# ======================= options =======================
while getopts "a:b:d:hi:r:s:t:-" OPTION; do
case $OPTION in
a)
ARCHS="$OPTARG"
;;
b)
BUILD_TYPE="$OPTARG"
;;
d)
DEVELOPER_ROOT="$OPTARG"
;;
h)
echo "usage: $0 [options] -r SOURCE_DIR [-- [cmake options]]"
echo "options:"
echo "-a [archs] which arch need to built, multiple values must be split by space(default: $ARCHS, available: i386 x86_64 armv7 armv7s arm64)"
echo "-b [build type] build type(default: $BUILD_TYPE, available: Debug, Release, RelWithDebInfo, MinSizeRel)"
echo "-d [developer root directory] developer root directory, we use xcode-select -print-path to find default value.(default: $DEVELOPER_ROOT)"
echo "-h help message."
echo "-i [option] bitcode option(default: $BUILD_WITH_EMBED_BITCODE, available: off, all, bitcode, marker)"
echo "-s [sdk version] sdk version, we use xcrun -sdk iphoneos --show-sdk-version to find default value.(default: $SDKVERSION)"
echo "-t [deployment target] deployment target. (default: 8.0)"
echo "-r [source dir] root directory of this library"
exit 0
;;
i)
BUILD_WITH_EMBED_BITCODE="-fembed-bitcode=$OPTARG"
;;
r)
SOURCE_DIR="$OPTARG"
;;
s)
SDKVERSION="$OPTARG"
;;
t)
DEPLOYMENT_TARGET="$OPTARG"
;;
-)
break
break
;;
?) #当有不认识的选项的时候arg为?
echo "unkonw argument detected"
exit 1
;;
esac
done
shift $(($OPTIND - 1))
HOST_CC="$(which clang 2>/dev/null)"
HOST_CXX="$(which clang++ 2>/dev/null)"
echo "Ready to build for ios"
echo "WORKING_DIR=${WORKING_DIR}"
echo "ARCHS=${ARCHS}"
echo "DEVELOPER_ROOT=${DEVELOPER_ROOT}"
echo "SDKVERSION=${SDKVERSION}"
echo "DEPLOYMENT_TARGET=${DEPLOYMENT_TARGET}"
echo "cmake options=$@"
echo "SOURCE=$SOURCE_DIR"
echo "HOST_CC=$HOST_CC"
echo "HOST_CXX=$HOST_CXX"
##########
if [ ! -e "$SOURCE_DIR/CMakeLists.txt" ]; then
echo "$SOURCE_DIR/CMakeLists.txt not found"
exit -2
fi
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
SOURCE_DIR="$(cd $SOURCE_DIR && pwd)"
mkdir -p "$WORKING_DIR/lib"
HOST_PYTHON3_EXECUTABLE="$(which python3)"
for ARCH in ${ARCHS}; do
echo "================== Compling $ARCH =================="
EXT_OPTIONS="-DCMAKE_SYSTEM_NAME=iOS"
if [[ "${ARCH}" == "i386" ]]; then
PLATFORM="iPhoneSimulator"
EXT_OPTIONS="$EXT_OPTIONS -DCMAKE_SYSTEM_PROCESSOR=i386"
elif [[ "${ARCH}" == "x86_64" ]]; then
PLATFORM="iPhoneSimulator"
EXT_OPTIONS="$EXT_OPTIONS -DCMAKE_SYSTEM_PROCESSOR=x86_64"
else
PLATFORM="iPhoneOS"
if [[ "${ARCH}" == "armv7" ]] || [[ "${ARCH}" == "armv7s" ]]; then
EXT_OPTIONS="$EXT_OPTIONS -DCMAKE_SYSTEM_PROCESSOR=arm"
else
EXT_OPTIONS="$EXT_OPTIONS -DCMAKE_SYSTEM_PROCESSOR=aarch64"
fi
fi
if [[ "x$HOST_PYTHON3_EXECUTABLE" != "x" ]]; then
EXT_OPTIONS="$EXT_OPTIONS -DPython3_EXECUTABLE=$HOST_PYTHON3_EXECUTABLE"
fi
echo "Building for ${PLATFORM} ${SDKVERSION} ${ARCH}"
echo "Please stand by..."
export DEVROOT="${DEVELOPER_ROOT}/Platforms/${PLATFORM}.platform/Developer"
export SDKROOT="${DEVROOT}/SDKs/${PLATFORM}${SDKVERSION}.sdk"
mkdir -p "$WORKING_DIR/build_jobs_$ARCH"
cd "$WORKING_DIR/build_jobs_$ARCH"
echo "PWD=$PWD"
echo "HOST_BUILD_DIR=$WORKING_DIR/build_jobs_host"
# For openssl
export CROSS_TOP="${DEVROOT}"
export CROSS_SDK="${PLATFORM}${SDKVERSION}.sdk"
if [[ $(echo $DEPLOYMENT_TARGET | cut -d. -f 1) -lt 11 ]]; then
EXT_OPTIONS="$EXT_OPTIONS -DCMAKE_CXX_STANDARD=14"
fi
if [[ "x$HOST_CC" != "x" ]]; then
EXT_OPTIONS="$EXT_OPTIONS -DCMAKE_HOST_C_COMPILER=$HOST_CC"
fi
if [[ "x$HOST_CXX" != "x" ]]; then
EXT_OPTIONS="$EXT_OPTIONS -DCMAKE_HOST_CXX_COMPILER=$HOST_CXX"
fi
# for CACHE_SRC in $(find "$SOURCE_DIR/protocol" -name "*.pb.h") ; do
# rm -f "$CACHE_SRC";
# done
# add -DCMAKE_OSX_DEPLOYMENT_TARGET=7.1 to specify the min SDK version
# export IPHONEOS_DEPLOYMENT_TARGET=${DEPLOYMENT_TARGET}
set -x
cmake "$SOURCE_DIR" -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
-DCMAKE_POLICY_DEFAULT_CMP0057=NEW \
-DCMAKE_OSX_SYSROOT=$SDKROOT -DCMAKE_SYSROOT=$SDKROOT \
-DCMAKE_OSX_ARCHITECTURES=$ARCH \
-DCMAKE_CXX_FLAGS="$OTHER_CFLAGS -fembed-bitcode=$BUILD_WITH_EMBED_BITCODE" \
-DCMAKE_C_FLAGS="$OTHER_CFLAGS -fembed-bitcode=$BUILD_WITH_EMBED_BITCODE" \
-DCMAKE_OSX_DEPLOYMENT_TARGET=${DEPLOYMENT_TARGET} \
-DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER \
-DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY \
-DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=ONLY \
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ONLY \
-DCMAKE_FIND_ROOT_PATH=${SDKROOT} \
"-DATFRAMEWORK_CMAKE_TOOLSET_THIRD_PARTY_HOST_BUILD_DIR=$WORKING_DIR/build_jobs_host" \
$EXT_OPTIONS "$@"
if [[ $LAST_EXIT_CODE -ne 0 ]]; then
echo "run cmake failed"
exit $LAST_EXIT_CODE
fi
set +x
done
# Run lipo -create SRCS -output DST to archive all .a into one