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

Draft: Propagate rustc native-static-libs to CMake #472

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from

Conversation

jschwe
Copy link
Collaborator

@jschwe jschwe commented Dec 29, 2023

CMake usually requires all libraries required for linking to be
specified at CMake configure time. However, in Rust native dependencies
may be specified inside the source code via the link attribute.
rustc can be instructed to print all required native libraries at
build time, however this is already after the CMake configure stage
has finished. To work around this we wrap the build command for Rust
static libraries in a wrapper script, that parses the required libraries
from the rustc output and writes them into an rsp file.
At configure time we can then instruct CMake to read the linker
arguments from the file that our wrapper will generate later at build
time.

Closes #335

@jschwe jschwe changed the title Propagate rustc native-static-libs to CMake Draft: Propagate rustc native-static-libs to CMake Dec 29, 2023
@jschwe jschwe marked this pull request as draft December 29, 2023 15:18
@jschwe jschwe force-pushed the dynamic_link_libs branch 3 times, most recently from 94a9196 to b6b4ad6 Compare December 31, 2023 18:13
@jschwe
Copy link
Collaborator Author

jschwe commented Jan 8, 2024

status: works locally on an M1 mac and x86 fedora 39, but on CI ld is behaving weirdly and not finding libraries that are definitely there. Currently don't have time to investigate.

Update: Well, I guess debugging this issue would have been much faster if I had thought of looking at the raw github logs instead of the default rendered version. Turns out that in CI the github action for installing rust, set an environment variable for --color=always, which causes an ansi escape code to be in the line containing the link libraries. Adding --color=never solves this, but potentially the regex could also be hardened to strip trailing ansi escape codes.

Now the remaning issues are:

  • MSVC / link.exe do not seem to accept the rsp file
  • the order of linker libraries is important for some linkers so using target_link_options may not work if the linker just scans left to right in one pass. Manually setting INTERFACE_LINK_LIBRARIES might be an option, but there seem to be problems if the path to the rsp file contains spaces when using this solution.

Overall it all seems a bit fragile, so most likely this will only be an optional feature.

@jschwe jschwe force-pushed the dynamic_link_libs branch 10 times, most recently from 8a1e42f to 0e6b78f Compare February 18, 2024 21:17
CMake usually requires all libraries required for linking to be
specified at CMake configure time. However, in Rust native dependencies
may be specified inside the source code via the [`link`] attribute.
`rustc` can be instructed to print all required native libraries at
build time, however this is already after the CMake configure stage
has finished. To work around this we wrap the build command for Rust
static libraries in a wrapper script, that parses the required libraries
from the `rustc` output and writes them into an `rsp` file.
At configure time we can then instruct CMake to read the linker
arguments from the file that our wrapper will generate later at build
time.

[`link`]: https://doc.rust-lang.org/reference/items/external-blocks.html#the-link-attribute
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.

Parse the native staticlib dependencies output from rustc
1 participant