13
13
import abc
14
14
import os
15
15
16
+ from build_swift .build_swift .wrappers import xcrun
17
+
16
18
from .. import cmake
19
+ from .. import shell
17
20
from .. import targets
18
21
19
22
@@ -193,14 +196,15 @@ def install_toolchain_path(self, host_target):
193
196
return targets .toolchain_path (install_destdir ,
194
197
self .args .install_prefix )
195
198
199
+ def is_darwin_host (self , host_target ):
200
+ return host_target .startswith ("macosx" ) or \
201
+ host_target .startswith ("iphone" ) or \
202
+ host_target .startswith ("appletv" ) or \
203
+ host_target .startswith ("watch" )
204
+
196
205
def should_include_host_in_lipo (self , host_target ):
197
- if self .args .cross_compile_hosts :
198
- if host_target .startswith ("macosx" ) or \
199
- host_target .startswith ("iphone" ) or \
200
- host_target .startswith ("appletv" ) or \
201
- host_target .startswith ("watch" ):
202
- return True
203
- return False
206
+ return self .args .cross_compile_hosts and \
207
+ self .is_darwin_host (host_target )
204
208
205
209
def host_install_destdir (self , host_target ):
206
210
if self .args .cross_compile_hosts :
@@ -222,38 +226,62 @@ def is_cross_compile_target(self, host_target):
222
226
return self .args .cross_compile_hosts and \
223
227
host_target in self .args .cross_compile_hosts
224
228
225
- # TODO: Remove once we've moved over to cmake toolchains
226
- def common_cross_c_flags ( self , platform , arch ):
227
- cross_flags = []
229
+ def generate_darwin_toolchain_file ( self , platform , arch ):
230
+ shell . makedirs ( self . build_dir )
231
+ toolchain_file = os . path . join ( self . build_dir , 'BuildScriptToolchain.cmake' )
228
232
233
+ cmake_osx_sysroot = xcrun .sdk_path (platform )
234
+
235
+ target = None
229
236
if platform == 'macosx' :
230
237
target = '{}-apple-macosx{}' .format (
231
238
arch , self .args .darwin_deployment_version_osx )
232
- cross_flags .extend (['-arch' , arch , '-target' , target ])
233
239
elif platform == 'iphonesimulator' :
234
240
target = '{}-apple-ios{}' .format (
235
241
arch , self .args .darwin_deployment_version_ios )
236
- cross_flags .extend (['-arch' , arch , '-target' , target ])
237
242
elif platform == 'iphoneos' :
238
243
target = '{}-apple-ios{}' .format (
239
244
arch , self .args .darwin_deployment_version_ios )
240
- cross_flags .extend (['-arch' , arch , '-target' , target ])
241
245
elif platform == 'appletvsimulator' :
242
246
target = '{}-apple-tvos{}' .format (
243
247
arch , self .args .darwin_deployment_version_tvos )
244
- cross_flags .extend (['-arch' , arch , '-target' , target ])
245
248
elif platform == 'appletvos' :
246
249
target = '{}-apple-tvos{}' .format (
247
250
arch , self .args .darwin_deployment_version_tvos )
248
- cross_flags .extend (['-arch' , arch , '-target' , target ])
249
251
elif platform == 'watchsimulator' :
250
252
target = '{}-apple-watchos{}' .format (
251
253
arch , self .args .darwin_deployment_version_watchos )
252
- cross_flags .extend (['-arch' , arch , '-target' , target ])
253
254
elif platform == 'watchos' :
254
255
target = '{}-apple-watchos{}' .format (
255
256
arch , self .args .darwin_deployment_version_watchos )
256
- cross_flags .extend (['-arch' , arch , '-target' , target ])
257
+ else :
258
+ raise RuntimeError ("Unhandled platform?!" )
259
+
260
+ toolchain_args = {}
261
+
262
+ toolchain_args ['CMAKE_SYSTEM_NAME' ] = 'Darwin'
263
+ toolchain_args ['CMAKE_OSX_SYSROOT' ] = cmake_osx_sysroot
264
+ toolchain_args ['CMAKE_OSX_ARCHITECTURES' ] = arch
265
+
266
+ if self .toolchain .cc .endswith ('clang' ):
267
+ toolchain_args ['CMAKE_C_COMPILER_TARGET' ] = target
268
+ if self .toolchain .cxx .endswith ('clang++' ):
269
+ toolchain_args ['CMAKE_CXX_COMPILER_TARGET' ] = target
270
+ # Swift always supports cross compiling.
271
+ toolchain_args ['CMAKE_Swift_COMPILER_TARGET' ] = target
272
+
273
+ # Sort by the key so that we always produce the same toolchain file
274
+ data = sorted (toolchain_args .items (), key = lambda x : x [0 ])
275
+ if not self .args .dry_run :
276
+ with open (toolchain_file , 'w' ) as f :
277
+ f .writelines ("set({} {})\n " .format (k , v ) for k , v in data )
278
+ else :
279
+ print ("DRY_RUN! Writing Toolchain file to path: {}" .format (toolchain_file ))
280
+
281
+ return toolchain_file
282
+
283
+ def common_cross_c_flags (self , platform , arch ):
284
+ cross_flags = []
257
285
258
286
if self .is_release ():
259
287
cross_flags .append ('-fno-stack-protector' )
0 commit comments