From 7af4661aa419ba00f175268f101789ccd3287692 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Wed, 29 Jan 2025 14:08:28 -0500 Subject: [PATCH 01/19] Set system processor in Libdispatch.cmake for LINUX sdks - This enables libdispatch to be built for the arch passed and not neccessarily the host arch. --- cmake/modules/Libdispatch.cmake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmake/modules/Libdispatch.cmake b/cmake/modules/Libdispatch.cmake index c8977e42d479f..266210b6dcd74 100644 --- a/cmake/modules/Libdispatch.cmake +++ b/cmake/modules/Libdispatch.cmake @@ -73,8 +73,8 @@ foreach(sdk ${DISPATCH_SDKS}) foreach(arch ${ARCHS}) set(LIBDISPATCH_VARIANT_NAME "libdispatch-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}") - if(sdk MATCHES WINDOWS) - set(SWIFT_LIBDISPATCH_COMPILER_TRIPLE_CMAKE_ARGS -DCMAKE_C_COMPILER_TARGET=${SWIFT_SDK_WINDOWS_ARCH_${arch}_TRIPLE};-DCMAKE_CXX_COMPILER_TARGET=${SWIFT_SDK_WINDOWS_ARCH_${arch}_TRIPLE}) + if(sdk MATCHES WINDOWS OR sdk MATCHES LINUX) + set(SWIFT_LIBDISPATCH_COMPILER_TRIPLE_CMAKE_ARGS -DCMAKE_C_COMPILER_TARGET=${SWIFT_SDK_${sdk}_ARCH_${arch}_TRIPLE};-DCMAKE_CXX_COMPILER_TARGET=${SWIFT_SDK_${sdk}_ARCH_${arch}_TRIPLE}) endif() if("${sdk}" STREQUAL "ANDROID") From f7f3004634390e16cf4e599da58e22ea8e4881b2 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Thu, 30 Jan 2025 12:31:29 -0500 Subject: [PATCH 02/19] Add ability to compile Swift stdlib with external sysroot - The name CROSS_COMPILE_SYSROOT is chosen since any time we use a sysroot not at "/" we are cross-compiling for some platform or arch. --- cmake/modules/Libdispatch.cmake | 8 +++++++- cmake/modules/SwiftConfigureSDK.cmake | 6 ++++++ utils/build-script-impl | 9 +++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/cmake/modules/Libdispatch.cmake b/cmake/modules/Libdispatch.cmake index 266210b6dcd74..cd4a6e70e5a43 100644 --- a/cmake/modules/Libdispatch.cmake +++ b/cmake/modules/Libdispatch.cmake @@ -73,8 +73,14 @@ foreach(sdk ${DISPATCH_SDKS}) foreach(arch ${ARCHS}) set(LIBDISPATCH_VARIANT_NAME "libdispatch-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}") - if(sdk MATCHES WINDOWS OR sdk MATCHES LINUX) + if(sdk MATCHES WINDOWS) + set(SWIFT_LIBDISPATCH_COMPILER_TRIPLE_CMAKE_ARGS -DCMAKE_C_COMPILER_TARGET=${SWIFT_SDK_WINDOWS_ARCH_${arch}_TRIPLE};-DCMAKE_CXX_COMPILER_TARGET=${SWIFT_SDK_WINDOWS_ARCH_${arch}_TRIPLE}) + elseif(sdk MATCHES LINUX) + set(SWIFT_LIBDISPATCH_COMPILER_CMAKE_ARGS ${SWIFT_LIBDISPATCH_COMPILER_CMAKE_ARGS};-DCMAKE_SYSROOT=${SWIFT_SDK_${sdk}_ARCH_${arch}_PATH}) set(SWIFT_LIBDISPATCH_COMPILER_TRIPLE_CMAKE_ARGS -DCMAKE_C_COMPILER_TARGET=${SWIFT_SDK_${sdk}_ARCH_${arch}_TRIPLE};-DCMAKE_CXX_COMPILER_TARGET=${SWIFT_SDK_${sdk}_ARCH_${arch}_TRIPLE}) + # Use linker used to build Swift + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -w -fuse-ld=${SWIFT_USE_LINKER}") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w -fuse-ld=${SWIFT_USE_LINKER}") endif() if("${sdk}" STREQUAL "ANDROID") diff --git a/cmake/modules/SwiftConfigureSDK.cmake b/cmake/modules/SwiftConfigureSDK.cmake index 45b1900a63e9a..71b34bce59113 100644 --- a/cmake/modules/SwiftConfigureSDK.cmake +++ b/cmake/modules/SwiftConfigureSDK.cmake @@ -419,6 +419,12 @@ macro(configure_sdk_unix name architectures) else() message(FATAL_ERROR "unknown arch for ${prefix}: ${arch}") endif() + + # If we are using an external sysroot, update path and CXX compile flags to point to it + if(CROSS_COMPILE_SYSROOT) + set(SWIFT_SDK_${prefix}_ARCH_${arch}_PATH ${CROSS_COMPILE_SYSROOT}) + set(SWIFT_SDK_${prefix}_CXX_OVERLAY_SWIFT_COMPILE_FLAGS -Xcc --gcc-toolchain=${CROSS_COMPILE_SYSROOT}/usr) + endif() elseif("${prefix}" STREQUAL "FREEBSD") if(NOT arch MATCHES "(arm64|x86_64)") message(FATAL_ERROR "unsupported arch for FreeBSD: ${arch}") diff --git a/utils/build-script-impl b/utils/build-script-impl index 097cb6b981ced..65fcfffd194a5 100755 --- a/utils/build-script-impl +++ b/utils/build-script-impl @@ -1751,6 +1751,15 @@ for host in "${ALL_HOSTS[@]}"; do ) fi + # TODO: Add a new option here like --cross-compile-sysroot/CROSS_COMPILE_SYSROOT + if [ ! -z "${CROSS_COMPILE_DEPS_PATH}" ]; then + cmake_options=( + "${cmake_options[@]}" + -DCROSS_COMPILE_SYSROOT:STRING="${CROSS_COMPILE_DEPS_PATH}" + -DSWIFT_USE_LINKER:STRING="lld" + ) + fi + if [[ "${DARWIN_OVERLAY_TARGET}" != "" ]]; then # Split LOCAL_HOST into a pair ``arch-sdk`` # Example LOCAL_HOST: macosx-x86_64 From f6f70e12a3d407c391a4ee23c78e6b27842cb478 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Thu, 30 Jan 2025 17:57:48 -0500 Subject: [PATCH 03/19] Add cross-compile-sysroot option to build scripts --- utils/build-script-impl | 7 ++++--- utils/build_swift/build_swift/driver_arguments.py | 5 +++++ utils/build_swift/tests/expected_options.py | 2 ++ .../swift_build_support/build_script_invocation.py | 4 ++++ 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/utils/build-script-impl b/utils/build-script-impl index 65fcfffd194a5..94be5ee159fb3 100755 --- a/utils/build-script-impl +++ b/utils/build-script-impl @@ -288,6 +288,7 @@ KNOWN_SETTINGS=( cross-compile-with-host-tools "" "set to use the clang we build for the host to then build the cross-compile hosts" cross-compile-install-prefixes "" "semicolon-separated list of install prefixes to use for the cross-compiled hosts. The list expands, so if there are more cross-compile hosts than prefixes, unmatched hosts use the last prefix in the list" cross-compile-deps-path "" "path for CMake to look for cross-compiled library dependencies, such as libXML2" + cross-compile-sysroot "" "path for CMake to look for for a cross-compilation sysroot for a different platform and/or architecture" cross-compile-append-host-target-to-destdir "1" "turns on appending the host target name of each cross-compiled toolchain to its install-destdir, to keep them separate from the natively-built toolchain" skip-merge-lipo-cross-compile-tools "" "set to skip running merge-lipo after installing cross-compiled host Swift tools" coverage-db "" "If set, coverage database to use when prioritizing testing" @@ -1751,11 +1752,11 @@ for host in "${ALL_HOSTS[@]}"; do ) fi - # TODO: Add a new option here like --cross-compile-sysroot/CROSS_COMPILE_SYSROOT - if [ ! -z "${CROSS_COMPILE_DEPS_PATH}" ]; then + # This allows passing a sysroot other than "/" to compile Swift and libraries fr + if [ ! -z "${CROSS_COMPILE_SYSROOT}" ]; then cmake_options=( "${cmake_options[@]}" - -DCROSS_COMPILE_SYSROOT:STRING="${CROSS_COMPILE_DEPS_PATH}" + -DCROSS_COMPILE_SYSROOT:STRING="${CROSS_COMPILE_SYSROOT}" -DSWIFT_USE_LINKER:STRING="lld" ) fi diff --git a/utils/build_swift/build_swift/driver_arguments.py b/utils/build_swift/build_swift/driver_arguments.py index db35c857dafac..8615241dcdb5f 100644 --- a/utils/build_swift/build_swift/driver_arguments.py +++ b/utils/build_swift/build_swift/driver_arguments.py @@ -669,6 +669,11 @@ def create_argument_parser(): 'library dependencies of the corelibs and other Swift repos, ' 'such as the libcurl dependency of FoundationNetworking') + option('--cross-compile-sysroot', store_path, + help='The path to a directory that contains a sysroot for a different ' + 'platform and/or architecture for cross-compiling Swift to. The sysroot ' + 'must contain libraries that match the architecture that is being built for.') + option('--cross-compile-append-host-target-to-destdir', toggle_true, default=True, help="Append each cross-compilation host target's name as a subdirectory " diff --git a/utils/build_swift/tests/expected_options.py b/utils/build_swift/tests/expected_options.py index 120765d2ee301..df0f3b50fcd99 100644 --- a/utils/build_swift/tests/expected_options.py +++ b/utils/build_swift/tests/expected_options.py @@ -156,6 +156,7 @@ 'coverage_db': None, 'cross_compile_append_host_target_to_destdir': True, 'cross_compile_deps_path': None, + 'cross_compile_sysroot': None, 'cross_compile_hosts': [], 'infer_cross_compile_hosts_on_darwin': False, 'darwin_deployment_version_ios': @@ -813,6 +814,7 @@ class BuildScriptImplOption(_BaseOption): PathOption('--cmake'), PathOption('--coverage-db'), PathOption('--cross-compile-deps-path'), + PathOption('--cross-compile-sysroot'), PathOption('--host-cc'), PathOption('--host-cxx'), PathOption('--host-libtool'), diff --git a/utils/swift_build_support/swift_build_support/build_script_invocation.py b/utils/swift_build_support/swift_build_support/build_script_invocation.py index 3cb5f21552b06..76f7097c7ff0a 100644 --- a/utils/swift_build_support/swift_build_support/build_script_invocation.py +++ b/utils/swift_build_support/swift_build_support/build_script_invocation.py @@ -199,6 +199,10 @@ def convert_to_impl_arguments(self): impl_args += [ "--cross-compile-deps-path=%s" % args.cross_compile_deps_path ] + if args.cross_compile_sysroot is not None: + impl_args += [ + "--cross-compile-sysroot=%s" % args.cross_compile_sysroot + ] if args.test_paths: impl_args += ["--test-paths", " ".join(args.test_paths)] From 15826c40748befabeabfde07e8a950a629287b38 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Fri, 31 Jan 2025 17:04:30 -0500 Subject: [PATCH 04/19] Only include CROSS_COMPILE_SYSROOT if cross-compiling --- utils/build-script-impl | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/utils/build-script-impl b/utils/build-script-impl index 94be5ee159fb3..4a62069da073b 100755 --- a/utils/build-script-impl +++ b/utils/build-script-impl @@ -1752,14 +1752,6 @@ for host in "${ALL_HOSTS[@]}"; do ) fi - # This allows passing a sysroot other than "/" to compile Swift and libraries fr - if [ ! -z "${CROSS_COMPILE_SYSROOT}" ]; then - cmake_options=( - "${cmake_options[@]}" - -DCROSS_COMPILE_SYSROOT:STRING="${CROSS_COMPILE_SYSROOT}" - -DSWIFT_USE_LINKER:STRING="lld" - ) - fi if [[ "${DARWIN_OVERLAY_TARGET}" != "" ]]; then # Split LOCAL_HOST into a pair ``arch-sdk`` @@ -1797,6 +1789,16 @@ for host in "${ALL_HOSTS[@]}"; do -DLLVM_TABLEGEN=$(build_directory "${LOCAL_HOST}" llvm)/bin/llvm-tblgen -DSWIFT_INCLUDE_TEST_BINARIES:BOOL=FALSE ) + + # This allows passing a sysroot other than "/" to compile Swift and libraries + # It also assumes the sysroot contains arch libraries matching --stdlib-deployment-targets + if [ ! -z "${CROSS_COMPILE_SYSROOT}" ]; then + cmake_options=( + "${cmake_options[@]}" + -DCROSS_COMPILE_SYSROOT:STRING="${CROSS_COMPILE_SYSROOT}" + -DSWIFT_USE_LINKER:STRING="lld" + ) + fi fi # Command-line parameters override any autodetection that we From 47403ae4859bd9bedf285c58b795b91aef52708b Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Fri, 31 Jan 2025 17:49:15 -0500 Subject: [PATCH 05/19] Use lld in product.py if cross_compile_sysroot is used --- .../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 e62eb23c8bb5e..1b25bc196861c 100644 --- a/utils/swift_build_support/swift_build_support/products/product.py +++ b/utils/swift_build_support/swift_build_support/products/product.py @@ -479,6 +479,10 @@ def common_cross_c_flags(self, platform, arch, include_arch=False): if self.is_release(): cross_flags.append('-fno-stack-protector') + # Use lld if external sysroot is provided + if self.is_cross_compile_target('{}-{}'.format(platform, arch)) and self.args.cross_compile_sysroot: + cross_flags.append('-w -fuse-ld=lld') + return self.common_c_flags + cross_flags From 726cb1807e5dde5a900a4eea1a3c14e162f48fe9 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Fri, 31 Jan 2025 17:54:23 -0500 Subject: [PATCH 06/19] Use cross_compile_sysroot in get_linux_sysroot if set - This forces it to use the external sysroot instead of the default --- .../swift_build_support/swift_build_support/products/product.py | 2 ++ 1 file changed, 2 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 1b25bc196861c..6bda9dc6812b9 100644 --- a/utils/swift_build_support/swift_build_support/products/product.py +++ b/utils/swift_build_support/swift_build_support/products/product.py @@ -377,6 +377,8 @@ def get_linux_target_components(self, arch): return sysroot_arch, vendor, abi def get_linux_sysroot(self, platform, arch): + if self.args.cross_compile_sysroot: + return self.args.cross_compile_sysroot if not self.is_cross_compile_target('{}-{}'.format(platform, arch)): return None sysroot_arch, abi = self.get_linux_target_components(arch) From a31141d36df27a067fb6ab8954622e1ca3ec0ba2 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Fri, 31 Jan 2025 17:58:58 -0500 Subject: [PATCH 07/19] Fix linting issues --- utils/build_swift/build_swift/driver_arguments.py | 5 +++-- .../swift_build_support/products/product.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/utils/build_swift/build_swift/driver_arguments.py b/utils/build_swift/build_swift/driver_arguments.py index 8615241dcdb5f..052c66bda13bc 100644 --- a/utils/build_swift/build_swift/driver_arguments.py +++ b/utils/build_swift/build_swift/driver_arguments.py @@ -671,8 +671,9 @@ def create_argument_parser(): option('--cross-compile-sysroot', store_path, help='The path to a directory that contains a sysroot for a different ' - 'platform and/or architecture for cross-compiling Swift to. The sysroot ' - 'must contain libraries that match the architecture that is being built for.') + 'platform and/or architecture for cross-compiling Swift to. ' + 'The sysroot must contain libraries that match the architecture ' + 'that is being built for.') option('--cross-compile-append-host-target-to-destdir', toggle_true, default=True, 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 6bda9dc6812b9..d5f43976893d3 100644 --- a/utils/swift_build_support/swift_build_support/products/product.py +++ b/utils/swift_build_support/swift_build_support/products/product.py @@ -482,7 +482,7 @@ def common_cross_c_flags(self, platform, arch, include_arch=False): cross_flags.append('-fno-stack-protector') # Use lld if external sysroot is provided - if self.is_cross_compile_target('{}-{}'.format(platform, arch)) and self.args.cross_compile_sysroot: + if self.args.cross_compile_sysroot: cross_flags.append('-w -fuse-ld=lld') return self.common_c_flags + cross_flags From 07f3af120fb25fe5157fce9231fe7c67fd51d5bb Mon Sep 17 00:00:00 2001 From: Max Desiatov Date: Mon, 27 Jan 2025 20:20:01 +0000 Subject: [PATCH 08/19] Merge pull request #78909 from xtremekforever/#75341-fix-swift-build-support-typo Fix typo in get_linux_sysroot that makes cross-compilation fail --- .../swift_build_support/products/product.py | 2 +- .../products/test_llvm_linux_cross_compile.py | 69 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 utils/swift_build_support/tests/products/test_llvm_linux_cross_compile.py 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 d5f43976893d3..ecd3ced1b29ea 100644 --- a/utils/swift_build_support/swift_build_support/products/product.py +++ b/utils/swift_build_support/swift_build_support/products/product.py @@ -381,7 +381,7 @@ def get_linux_sysroot(self, platform, arch): return self.args.cross_compile_sysroot if not self.is_cross_compile_target('{}-{}'.format(platform, arch)): return None - sysroot_arch, abi = self.get_linux_target_components(arch) + sysroot_arch, _, abi = self.get_linux_target_components(arch) # $ARCH-$PLATFORM-$ABI # E.x.: aarch64-linux-gnu sysroot_dirname = '{}-{}-{}'.format(sysroot_arch, platform, abi) 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 new file mode 100644 index 0000000000000..1691c3eae78fd --- /dev/null +++ b/utils/swift_build_support/tests/products/test_llvm_linux_cross_compile.py @@ -0,0 +1,69 @@ +# tests/products/test_llvm_linux_cross_compile.py ---------------*- python -*- +# +# This source file is part of the LLVM.org open source project +# +# Copyright (c) 2014 - 2025 Apple Inc. and the LLVM project authors +# Licensed under Apache License v2.0 with Runtime Library Exception +# +# See https://swift.org/LICENSE.txt for license information +# See https://swift.org/CONTRIBUTORS.txt for the list of LLVM project authors +# ---------------------------------------------------------------------------- + +import argparse +import os +import sys +import tempfile +import unittest +from io import StringIO + +from swift_build_support import shell +from swift_build_support.products import LLVM +from swift_build_support.toolchain import host_toolchain + + +class LLVMLinuxCrossCompileTestCase(unittest.TestCase): + def setUp(self): + # Setup workspace + tmpdir1 = os.path.realpath(tempfile.mkdtemp()) + os.makedirs(os.path.join(tmpdir1, 'llvm')) + + # Setup toolchain + self.toolchain = host_toolchain() + self.toolchain.cc = '/path/to/cc' + self.toolchain.cxx = '/path/to/cxx' + + # Setup args + 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-aarch64', + cross_compile_deps_path='sysroot' + ) + + # Setup shell + shell.dry_run = True + self._orig_stdout = sys.stdout + self._orig_stderr = sys.stderr + self.stdout = StringIO() + self.stderr = StringIO() + sys.stdout = self.stdout + sys.stderr = self.stderr + + def tearDown(self): + sys.stdout = self._orig_stdout + sys.stderr = self._orig_stderr + shell.dry_run = False + self.toolchain = None + self.args = None + + def test_llvm_get_linux_sysroot(self): + llvm = LLVM( + args=self.args, + toolchain=self.toolchain, + source_dir='/path/to/src', + build_dir='/path/to/build') + expected_arg = '/usr/aarch64-linux-gnu' + self.assertIn(expected_arg, llvm.get_linux_sysroot("linux", "aarch64")) From d7ee7c897172c2bb28b31f93c7613cda8ca05b52 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Sat, 1 Feb 2025 14:07:32 -0500 Subject: [PATCH 09/19] Find gcc-install-dir in CROSS_COMPILE_SYSROOT for better locating of libstdc++ --- cmake/modules/SwiftConfigureSDK.cmake | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/cmake/modules/SwiftConfigureSDK.cmake b/cmake/modules/SwiftConfigureSDK.cmake index 71b34bce59113..0bf78129eac8e 100644 --- a/cmake/modules/SwiftConfigureSDK.cmake +++ b/cmake/modules/SwiftConfigureSDK.cmake @@ -411,19 +411,32 @@ macro(configure_sdk_unix name architectures) if("${prefix}" STREQUAL "LINUX") if(arch MATCHES "(armv5)") - set(SWIFT_SDK_LINUX_ARCH_${arch}_TRIPLE "${arch}-unknown-linux-gnueabi") + set(SWIFT_SDK_LINUX_ARCH_${arch}_SUFFIX linux-gnueabi) elseif(arch MATCHES "(armv6|armv7)") - set(SWIFT_SDK_LINUX_ARCH_${arch}_TRIPLE "${arch}-unknown-linux-gnueabihf") + set(SWIFT_SDK_LINUX_ARCH_${arch}_SUFFIX linux-gnueabihf) elseif(arch MATCHES "(aarch64|i686|powerpc|powerpc64|powerpc64le|s390x|x86_64|riscv64)") - set(SWIFT_SDK_LINUX_ARCH_${arch}_TRIPLE "${arch}-unknown-linux-gnu") + set(SWIFT_SDK_LINUX_ARCH_${arch}_SUFFIX linux-gnu) else() message(FATAL_ERROR "unknown arch for ${prefix}: ${arch}") endif() + set(SWIFT_SDK_LINUX_ARCH_${arch}_TRIPLE "${arch}-unknown-${SWIFT_SDK_LINUX_ARCH_${arch}_SUFFIX}") + # If we are using an external sysroot, update path and CXX compile flags to point to it if(CROSS_COMPILE_SYSROOT) set(SWIFT_SDK_${prefix}_ARCH_${arch}_PATH ${CROSS_COMPILE_SYSROOT}) - set(SWIFT_SDK_${prefix}_CXX_OVERLAY_SWIFT_COMPILE_FLAGS -Xcc --gcc-toolchain=${CROSS_COMPILE_SYSROOT}/usr) + + # Find toolchain install dir + execute_process( + COMMAND dirname $(find . -name crtbegin.o | grep ${SWIFT_SDK_LINUX_ARCH_${arch}_SUFFIX}) + WORKING_DIRECTORY ${CROSS_COMPILE_SYSROOT} + OUTPUT_VARIABLE GCC_INSTALL_DIR + RESULT_VARIABLE FOUND_GCC_INSTALL_DIR) + if(FOUND_GCC_INSTALL_DIR) + set(SWIFT_SDK_${prefix}_CXX_OVERLAY_SWIFT_COMPILE_FLAGS -Xcc --gcc-install-dir=${GCC_INSTALL_DIR}) + else() + set(SWIFT_SDK_${prefix}_CXX_OVERLAY_SWIFT_COMPILE_FLAGS -Xcc --gcc-toolchain=${CROSS_COMPILE_SYSROOT}) + endif() endif() elseif("${prefix}" STREQUAL "FREEBSD") if(NOT arch MATCHES "(arm64|x86_64)") From b7d4eef184b82ef067ba1b40fe664d9d34f5ea33 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Sat, 1 Feb 2025 15:00:16 -0500 Subject: [PATCH 10/19] Open up cross-compilation to 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 4a62069da073b..10351f218c0d7 100755 --- a/utils/build-script-impl +++ b/utils/build-script-impl @@ -1118,7 +1118,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* | linux-* | android-* | openbsd-* | linux-static-* ) ;; *) echo "Unknown host to cross-compile for: ${t}" From c04520453ebbea698008f972790aa51738d20b15 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Sun, 2 Feb 2025 07:32:49 -0500 Subject: [PATCH 11/19] Fix tests for llvm_linux_cross_compile support, add more tests --- .../swift_build_support/products/product.py | 7 +- .../products/test_llvm_linux_cross_compile.py | 64 +++++++++++++++---- 2 files changed, 55 insertions(+), 16 deletions(-) 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 ecd3ced1b29ea..00f569dd3f6db 100644 --- a/utils/swift_build_support/swift_build_support/products/product.py +++ b/utils/swift_build_support/swift_build_support/products/product.py @@ -377,10 +377,10 @@ def get_linux_target_components(self, arch): return sysroot_arch, vendor, abi def get_linux_sysroot(self, platform, arch): - if self.args.cross_compile_sysroot: - return self.args.cross_compile_sysroot if not self.is_cross_compile_target('{}-{}'.format(platform, arch)): return None + if self.args.cross_compile_sysroot: + return self.args.cross_compile_sysroot sysroot_arch, _, abi = self.get_linux_target_components(arch) # $ARCH-$PLATFORM-$ABI # E.x.: aarch64-linux-gnu @@ -482,7 +482,8 @@ def common_cross_c_flags(self, platform, arch, include_arch=False): cross_flags.append('-fno-stack-protector') # Use lld if external sysroot is provided - if self.args.cross_compile_sysroot: + if (self.is_cross_compile_target('{}-{}'.format(platform, arch)) + and self.args.cross_compile_sysroot): cross_flags.append('-w -fuse-ld=lld') return self.common_c_flags + cross_flags 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..b485399251fcd 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 @@ -32,17 +32,6 @@ def setUp(self): self.toolchain.cc = '/path/to/cc' self.toolchain.cxx = '/path/to/cxx' - # Setup args - 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-aarch64', - cross_compile_deps_path='sysroot' - ) - # Setup shell shell.dry_run = True self._orig_stdout = sys.stdout @@ -59,11 +48,60 @@ def tearDown(self): self.toolchain = None self.args = None - def test_llvm_get_linux_sysroot(self): + def test_llvm_get_linux_sysroot_default(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-aarch64', + cross_compile_sysroot=None + ) + llvm = LLVM( - args=self.args, + args=args, toolchain=self.toolchain, source_dir='/path/to/src', 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): + 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_sysroot='sysroot' + ) + + llvm = LLVM( + args=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_common_cross_c_flags_external_sysroot(self): + args = argparse.Namespace( + llvm_targets_to_build='X86;ARM;AArch64', + llvm_assertions='true', + compiler_vendor='none', + clang_compiler_version=None, + build_variant='Debug', + clang_user_visible_version=None, + cross_compile_hosts='linux-armv7', + cross_compile_sysroot='sysroot' + ) + + llvm = LLVM( + args=args, + toolchain=self.toolchain, + source_dir='/path/to/src', + build_dir='/path/to/build') + expected_arg = '-w -fuse-ld=lld' + self.assertIn(expected_arg, llvm.common_cross_c_flags("linux", "armv7")) From 47c0498fc7a0c720dd797a3562418610a0a60782 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Sun, 2 Feb 2025 12:00:25 -0500 Subject: [PATCH 12/19] Find --gcc-install-dir in usr directory of sysroot - This is where it will be installed, and we don't want to conflict with other gcc installations at /home or otherwise. --- cmake/modules/SwiftConfigureSDK.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/modules/SwiftConfigureSDK.cmake b/cmake/modules/SwiftConfigureSDK.cmake index c1f1f9c8c498b..dbee50ddc7058 100644 --- a/cmake/modules/SwiftConfigureSDK.cmake +++ b/cmake/modules/SwiftConfigureSDK.cmake @@ -424,7 +424,7 @@ macro(configure_sdk_unix name architectures) # Find toolchain install dir execute_process( - COMMAND dirname $(find . -name crtbegin.o | grep ${SWIFT_SDK_LINUX_ARCH_${arch}_SUFFIX}) + COMMAND dirname $(find ${CROSS_COMPILE_SYSROOT}/usr -name crtbegin.o | grep ${SWIFT_SDK_LINUX_ARCH_${arch}_SUFFIX}) WORKING_DIRECTORY ${CROSS_COMPILE_SYSROOT} OUTPUT_VARIABLE GCC_INSTALL_DIR RESULT_VARIABLE FOUND_GCC_INSTALL_DIR) From 91cad22f76767d4dfb4394d2379bb1574f4b3ac7 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Mon, 3 Feb 2025 23:00:31 -0500 Subject: [PATCH 13/19] Rename cross-compile-sysroot -> cross-compile-sysroots and update wording --- cmake/modules/SwiftConfigureSDK.cmake | 10 +++++----- utils/build-script-impl | 6 +++--- utils/build_swift/build_swift/driver_arguments.py | 10 +++++----- utils/build_swift/tests/expected_options.py | 4 ++-- .../swift_build_support/build_script_invocation.py | 4 ++-- .../swift_build_support/products/product.py | 6 +++--- .../tests/products/test_llvm_linux_cross_compile.py | 6 +++--- 7 files changed, 23 insertions(+), 23 deletions(-) diff --git a/cmake/modules/SwiftConfigureSDK.cmake b/cmake/modules/SwiftConfigureSDK.cmake index dbee50ddc7058..36fe6b12d1bb2 100644 --- a/cmake/modules/SwiftConfigureSDK.cmake +++ b/cmake/modules/SwiftConfigureSDK.cmake @@ -419,19 +419,19 @@ macro(configure_sdk_unix name architectures) set(SWIFT_SDK_LINUX_ARCH_${arch}_TRIPLE "${arch}-unknown-${SWIFT_SDK_LINUX_ARCH_${arch}_SUFFIX}") # If we are using an external sysroot, update path and CXX compile flags to point to it - if(CROSS_COMPILE_SYSROOT) - set(SWIFT_SDK_${prefix}_ARCH_${arch}_PATH ${CROSS_COMPILE_SYSROOT}) + if(CROSS_COMPILE_SYSROOTS) + set(SWIFT_SDK_${prefix}_ARCH_${arch}_PATH ${CROSS_COMPILE_SYSROOTS}) # Find toolchain install dir execute_process( - COMMAND dirname $(find ${CROSS_COMPILE_SYSROOT}/usr -name crtbegin.o | grep ${SWIFT_SDK_LINUX_ARCH_${arch}_SUFFIX}) - WORKING_DIRECTORY ${CROSS_COMPILE_SYSROOT} + COMMAND dirname $(find ${CROSS_COMPILE_SYSROOTS}/usr -name crtbegin.o | grep ${SWIFT_SDK_LINUX_ARCH_${arch}_SUFFIX}) + WORKING_DIRECTORY ${CROSS_COMPILE_SYSROOTS} OUTPUT_VARIABLE GCC_INSTALL_DIR RESULT_VARIABLE FOUND_GCC_INSTALL_DIR) if(FOUND_GCC_INSTALL_DIR) set(SWIFT_SDK_${prefix}_CXX_OVERLAY_SWIFT_COMPILE_FLAGS -Xcc --gcc-install-dir=${GCC_INSTALL_DIR}) else() - set(SWIFT_SDK_${prefix}_CXX_OVERLAY_SWIFT_COMPILE_FLAGS -Xcc --gcc-toolchain=${CROSS_COMPILE_SYSROOT}) + set(SWIFT_SDK_${prefix}_CXX_OVERLAY_SWIFT_COMPILE_FLAGS -Xcc --gcc-toolchain=${CROSS_COMPILE_SYSROOTS}) endif() endif() elseif("${prefix}" STREQUAL "FREEBSD") diff --git a/utils/build-script-impl b/utils/build-script-impl index d4e83771381fc..8b48a190a3566 100755 --- a/utils/build-script-impl +++ b/utils/build-script-impl @@ -288,7 +288,7 @@ KNOWN_SETTINGS=( cross-compile-with-host-tools "" "set to use the clang we build for the host to then build the cross-compile hosts" cross-compile-install-prefixes "" "semicolon-separated list of install prefixes to use for the cross-compiled hosts. The list expands, so if there are more cross-compile hosts than prefixes, unmatched hosts use the last prefix in the list" cross-compile-deps-path "" "path for CMake to look for cross-compiled library dependencies, such as libXML2" - cross-compile-sysroot "" "path for CMake to look for for a cross-compilation sysroot for a different platform and/or architecture" + cross-compile-sysroots "" "path for CMake to look for a cross-compilation sysroot for a different platform and/or architecture" cross-compile-append-host-target-to-destdir "1" "turns on appending the host target name of each cross-compiled toolchain to its install-destdir, to keep them separate from the natively-built toolchain" skip-merge-lipo-cross-compile-tools "" "set to skip running merge-lipo after installing cross-compiled host Swift tools" coverage-db "" "If set, coverage database to use when prioritizing testing" @@ -1784,10 +1784,10 @@ for host in "${ALL_HOSTS[@]}"; do # This allows passing a sysroot other than "/" to compile Swift and libraries # It also assumes the sysroot contains arch libraries matching --stdlib-deployment-targets - if [ ! -z "${CROSS_COMPILE_SYSROOT}" ]; then + if [ ! -z "${CROSS_COMPILE_SYSROOTS}" ]; then cmake_options=( "${cmake_options[@]}" - -DCROSS_COMPILE_SYSROOT:STRING="${CROSS_COMPILE_SYSROOT}" + -DCROSS_COMPILE_SYSROOTS:STRING="${CROSS_COMPILE_SYSROOTS}" -DSWIFT_USE_LINKER:STRING="lld" ) fi diff --git a/utils/build_swift/build_swift/driver_arguments.py b/utils/build_swift/build_swift/driver_arguments.py index ca3be0df4dd24..ff7482d67ae1b 100644 --- a/utils/build_swift/build_swift/driver_arguments.py +++ b/utils/build_swift/build_swift/driver_arguments.py @@ -669,11 +669,11 @@ def create_argument_parser(): 'library dependencies of the corelibs and other Swift repos, ' 'such as the libcurl dependency of FoundationNetworking') - option('--cross-compile-sysroot', store_path, - help='The path to a directory that contains a sysroot for a different ' - 'platform and/or architecture for cross-compiling Swift to. ' - 'The sysroot must contain libraries that match the architecture ' - 'that is being built for.') + option('--cross-compile-sysroots', store_path, + help='The path to a directory that contains a C/C++ sysroot for a ' + 'different platform and/or architecture for cross-compiling Swift. ' + 'The sysroot must contain C/C++ libraries that match the ' + 'architecture that is being built for.') option('--cross-compile-append-host-target-to-destdir', toggle_true, default=True, diff --git a/utils/build_swift/tests/expected_options.py b/utils/build_swift/tests/expected_options.py index 3b3f2973d99db..afc8822617ee5 100644 --- a/utils/build_swift/tests/expected_options.py +++ b/utils/build_swift/tests/expected_options.py @@ -157,7 +157,7 @@ 'coverage_db': None, 'cross_compile_append_host_target_to_destdir': True, 'cross_compile_deps_path': None, - 'cross_compile_sysroot': None, + 'cross_compile_sysroots': None, 'cross_compile_hosts': [], 'infer_cross_compile_hosts_on_darwin': False, 'darwin_deployment_version_ios': @@ -816,7 +816,7 @@ class BuildScriptImplOption(_BaseOption): PathOption('--cmake'), PathOption('--coverage-db'), PathOption('--cross-compile-deps-path'), - PathOption('--cross-compile-sysroot'), + PathOption('--cross-compile-sysroots'), PathOption('--host-cc'), PathOption('--host-cxx'), PathOption('--host-libtool'), diff --git a/utils/swift_build_support/swift_build_support/build_script_invocation.py b/utils/swift_build_support/swift_build_support/build_script_invocation.py index 76f7097c7ff0a..7d40daccf24ca 100644 --- a/utils/swift_build_support/swift_build_support/build_script_invocation.py +++ b/utils/swift_build_support/swift_build_support/build_script_invocation.py @@ -199,9 +199,9 @@ def convert_to_impl_arguments(self): impl_args += [ "--cross-compile-deps-path=%s" % args.cross_compile_deps_path ] - if args.cross_compile_sysroot is not None: + if args.cross_compile_sysroots is not None: impl_args += [ - "--cross-compile-sysroot=%s" % args.cross_compile_sysroot + "--cross-compile-sysroots=%s" % args.cross_compile_sysroots ] if args.test_paths: 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 00f569dd3f6db..48454c1fdad08 100644 --- a/utils/swift_build_support/swift_build_support/products/product.py +++ b/utils/swift_build_support/swift_build_support/products/product.py @@ -379,8 +379,8 @@ 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 self.args.cross_compile_sysroot: - return self.args.cross_compile_sysroot + if self.args.cross_compile_sysroots: + return self.args.cross_compile_sysroots sysroot_arch, _, abi = self.get_linux_target_components(arch) # $ARCH-$PLATFORM-$ABI # E.x.: aarch64-linux-gnu @@ -483,7 +483,7 @@ def common_cross_c_flags(self, platform, arch, include_arch=False): # Use lld if external sysroot is provided if (self.is_cross_compile_target('{}-{}'.format(platform, arch)) - and self.args.cross_compile_sysroot): + and self.args.cross_compile_sysroots): cross_flags.append('-w -fuse-ld=lld') return self.common_c_flags + cross_flags 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 b485399251fcd..8d45a891e71de 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 @@ -56,7 +56,7 @@ def test_llvm_get_linux_sysroot_default(self): clang_compiler_version=None, clang_user_visible_version=None, cross_compile_hosts='linux-aarch64', - cross_compile_sysroot=None + cross_compile_sysroots=None ) llvm = LLVM( @@ -75,7 +75,7 @@ def test_llvm_get_linux_sysroot_external(self): clang_compiler_version=None, clang_user_visible_version=None, cross_compile_hosts='linux-armv7', - cross_compile_sysroot='sysroot' + cross_compile_sysroots='sysroot' ) llvm = LLVM( @@ -95,7 +95,7 @@ def test_llvm_common_cross_c_flags_external_sysroot(self): build_variant='Debug', clang_user_visible_version=None, cross_compile_hosts='linux-armv7', - cross_compile_sysroot='sysroot' + cross_compile_sysroots='sysroot' ) llvm = LLVM( From 5bbda49bb5b4d848ba18727fa0e563beb42da401 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Mon, 3 Feb 2025 23:03:45 -0500 Subject: [PATCH 14/19] Remove redundant case for linux-static-*, remove extra whitespace --- utils/build-script-impl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/utils/build-script-impl b/utils/build-script-impl index 8b48a190a3566..5825b8c175087 100755 --- a/utils/build-script-impl +++ b/utils/build-script-impl @@ -1110,7 +1110,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-* | android-* | openbsd-* | linux-static-* ) + macosx* | iphone* | appletv* | watch* | linux-* | android-* | openbsd-* ) ;; *) echo "Unknown host to cross-compile for: ${t}" @@ -1744,7 +1744,6 @@ for host in "${ALL_HOSTS[@]}"; do ) fi - if [[ "${DARWIN_OVERLAY_TARGET}" != "" ]]; then # Split LOCAL_HOST into a pair ``arch-sdk`` # Example LOCAL_HOST: macosx-x86_64 From 3fc18387482203f794978ba3e48a71d54843b7b8 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Wed, 5 Feb 2025 08:33:58 -0500 Subject: [PATCH 15/19] Remove --gcc-install-dir and --gcc-toolchain code when using external sysroot - Turns out the compiler and linker are capable of finding libstdc++ with all versions of Ubuntu/Debian sysroots passed to it, so we use that instead of the hardcoded --gcc-toolchain for regular builds. --- cmake/modules/SwiftConfigureSDK.cmake | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/cmake/modules/SwiftConfigureSDK.cmake b/cmake/modules/SwiftConfigureSDK.cmake index 36fe6b12d1bb2..a6644f7cbb221 100644 --- a/cmake/modules/SwiftConfigureSDK.cmake +++ b/cmake/modules/SwiftConfigureSDK.cmake @@ -422,17 +422,8 @@ macro(configure_sdk_unix name architectures) if(CROSS_COMPILE_SYSROOTS) set(SWIFT_SDK_${prefix}_ARCH_${arch}_PATH ${CROSS_COMPILE_SYSROOTS}) - # Find toolchain install dir - execute_process( - COMMAND dirname $(find ${CROSS_COMPILE_SYSROOTS}/usr -name crtbegin.o | grep ${SWIFT_SDK_LINUX_ARCH_${arch}_SUFFIX}) - WORKING_DIRECTORY ${CROSS_COMPILE_SYSROOTS} - OUTPUT_VARIABLE GCC_INSTALL_DIR - RESULT_VARIABLE FOUND_GCC_INSTALL_DIR) - if(FOUND_GCC_INSTALL_DIR) - set(SWIFT_SDK_${prefix}_CXX_OVERLAY_SWIFT_COMPILE_FLAGS -Xcc --gcc-install-dir=${GCC_INSTALL_DIR}) - else() - set(SWIFT_SDK_${prefix}_CXX_OVERLAY_SWIFT_COMPILE_FLAGS -Xcc --gcc-toolchain=${CROSS_COMPILE_SYSROOTS}) - endif() + # Clear hardcoded --gcc-toolchain to let the external sysroot be used to find libstdc++ + set(SWIFT_SDK_${prefix}_CXX_OVERLAY_SWIFT_COMPILE_FLAGS "") endif() elseif("${prefix}" STREQUAL "FREEBSD") if(NOT arch MATCHES "(arm64|x86_64)") From e205a48657ee5fcadbccc84464a8beed51adcadd Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Wed, 5 Feb 2025 08:49:49 -0500 Subject: [PATCH 16/19] Restore original code for setting SWIFT_SDK_LINUX_ARCH_${arch}_TRIPLE for LINUX --- cmake/modules/SwiftConfigureSDK.cmake | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/cmake/modules/SwiftConfigureSDK.cmake b/cmake/modules/SwiftConfigureSDK.cmake index a6644f7cbb221..8050a138f1e3a 100644 --- a/cmake/modules/SwiftConfigureSDK.cmake +++ b/cmake/modules/SwiftConfigureSDK.cmake @@ -407,17 +407,15 @@ macro(configure_sdk_unix name architectures) if("${prefix}" STREQUAL "LINUX") if(arch MATCHES "(armv5)") - set(SWIFT_SDK_LINUX_ARCH_${arch}_SUFFIX linux-gnueabi) + set(SWIFT_SDK_LINUX_ARCH_${arch}_TRIPLE "${arch}-unknown-linux-gnueabi") elseif(arch MATCHES "(armv6|armv7)") - set(SWIFT_SDK_LINUX_ARCH_${arch}_SUFFIX linux-gnueabihf) + set(SWIFT_SDK_LINUX_ARCH_${arch}_TRIPLE "${arch}-unknown-linux-gnueabihf") elseif(arch MATCHES "(aarch64|i686|powerpc|powerpc64|powerpc64le|s390x|x86_64|riscv64)") - set(SWIFT_SDK_LINUX_ARCH_${arch}_SUFFIX linux-gnu) + set(SWIFT_SDK_LINUX_ARCH_${arch}_TRIPLE "${arch}-unknown-linux-gnu") else() message(FATAL_ERROR "unknown arch for ${prefix}: ${arch}") endif() - set(SWIFT_SDK_LINUX_ARCH_${arch}_TRIPLE "${arch}-unknown-${SWIFT_SDK_LINUX_ARCH_${arch}_SUFFIX}") - # If we are using an external sysroot, update path and CXX compile flags to point to it if(CROSS_COMPILE_SYSROOTS) set(SWIFT_SDK_${prefix}_ARCH_${arch}_PATH ${CROSS_COMPILE_SYSROOTS}) From f046aadb32f3a74309f58690dd71bac22a7ab8d7 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Thu, 6 Feb 2025 08:57:02 -0500 Subject: [PATCH 17/19] Add SWIFT_ prefix to CROSS_COMPILE_SYSROOTS when passed to Swift cmake invocation --- cmake/modules/SwiftConfigureSDK.cmake | 4 ++-- utils/build-script-impl | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmake/modules/SwiftConfigureSDK.cmake b/cmake/modules/SwiftConfigureSDK.cmake index 8050a138f1e3a..6e2ca6d9737d4 100644 --- a/cmake/modules/SwiftConfigureSDK.cmake +++ b/cmake/modules/SwiftConfigureSDK.cmake @@ -417,8 +417,8 @@ macro(configure_sdk_unix name architectures) endif() # If we are using an external sysroot, update path and CXX compile flags to point to it - if(CROSS_COMPILE_SYSROOTS) - set(SWIFT_SDK_${prefix}_ARCH_${arch}_PATH ${CROSS_COMPILE_SYSROOTS}) + if(SWIFT_CROSS_COMPILE_SYSROOTS) + set(SWIFT_SDK_${prefix}_ARCH_${arch}_PATH ${SWIFT_CROSS_COMPILE_SYSROOTS}) # Clear hardcoded --gcc-toolchain to let the external sysroot be used to find libstdc++ set(SWIFT_SDK_${prefix}_CXX_OVERLAY_SWIFT_COMPILE_FLAGS "") diff --git a/utils/build-script-impl b/utils/build-script-impl index 5825b8c175087..a60fe98de6ad2 100755 --- a/utils/build-script-impl +++ b/utils/build-script-impl @@ -1786,7 +1786,7 @@ for host in "${ALL_HOSTS[@]}"; do if [ ! -z "${CROSS_COMPILE_SYSROOTS}" ]; then cmake_options=( "${cmake_options[@]}" - -DCROSS_COMPILE_SYSROOTS:STRING="${CROSS_COMPILE_SYSROOTS}" + -DSWIFT_CROSS_COMPILE_SYSROOTS:STRING="${CROSS_COMPILE_SYSROOTS}" -DSWIFT_USE_LINKER:STRING="lld" ) fi From 644872925b18463764cfc5d905dc1739e96ab0dc Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Thu, 6 Feb 2025 09:14:12 -0500 Subject: [PATCH 18/19] Implement new SWIFT_LIBDISPATCH_C_FLAGS and SWIFT_LIBDISPATCH_CXX_FLAGS variables in Libdispatch.cmake - So we do not overwrite CMAKE_C_FLAGS and CMAKE_CXX_FLAGS --- cmake/modules/Libdispatch.cmake | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/cmake/modules/Libdispatch.cmake b/cmake/modules/Libdispatch.cmake index cd4a6e70e5a43..7cdb8177e92a0 100644 --- a/cmake/modules/Libdispatch.cmake +++ b/cmake/modules/Libdispatch.cmake @@ -72,6 +72,8 @@ foreach(sdk ${DISPATCH_SDKS}) foreach(arch ${ARCHS}) set(LIBDISPATCH_VARIANT_NAME "libdispatch-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}") + set(SWIFT_LIBDISPATCH_C_FLAGS ${CMAKE_C_FLAGS}) + set(SWIFT_LIBDISPATCH_CXX_FLAGS ${CMAKE_CXX_FLAGS}) if(sdk MATCHES WINDOWS) set(SWIFT_LIBDISPATCH_COMPILER_TRIPLE_CMAKE_ARGS -DCMAKE_C_COMPILER_TARGET=${SWIFT_SDK_WINDOWS_ARCH_${arch}_TRIPLE};-DCMAKE_CXX_COMPILER_TARGET=${SWIFT_SDK_WINDOWS_ARCH_${arch}_TRIPLE}) @@ -79,8 +81,8 @@ foreach(sdk ${DISPATCH_SDKS}) set(SWIFT_LIBDISPATCH_COMPILER_CMAKE_ARGS ${SWIFT_LIBDISPATCH_COMPILER_CMAKE_ARGS};-DCMAKE_SYSROOT=${SWIFT_SDK_${sdk}_ARCH_${arch}_PATH}) set(SWIFT_LIBDISPATCH_COMPILER_TRIPLE_CMAKE_ARGS -DCMAKE_C_COMPILER_TARGET=${SWIFT_SDK_${sdk}_ARCH_${arch}_TRIPLE};-DCMAKE_CXX_COMPILER_TARGET=${SWIFT_SDK_${sdk}_ARCH_${arch}_TRIPLE}) # Use linker used to build Swift - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -w -fuse-ld=${SWIFT_USE_LINKER}") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w -fuse-ld=${SWIFT_USE_LINKER}") + set(SWIFT_LIBDISPATCH_C_FLAGS "${SWIFT_LIBDISPATCH_C_FLAGS} -w -fuse-ld=${SWIFT_USE_LINKER}") + set(SWIFT_LIBDISPATCH_CXX_FLAGS "${SWIFT_LIBDISPATCH_CXX_FLAGS} -w -fuse-ld=${SWIFT_USE_LINKER}") endif() if("${sdk}" STREQUAL "ANDROID") @@ -99,8 +101,8 @@ foreach(sdk ${DISPATCH_SDKS}) -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} ${SWIFT_LIBDISPATCH_COMPILER_CMAKE_ARGS} ${SWIFT_LIBDISPATCH_COMPILER_TRIPLE_CMAKE_ARGS} - -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS} - -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS} + -DCMAKE_C_FLAGS=${SWIFT_LIBDISPATCH_C_FLAGS} + -DCMAKE_CXX_FLAGS=${SWIFT_LIBDISPATCH_CXX_FLAGS} -DCMAKE_EXE_LINKER_FLAGS=${CMAKE_EXE_LINKER_FLAGS} -DCMAKE_SHARED_LINKER_FLAGS=${CMAKE_SHARED_LINKER_FLAGS} -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM} @@ -199,8 +201,8 @@ foreach(sdk ${DISPATCH_SDKS}) -DCMAKE_AR=${CMAKE_AR} -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} ${SWIFT_LIBDISPATCH_COMPILER_CMAKE_ARGS} - -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS} - -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS} + -DCMAKE_C_FLAGS=${SWIFT_LIBDISPATCH_C_FLAGS} + -DCMAKE_CXX_FLAGS=${SWIFT_LIBDISPATCH_CXX_FLAGS} -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM} -DCMAKE_INSTALL_LIBDIR=lib -DCMAKE_INSTALL_PREFIX= From c1fb6cba9edb3490f2f6d4bd5cd33b4f331bfdd9 Mon Sep 17 00:00:00 2001 From: "Jesse L. Zamora" Date: Fri, 7 Feb 2025 13:50:22 -0500 Subject: [PATCH 19/19] Update wording for cross-compile-sysroots option --- utils/build-script-impl | 2 +- utils/build_swift/build_swift/driver_arguments.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/utils/build-script-impl b/utils/build-script-impl index a60fe98de6ad2..03880ea2facb6 100755 --- a/utils/build-script-impl +++ b/utils/build-script-impl @@ -288,7 +288,7 @@ KNOWN_SETTINGS=( cross-compile-with-host-tools "" "set to use the clang we build for the host to then build the cross-compile hosts" cross-compile-install-prefixes "" "semicolon-separated list of install prefixes to use for the cross-compiled hosts. The list expands, so if there are more cross-compile hosts than prefixes, unmatched hosts use the last prefix in the list" cross-compile-deps-path "" "path for CMake to look for cross-compiled library dependencies, such as libXML2" - cross-compile-sysroots "" "path for CMake to look for a cross-compilation sysroot for a different platform and/or architecture" + cross-compile-sysroots "" "path(s) for CMake to look for cross-compilation sysroots for different platforms or architectures" cross-compile-append-host-target-to-destdir "1" "turns on appending the host target name of each cross-compiled toolchain to its install-destdir, to keep them separate from the natively-built toolchain" skip-merge-lipo-cross-compile-tools "" "set to skip running merge-lipo after installing cross-compiled host Swift tools" coverage-db "" "If set, coverage database to use when prioritizing testing" diff --git a/utils/build_swift/build_swift/driver_arguments.py b/utils/build_swift/build_swift/driver_arguments.py index ff7482d67ae1b..2f00411a8fae8 100644 --- a/utils/build_swift/build_swift/driver_arguments.py +++ b/utils/build_swift/build_swift/driver_arguments.py @@ -670,10 +670,10 @@ def create_argument_parser(): 'such as the libcurl dependency of FoundationNetworking') option('--cross-compile-sysroots', store_path, - help='The path to a directory that contains a C/C++ sysroot for a ' - 'different platform and/or architecture for cross-compiling Swift. ' - 'The sysroot must contain C/C++ libraries that match the ' - 'architecture that is being built for.') + help='Path(s) to one or more directories that contain C/C++ sysroots for ' + 'different platforms and/or architectures for cross-compiling Swift. ' + 'The sysroots must contain C/C++ libraries that match the ' + 'architectures that are being built for.') option('--cross-compile-append-host-target-to-destdir', toggle_true, default=True,