Build-time detection of linked libraries

A recent rustc linking change has exposed that some crates link to libraries that can't be put in the crate's manifest links because the linked libraries aren't known until build-time, and that can cause build errors when other crates attempt to link against the same library.

In the case of llvm-sys, libffi is sometimes pulled in because LLVM can be configured to use libffi itself, but this is impossible to know until build time since it's an optional dependency of LLVM. I imagine there may be other C library bindings that may exhibit similar behavior, but am not aware of any right now.

It seems like handling this correctly would require allowing build scripts to emit a build-time equivalent of the links field in the crate manifest: is that a feasible thing to support? Or is there an alternative way to handle this sort of situation that I'm missing?

Isn’t it fine to have multiple crates emit rustc-link-lib for the same library? AFAIK the links key is just a mitigation against having multiple -sys libraries attempting to manage the global state of the C library. (So not having this feature allows builds to succeed and run into runtime issues, not fail to compile).

1 Like