@@ -143,6 +143,12 @@ def add_build_args(parser):
143
143
dest = "cross_compile_hosts" ,
144
144
help = "List of cross compile hosts targets." ,
145
145
default = [])
146
+ parser .add_argument (
147
+ "--cross-compile-flags" ,
148
+ help = "Swift flags to cross-compile the PackageDescription libraries with CMake" )
149
+ parser .add_argument (
150
+ "--cross-compile-config" ,
151
+ help = "Swift flags to cross-compile SPM with itself" )
146
152
147
153
def add_test_args (parser ):
148
154
"""Configures the parser with the arguments necessary for the test action."""
@@ -196,8 +202,14 @@ def parse_build_args(args):
196
202
args .clang_path = get_clang_path (args )
197
203
args .cmake_path = get_cmake_path (args )
198
204
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" )
205
+ if args .cross_compile_hosts :
206
+ if "macosx-arm64" in args .cross_compile_hosts :
207
+ # Use XCBuild target directory when building for multiple arches.
208
+ args .target_dir = os .path .join (args .build_dir , "apple/Products" )
209
+ elif re .match ('android-' , args .cross_compile_hosts ):
210
+ args .target_dir = os .path .join (
211
+ args .build_dir ,
212
+ get_build_target (args ,cross_compile = True ))
201
213
else :
202
214
args .target_dir = os .path .join (args .build_dir , get_build_target (args ))
203
215
args .bootstrap_dir = os .path .join (args .target_dir , "bootstrap" )
@@ -271,10 +283,18 @@ def get_ninja_path(args):
271
283
else :
272
284
return call_output (["which" , "ninja" ], verbose = args .verbose )
273
285
274
- def get_build_target (args ):
286
+ def get_build_target (args , cross_compile = False ):
275
287
"""Returns the target-triple of the current machine."""
276
288
try :
277
- target_info_json = subprocess .check_output ([args .swiftc_path , '-print-target-info' ], stderr = subprocess .PIPE , universal_newlines = True ).strip ()
289
+ if cross_compile :
290
+ target_info_json = subprocess .check_output (
291
+ [args .swiftc_path , '-print-target-info' ] +
292
+ args .cross_compile_flags .split (),
293
+ stderr = subprocess .PIPE , universal_newlines = True ).strip ()
294
+ else :
295
+ target_info_json = subprocess .check_output (
296
+ [args .swiftc_path , '-print-target-info' ],
297
+ stderr = subprocess .PIPE , universal_newlines = True ).strip ()
278
298
args .target_info = json .loads (target_info_json )
279
299
return args .target_info ["target" ]["unversionedTriple" ]
280
300
except Exception as e :
@@ -309,8 +329,11 @@ def build(args):
309
329
build_swift_argument_parser (args )
310
330
build_swift_driver (args )
311
331
build_swift_crypto (args )
332
+ build_swiftpm_with_cmake (args )
333
+
334
+ if args .cross_compile_flags :
335
+ build_packagedescription_libs_with_cmake (args )
312
336
313
- build_swiftpm_with_cmake (args )
314
337
build_swiftpm_with_swiftpm (args ,integrated_swift_driver = False )
315
338
316
339
def test (args ):
@@ -459,11 +482,15 @@ def install_binary(args, binary, dest_dir):
459
482
# Build functions
460
483
# -----------------------------------------------------------
461
484
462
- def build_with_cmake (args , cmake_args , source_path , build_dir , targets = []):
485
+ def build_with_cmake (args , cmake_args , source_path , build_dir , targets = [], cross_compile = False ):
463
486
"""Runs CMake if needed, then builds with Ninja."""
464
487
cache_path = os .path .join (build_dir , "CMakeCache.txt" )
465
488
if args .reconfigure or not os .path .isfile (cache_path ) or not args .swiftc_path in open (cache_path ).read ():
466
- swift_flags = ""
489
+ if cross_compile :
490
+ swift_flags = args .cross_compile_flags
491
+ else :
492
+ swift_flags = ""
493
+
467
494
if args .sysroot :
468
495
swift_flags = "-sdk %s" % args .sysroot
469
496
@@ -593,30 +620,38 @@ def add_rpath_for_cmake_build(args, rpath):
593
620
note (' ' .join (add_rpath_cmd ))
594
621
subprocess .call (add_rpath_cmd , stderr = subprocess .PIPE )
595
622
623
+ def build_packagedescription_libs_with_cmake (args ):
624
+ """Builds the PackageDescription libraries using CMake."""
625
+ note ("Building PackageDescription libraries (with CMake)" )
626
+
627
+ cmake_flags = ["-DFIND_PM_DEPS:BOOL=NO" ]
628
+ targets = ["PD4" , "PD4_2" , "PackagePlugin" ]
629
+ if re .match ('android-' , args .cross_compile_hosts ):
630
+ cmake_flags .append ("-DCMAKE_SYSTEM_NAME=Android" )
631
+ cmake_flags .append ("-DCMAKE_SYSTEM_VERSION=1" )
632
+
633
+ build_with_cmake (args , cmake_flags , args .project_root , args .bootstrap_dir ,
634
+ targets , cross_compile = True )
635
+
596
636
def build_swiftpm_with_cmake (args ):
597
637
"""Builds SwiftPM using CMake."""
598
638
note ("Building SwiftPM (with CMake)" )
599
639
600
- if args .bootstrap :
601
- cmake_flags = [
602
- get_llbuild_cmake_arg (args ),
603
- "-DTSC_DIR=" + os .path .join (args .tsc_build_dir , "cmake/modules" ),
604
- "-DYams_DIR=" + os .path .join (args .yams_build_dir , "cmake/modules" ),
605
- "-DArgumentParser_DIR=" + os .path .join (args .swift_argument_parser_build_dir , "cmake/modules" ),
606
- "-DSwiftDriver_DIR=" + os .path .join (args .swift_driver_build_dir , "cmake/modules" ),
607
- "-DSwiftCrypto_DIR=" + os .path .join (args .swift_crypto_build_dir , "cmake/modules" ),
608
- "-DFIND_PM_DEPS:BOOL=YES" ,
609
- ]
610
- else :
611
- cmake_flags = [ "-DFIND_PM_DEPS:BOOL=NO" ]
640
+ cmake_flags = [
641
+ get_llbuild_cmake_arg (args ),
642
+ "-DTSC_DIR=" + os .path .join (args .tsc_build_dir , "cmake/modules" ),
643
+ "-DYams_DIR=" + os .path .join (args .yams_build_dir , "cmake/modules" ),
644
+ "-DArgumentParser_DIR=" + os .path .join (args .swift_argument_parser_build_dir , "cmake/modules" ),
645
+ "-DSwiftDriver_DIR=" + os .path .join (args .swift_driver_build_dir , "cmake/modules" ),
646
+ "-DSwiftCrypto_DIR=" + os .path .join (args .swift_crypto_build_dir , "cmake/modules" ),
647
+ "-DFIND_PM_DEPS:BOOL=YES" ,
648
+ ]
612
649
613
650
if platform .system () == 'Darwin' :
614
651
cmake_flags .append ("-DCMAKE_C_FLAGS=-target %s%s" % (get_build_target (args ), g_macos_deployment_target ))
615
652
cmake_flags .append ("-DCMAKE_OSX_DEPLOYMENT_TARGET=%s" % g_macos_deployment_target )
616
653
617
- targets = [] if args .bootstrap else ["PD4" , "PD4_2" ]
618
-
619
- build_with_cmake (args , cmake_flags , args .project_root , args .bootstrap_dir , targets )
654
+ build_with_cmake (args , cmake_flags , args .project_root , args .bootstrap_dir )
620
655
621
656
if args .llbuild_link_framework :
622
657
add_rpath_for_cmake_build (args , args .llbuild_build_dir )
@@ -792,7 +827,9 @@ def get_swiftpm_flags(args):
792
827
)
793
828
794
829
# Don't use GNU strerror_r on Android.
795
- if 'ANDROID_DATA' in os .environ :
830
+ if 'ANDROID_DATA' in os .environ or
831
+ (args .cross_compile_hosts and
832
+ re .match ('android-' , args .cross_compile_hosts )):
796
833
build_flags .extend (["-Xswiftc" , "-Xcc" , "-Xswiftc" , "-U_GNU_SOURCE" ])
797
834
798
835
# On ELF platforms, remove the host toolchain's stdlib absolute rpath from
@@ -804,6 +841,8 @@ def get_swiftpm_flags(args):
804
841
cross_compile_hosts = args .cross_compile_hosts
805
842
if build_target == 'x86_64-apple-macosx' and "macosx-arm64" in cross_compile_hosts :
806
843
build_flags += ["--arch" , "x86_64" , "--arch" , "arm64" ]
844
+ elif cross_compile_hosts and re .match ('android-' , cross_compile_hosts ):
845
+ build_flags .extend (["--destination" , args .cross_compile_config ])
807
846
elif cross_compile_hosts :
808
847
error ("cannot cross-compile for %s" % cross_compile_hosts )
809
848
0 commit comments