From 004a738c70fc24cd5faf6794a6dc217b38f86e02 Mon Sep 17 00:00:00 2001 From: Evan Wilde Date: Wed, 7 May 2025 12:47:27 -0700 Subject: [PATCH] [Build-Script]: Extend Swift cmake options The existing swift-cmake-options flag overwrites all flags computed by build-script. Sometimes it is useful to be able to append additional CMake flags without overwriting the existing flags. This patch adds `--extra-swift-cmake-options` that adds the specified flags to the Swift CMake configuration instead of overwriting them. This also adds a similar `--extra-llvm-cmake-options`, which adds the new flags to the end, allowing one to replace and overwrite CMake flags that build-script computed. Due to the parameter passing mechanisms in build-script-impl, while this behavior would be useful for Swift, it is not immediately apparent how one would best implement this at this time. --- utils/build-presets.ini | 16 ++++++++-------- .../build_swift/build_swift/driver_arguments.py | 13 +++++++++++++ utils/build_swift/tests/expected_options.py | 4 ++++ .../swift_build_support/products/llvm.py | 1 + .../swift_build_support/products/swift.py | 2 ++ .../tests/products/test_swift.py | 3 +++ 6 files changed, 31 insertions(+), 8 deletions(-) diff --git a/utils/build-presets.ini b/utils/build-presets.ini index c610ee4f5c900..992787f954ecb 100644 --- a/utils/build-presets.ini +++ b/utils/build-presets.ini @@ -3138,20 +3138,20 @@ build-swift-examples=0 build-runtime-with-host-compiler=0 build-swift-libexec=0 build-swift-remote-mirror=0 +build-swift-static-sdk-overlay=0 +build-swift-static-stdlib=0 +enable-experimental-concurrency=1 +enable-experimental-distributed=0 +enable-experimental-observation=0 +enable-experimental-differentiable-programming=0 -extra-cmake-options= +extra-llvm-cmake-options= -DLLVM_TARGETS_TO_BUILD=AArch64;X86 -swift-cmake-options= - -DSWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY:BOOL=YES - -DSWIFT_ENABLE_EXPERIMENTAL_OBSERVATION:BOOL=NO - -DSWIFT_ENABLE_EXPERIMENTAL_DISTRIBUTED:BOOL=NO - -DSWIFT_ENABLE_EXPERIMENTAL_DIFFERENTIABLE_PROGRAMMING:BOOL=NO +extra-swift-cmake-options= -DSWIFT_ENABLE_SWIFT_IN_SWIFT:BOOL=NO -DSWIFT_INCLUDE_DOCS:BOOL=NO -DSWIFT_BUILD_EXAMPLES:BOOL=OFF - -DSWIFT_BUILD_STATIC_SDK_OVERLAY:BOOL=NO - -DSWIFT_BUILD_STATIC_STDLIB:BOOL=NO build-subdir=%(build_subdir)s install-destdir=%(install_destdir)s diff --git a/utils/build_swift/build_swift/driver_arguments.py b/utils/build_swift/build_swift/driver_arguments.py index 59250d9220d3b..59f28571d5098 100644 --- a/utils/build_swift/build_swift/driver_arguments.py +++ b/utils/build_swift/build_swift/driver_arguments.py @@ -592,6 +592,12 @@ def create_argument_parser(): 'will get little benefit from it (e.g. tools for ' 'bootstrapping or debugging Swift)') + option('--extra-swift-cmake-options', append, + type=argparse.ShellSplitType(), + help='Pass additional CMake options to the Swift build. ' + 'Can be passed multiple times to add multiple options.', + default=[]) + option('--dsymutil-jobs', store_int, default=defaults.DSYMUTIL_JOBS, metavar='COUNT', @@ -1403,6 +1409,13 @@ def create_argument_parser(): help='CMake options used for llvm in the form of comma ' 'separated options "-DCMAKE_VAR1=YES,-DCMAKE_VAR2=/tmp". Can ' 'be called multiple times to add multiple such options.') + option('--extra-llvm-cmake-options', append, + type=argparse.ShellSplitType(), + help='Pass additional CMake options to the LLVM build. ' + 'Can be passed multiple times to add multiple options. ' + 'These are the last arguments passed to CMake and can override ' + 'existing options.', + default=[]) option('--llvm-build-compiler-rt-with-use-runtimes', toggle_true, default=True, help='Switch to LLVM_ENABLE_RUNTIMES as the mechanism to build compiler-rt' diff --git a/utils/build_swift/tests/expected_options.py b/utils/build_swift/tests/expected_options.py index 058383946555f..407afe9a9b26a 100644 --- a/utils/build_swift/tests/expected_options.py +++ b/utils/build_swift/tests/expected_options.py @@ -197,7 +197,9 @@ 'enable_ubsan': False, 'export_compile_commands': False, 'extra_cmake_options': [], + 'extra_llvm_cmake_options': [], 'extra_swift_args': [], + 'extra_swift_cmake_options': [], 'swift_debuginfo_non_lto_args': None, 'force_optimized_typechecker': False, 'foundation_build_variant': 'Debug', @@ -803,6 +805,7 @@ class BuildScriptImplOption(_BaseOption): StrOption('--swift-darwin-module-archs'), StrOption('--swift-darwin-supported-archs'), SetTrueOption('--swift-freestanding-is-darwin'), + AppendOption('--extra-swift-cmake-options'), StrOption('--linux-archs'), StrOption('--linux-static-archs'), @@ -853,6 +856,7 @@ class BuildScriptImplOption(_BaseOption): AppendOption('--llvm-ninja-targets'), AppendOption('--llvm-ninja-targets-for-cross-compile-hosts'), AppendOption('--llvm-cmake-options'), + AppendOption('--extra-llvm-cmake-options'), EnableOption('--llvm-build-compiler-rt-with-use-runtimes'), AppendOption('--darwin-symroot-path-filters'), 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 6fc15415e40f5..362bd6d9c84bc 100644 --- a/utils/swift_build_support/swift_build_support/products/llvm.py +++ b/utils/swift_build_support/swift_build_support/products/llvm.py @@ -453,6 +453,7 @@ def build(self, host_target): self.cmake_options.extend(host_config.cmake_options) self.cmake_options.extend(llvm_cmake_options) + self.cmake_options.extend_raw(self.args.extra_llvm_cmake_options) self._handle_cxx_headers(host_target, platform) diff --git a/utils/swift_build_support/swift_build_support/products/swift.py b/utils/swift_build_support/swift_build_support/products/swift.py index 483be3b409d1c..03ec91e41fafa 100644 --- a/utils/swift_build_support/swift_build_support/products/swift.py +++ b/utils/swift_build_support/swift_build_support/products/swift.py @@ -104,6 +104,8 @@ def __init__(self, args, toolchain, source_dir, build_dir): self.cmake_options.extend( self._enable_new_runtime_build) + self.cmake_options.extend_raw(self.args.extra_swift_cmake_options) + @classmethod def product_source_name(cls): """product_source_name() -> str diff --git a/utils/swift_build_support/tests/products/test_swift.py b/utils/swift_build_support/tests/products/test_swift.py index 311794db2f29c..9cb4e70a85c79 100644 --- a/utils/swift_build_support/tests/products/test_swift.py +++ b/utils/swift_build_support/tests/products/test_swift.py @@ -52,6 +52,7 @@ def setUp(self): benchmark_num_o_iterations=3, disable_guaranteed_normal_arguments=True, force_optimized_typechecker=False, + extra_swift_cmake_options=["-DHELLO=YES"], enable_stdlibcore_exclusivity_checking=False, enable_experimental_differentiable_programming=False, enable_experimental_concurrency=False, @@ -125,6 +126,7 @@ def test_by_default_no_cmake_options(self): '-USWIFT_DEBUGINFO_NON_LTO_ARGS', '-DSWIFT_STDLIB_BUILD_SYMBOL_GRAPHS:BOOL=FALSE', '-DSWIFT_ENABLE_NEW_RUNTIME_BUILD:BOOL=FALSE', + '-DHELLO=YES', ] self.assertEqual(set(swift.cmake_options), set(expected)) @@ -161,6 +163,7 @@ def test_swift_runtime_tsan(self): '-USWIFT_DEBUGINFO_NON_LTO_ARGS', '-DSWIFT_STDLIB_BUILD_SYMBOL_GRAPHS:BOOL=FALSE', '-DSWIFT_ENABLE_NEW_RUNTIME_BUILD:BOOL=FALSE', + '-DHELLO=YES', ] self.assertEqual(set(swift.cmake_options), set(flags_set))