cmake: handle builds that use static toolchain libs #1256
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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_INITThis is a kind of hacky way to handle this; I suspect once bazelbuild/bazel#23204 lands, it will be possible to do this better.