Skip to content

Commit 38df7c3

Browse files
authored
Android: add cross-compilation configuration file flag (#3403)
In addition to '--cross-compile-hosts', add a new bootstrap flag, '--cross-compile-config', that will pass in a JSON file with the destination cross-compilation flags.
1 parent e3ea6ed commit 38df7c3

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

Utilities/bootstrap

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ def add_build_args(parser):
144144
dest="cross_compile_hosts",
145145
help="List of cross compile hosts targets.",
146146
default=[])
147+
parser.add_argument(
148+
"--cross-compile-config",
149+
help="Swift flags to cross-compile SPM with itself")
147150

148151
def add_test_args(parser):
149152
"""Configures the parser with the arguments necessary for the test action."""
@@ -197,8 +200,14 @@ def parse_build_args(args):
197200
args.clang_path = get_clang_path(args)
198201
args.cmake_path = get_cmake_path(args)
199202
args.ninja_path = get_ninja_path(args)
200-
if args.cross_compile_hosts: # Use XCBuild target directory when building for multiple arches.
201-
args.target_dir = os.path.join(args.build_dir, "apple/Products")
203+
if args.cross_compile_hosts:
204+
if "macosx-arm64" in args.cross_compile_hosts:
205+
# Use XCBuild target directory when building for multiple arches.
206+
args.target_dir = os.path.join(args.build_dir, "apple/Products")
207+
elif re.match('android-', args.cross_compile_hosts):
208+
args.target_dir = os.path.join(
209+
args.build_dir,
210+
get_build_target(args,cross_compile=True))
202211
else:
203212
args.target_dir = os.path.join(args.build_dir, get_build_target(args))
204213
args.bootstrap_dir = os.path.join(args.target_dir, "bootstrap")
@@ -272,10 +281,15 @@ def get_ninja_path(args):
272281
else:
273282
return call_output(["which", "ninja"], verbose=args.verbose)
274283

275-
def get_build_target(args):
276-
"""Returns the target-triple of the current machine."""
284+
def get_build_target(args, cross_compile=False):
285+
"""Returns the target-triple of the current machine or for cross-compilation."""
277286
try:
278-
target_info_json = subprocess.check_output([args.swiftc_path, '-print-target-info'], stderr=subprocess.PIPE, universal_newlines=True).strip()
287+
command = [args.swiftc_path, '-print-target-info']
288+
if cross_compile:
289+
cross_compile_json = json.load(open(args.cross_compile_config))
290+
command += ['-target', cross_compile_json["target"]]
291+
target_info_json = subprocess.check_output(command,
292+
stderr=subprocess.PIPE, universal_newlines=True).strip()
279293
args.target_info = json.loads(target_info_json)
280294
return args.target_info["target"]["unversionedTriple"]
281295
except Exception as e:
@@ -310,8 +324,8 @@ def build(args):
310324
build_swift_argument_parser(args)
311325
build_swift_driver(args)
312326
build_swift_crypto(args)
327+
build_swiftpm_with_cmake(args)
313328

314-
build_swiftpm_with_cmake(args)
315329
build_swiftpm_with_swiftpm(args,integrated_swift_driver=False)
316330

317331
def test(args):
@@ -466,7 +480,7 @@ def install_binary(args, binary, dest_dir):
466480
# Build functions
467481
# -----------------------------------------------------------
468482

469-
def build_with_cmake(args, cmake_args, source_path, build_dir, targets=[]):
483+
def build_with_cmake(args, cmake_args, source_path, build_dir):
470484
"""Runs CMake if needed, then builds with Ninja."""
471485
cache_path = os.path.join(build_dir, "CMakeCache.txt")
472486
if args.reconfigure or not os.path.isfile(cache_path) or not args.swiftc_path in open(cache_path).read():
@@ -497,8 +511,6 @@ def build_with_cmake(args, cmake_args, source_path, build_dir, targets=[]):
497511
if args.verbose:
498512
ninja_cmd.append("-v")
499513

500-
ninja_cmd += targets
501-
502514
call(ninja_cmd, cwd=build_dir, verbose=args.verbose)
503515

504516
def build_llbuild(args):
@@ -604,26 +616,20 @@ def build_swiftpm_with_cmake(args):
604616
"""Builds SwiftPM using CMake."""
605617
note("Building SwiftPM (with CMake)")
606618

607-
if args.bootstrap:
608-
cmake_flags = [
609-
get_llbuild_cmake_arg(args),
610-
"-DTSC_DIR=" + os.path.join(args.tsc_build_dir, "cmake/modules"),
611-
"-DYams_DIR=" + os.path.join(args.yams_build_dir, "cmake/modules"),
612-
"-DArgumentParser_DIR=" + os.path.join(args.swift_argument_parser_build_dir, "cmake/modules"),
613-
"-DSwiftDriver_DIR=" + os.path.join(args.swift_driver_build_dir, "cmake/modules"),
614-
"-DSwiftCrypto_DIR=" + os.path.join(args.swift_crypto_build_dir, "cmake/modules"),
615-
"-DFIND_PM_DEPS:BOOL=YES",
616-
]
617-
else:
618-
cmake_flags = [ "-DFIND_PM_DEPS:BOOL=NO" ]
619+
cmake_flags = [
620+
get_llbuild_cmake_arg(args),
621+
"-DTSC_DIR=" + os.path.join(args.tsc_build_dir, "cmake/modules"),
622+
"-DYams_DIR=" + os.path.join(args.yams_build_dir, "cmake/modules"),
623+
"-DArgumentParser_DIR=" + os.path.join(args.swift_argument_parser_build_dir, "cmake/modules"),
624+
"-DSwiftDriver_DIR=" + os.path.join(args.swift_driver_build_dir, "cmake/modules"),
625+
"-DSwiftCrypto_DIR=" + os.path.join(args.swift_crypto_build_dir, "cmake/modules"),
626+
]
619627

620628
if platform.system() == 'Darwin':
621629
cmake_flags.append("-DCMAKE_C_FLAGS=-target %s%s" % (get_build_target(args), g_macos_deployment_target))
622630
cmake_flags.append("-DCMAKE_OSX_DEPLOYMENT_TARGET=%s" % g_macos_deployment_target)
623631

624-
targets = [] if args.bootstrap else ["PD4", "PD4_2"]
625-
626-
build_with_cmake(args, cmake_flags, args.project_root, args.bootstrap_dir, targets)
632+
build_with_cmake(args, cmake_flags, args.project_root, args.bootstrap_dir)
627633

628634
if args.llbuild_link_framework:
629635
add_rpath_for_cmake_build(args, args.llbuild_build_dir)
@@ -809,7 +815,8 @@ def get_swiftpm_flags(args):
809815
)
810816

811817
# Don't use GNU strerror_r on Android.
812-
if 'ANDROID_DATA' in os.environ:
818+
if 'ANDROID_DATA' in os.environ or (args.cross_compile_hosts and re.match(
819+
'android-', args.cross_compile_hosts)):
813820
build_flags.extend(["-Xswiftc", "-Xcc", "-Xswiftc", "-U_GNU_SOURCE"])
814821

815822
# On ELF platforms, remove the host toolchain's stdlib absolute rpath from
@@ -821,6 +828,8 @@ def get_swiftpm_flags(args):
821828
cross_compile_hosts = args.cross_compile_hosts
822829
if build_target == 'x86_64-apple-macosx' and "macosx-arm64" in cross_compile_hosts:
823830
build_flags += ["--arch", "x86_64", "--arch", "arm64"]
831+
elif cross_compile_hosts and re.match('android-', cross_compile_hosts):
832+
build_flags.extend(["--destination", args.cross_compile_config])
824833
elif cross_compile_hosts:
825834
error("cannot cross-compile for %s" % cross_compile_hosts)
826835

0 commit comments

Comments
 (0)