From 5a66841608c6c5a6124961389465f18cd3ec02b8 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Tue, 27 Feb 2024 15:57:35 +0000 Subject: [PATCH 1/2] build: Respect `native-*-tools-path` when building Wasm stdlib This change allows the Wasm stdlib to be built using the prebuilt Swift toolchain instead of just built from source. --- .../swift_build_support/cmake.py | 2 +- .../products/wasisysroot.py | 24 ++++++++++++------- .../products/wasmstdlib.py | 2 +- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/utils/swift_build_support/swift_build_support/cmake.py b/utils/swift_build_support/swift_build_support/cmake.py index 61e255e759fbd..7b1e33f40302f 100644 --- a/utils/swift_build_support/swift_build_support/cmake.py +++ b/utils/swift_build_support/swift_build_support/cmake.py @@ -147,7 +147,7 @@ def common_options(self, product=None): define("CMAKE_CXX_COMPILER_LAUNCHER:PATH", args.cmake_cxx_launcher) if self.prefer_just_built_toolchain and product: - toolchain_path = product.install_toolchain_path(args.host_target) + toolchain_path = product.native_toolchain_path(args.host_target) cmake_swiftc_path = os.getenv('CMAKE_Swift_COMPILER', os.path.join(toolchain_path, 'bin', 'swiftc')) define("CMAKE_C_COMPILER:PATH", os.path.join(toolchain_path, diff --git a/utils/swift_build_support/swift_build_support/products/wasisysroot.py b/utils/swift_build_support/swift_build_support/products/wasisysroot.py index 59f8b738bc929..22034e6505c70 100644 --- a/utils/swift_build_support/swift_build_support/products/wasisysroot.py +++ b/utils/swift_build_support/swift_build_support/products/wasisysroot.py @@ -45,7 +45,10 @@ def should_install(self, host_target): def build(self, host_target): build_root = os.path.dirname(self.build_dir) - llvm_build_dir = os.path.join('..', build_root, '%s-%s' % ('llvm', host_target)) + llvm_build_bin_dir = os.path.join( + '..', build_root, '%s-%s' % ('llvm', host_target), 'bin') + llvm_tools_path = self.args.native_llvm_tools_path or llvm_build_bin_dir + clang_tools_path = self.args.native_clang_tools_path or llvm_build_bin_dir build_jobs = self.args.build_jobs or multiprocessing.cpu_count() sysroot_build_dir = WASILibc.sysroot_build_path(build_root, host_target) @@ -66,9 +69,9 @@ def build(self, host_target): 'OBJDIR=' + os.path.join(self.build_dir, 'obj'), 'SYSROOT=' + sysroot_build_dir, 'INSTALL_DIR=' + WASILibc.sysroot_install_path(build_root), - 'CC=' + os.path.join(llvm_build_dir, 'bin', 'clang'), - 'AR=' + os.path.join(llvm_build_dir, 'bin', 'llvm-ar'), - 'NM=' + os.path.join(llvm_build_dir, 'bin', 'llvm-nm'), + 'CC=' + os.path.join(clang_tools_path, 'clang'), + 'AR=' + os.path.join(llvm_tools_path, 'llvm-ar'), + 'NM=' + os.path.join(llvm_tools_path, 'llvm-nm'), ]) @classmethod @@ -119,7 +122,10 @@ def should_install(self, host_target): def build(self, host_target): build_root = os.path.dirname(self.build_dir) - llvm_build_dir = os.path.join('..', build_root, '%s-%s' % ('llvm', host_target)) + llvm_build_bin_dir = os.path.join( + '..', build_root, '%s-%s' % ('llvm', host_target), 'bin') + llvm_tools_path = self.args.native_llvm_tools_path or llvm_build_bin_dir + clang_tools_path = self.args.native_clang_tools_path or llvm_build_bin_dir self.cmake_options.define('CMAKE_SYSROOT:PATH', WASILibc.sysroot_build_path(build_root, host_target)) @@ -144,13 +150,13 @@ def build(self, host_target): self.cmake_options.define('CMAKE_SYSTEM_NAME:STRING', 'WASI') self.cmake_options.define('CMAKE_SYSTEM_PROCESSOR:STRING', 'wasm32') self.cmake_options.define('CMAKE_AR:FILEPATH', - os.path.join(llvm_build_dir, 'bin', 'llvm-ar')) + os.path.join(llvm_tools_path, 'llvm-ar')) self.cmake_options.define('CMAKE_RANLIB:FILEPATH', - os.path.join(llvm_build_dir, 'bin', 'llvm-ranlib')) + os.path.join(llvm_tools_path, 'llvm-ranlib')) self.cmake_options.define('CMAKE_C_COMPILER:FILEPATH', - os.path.join(llvm_build_dir, 'bin', 'clang')) + os.path.join(clang_tools_path, 'clang')) self.cmake_options.define('CMAKE_CXX_COMPILER:STRING', - os.path.join(llvm_build_dir, 'bin', 'clang++')) + os.path.join(clang_tools_path, 'clang++')) # Explicitly disable exceptions even though it's usually implicitly disabled by # LIBCXX_ENABLE_EXCEPTIONS because the CMake feature check fails to detect # -fno-exceptions support in clang due to missing compiler-rt while configuring diff --git a/utils/swift_build_support/swift_build_support/products/wasmstdlib.py b/utils/swift_build_support/swift_build_support/products/wasmstdlib.py index af138d71ae7d6..fee63aba7cc39 100644 --- a/utils/swift_build_support/swift_build_support/products/wasmstdlib.py +++ b/utils/swift_build_support/swift_build_support/products/wasmstdlib.py @@ -44,7 +44,7 @@ def build(self, host_target): 'SWIFT_STDLIB_BUILD_TYPE:STRING', self._build_variant) # Toolchain configuration - toolchain_path = self.install_toolchain_path(host_target) + toolchain_path = self.native_toolchain_path(host_target) # Explicitly set the CMake AR and RANLIB to force it to use llvm-ar/llvm-ranlib # instead of the system ar/ranlib, which usually don't support WebAssembly # object files. From 82b1d9c8038fe4c72f5b3bc6e09b5430164d9e27 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 8 Mar 2024 16:24:10 +0000 Subject: [PATCH 2/2] build: Rename `prefer_just_built_toolchain` -> `prefer_native_toolchain` --- .../swift_build_support/cmake.py | 13 ++++++++----- .../swift_build_support/products/cmake_product.py | 4 ++-- .../swift_build_support/products/minimalstdlib.py | 2 +- .../swift_build_support/products/wasisysroot.py | 2 +- .../swift_build_support/products/wasmstdlib.py | 2 +- 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/utils/swift_build_support/swift_build_support/cmake.py b/utils/swift_build_support/swift_build_support/cmake.py index 7b1e33f40302f..b8f8994ce72f3 100644 --- a/utils/swift_build_support/swift_build_support/cmake.py +++ b/utils/swift_build_support/swift_build_support/cmake.py @@ -97,13 +97,16 @@ def __iadd__(self, other): class CMake(object): - def __init__(self, args, toolchain, prefer_just_built_toolchain=False): - """If prefer_just_built_toolchain is set to True, we set the clang, clang++, - and Swift compilers from the installed toolchain. + def __init__(self, args, toolchain, prefer_native_toolchain=False): + """If prefer_native_toolchain is set to True, we set the clang, clang++, + and Swift compilers from the toolchain explicitly specified by the + native-*-tools-path options or just installed toolchain if the options + are not specified. If prefer_native_toolchain is set to False, we use + system defaults. """ self.args = args self.toolchain = toolchain - self.prefer_just_built_toolchain = prefer_just_built_toolchain + self.prefer_native_toolchain = prefer_native_toolchain def common_options(self, product=None): """Return options used for all products, including LLVM/Clang @@ -146,7 +149,7 @@ def common_options(self, product=None): if args.cmake_cxx_launcher: define("CMAKE_CXX_COMPILER_LAUNCHER:PATH", args.cmake_cxx_launcher) - if self.prefer_just_built_toolchain and product: + if self.prefer_native_toolchain and product: toolchain_path = product.native_toolchain_path(args.host_target) cmake_swiftc_path = os.getenv('CMAKE_Swift_COMPILER', os.path.join(toolchain_path, 'bin', 'swiftc')) diff --git a/utils/swift_build_support/swift_build_support/products/cmake_product.py b/utils/swift_build_support/swift_build_support/products/cmake_product.py index 18b5e3b7291ad..568e9069a8056 100644 --- a/utils/swift_build_support/swift_build_support/products/cmake_product.py +++ b/utils/swift_build_support/swift_build_support/products/cmake_product.py @@ -24,11 +24,11 @@ def is_verbose(self): return self.args.verbose_build def build_with_cmake(self, build_targets, build_type, build_args, - prefer_just_built_toolchain=False): + prefer_native_toolchain=False): assert self.toolchain.cmake is not None cmake_build = [] _cmake = cmake.CMake(self.args, self.toolchain, - prefer_just_built_toolchain) + prefer_native_toolchain) if self.toolchain.distcc_pump: cmake_build.append(self.toolchain.distcc_pump) diff --git a/utils/swift_build_support/swift_build_support/products/minimalstdlib.py b/utils/swift_build_support/swift_build_support/products/minimalstdlib.py index b27561139698b..51066c787a6c9 100644 --- a/utils/swift_build_support/swift_build_support/products/minimalstdlib.py +++ b/utils/swift_build_support/swift_build_support/products/minimalstdlib.py @@ -174,7 +174,7 @@ def build(self, host_target): # Build! self.build_with_cmake(["swift-stdlib-freestanding"], build_variant, [], - prefer_just_built_toolchain=True) + prefer_native_toolchain=True) def should_test(self, host_target): return False diff --git a/utils/swift_build_support/swift_build_support/products/wasisysroot.py b/utils/swift_build_support/swift_build_support/products/wasisysroot.py index 22034e6505c70..11abc9f4cff0a 100644 --- a/utils/swift_build_support/swift_build_support/products/wasisysroot.py +++ b/utils/swift_build_support/swift_build_support/products/wasisysroot.py @@ -194,7 +194,7 @@ def build(self, host_target): self.cmake_options.define('UNIX:BOOL', 'TRUE') self.build_with_cmake([], self.args.build_variant, [], - prefer_just_built_toolchain=True) + prefer_native_toolchain=True) self.install_with_cmake( ["install"], WASILibc.sysroot_install_path(build_root)) diff --git a/utils/swift_build_support/swift_build_support/products/wasmstdlib.py b/utils/swift_build_support/swift_build_support/products/wasmstdlib.py index fee63aba7cc39..d70bacb288948 100644 --- a/utils/swift_build_support/swift_build_support/products/wasmstdlib.py +++ b/utils/swift_build_support/swift_build_support/products/wasmstdlib.py @@ -117,7 +117,7 @@ def build(self, host_target): # Configure with WebAssembly target variant, and build with just-built toolchain self.build_with_cmake([], self._build_variant, [], - prefer_just_built_toolchain=True) + prefer_native_toolchain=True) def test(self, host_target): build_root = os.path.dirname(self.build_dir)