Skip to content

[Build-Script]: Extend Swift cmake options #81364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

etcwilde
Copy link
Contributor

@etcwilde etcwilde commented May 7, 2025

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.

@etcwilde
Copy link
Contributor Author

etcwilde commented May 7, 2025

@AnthonyLatsis, this should help simplify #81284.
You should be able to avoid the SWIFT_CXX_FLAGS hack and use --extra-swift-cmake-options.

@etcwilde
Copy link
Contributor Author

etcwilde commented May 7, 2025

@swift-ci please test

@etcwilde
Copy link
Contributor Author

etcwilde commented May 7, 2025

preset=bootstrap_stage0
@swift-ci please test with preset Linux Platform

@etcwilde
Copy link
Contributor Author

etcwilde commented May 7, 2025

Bootstrap preset failed with unrelated failure:

[2025-05-07T20:28:09.097Z] [./swift/utils/build-script] ERROR: no value found for build_subdir in "%(build_subdir)s"

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.
@etcwilde etcwilde force-pushed the ewilde/extend-swift-cmake-options branch from 9269b5a to 004a738 Compare May 7, 2025 21:49
@etcwilde
Copy link
Contributor Author

etcwilde commented May 7, 2025

@swift-ci please test

Copy link
Collaborator

@AnthonyLatsis AnthonyLatsis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thank you!

The existing swift-cmake-options flag overwrites all flags computed by build-script

Where does this happen? They appear to be added to the front here:

swift/utils/build-script-impl

Lines 1604 to 1609 in 78234e2

# Add any specific cmake options specified by build-script
product_cmake_options_name=$(to_varname "${product%%_*}")_CMAKE_OPTIONS
product_cmake_options=(${!product_cmake_options_name}) # convert to array
cmake_options+=("${product_cmake_options[@]}")

@etcwilde
Copy link
Contributor Author

etcwilde commented May 8, 2025

Some of the options are computed in swift.py inside of the Python bits of build-script. Those are passed to build-script-impl via the --swift-cmake-options flag. When someone passes --swift-cmake-options explicitly, build-script will pass --swift-cmake-options to build-script-impl twice. Once with the flags that it computed, and once again with the flags that you set, rather than merging them.

build-script-impl only picks up the last instance of a flag, so that's the one we see.

e.g

./utils/build-script ... --enable-experimental-concurrency=1 --swift-cmake-options='-DI_AM_A_TEAPOT=YES'

results in a build-script-impl invocation with the following flags:

'--workspace', '/Users/ewilde/Work/Swift-Project', '--build-dir', ... '--swift-cmake-options=-DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE ... -DSWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY:BOOL=TRUE ...', ... '--swift-cmake-options=-DI_AM_A_TEAPOT=YES' ...

While I don't mind the idea of fixing that so that swift.py merges all of the flags and build-script (python) doesn't pass --swift-cmake-options again, I don't have time to fix the fallout from the resulting behavior change at the moment.

@AnthonyLatsis
Copy link
Collaborator

I see, thanks for clarifying.

Copy link
Contributor

@edymtt edymtt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for this improvement! Looking forward to adopt it in a fix I need for the compiler-rt logic.

@etcwilde etcwilde merged commit 6ad5c40 into swiftlang:main May 9, 2025
5 checks passed
@etcwilde etcwilde deleted the ewilde/extend-swift-cmake-options branch May 9, 2025 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants