1313import abc
1414import os
1515
16+ from build_swift .build_swift .wrappers import xcrun
17+
1618from .. import cmake
19+ from .. import shell
1720from .. import targets
1821
1922
@@ -204,14 +207,15 @@ def install_toolchain_path(self, host_target):
204207 return targets .toolchain_path (install_destdir ,
205208 self .args .install_prefix )
206209
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+
207216 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 )
215219
216220 def host_install_destdir (self , host_target ):
217221 if self .args .cross_compile_hosts :
@@ -233,38 +237,62 @@ def is_cross_compile_target(self, host_target):
233237 return self .args .cross_compile_hosts and \
234238 host_target in self .args .cross_compile_hosts
235239
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' )
239243
244+ cmake_osx_sysroot = xcrun .sdk_path (platform )
245+
246+ target = None
240247 if platform == 'macosx' :
241248 target = '{}-apple-macosx{}' .format (
242249 arch , self .args .darwin_deployment_version_osx )
243- cross_flags .extend (['-arch' , arch , '-target' , target ])
244250 elif platform == 'iphonesimulator' :
245251 target = '{}-apple-ios{}' .format (
246252 arch , self .args .darwin_deployment_version_ios )
247- cross_flags .extend (['-arch' , arch , '-target' , target ])
248253 elif platform == 'iphoneos' :
249254 target = '{}-apple-ios{}' .format (
250255 arch , self .args .darwin_deployment_version_ios )
251- cross_flags .extend (['-arch' , arch , '-target' , target ])
252256 elif platform == 'appletvsimulator' :
253257 target = '{}-apple-tvos{}' .format (
254258 arch , self .args .darwin_deployment_version_tvos )
255- cross_flags .extend (['-arch' , arch , '-target' , target ])
256259 elif platform == 'appletvos' :
257260 target = '{}-apple-tvos{}' .format (
258261 arch , self .args .darwin_deployment_version_tvos )
259- cross_flags .extend (['-arch' , arch , '-target' , target ])
260262 elif platform == 'watchsimulator' :
261263 target = '{}-apple-watchos{}' .format (
262264 arch , self .args .darwin_deployment_version_watchos )
263- cross_flags .extend (['-arch' , arch , '-target' , target ])
264265 elif platform == 'watchos' :
265266 target = '{}-apple-watchos{}' .format (
266267 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 = []
268296
269297 if self .is_release ():
270298 cross_flags .append ('-fno-stack-protector' )
0 commit comments