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
@@ -204,14 +207,15 @@ def install_toolchain_path(self, host_target):
204
207
return targets .toolchain_path (install_destdir ,
205
208
self .args .install_prefix )
206
209
210
+ def is_darwin_host (self , host_target ):
211
+ return host_target .startswith ("macosx" ) or \
212
+ host_target .startswith ("iphone" ) or \
213
+ host_target .startswith ("appletv" ) or \
214
+ host_target .startswith ("watch" )
215
+
207
216
def should_include_host_in_lipo (self , host_target ):
208
- if self .args .cross_compile_hosts :
209
- if host_target .startswith ("macosx" ) or \
210
- host_target .startswith ("iphone" ) or \
211
- host_target .startswith ("appletv" ) or \
212
- host_target .startswith ("watch" ):
213
- return True
214
- return False
217
+ return self .args .cross_compile_hosts and \
218
+ self .is_darwin_host (host_target )
215
219
216
220
def host_install_destdir (self , host_target ):
217
221
if self .args .cross_compile_hosts :
@@ -233,38 +237,62 @@ def is_cross_compile_target(self, host_target):
233
237
return self .args .cross_compile_hosts and \
234
238
host_target in self .args .cross_compile_hosts
235
239
236
- # TODO: Remove once we've moved over to cmake toolchains
237
- def common_cross_c_flags ( self , platform , arch ):
238
- cross_flags = []
240
+ def generate_darwin_toolchain_file ( self , platform , arch ):
241
+ shell . makedirs ( self . build_dir )
242
+ toolchain_file = os . path . join ( self . build_dir , 'BuildScriptToolchain.cmake' )
239
243
244
+ cmake_osx_sysroot = xcrun .sdk_path (platform )
245
+
246
+ target = None
240
247
if platform == 'macosx' :
241
248
target = '{}-apple-macosx{}' .format (
242
249
arch , self .args .darwin_deployment_version_osx )
243
- cross_flags .extend (['-arch' , arch , '-target' , target ])
244
250
elif platform == 'iphonesimulator' :
245
251
target = '{}-apple-ios{}' .format (
246
252
arch , self .args .darwin_deployment_version_ios )
247
- cross_flags .extend (['-arch' , arch , '-target' , target ])
248
253
elif platform == 'iphoneos' :
249
254
target = '{}-apple-ios{}' .format (
250
255
arch , self .args .darwin_deployment_version_ios )
251
- cross_flags .extend (['-arch' , arch , '-target' , target ])
252
256
elif platform == 'appletvsimulator' :
253
257
target = '{}-apple-tvos{}' .format (
254
258
arch , self .args .darwin_deployment_version_tvos )
255
- cross_flags .extend (['-arch' , arch , '-target' , target ])
256
259
elif platform == 'appletvos' :
257
260
target = '{}-apple-tvos{}' .format (
258
261
arch , self .args .darwin_deployment_version_tvos )
259
- cross_flags .extend (['-arch' , arch , '-target' , target ])
260
262
elif platform == 'watchsimulator' :
261
263
target = '{}-apple-watchos{}' .format (
262
264
arch , self .args .darwin_deployment_version_watchos )
263
- cross_flags .extend (['-arch' , arch , '-target' , target ])
264
265
elif platform == 'watchos' :
265
266
target = '{}-apple-watchos{}' .format (
266
267
arch , self .args .darwin_deployment_version_watchos )
267
- cross_flags .extend (['-arch' , arch , '-target' , target ])
268
+ else :
269
+ raise RuntimeError ("Unhandled platform?!" )
270
+
271
+ toolchain_args = {}
272
+
273
+ toolchain_args ['CMAKE_SYSTEM_NAME' ] = 'Darwin'
274
+ toolchain_args ['CMAKE_OSX_SYSROOT' ] = cmake_osx_sysroot
275
+ toolchain_args ['CMAKE_OSX_ARCHITECTURES' ] = arch
276
+
277
+ if self .toolchain .cc .endswith ('clang' ):
278
+ toolchain_args ['CMAKE_C_COMPILER_TARGET' ] = target
279
+ if self .toolchain .cxx .endswith ('clang++' ):
280
+ toolchain_args ['CMAKE_CXX_COMPILER_TARGET' ] = target
281
+ # Swift always supports cross compiling.
282
+ toolchain_args ['CMAKE_Swift_COMPILER_TARGET' ] = target
283
+
284
+ # Sort by the key so that we always produce the same toolchain file
285
+ data = sorted (toolchain_args .items (), key = lambda x : x [0 ])
286
+ if not self .args .dry_run :
287
+ with open (toolchain_file , 'w' ) as f :
288
+ f .writelines ("set({} {})\n " .format (k , v ) for k , v in data )
289
+ else :
290
+ print ("DRY_RUN! Writing Toolchain file to path: {}" .format (toolchain_file ))
291
+
292
+ return toolchain_file
293
+
294
+ def common_cross_c_flags (self , platform , arch ):
295
+ cross_flags = []
268
296
269
297
if self .is_release ():
270
298
cross_flags .append ('-fno-stack-protector' )
0 commit comments