From efce129650d503820f3ba0e21924e2020d4dda9e Mon Sep 17 00:00:00 2001 From: Eric Miotto <1094986+edymtt@users.noreply.github.com> Date: Thu, 17 Aug 2023 07:36:41 -0700 Subject: [PATCH 1/5] Use LLVM runtimes build LLVM plans to remove the legacy methods for building compiler-rt. This change updates build-script to use LLVM_ENABLE_RUNTIMES to build compiler-rt with the just-built clang instead of the legacy methods. This does not update the llvm-install-components in build-presets.ini -- this is done on purpose to stress more the migration logic, some of the presets will be updated with a subsequent PRs Addresses rdar://113972453 --- utils/build-presets.ini | 6 -- utils/build-script-impl | 14 ----- utils/build_swift/build_swift/defaults.py | 2 +- .../products/cmake_product.py | 13 ----- .../swift_build_support/products/llvm.py | 55 +++++++++++++++---- 5 files changed, 45 insertions(+), 45 deletions(-) diff --git a/utils/build-presets.ini b/utils/build-presets.ini index 27223b02310fa..5c45994cc93cd 100644 --- a/utils/build-presets.ini +++ b/utils/build-presets.ini @@ -697,12 +697,6 @@ reconfigure verbose-build build-ninja -# TODO: remove when we will transition from LLVM_TOOL_COMPILER_RT_BUILD -# to LLVM_USE_RUNTIMES to build compiler-rt (#60993), so we can leverage -# the value for SANITIZER_MIN_OSX_VERSION set in cmake_product.py -extra-cmake-options= - -DCLANG_COMPILER_RT_CMAKE_ARGS:STRING="-DSANITIZER_MIN_OSX_VERSION:STRING=13.0" - # Do not build swift or cmark skip-build-swift skip-build-cmark diff --git a/utils/build-script-impl b/utils/build-script-impl index 06b54cc6c8eb2..dea3f5931a4f9 100755 --- a/utils/build-script-impl +++ b/utils/build-script-impl @@ -803,20 +803,6 @@ function set_build_options_for_host() { esac - # We don't currently support building compiler-rt for cross-compile targets. - # It's not clear that's useful anyway. - if [[ $(is_cross_tools_host "${host}") ]] ; then - llvm_cmake_options+=( - -DLLVM_TOOL_COMPILER_RT_BUILD:BOOL=FALSE - -DLLVM_BUILD_EXTERNAL_COMPILER_RT:BOOL=FALSE - ) - else - llvm_cmake_options+=( - -DLLVM_TOOL_COMPILER_RT_BUILD:BOOL="$(false_true ${SKIP_BUILD_COMPILER_RT})" - -DLLVM_BUILD_EXTERNAL_COMPILER_RT:BOOL="$(false_true ${SKIP_BUILD_COMPILER_RT})" - ) - fi - # If we are asked to not generate test targets for LLVM and or Swift, # disable as many LLVM tools as we can. This improves compile time when # compiling with LTO. diff --git a/utils/build_swift/build_swift/defaults.py b/utils/build_swift/build_swift/defaults.py index 99b7b85d72aad..650a809f4dad3 100644 --- a/utils/build_swift/build_swift/defaults.py +++ b/utils/build_swift/build_swift/defaults.py @@ -123,7 +123,7 @@ def llvm_install_components(): platforms. """ components = ['llvm-ar', 'llvm-cov', 'llvm-profdata', 'IndexStore', 'clang', - 'clang-resource-headers', 'compiler-rt', 'clangd', 'LTO', + 'clang-resource-headers', 'builtins', 'runtimes', 'clangd', 'LTO', 'lld'] if os.sys.platform == 'darwin': components.extend(['dsymutil']) 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 dc338334f284d..8804f6d8ca441 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 @@ -381,19 +381,6 @@ def host_cmake_options(self, host_target): # in the compiler checks CMake performs swift_cmake_options.define('CMAKE_OSX_ARCHITECTURES', arch) - # We don't currently support building compiler-rt for cross-compile targets. - # It's not clear that's useful anyway. - if self.is_cross_compile_target(host_target): - llvm_cmake_options.define('LLVM_TOOL_COMPILER_RT_BUILD:BOOL', 'FALSE') - llvm_cmake_options.define('LLVM_BUILD_EXTERNAL_COMPILER_RT:BOOL', 'FALSE') - else: - llvm_cmake_options.define('LLVM_TOOL_COMPILER_RT_BUILD:BOOL', - cmake.CMakeOptions.true_false( - self.args.build_compiler_rt)) - llvm_cmake_options.define('LLVM_BUILD_EXTERNAL_COMPILER_RT:BOOL', - cmake.CMakeOptions.true_false( - self.args.build_compiler_rt)) - # If we are asked to not generate test targets for LLVM and or Swift, # disable as many LLVM tools as we can. This improves compile time when # compiling with LTO. diff --git a/utils/swift_build_support/swift_build_support/products/llvm.py b/utils/swift_build_support/swift_build_support/products/llvm.py index 48f77c0af0b93..42f1738a1e327 100644 --- a/utils/swift_build_support/swift_build_support/products/llvm.py +++ b/utils/swift_build_support/swift_build_support/products/llvm.py @@ -217,6 +217,22 @@ def should_build(self, host_target): # LLVM will always be built in part return True + @classmethod + def compute_cmake_args_for_runtimes(cls, host_target): + # Swift expects the old layout for the runtime directory + args_for_runtime = '-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR:BOOL=OFF' + + if host_target.startswith('linux'): + # This preserves the behaviour we had when using + # LLVM_BUILD_EXTERNAL COMPILER_RT -- + # that is, having the linker not complaing if symbols used + # by TSan are undefined (namely the ones for Blocks Runtime) + # In the long term, we want to remove this and + # build Blocks Runtime before LLVM + args_for_runtime += ';-DSANITIZER_COMMON_LINK_FLAGS:STRING=-Wl,-z,undefs' + + return args_for_runtime + def build(self, host_target): """build() -> void @@ -291,6 +307,9 @@ def build(self, host_target): llvm_cmake_options.define('LLVM_INCLUDE_DOCS:BOOL', 'TRUE') llvm_cmake_options.define('LLVM_ENABLE_LTO:STRING', self.args.lto_type) llvm_cmake_options.define('COMPILER_RT_INTERCEPT_LIBDISPATCH', 'ON') + llvm_cmake_options.define( + 'RUNTIMES_CMAKE_ARGS', + LLVM.compute_cmake_args_for_runtimes(host_target)) if self.args.build_embedded_stdlib and system() == "Darwin": # Ask for Mach-O cross-compilation builtins (for Embedded Swift) @@ -299,10 +318,12 @@ def build(self, host_target): 'armv6 armv6m armv7 armv7m armv7em') llvm_enable_projects = ['clang'] + llvm_enable_runtimes = [] if self.args.build_compiler_rt and \ not self.is_cross_compile_target(host_target): - llvm_enable_projects.append('compiler-rt') + llvm_enable_runtimes.append('compiler-rt') + build_targets.append("runtimes") if self.args.build_clang_tools_extra: llvm_enable_projects.append('clang-tools-extra') @@ -316,18 +337,20 @@ def build(self, host_target): if self.args.build_lld: llvm_enable_projects.append('lld') + if self.args.test: + # LLVMTestingSupport is not built at part of `all` + # and is required by some Swift tests + build_targets.append('LLVMTestingSupport') + llvm_cmake_options.define('LLVM_ENABLE_PROJECTS', ';'.join(llvm_enable_projects)) + llvm_cmake_options.define('LLVM_ENABLE_RUNTIMES', + ';'.join(llvm_enable_runtimes)) - # In the near future we are aiming to build compiler-rt with - # LLVM_ENABLE_RUNTIMES - # Until that happens, we need to unset this variable from - # LLVM CMakeCache.txt for two reasons - # * prevent PRs testing this variable to affect other runs landing - # unrelated features - # * avoid fallouts should we land such change and then have to revert - # it to account for unforeseen regressions - llvm_cmake_options.undefine('LLVM_ENABLE_RUNTIMES') + # This accounts for previous incremental runs that may have set + # those in the LLVM CMakeCache.txt + llvm_cmake_options.undefine('LLVM_TOOL_COMPILER_RT_BUILD') + llvm_cmake_options.undefine('LLVM_BUILD_EXTERNAL_COMPILER_RT') # NOTE: This is not a dead option! It is relied upon for certain # bots/build-configs! @@ -491,9 +514,19 @@ def install(self, host_target): self.args.llvm_install_components != 'all': install_targets = [] components = self.args.llvm_install_components.split(';') + if 'compiler-rt' in components: + # This is a courtesy fallback to avoid breaking downstream presets + # we are not aware of + components.remove('compiler-rt') + components.append('builtins') + components.append('runtimes') + print('warning: replaced legacy LLVM component compiler-rt ' + 'with builtins;runtimes -- consider updating your preset', + flush=True) + for component in components: if self.is_cross_compile_target(host_target): - if component == 'compiler-rt': + if component in ['builtins', 'runtimes']: continue install_targets.append('install-{}'.format(component)) From a7bc9bdf4af8215c742a0a1508d8b6db65dcaa7f Mon Sep 17 00:00:00 2001 From: Eric Miotto <1094986+edymtt@users.noreply.github.com> Date: Mon, 17 Mar 2025 15:39:18 -0700 Subject: [PATCH 2/5] Allow correct detection of Apple SDKs when building compiler-rt --- .../swift_build_support/swift_build_support/products/llvm.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/utils/swift_build_support/swift_build_support/products/llvm.py b/utils/swift_build_support/swift_build_support/products/llvm.py index 42f1738a1e327..4f8fae9938bf0 100644 --- a/utils/swift_build_support/swift_build_support/products/llvm.py +++ b/utils/swift_build_support/swift_build_support/products/llvm.py @@ -310,6 +310,10 @@ def build(self, host_target): llvm_cmake_options.define( 'RUNTIMES_CMAKE_ARGS', LLVM.compute_cmake_args_for_runtimes(host_target)) + if system() == "Darwin": + llvm_cmake_options.define('LLVM_BUILTIN_TARGETS', 'arm64-apple-darwin') + llvm_cmake_options.define('LLVM_RUNTIME_TARGETS', 'arm64-apple-darwin') + llvm_cmake_options.define('RUNTIMES_BUILD_ALLOW_DARWIN', 'ON') if self.args.build_embedded_stdlib and system() == "Darwin": # Ask for Mach-O cross-compilation builtins (for Embedded Swift) From f38f541a0714e27ab224e7360333562a9514b220 Mon Sep 17 00:00:00 2001 From: Eric Miotto Date: Mon, 24 Mar 2025 07:44:42 -0700 Subject: [PATCH 3/5] Darwin: do not build RTSan, ensure we build builtins for Swift Embedded --- .../swift_build_support/products/llvm.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/utils/swift_build_support/swift_build_support/products/llvm.py b/utils/swift_build_support/swift_build_support/products/llvm.py index 4f8fae9938bf0..f85585c39b7e3 100644 --- a/utils/swift_build_support/swift_build_support/products/llvm.py +++ b/utils/swift_build_support/swift_build_support/products/llvm.py @@ -314,12 +314,20 @@ def build(self, host_target): llvm_cmake_options.define('LLVM_BUILTIN_TARGETS', 'arm64-apple-darwin') llvm_cmake_options.define('LLVM_RUNTIME_TARGETS', 'arm64-apple-darwin') llvm_cmake_options.define('RUNTIMES_BUILD_ALLOW_DARWIN', 'ON') + llvm_cmake_options.define( + 'RUNTIMES_arm64-apple-darwin_COMPILER_RT_SANITIZERS_TO_BUILD', + 'asan;dfsan;msan;hwasan;tsan;safestack;cfi;scudo_standalone' + ';ubsan_minimal;gwp_asan;nsan;asan_abi') if self.args.build_embedded_stdlib and system() == "Darwin": # Ask for Mach-O cross-compilation builtins (for Embedded Swift) llvm_cmake_options.define( 'COMPILER_RT_FORCE_BUILD_BAREMETAL_MACHO_BUILTINS_ARCHS:STRING', 'armv6 armv6m armv7 armv7m armv7em') + llvm_cmake_options.define( + 'BUILTINS_arm64-apple-darwin_' + 'COMPILER_RT_FORCE_BUILD_BAREMETAL_MACHO_BUILTINS_ARCHS:' + 'STRING=armv6 armv6m armv7 armv7m armv7em') llvm_enable_projects = ['clang'] llvm_enable_runtimes = [] From e80b470104ab3d5602cda73162bd1941fb936b45 Mon Sep 17 00:00:00 2001 From: Eric Miotto Date: Mon, 24 Mar 2025 07:47:49 -0700 Subject: [PATCH 4/5] Try to apply a couple of LLVM options directly so they are easier to override --- .../swift_build_support/products/llvm.py | 30 +++++++------------ 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/utils/swift_build_support/swift_build_support/products/llvm.py b/utils/swift_build_support/swift_build_support/products/llvm.py index f85585c39b7e3..1836daad392f4 100644 --- a/utils/swift_build_support/swift_build_support/products/llvm.py +++ b/utils/swift_build_support/swift_build_support/products/llvm.py @@ -217,22 +217,6 @@ def should_build(self, host_target): # LLVM will always be built in part return True - @classmethod - def compute_cmake_args_for_runtimes(cls, host_target): - # Swift expects the old layout for the runtime directory - args_for_runtime = '-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR:BOOL=OFF' - - if host_target.startswith('linux'): - # This preserves the behaviour we had when using - # LLVM_BUILD_EXTERNAL COMPILER_RT -- - # that is, having the linker not complaing if symbols used - # by TSan are undefined (namely the ones for Blocks Runtime) - # In the long term, we want to remove this and - # build Blocks Runtime before LLVM - args_for_runtime += ';-DSANITIZER_COMMON_LINK_FLAGS:STRING=-Wl,-z,undefs' - - return args_for_runtime - def build(self, host_target): """build() -> void @@ -307,9 +291,17 @@ def build(self, host_target): llvm_cmake_options.define('LLVM_INCLUDE_DOCS:BOOL', 'TRUE') llvm_cmake_options.define('LLVM_ENABLE_LTO:STRING', self.args.lto_type) llvm_cmake_options.define('COMPILER_RT_INTERCEPT_LIBDISPATCH', 'ON') - llvm_cmake_options.define( - 'RUNTIMES_CMAKE_ARGS', - LLVM.compute_cmake_args_for_runtimes(host_target)) + # Swift expects the old layout for the runtime directory + llvm_cmake_options.define('LLVM_ENABLE_PER_TARGET_RUNTIME_DIR:BOOL', 'OFF') + if host_target.startswith('linux'): + # This preserves the behaviour we had when using + # LLVM_BUILD_EXTERNAL COMPILER_RT -- + # that is, having the linker not complaing if symbols used + # by TSan are undefined (namely the ones for Blocks Runtime) + # In the long term, we want to remove this and + # build Blocks Runtime before LLVM + llvm_cmake_options.define( + 'SANITIZER_COMMON_LINK_FLAGS:STRING', '-Wl,-z,undefs') if system() == "Darwin": llvm_cmake_options.define('LLVM_BUILTIN_TARGETS', 'arm64-apple-darwin') llvm_cmake_options.define('LLVM_RUNTIME_TARGETS', 'arm64-apple-darwin') From c805897a72e52ffd96634c4f709e90cb3700bd28 Mon Sep 17 00:00:00 2001 From: Eric Miotto Date: Mon, 24 Mar 2025 09:11:28 -0700 Subject: [PATCH 5/5] Fix runtime errors in build-script --- .../swift_build_support/swift_build_support/products/llvm.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/swift_build_support/swift_build_support/products/llvm.py b/utils/swift_build_support/swift_build_support/products/llvm.py index 1836daad392f4..ca59be23eab8d 100644 --- a/utils/swift_build_support/swift_build_support/products/llvm.py +++ b/utils/swift_build_support/swift_build_support/products/llvm.py @@ -292,7 +292,7 @@ def build(self, host_target): llvm_cmake_options.define('LLVM_ENABLE_LTO:STRING', self.args.lto_type) llvm_cmake_options.define('COMPILER_RT_INTERCEPT_LIBDISPATCH', 'ON') # Swift expects the old layout for the runtime directory - llvm_cmake_options.define('LLVM_ENABLE_PER_TARGET_RUNTIME_DIR:BOOL', 'OFF') + llvm_cmake_options.define('LLVM_ENABLE_PER_TARGET_RUNTIME_DIR', 'OFF') if host_target.startswith('linux'): # This preserves the behaviour we had when using # LLVM_BUILD_EXTERNAL COMPILER_RT -- @@ -319,7 +319,7 @@ def build(self, host_target): llvm_cmake_options.define( 'BUILTINS_arm64-apple-darwin_' 'COMPILER_RT_FORCE_BUILD_BAREMETAL_MACHO_BUILTINS_ARCHS:' - 'STRING=armv6 armv6m armv7 armv7m armv7em') + 'STRING', 'armv6 armv6m armv7 armv7m armv7em') llvm_enable_projects = ['clang'] llvm_enable_runtimes = []