I’m currently maintaining Corrosion which integrates Rusts cargo build system into CMake.
When linking Rust static libraries into C/C++ code (managed by CMake), we also need to link some system libraries (e.g.
pthread). Currently, the required libraries are hardcoded in corrosion, based on what is usually required for the target OS. Ideally though, we would like to ask
rustc which libraries we need to link.
rustc provides an option
--print native-static-libs which prints a list of required libraries.
This list is produced during the actual build of the static library. Using a dummy demo project to get a list of libraries is also not sufficient in all cases, since
rustc will actually take all dependencies into consideration and any
#[link] attributes in the source code, which may specify libraries to link against. Build-scripts may also add additional libraries to the list of libraries that should be linked.
Is there any way that we could respect this list of required libraries, generated at build-time, and add them to the target CMake C/C++ executable via
target_link_libraries()? From my understanding currently there is no way to do this in CMake, unless we would prebuild the Rust project at configure time.