@@ -143,6 +143,9 @@ def add_build_args(parser):
143143 dest = "cross_compile_hosts" ,
144144 help = "List of cross compile hosts targets." ,
145145 default = [])
146+ parser .add_argument (
147+ "--cross-compile-config" ,
148+ help = "Swift flags to cross-compile SPM with itself" )
146149
147150def add_test_args (parser ):
148151 """Configures the parser with the arguments necessary for the test action."""
@@ -196,8 +199,14 @@ def parse_build_args(args):
196199 args .clang_path = get_clang_path (args )
197200 args .cmake_path = get_cmake_path (args )
198201 args .ninja_path = get_ninja_path (args )
199- if args .cross_compile_hosts : # Use XCBuild target directory when building for multiple arches.
200- args .target_dir = os .path .join (args .build_dir , "apple/Products" )
202+ if args .cross_compile_hosts :
203+ if "macosx-arm64" in args .cross_compile_hosts :
204+ # Use XCBuild target directory when building for multiple arches.
205+ args .target_dir = os .path .join (args .build_dir , "apple/Products" )
206+ elif re .match ('android-' , args .cross_compile_hosts ):
207+ args .target_dir = os .path .join (
208+ args .build_dir ,
209+ get_build_target (args ,cross_compile = True ))
201210 else :
202211 args .target_dir = os .path .join (args .build_dir , get_build_target (args ))
203212 args .bootstrap_dir = os .path .join (args .target_dir , "bootstrap" )
@@ -271,10 +280,15 @@ def get_ninja_path(args):
271280 else :
272281 return call_output (["which" , "ninja" ], verbose = args .verbose )
273282
274- def get_build_target (args ):
275- """Returns the target-triple of the current machine."""
283+ def get_build_target (args , cross_compile = False ):
284+ """Returns the target-triple of the current machine or for cross-compilation ."""
276285 try :
277- target_info_json = subprocess .check_output ([args .swiftc_path , '-print-target-info' ], stderr = subprocess .PIPE , universal_newlines = True ).strip ()
286+ command = [args .swiftc_path , '-print-target-info' ]
287+ if cross_compile :
288+ cross_compile_json = json .load (open (args .cross_compile_config ))
289+ command += ['-target' , cross_compile_json ["target" ]]
290+ target_info_json = subprocess .check_output (command ,
291+ stderr = subprocess .PIPE , universal_newlines = True ).strip ()
278292 args .target_info = json .loads (target_info_json )
279293 return args .target_info ["target" ]["unversionedTriple" ]
280294 except Exception as e :
@@ -309,8 +323,8 @@ def build(args):
309323 build_swift_argument_parser (args )
310324 build_swift_driver (args )
311325 build_swift_crypto (args )
326+ build_swiftpm_with_cmake (args )
312327
313- build_swiftpm_with_cmake (args )
314328 build_swiftpm_with_swiftpm (args ,integrated_swift_driver = False )
315329
316330def test (args ):
@@ -458,7 +472,7 @@ def install_binary(args, binary, dest_dir):
458472# Build functions
459473# -----------------------------------------------------------
460474
461- def build_with_cmake (args , cmake_args , source_path , build_dir , targets = [] ):
475+ def build_with_cmake (args , cmake_args , source_path , build_dir ):
462476 """Runs CMake if needed, then builds with Ninja."""
463477 cache_path = os .path .join (build_dir , "CMakeCache.txt" )
464478 if args .reconfigure or not os .path .isfile (cache_path ) or not args .swiftc_path in open (cache_path ).read ():
@@ -489,8 +503,6 @@ def build_with_cmake(args, cmake_args, source_path, build_dir, targets=[]):
489503 if args .verbose :
490504 ninja_cmd .append ("-v" )
491505
492- ninja_cmd += targets
493-
494506 call (ninja_cmd , cwd = build_dir , verbose = args .verbose )
495507
496508def build_llbuild (args ):
@@ -596,26 +608,20 @@ def build_swiftpm_with_cmake(args):
596608 """Builds SwiftPM using CMake."""
597609 note ("Building SwiftPM (with CMake)" )
598610
599- if args .bootstrap :
600- cmake_flags = [
601- get_llbuild_cmake_arg (args ),
602- "-DTSC_DIR=" + os .path .join (args .tsc_build_dir , "cmake/modules" ),
603- "-DYams_DIR=" + os .path .join (args .yams_build_dir , "cmake/modules" ),
604- "-DArgumentParser_DIR=" + os .path .join (args .swift_argument_parser_build_dir , "cmake/modules" ),
605- "-DSwiftDriver_DIR=" + os .path .join (args .swift_driver_build_dir , "cmake/modules" ),
606- "-DSwiftCrypto_DIR=" + os .path .join (args .swift_crypto_build_dir , "cmake/modules" ),
607- "-DFIND_PM_DEPS:BOOL=YES" ,
608- ]
609- else :
610- cmake_flags = [ "-DFIND_PM_DEPS:BOOL=NO" ]
611+ cmake_flags = [
612+ get_llbuild_cmake_arg (args ),
613+ "-DTSC_DIR=" + os .path .join (args .tsc_build_dir , "cmake/modules" ),
614+ "-DYams_DIR=" + os .path .join (args .yams_build_dir , "cmake/modules" ),
615+ "-DArgumentParser_DIR=" + os .path .join (args .swift_argument_parser_build_dir , "cmake/modules" ),
616+ "-DSwiftDriver_DIR=" + os .path .join (args .swift_driver_build_dir , "cmake/modules" ),
617+ "-DSwiftCrypto_DIR=" + os .path .join (args .swift_crypto_build_dir , "cmake/modules" ),
618+ ]
611619
612620 if platform .system () == 'Darwin' :
613621 cmake_flags .append ("-DCMAKE_C_FLAGS=-target %s%s" % (get_build_target (args ), g_macos_deployment_target ))
614622 cmake_flags .append ("-DCMAKE_OSX_DEPLOYMENT_TARGET=%s" % g_macos_deployment_target )
615623
616- targets = [] if args .bootstrap else ["PD4" , "PD4_2" ]
617-
618- build_with_cmake (args , cmake_flags , args .project_root , args .bootstrap_dir , targets )
624+ build_with_cmake (args , cmake_flags , args .project_root , args .bootstrap_dir )
619625
620626 if args .llbuild_link_framework :
621627 add_rpath_for_cmake_build (args , args .llbuild_build_dir )
@@ -791,7 +797,8 @@ def get_swiftpm_flags(args):
791797 )
792798
793799 # Don't use GNU strerror_r on Android.
794- if 'ANDROID_DATA' in os .environ :
800+ if 'ANDROID_DATA' in os .environ or (args .cross_compile_hosts and re .match (
801+ 'android-' , args .cross_compile_hosts )):
795802 build_flags .extend (["-Xswiftc" , "-Xcc" , "-Xswiftc" , "-U_GNU_SOURCE" ])
796803
797804 # On ELF platforms, remove the host toolchain's stdlib absolute rpath from
@@ -803,6 +810,8 @@ def get_swiftpm_flags(args):
803810 cross_compile_hosts = args .cross_compile_hosts
804811 if build_target == 'x86_64-apple-macosx' and "macosx-arm64" in cross_compile_hosts :
805812 build_flags += ["--arch" , "x86_64" , "--arch" , "arm64" ]
813+ elif cross_compile_hosts and re .match ('android-' , cross_compile_hosts ):
814+ build_flags .extend (["--destination" , args .cross_compile_config ])
806815 elif cross_compile_hosts :
807816 error ("cannot cross-compile for %s" % cross_compile_hosts )
808817
0 commit comments