Skip to content
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

cmake: handle builds that use static toolchain libs #1256

Merged
merged 2 commits into from
Aug 12, 2024

Conversation

novas0x2a
Copy link
Contributor

@novas0x2a novas0x2a commented Aug 10, 2024

The problem being solved here is: if a toolchain wants to link the toolchain libs statically, there are some flags that need to be passed. Unfortunately, static linking is notoriously order-sensitive (if an object needs a symbol, it can only be resolved by libraries later than it on the command line). This means there are scenarios where
this works:
gcc thing.o -o stuff -l:libstdc++.a
this fails with missing symbols (like std::cout):
gcc -l:libstdc++.a -o stuff thing.o

In other words, we need these flags to be in <LINK_LIBRARIES> and not just <LINK_FLAGS>, so they fall after the <OBJECTS> that might need them and that is what this code does, by injecting these indicative flags into CMAKE_CXX_STANDARD_LIBRARIES_INIT

This is a kind of hacky way to handle this; I suspect once bazelbuild/bazel#23204 lands, it will be possible to do this better.

The problem being solved here is: if a toolchain wants to link the
toolchain libs statically, there are some flags that need to be passed.
Unfortunately, static linking is notoriously order-sensitive (if an
object needs a symbol, it can only be resolved by libraries _later_ than
it on the command line). This means there are scenarios where:
this works:
  gcc thing.o -o stuff -l:libstdc++.a
this fails with missing symbols (like std::cout):
  gcc -l:libstdc++.a -o stuff thing.o

In other words, we need these flags to be in "<LINK_LIBRARIES>" and not
just "<LINK_FLAGS>", so they fall after the "<OBJECTS>" that might need
them and that is what this code does, by injecting these indicative flags
into CMAKE_CXX_STANDARD_LIBRARIES_INIT
@jsharpe jsharpe enabled auto-merge (squash) August 12, 2024 21:10
@jsharpe jsharpe merged commit 3ffc4bd into bazel-contrib:main Aug 12, 2024
2 checks passed
jsharpe pushed a commit to jsharpe/rules_foreign_cc that referenced this pull request Aug 23, 2024
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