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

Extend rustc-link-args to support individual examples/tests/benches. #11801

Open
mibu138 opened this issue Mar 5, 2023 · 0 comments
Open

Extend rustc-link-args to support individual examples/tests/benches. #11801

mibu138 opened this issue Mar 5, 2023 · 0 comments
Labels
A-build-scripts Area: build.rs scripts A-linkage Area: linker issues, dylib, cdylib, shared libraries, so C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`

Comments

@mibu138
Copy link

mibu138 commented Mar 5, 2023

Problem

Cargo does not appear to support passing extra link arguments on a per example basis. This poses a problem for my project which requires a specialized extra linking step to occur for any executable that uses my library.

I saw that #9557 added support for passing extra link flags via a build script that allows some level of customization at the target level. However, while you can pass extra link flags to all your examples via cargo:rustc:link-arg-examples=FLAG, you cannot pass extra flags to a specific, single example.

This is supported for package binaries via cargo:rustc-link-arg-bin=BIN=FLAG in a build script, which is the workaround I'm using currently, but it would be very helpful if this specification syntax was extended to examples, and probably the rest of target types as well (benches, tests,... is that all?)

The reason why just using package binaries in place of examples is a problem is that I cannot then build with dev.dependencies, which would be quite useful since these binaries very much are examples: they are meant to demonstrate how to use the library in a broader context, which will likely include external crates. Demonstrating usage of my library through examples is all the more important given the relatively unorthodox extra linking step that is required.

Proposed Solution

Essentially just complete what appears to already be considered future work in this department. Namely, add build commands of the form

cargo:rustc-link-arg-example=NAME=…
cargo:rustc-link-arg-test=NAME=…
cargo:rustc-link-arg-bench=NAME=…

I

Notes

If you are curious about why I need this extra linking step: I perform some code generation based on artifacts that may or may not be accompanying a binary that uses my package. That generated code gets compiled and then needs to be linked into the final executable. The code generation is done by another binary that gets included in my package, and is conceptually just part of using the package.

@mibu138 mibu138 added the C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` label Mar 5, 2023
@ehuss ehuss added A-linkage Area: linker issues, dylib, cdylib, shared libraries, so A-build-scripts Area: build.rs scripts labels Mar 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-build-scripts Area: build.rs scripts A-linkage Area: linker issues, dylib, cdylib, shared libraries, so C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`
Projects
None yet
Development

No branches or pull requests

2 participants