]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/unbound/contrib/ios/setenv_ios.sh
MFV 364468:
[FreeBSD/FreeBSD.git] / contrib / unbound / contrib / ios / setenv_ios.sh
1 #!/usr/bin/env bash
2
3 # ====================================================================
4 # Sets the cross compile environment for Xcode/iOS
5 #
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 # ====================================================================
10
11 #########################################
12 #####        Some validation        #####
13 #########################################
14
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.
18
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
22 fi
23
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
27 fi
28
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."
32 fi
33
34 #########################################
35 #####    Small Fixups, if needed    #####
36 #########################################
37
38 if [[ "$IOS_SDK" == "iPhone" ]]; then
39     IOS_SDK=iPhoneOS
40 fi
41
42 if [[ "$IOS_SDK" == "iPhoneOSSimulator" ]]; then
43     IOS_SDK=iPhoneSimulator
44 fi
45
46 if [[ "$IOS_SDK" == "TV" || "$IOS_SDK" == "AppleTV" ]]; then
47     IOS_SDK=AppleTVOS
48 fi
49
50 if [[ "$IOS_SDK" == "Watch" || "$IOS_SDK" == "AppleWatch" ]]; then
51     IOS_SDK=WatchOS
52 fi
53
54 if [[ "$IOS_CPU" == "aarch64" || "$IOS_CPU" == "armv8"* ]] ; then
55     IOS_CPU=arm64
56 fi
57
58 ########################################
59 #####         Environment          #####
60 ########################################
61
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.
66
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
72
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
82
83 # Simulator builds
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
88
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
96
97 # Simulator builds
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
102
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
109
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
116
117 # And the final catch-all
118 else
119     echo "IOS_SDK and IOS_CPU are not valid. Please fix them"
120     [[ "$0" = "${BASH_SOURCE[0]}" ]] && exit 1 || return 1
121 fi
122
123 #####################################################################
124
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.
128
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
134
135     if [ "$XCODE_VERSION" -le 6 ]; then
136         MIN_VER="${MIN_VER//iphonesimulator/iphoneos}"
137     fi
138 fi
139
140 #####################################################################
141
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)
146 fi
147
148 if [ ! -d "$XCODE_DEVELOPER" ]; then
149   echo "ERROR: unable to find XCODE_DEVELOPER directory."
150   [ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
151 fi
152
153 # XCODE_DEVELOPER_SDK is the SDK location.
154 XCODE_DEVELOPER_SDK="$XCODE_DEVELOPER/Platforms/$IOS_SDK.platform"
155
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
160 fi
161
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/"
167 fi
168
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
172 fi
173
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.
177 XCODE_SDK=
178 for i in $(seq -f "%.1f" 30.0 -0.1 1.0)
179 do
180     if [ -d "$XCODE_DEVELOPER_SDK/Developer/SDKs/$IOS_SDK$i.sdk" ]; then
181         XCODE_SDK="$IOS_SDK$i.sdk"
182         break
183     fi
184 done
185
186 # Error checking
187 if [ -z "$XCODE_SDK" ]; then
188     echo "ERROR: unable to find a SDK."
189     [ "$0" = "${BASH_SOURCE[0]}" ] && exit 1 || return 1
190 fi
191
192 IOS_SYSROOT="$XCODE_DEVELOPER_SDK/Developer/SDKs/$XCODE_SDK"
193
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
197 fi
198
199 #####################################################################
200
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
205
206 # Error checking
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
210 fi
211
212 # Error checking
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
216 fi
217
218 # Error checking
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
222 fi
223
224 # Error checking
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
228 fi
229
230 # Error checking
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
234 fi
235
236 # Error checking
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
240 fi
241
242 #####################################################################
243
244 LENGTH=${#XCODE_TOOLCHAIN}
245 SUBSTR=${PATH:0:$LENGTH}
246 if [ "$SUBSTR" != "$XCODE_TOOLCHAIN" ]; then
247     export PATH="$XCODE_TOOLCHAIN":"$PATH"
248 fi
249
250 #####################################################################
251
252 export CPP CC CXX LD AS AR RANLIB STRIP
253 export IOS_SYSROOT
254 export CFLAGS="-arch $IOS_CPU $MIN_VER --sysroot=$IOS_SYSROOT"
255 export CXXFLAGS="-arch $IOS_CPU $MIN_VER -stdlib-libc++ --sysroot=$IOS_SYSROOT"
256
257 #####################################################################
258
259 echo "XCODE_TOOLCHAIN: $XCODE_TOOLCHAIN"
260
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")"
267
268 echo "IOS_SYSROOT: $IOS_SYSROOT"
269
270 echo "CPPFLAGS: $CPPFLAGS"
271 echo "CFLAGS: $CFLAGS"
272 echo "CXXFLAGS: $CXXFLAGS"
273
274 [ "$0" = "${BASH_SOURCE[0]}" ] && exit 0 || return 0