3 # ====================================================================
4 # Sets the cross compile environment for Xcode/iOS
6 # Based upon OpenSSL's setenv-ios.sh by TH, JW, and SM.
7 # Heavily modified by JWW for Crypto++.
8 # Modified by JWW for Unbound.
9 # ====================================================================
11 #########################################
12 ##### Some validation #####
13 #########################################
15 # In the past we could mostly infer arch or cpu from the SDK (and
16 # mostly vice-versa). Nowadays we need it set for us because Apple
17 # platforms can be either 32-bit or 64-bit.
19 if [ -z "$IOS_SDK" ]; then
20 echo "IOS_SDK is not set. Please set it"
21 [[ "$0" = "${BASH_SOURCE[0]}" ]] && exit 1 || return 1
24 if [ -z "$IOS_CPU" ]; then
25 echo "IOS_CPU is not set. Please set it"
26 [[ "$0" = "${BASH_SOURCE[0]}" ]] && exit 1 || return 1
29 # cryptest-ios.sh may run this script without sourcing.
30 if [ "$0" = "${BASH_SOURCE[0]}" ]; then
31 echo "setenv-ios.sh is usually sourced, but not this time."
34 #########################################
35 ##### Small Fixups, if needed #####
36 #########################################
38 if [[ "$IOS_SDK" == "iPhone" ]]; then
42 if [[ "$IOS_SDK" == "iPhoneOSSimulator" ]]; then
43 IOS_SDK=iPhoneSimulator
46 if [[ "$IOS_SDK" == "TV" || "$IOS_SDK" == "AppleTV" ]]; then
50 if [[ "$IOS_SDK" == "Watch" || "$IOS_SDK" == "AppleWatch" ]]; then
54 if [[ "$IOS_CPU" == "aarch64" || "$IOS_CPU" == "armv8"* ]] ; then
58 ########################################
59 ##### Environment #####
60 ########################################
62 # The flags below were tested with Xcode 8 on Travis. If
63 # you use downlevel versions of Xcode, then you can push
64 # xxx-version-min=n lower. For example, Xcode 6 can use
65 # -miphoneos-version-min=5.
67 # iPhones can be either 32-bit or 64-bit
68 if [[ "$IOS_SDK" == "iPhoneOS" && "$IOS_CPU" == "armv7"* ]]; then
69 MIN_VER=-miphoneos-version-min=6
70 elif [[ "$IOS_SDK" == "iPhoneOS" && "$IOS_CPU" == "arm64" ]]; then
71 MIN_VER=-miphoneos-version-min=6
73 # Fixups for convenience
74 elif [[ "$IOS_SDK" == "iPhoneOS" && "$IOS_CPU" == "i386" ]]; then
75 IOS_SDK=iPhoneSimulator
76 # MIN_VER=-miphoneos-version-min=6
77 MIN_VER=-miphonesimulator-version-min=6
78 elif [[ "$IOS_SDK" == "iPhoneOS" && "$IOS_CPU" == "x86_64" ]]; then
79 IOS_SDK=iPhoneSimulator
80 # MIN_VER=-miphoneos-version-min=6
81 MIN_VER=-miphonesimulator-version-min=6
84 elif [[ "$IOS_SDK" == "iPhoneSimulator" && "$IOS_CPU" == "i386" ]]; then
85 MIN_VER=-miphonesimulator-version-min=6
86 elif [[ "$IOS_SDK" == "iPhoneSimulator" && "$IOS_CPU" == "x86_64" ]]; then
87 MIN_VER=-miphonesimulator-version-min=6
89 # Apple TV can be 32-bit Intel (1st gen), 32-bit ARM (2nd, 3rd gen) or 64-bit ARM (4th gen)
90 elif [[ "$IOS_SDK" == "AppleTVOS" && "$IOS_CPU" == "i386" ]]; then
91 MIN_VER=-mappletvos-version-min=6
92 elif [[ "$IOS_SDK" == "AppleTVOS" && "$IOS_CPU" == "armv7"* ]]; then
93 MIN_VER=-mappletvos-version-min=6
94 elif [[ "$IOS_SDK" == "AppleTVOS" && "$IOS_CPU" == "arm64" ]]; then
95 MIN_VER=-mappletvos-version-min=6
98 elif [[ "$IOS_SDK" == "AppleTVSimulator" && "$IOS_CPU" == "i386" ]]; then
99 MIN_VER=-mappletvsimulator-version-min=6
100 elif [[ "$IOS_SDK" == "AppleTVSimulator" && "$IOS_CPU" == "x86_64" ]]; then
101 MIN_VER=-mappletvsimulator-version-min=6
103 # Watch can be either 32-bit or 64-bit ARM. TODO: figure out which
104 # -mwatchos-version-min=n is needed for arm64. 9 is not enough.
105 elif [[ "$IOS_SDK" == "WatchOS" && "$IOS_CPU" == "armv7"* ]]; then
106 MIN_VER=-mwatchos-version-min=6
107 elif [[ "$IOS_SDK" == "WatchOS" && "$IOS_CPU" == "arm64" ]]; then
108 MIN_VER=-mwatchos-version-min=10
110 # Simulator builds. TODO: figure out which -watchos-version-min=n
111 # is needed for arm64. 6 compiles and links, but is it correct?
112 elif [[ "$IOS_SDK" == "WatchSimulator" && "$IOS_CPU" == "i386" ]]; then
113 MIN_VER=-mwatchsimulator-version-min=6
114 elif [[ "$IOS_SDK" == "WatchSimulator" && "$IOS_CPU" == "x86_64" ]]; then
115 MIN_VER=-mwatchsimulator-version-min=6
117 # And the final catch-all
119 echo "IOS_SDK and IOS_CPU are not valid. Please fix them"
120 [[ "$0" = "${BASH_SOURCE[0]}" ]] && exit 1 || return 1
123 #####################################################################
125 # Xcode 6 and below cannot handle -miphonesimulator-version-min
126 # Fix it so the simulator will compile as expected. This trick
127 # may work on other SDKs, but it was not tested.
129 if [ -n "$(command -v xcodebuild 2>/dev/null)" ]; then
130 # Output of xcodebuild is similar to "Xcode 6.2". The first cut gets
131 # the dotted decimal value. The second cut gets the major version.
132 XCODE_VERSION=$(xcodebuild -version 2>/dev/null | head -n 1 | cut -f2 -d" " | cut -f1 -d".")
133 if [ -z "$XCODE_VERSION" ]; then XCODE_VERSION=100; fi
135 if [ "$XCODE_VERSION" -le 6 ]; then
136 MIN_VER="${MIN_VER//iphonesimulator/iphoneos}"
140 #####################################################################
142 # Allow a user override? I think we should be doing this. The use case is,
143 # move /Applications/Xcode somewhere else for a side-by-side installation.
144 if [ -z "${XCODE_DEVELOPER-}" ]; then
145 XCODE_DEVELOPER=$(xcode-select -print-path 2>/dev/null)
148 if [ ! -d "$XCODE_DEVELOPER" ]; then
149 echo "ERROR: unable to find XCODE_DEVELOPER directory."
150 [ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
153 # XCODE_DEVELOPER_SDK is the SDK location.
154 XCODE_DEVELOPER_SDK="$XCODE_DEVELOPER/Platforms/$IOS_SDK.platform"
156 if [ ! -d "$XCODE_DEVELOPER_SDK" ]; then
157 echo "ERROR: unable to find XCODE_DEVELOPER_SDK directory."
158 echo " Is the SDK supported by Xcode and installed?"
159 [ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
162 # XCODE_TOOLCHAIN is the location of the actual compiler tools.
163 if [ -d "$XCODE_DEVELOPER/Toolchains/XcodeDefault.xctoolchain/usr/bin/" ]; then
164 XCODE_TOOLCHAIN="$XCODE_DEVELOPER/Toolchains/XcodeDefault.xctoolchain/usr/bin/"
165 elif [ -d "$XCODE_DEVELOPER_SDK/Developer/usr/bin/" ]; then
166 XCODE_TOOLCHAIN="$XCODE_DEVELOPER_SDK/Developer/usr/bin/"
169 if [ -z "$XCODE_TOOLCHAIN" ] || [ ! -d "$XCODE_TOOLCHAIN" ]; then
170 echo "ERROR: unable to find Xcode cross-compiler tools."
171 [ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
174 # XCODE_SDK is the SDK name/version being used - adjust the list as appropriate.
175 # For example, remove 4.3, 6.2, and 6.1 if they are not installed. We go back to
176 # the 1.0 SDKs because Apple WatchOS uses low numbers, like 2.0 and 2.1.
178 for i in $(seq -f "%.1f" 30.0 -0.1 1.0)
180 if [ -d "$XCODE_DEVELOPER_SDK/Developer/SDKs/$IOS_SDK$i.sdk" ]; then
181 XCODE_SDK="$IOS_SDK$i.sdk"
187 if [ -z "$XCODE_SDK" ]; then
188 echo "ERROR: unable to find a SDK."
189 [ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
192 IOS_SYSROOT="$XCODE_DEVELOPER_SDK/Developer/SDKs/$XCODE_SDK"
194 if [ -z "$IOS_SYSROOT" ] || [ ! -d "$IOS_SYSROOT" ]; then
195 echo "ERROR: unable to find IOS_SYSROOT directory."
196 [ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
199 #####################################################################
201 # We want to set AR=libtool and ARFLAGS="-static -o",
202 # but I am not sure Autotools can handle it.
203 CPP=cpp; CC=clang; CXX=clang++; LD=ld
204 AS=as; AR=ar; RANLIB=ranlib; STRIP=strip
207 if [ ! -e "$XCODE_TOOLCHAIN/$CC" ]; then
208 echo "ERROR: Failed to find iOS clang. Please edit this script."
209 [ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
213 if [ ! -e "$XCODE_TOOLCHAIN/$CXX" ]; then
214 echo "ERROR: Failed to find iOS clang++. Please edit this script."
215 [ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
219 if [ ! -e "$XCODE_TOOLCHAIN/$RANLIB" ]; then
220 echo "ERROR: Failed to find iOS ranlib. Please edit this script."
221 [ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
225 if [ ! -e "$XCODE_TOOLCHAIN/$AR" ]; then
226 echo "ERROR: Failed to find iOS ar. Please edit this script."
227 [ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
231 if [ ! -e "$XCODE_TOOLCHAIN/$AS" ]; then
232 echo "ERROR: Failed to find iOS as. Please edit this script."
233 [ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
237 if [ ! -e "$XCODE_TOOLCHAIN/$LD" ]; then
238 echo "ERROR: Failed to find iOS ld. Please edit this script."
239 [ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
242 #####################################################################
244 LENGTH=${#XCODE_TOOLCHAIN}
245 SUBSTR=${PATH:0:$LENGTH}
246 if [ "$SUBSTR" != "$XCODE_TOOLCHAIN" ]; then
247 export PATH="$XCODE_TOOLCHAIN":"$PATH"
250 #####################################################################
252 export CPP CC CXX LD AS AR RANLIB STRIP
254 export CFLAGS="-arch $IOS_CPU $MIN_VER --sysroot=$IOS_SYSROOT"
255 export CXXFLAGS="-arch $IOS_CPU $MIN_VER -stdlib-libc++ --sysroot=$IOS_SYSROOT"
257 #####################################################################
259 echo "XCODE_TOOLCHAIN: $XCODE_TOOLCHAIN"
261 echo "CPP: $(command -v "$CPP")"
262 echo "CC: $(command -v "$CC")"
263 echo "CXX: $(command -v "$CXX")"
264 echo "LD: $(command -v "$LD")"
265 echo "AS: $(command -v "$AS")"
266 echo "AR: $(command -v "$AR")"
268 echo "IOS_SYSROOT: $IOS_SYSROOT"
270 echo "CPPFLAGS: $CPPFLAGS"
271 echo "CFLAGS: $CFLAGS"
272 echo "CXXFLAGS: $CXXFLAGS"
274 [ "$0" = "${BASH_SOURCE[0]}" ] && exit 0 || return 0