From a867698ad341ba107f1a56409ad76e4ebcba88c3 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Mon, 27 Jan 2025 19:42:06 -0500 Subject: [PATCH 1/5] Return cross_compile_deps_path from get_linux_sysroot if set - This allows specifying an external sysroot to use to compile Swift against --- .../swift_build_support/products/product.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/utils/swift_build_support/swift_build_support/products/product.py b/utils/swift_build_support/swift_build_support/products/product.py index 8e675f228a29f..ac409a059c978 100644 --- a/utils/swift_build_support/swift_build_support/products/product.py +++ b/utils/swift_build_support/swift_build_support/products/product.py @@ -379,6 +379,11 @@ def get_linux_target_components(self, arch): def get_linux_sysroot(self, platform, arch): if not self.is_cross_compile_target('{}-{}'.format(platform, arch)): return None + + # If the deps path was passed, use it instead + if self.args.cross_compile_deps_path: + return self.args.cross_compile_deps_path + sysroot_arch, _, abi = self.get_linux_target_components(arch) # $ARCH-$PLATFORM-$ABI # E.x.: aarch64-linux-gnu From 5604688f0e738b21fc02c9aa2c7a2c49ad6ec296 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Tue, 28 Jan 2025 14:48:49 -0500 Subject: [PATCH 2/5] Use lld when cross-compiling a product to a Linux arch - gold does not always know where to find libraries in different sysroots depending on the version of GCC and so forth --- .../swift_build_support/products/product.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/utils/swift_build_support/swift_build_support/products/product.py b/utils/swift_build_support/swift_build_support/products/product.py index ac409a059c978..a6ce4d925cf2e 100644 --- a/utils/swift_build_support/swift_build_support/products/product.py +++ b/utils/swift_build_support/swift_build_support/products/product.py @@ -484,6 +484,10 @@ def common_cross_c_flags(self, platform, arch, include_arch=False): if self.is_release(): cross_flags.append('-fno-stack-protector') + # Use lld when cross-compiling to a Linux arch + if self.is_cross_compile_target("{}-{}".format(platform, arch)) and platform == 'linux': + cross_flags.append('-w -fuse-ld=lld') + return self.common_c_flags + cross_flags From 2bdc8f447dd82e91b076eaa4cdc63d697f44ce34 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Tue, 28 Jan 2025 14:55:49 -0500 Subject: [PATCH 3/5] Add some tests for llvm when cross-compiling --- .../products/test_llvm_linux_cross_compile.py | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/utils/swift_build_support/tests/products/test_llvm_linux_cross_compile.py b/utils/swift_build_support/tests/products/test_llvm_linux_cross_compile.py index 1691c3eae78fd..df64a6ebbfd7b 100644 --- a/utils/swift_build_support/tests/products/test_llvm_linux_cross_compile.py +++ b/utils/swift_build_support/tests/products/test_llvm_linux_cross_compile.py @@ -40,7 +40,8 @@ def setUp(self): clang_compiler_version=None, clang_user_visible_version=None, cross_compile_hosts='linux-aarch64', - cross_compile_deps_path='sysroot' + cross_compile_deps_path=None, + build_variant='Release' ) # Setup shell @@ -59,7 +60,7 @@ def tearDown(self): self.toolchain = None self.args = None - def test_llvm_get_linux_sysroot(self): + def test_llvm_get_linux_sysroot_default(self): llvm = LLVM( args=self.args, toolchain=self.toolchain, @@ -67,3 +68,30 @@ def test_llvm_get_linux_sysroot(self): build_dir='/path/to/build') expected_arg = '/usr/aarch64-linux-gnu' self.assertIn(expected_arg, llvm.get_linux_sysroot("linux", "aarch64")) + + def test_llvm_get_linux_sysroot_external(self): + self.args = argparse.Namespace( + llvm_targets_to_build='X86;ARM;AArch64', + llvm_assertions='true', + compiler_vendor='none', + clang_compiler_version=None, + clang_user_visible_version=None, + cross_compile_hosts='linux-armv7', + cross_compile_deps_path='sysroot' + ) + llvm = LLVM( + args=self.args, + toolchain=self.toolchain, + source_dir='/path/to/src', + build_dir='/path/to/build') + expected_arg = 'sysroot' + self.assertIn(expected_arg, llvm.get_linux_sysroot("linux", "armv7")) + + def test_llvm_c_flags_includes_lld(self): + llvm = LLVM( + args=self.args, + toolchain=self.toolchain, + source_dir='/path/to/src', + build_dir='/path/to/build') + expected_flag = '-w -fuse-ld=lld' + self.assertIn(expected_flag, llvm.llvm_c_flags("linux", "aarch64")) From bbe8716d02aaa9750cb530b6de7923c82a531f6b Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Tue, 28 Jan 2025 15:25:29 -0500 Subject: [PATCH 4/5] Open up cross-compilation for all linux- hosts --- utils/build-script-impl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/build-script-impl b/utils/build-script-impl index 097cb6b981ced..50d93594c899b 100755 --- a/utils/build-script-impl +++ b/utils/build-script-impl @@ -1117,7 +1117,7 @@ function false_true() { CROSS_COMPILE_HOSTS=($CROSS_COMPILE_HOSTS) for t in "${CROSS_COMPILE_HOSTS[@]}"; do case ${t} in - macosx* | iphone* | appletv* | watch* | linux-armv5 | linux-armv6 | linux-armv7 | android-* | openbsd-* | linux-static-* ) + macosx* | iphone* | appletv* | watch* | android-* | openbsd-* | linux-* | linux-static-* ) ;; *) echo "Unknown host to cross-compile for: ${t}" From aa9005c34bbfbfcdd87db0d41fec3521fc6f9dd9 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Tue, 28 Jan 2025 15:41:29 -0500 Subject: [PATCH 5/5] Fix linting error in common_cross_c_flags --- .../swift_build_support/products/product.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/utils/swift_build_support/swift_build_support/products/product.py b/utils/swift_build_support/swift_build_support/products/product.py index a6ce4d925cf2e..2cee987ba5366 100644 --- a/utils/swift_build_support/swift_build_support/products/product.py +++ b/utils/swift_build_support/swift_build_support/products/product.py @@ -485,7 +485,8 @@ def common_cross_c_flags(self, platform, arch, include_arch=False): cross_flags.append('-fno-stack-protector') # Use lld when cross-compiling to a Linux arch - if self.is_cross_compile_target("{}-{}".format(platform, arch)) and platform == 'linux': + if (self.is_cross_compile_target("{}-{}".format(platform, arch)) + and platform == 'linux'): cross_flags.append('-w -fuse-ld=lld') return self.common_c_flags + cross_flags