Skip to content

utils: Pass linker flags to the Swift flags#88758

Open
Steelskin wants to merge 1 commit into
swiftlang:mainfrom
Steelskin:fabrice/cmake-linker-flags-unification
Open

utils: Pass linker flags to the Swift flags#88758
Steelskin wants to merge 1 commit into
swiftlang:mainfrom
Steelskin:fabrice/cmake-linker-flags-unification

Conversation

@Steelskin

Copy link
Copy Markdown
Contributor
  • Explanation:

Starting with CMake 3.30+, CMAKE_SHARED_LINKER_FLAGS are passed to Swift targets. This is an undocumented behavior change from CMake 3.29. However, the same does not apply to CMAKE_EXE_LINKER_FLAGS, which are only supported with CMake 4.4+ and CMP0214.

In order to work around these issues and keep the handling of linker arguments consistent, this changes build.ps1 to also pass linker flags to the Swift flags, maintaining consistency. At worst, this results in duplicate linker arguments, which is not a problem in practice.

  • Scope:

This should be a no-op when building with CMake 3.29, which is what Swift CI uses. This fixes some build issues with CMake 3.30+.

  • Risk:

No-op with CMake 3.29 and Swift CI. Should fix the last remaining issues with CMake 3.30+.

  • Testing:

Local testing with CMake 3.29, 3.30 and 4.0 + CI.

@Steelskin Steelskin requested a review from a team as a code owner April 30, 2026 15:49
@Steelskin

Copy link
Copy Markdown
Contributor Author

@swift-ci please test windows platform

@Steelskin

Copy link
Copy Markdown
Contributor Author

@swift-ci please build toolchain windows platform

@finagolfin

Copy link
Copy Markdown
Member

We've been working around this for a while for Android too: do you know if the CMake devs are going to fix this? It doesn't make sense to pass in arbitrary linker flags to all languages' compilers, as they do with Swift.

I know @compnerd filed an issue for it back then, but not sure what happened after.

@Steelskin

Copy link
Copy Markdown
Contributor Author

We've been working around this for a while for Android too: do you know if the CMake devs are going to fix this? It doesn't make sense to pass in arbitrary linker flags to all languages' compilers, as they do with Swift.

There was a behavior change in CMake 3.30, where CMake started passing CMAKE_SHARED_LINKER_FLAGS for Swift targets too. This should have been put behind a policy but it was missed and ended up shipping. It is too late to undo that change now so instead, CMake 4.4 and newer will also pass CMAKE_EXE_LINKER_FLAGS with CMP0214 set to new.

Regarding Android, the NDK ends up setting linker flags "the wrong way". You can take a look at the current state of build.ps1 to see how we have worked around this issue, specifically here, as well as this file. The NDK CMake files should be fixed upstream to properly leverage CMP0181 (CMake 4.0), when available.

Going forward, for Android, I would suggest enabling the following:

  • CMP0157 (CMake 3.29): Enable Swift split builds
  • CMP0181 (CMake 4.0): Parse the LINKER: prefix for CMAKE_*_LINKER_FLAGS
  • CMP0214 (CMake 4.4): Honor CMAKE_EXE_LINKER_FLAGS for Swift
  • CMP0215 (CMake 4.4): Emit Swift modules separately from compilation
  • CMP0216 (CMake 4.4): Swift targets have a default project name

For Android, you will also want some version of the workaround I mentioned above. I believe that once CMake 4.4 is released and the NDK files are updated to leverage CMP0181, we should be in a much better state.

@finagolfin

Copy link
Copy Markdown
Member

Thanks for the detailed writeup. 👍

@etcwilde, are there any plans to bump the CMake version in these Swift repos to use those new Swift features, as Fabrice recently proposed in #88609 and you were pushing for in the past?

Starting with CMake 3.30+, CMAKE_SHARED_LINKER_FLAGS are passed to Swift
targets. This is an undocumented behavior change from CMake 3.29.
However, the same does not apply to CMAKE_EXE_LINKER_FLAGS, which are
only supported with CMake 4.4+ and CMP0214.

In order to work around these issues and keep the handling of linker
arguments consistent, this changes build.ps1 to also pass linker flags
to the Swift flags, maintaining consistency. At worst, this results in
duplicate linker arguments, which is not a problem in practice.

This is a no-op when building with CMake 3.29.
@Steelskin Steelskin force-pushed the fabrice/cmake-linker-flags-unification branch from d7634b9 to 89fb25a Compare May 27, 2026 10:47
@Steelskin Steelskin requested a review from compnerd as a code owner May 27, 2026 10:47
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.

2 participants