Add option to add link libraries at build-time (via response file)
Problem background
I’m currently maintaining Corrosion which integrates Rusts cargo build system into CMake.
One feature of Rust, is that libraries can be added to the link line via build.rs
build-scripts,
or directly from the code via #[link = "mylib"]
.
When linking a Rust static library into C/C++, you of course want to pass these libraries onto the link line, since otherwise linking will fail.
Corrosion currently builds a minimal demo project at configure-time, to figure out the libraries which are always needed (by the Rust standard library), which works well enough for many cases.
However if User Rust code (or a transitive rust dependency) links to any other library like this, then Corrosion / CMake won't know about it and linking the C/C++ executable will fail.
The Rust compiler provides --print native-static-libs
option, which print a machine-parsable list of linker options - at build-time.
In a previous Discourse thread, Ben suggested parsing and writing the results to a response file.
The problem then is, how do I place the response file, at the correct position on the linker line as a library/module author (I can't just modify CMAKE_CXX_LINK_EXECUTABLE
).
What doesn't work
- I can't place the response file via
target_link_libraries()
, because of the leading@
-
target_link_options
also doesn't really work reliably, since for most linkers the position on the link line is important - Setting
INTERFACE_LINK_LIBRARIES
doesn't seem to work if the path to the rsp file contains spaces (and as a library author I can't control where the build directory is). - MSVC doesn't seem to accept the rspfile via any of the above ways (but that is probably out of scope for this issue)
Proposed Solution
Add an official way to add libraries to the linker line at build-time, via a response file, ideally as an option of target_link_libraries
.