@@ -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,12 @@ 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 (args .build_dir , get_build_target (args ,cross_compile = True ))
201
211
else :
202
212
args .target_dir = os .path .join (args .build_dir , get_build_target (args ))
203
213
args .bootstrap_dir = os .path .join (args .target_dir , "bootstrap" )
@@ -271,10 +281,13 @@ def get_ninja_path(args):
271
281
else :
272
282
return call_output (["which" , "ninja" ], verbose = args .verbose )
273
283
274
- def get_build_target (args ):
284
+ def get_build_target (args , cross_compile = False ):
275
285
"""Returns the target-triple of the current machine."""
276
286
try :
277
- target_info_json = subprocess .check_output ([args .swiftc_path , '-print-target-info' ], stderr = subprocess .PIPE , universal_newlines = True ).strip ()
287
+ if cross_compile :
288
+ target_info_json = subprocess .check_output ([args .swiftc_path , '-print-target-info' ] + args .cross_compile_flags .split (), stderr = subprocess .PIPE , universal_newlines = True ).strip ()
289
+ else :
290
+ target_info_json = subprocess .check_output ([args .swiftc_path , '-print-target-info' ], stderr = subprocess .PIPE , universal_newlines = True ).strip ()
278
291
args .target_info = json .loads (target_info_json )
279
292
return args .target_info ["target" ]["unversionedTriple" ]
280
293
except Exception as e :
@@ -309,8 +322,11 @@ def build(args):
309
322
build_swift_argument_parser (args )
310
323
build_swift_driver (args )
311
324
build_swift_crypto (args )
325
+ build_swiftpm_with_cmake (args )
326
+
327
+ if args .cross_compile_flags :
328
+ build_packagedescription_libs_with_cmake (args )
312
329
313
- build_swiftpm_with_cmake (args )
314
330
build_swiftpm_with_swiftpm (args ,integrated_swift_driver = False )
315
331
316
332
def test (args ):
@@ -459,11 +475,15 @@ def install_binary(args, binary, dest_dir):
459
475
# Build functions
460
476
# -----------------------------------------------------------
461
477
462
- def build_with_cmake (args , cmake_args , source_path , build_dir , targets = []):
478
+ def build_with_cmake (args , cmake_args , source_path , build_dir , targets = [], cross_compile = False ):
463
479
"""Runs CMake if needed, then builds with Ninja."""
464
480
cache_path = os .path .join (build_dir , "CMakeCache.txt" )
465
481
if args .reconfigure or not os .path .isfile (cache_path ) or not args .swiftc_path in open (cache_path ).read ():
466
- swift_flags = ""
482
+ if cross_compile :
483
+ swift_flags = args .cross_compile_flags
484
+ else :
485
+ swift_flags = ""
486
+
467
487
if args .sysroot :
468
488
swift_flags = "-sdk %s" % args .sysroot
469
489
@@ -593,30 +613,38 @@ def add_rpath_for_cmake_build(args, rpath):
593
613
note (' ' .join (add_rpath_cmd ))
594
614
subprocess .call (add_rpath_cmd , stderr = subprocess .PIPE )
595
615
616
+ def build_packagedescription_libs_with_cmake (args ):
617
+ """Builds the PackageDescription libraries using CMake."""
618
+ note ("Building PackageDescription libraries (with CMake)" )
619
+
620
+ cmake_flags = ["-DFIND_PM_DEPS:BOOL=NO" ]
621
+ targets = ["PD4" , "PD4_2" , "PackagePlugin" ]
622
+ if re .match ('android-' , args .cross_compile_hosts ):
623
+ cmake_flags .append ("-DCMAKE_SYSTEM_NAME=Android" )
624
+ cmake_flags .append ("-DCMAKE_SYSTEM_VERSION=1" )
625
+
626
+ build_with_cmake (args , cmake_flags , args .project_root , args .bootstrap_dir ,
627
+ targets , cross_compile = True )
628
+
596
629
def build_swiftpm_with_cmake (args ):
597
630
"""Builds SwiftPM using CMake."""
598
631
note ("Building SwiftPM (with CMake)" )
599
632
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" ]
633
+ cmake_flags = [
634
+ get_llbuild_cmake_arg (args ),
635
+ "-DTSC_DIR=" + os .path .join (args .tsc_build_dir , "cmake/modules" ),
636
+ "-DYams_DIR=" + os .path .join (args .yams_build_dir , "cmake/modules" ),
637
+ "-DArgumentParser_DIR=" + os .path .join (args .swift_argument_parser_build_dir , "cmake/modules" ),
638
+ "-DSwiftDriver_DIR=" + os .path .join (args .swift_driver_build_dir , "cmake/modules" ),
639
+ "-DSwiftCrypto_DIR=" + os .path .join (args .swift_crypto_build_dir , "cmake/modules" ),
640
+ "-DFIND_PM_DEPS:BOOL=YES" ,
641
+ ]
612
642
613
643
if platform .system () == 'Darwin' :
614
644
cmake_flags .append ("-DCMAKE_C_FLAGS=-target %s%s" % (get_build_target (args ), g_macos_deployment_target ))
615
645
cmake_flags .append ("-DCMAKE_OSX_DEPLOYMENT_TARGET=%s" % g_macos_deployment_target )
616
646
617
- targets = [] if args .bootstrap else ["PD4" , "PD4_2" ]
618
-
619
- build_with_cmake (args , cmake_flags , args .project_root , args .bootstrap_dir , targets )
647
+ build_with_cmake (args , cmake_flags , args .project_root , args .bootstrap_dir )
620
648
621
649
if args .llbuild_link_framework :
622
650
add_rpath_for_cmake_build (args , args .llbuild_build_dir )
@@ -792,7 +820,8 @@ def get_swiftpm_flags(args):
792
820
)
793
821
794
822
# Don't use GNU strerror_r on Android.
795
- if 'ANDROID_DATA' in os .environ :
823
+ if 'ANDROID_DATA' in os .environ or re .match (
824
+ 'android-' , args .cross_compile_hosts ):
796
825
build_flags .extend (["-Xswiftc" , "-Xcc" , "-Xswiftc" , "-U_GNU_SOURCE" ])
797
826
798
827
# On ELF platforms, remove the host toolchain's stdlib absolute rpath from
@@ -804,6 +833,8 @@ def get_swiftpm_flags(args):
804
833
cross_compile_hosts = args .cross_compile_hosts
805
834
if build_target == 'x86_64-apple-macosx' and "macosx-arm64" in cross_compile_hosts :
806
835
build_flags += ["--arch" , "x86_64" , "--arch" , "arm64" ]
836
+ elif cross_compile_hosts and re .match ('android-' , cross_compile_hosts ):
837
+ build_flags .extend (["--destination" , args .cross_compile_config ])
807
838
elif cross_compile_hosts :
808
839
error ("cannot cross-compile for %s" % cross_compile_hosts )
809
840
0 commit comments