-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
cargo:rustc-link-arg-tests
not available to unit/module tests
#10937
Comments
I'm not sure, but I think this was just an oversight. My original intent mentioned in #8441 (comment) was that it applied to unittests as well:
However, I think in #10274 that was an oversight. Implementation-wise, I think the check |
If the "equivalent to --tests" means the equivalence of target selection, it implies
I am not sure if that's really what we want. Also, what should cargo do when both
Another thing I am uncertain is if a user invoke We've got some small details to determine. I feel like the most important question is "what is counted as a unit test?" in this feature. A simple solution could be adding link arg from |
Here are my thoughts on the points you brought up, but I must admit to knowing very little of the
My wording there wasn't very precise, but I'm not sure what it should be. What it probably comes down to is how many linker invocations are there per target "kind", and does To illustrate, say we have a project with the files The problem is we have two dimensions we're trying to treat as one. We have (what I'm calling) the "target kind" ( However, in test mode, when
I'm not sure where Does this feel directionally correct? |
Thank your for providing a solution on this topic! Most of your understanding is correct. Each test target, either unit test or integration test results in a linker invocation. You proposal is also straightforward IMO, though just feels like a bit like feature bloat (not your fault, just cargo provides so many options). This #8441 (comment) proposes
which is similar to your solution, and somehow may resolve your original use case. There are also syntaxes to specify the name of a target, although they have yet been implemented so far. I couldn't think a way to avoid adding such an amount of new instructions 😞. |
Another option to keep it simple (until proven to be insufficient in the wild):
If down the road we need to differentiate between |
I bumped into this today. I have some embedded 'bin' code which uses a custom linker script to collect some static items tagged with a linker section so they appear next to each other in the resulting binary, the linker script also exposes start/end addresses so that the entire set can be accessed from rust code. It all works fine, but then I went to write so unit tests and ran into the error below.
build.rs snippet: println!("cargo:rustc-link-arg-tests=-Tlink/registry.ld"); Here's my use-case so you can see why I need this: linker script snippet:
extern "C" {
pub static mut _registry_items_section_vma_start: u32;
pub static mut _registry_items_section_vma_end: u32;
}
...
let first: *const RegistryItem = &_registry_items_section_vma_start as *const u32 as _;
let last: *const RegistryItem = &_registry_items_section_vma_end as *const u32 as _;
let item_count: usize = last.offset_from(first) as _;
Registry {
items: slice::from_raw_parts(first, item_count),
...
} and the codebase has many RegistryItems like this, scattered over many source files.
The idea is that the list of things is built at link-time and there is no source dependency between the 'things', or the Registry implementation itself. To recap - my goal is add to the linker script used by unit tests so that my '.registry.items.*' appear in a contiguous block of memory instead of where they would usually be placed. |
I think we are also running into this issue, see nabijaczleweli/rust-embed-resource#69. The only way I've managed to get linker flags to get passed through to the unit test binaries that I'd also like to request a small doc update to Thanks! |
Problem
According to The Cargo Book, the
build.rs
file can emit thecargo:rustc-link-arg-tests=FLAG
setting to have it applied passed torustc
as a-C link-arg=FLAG
but only when building a "tests target". From the documentation it's not immediately clear that this only works with integration-style tests intests/
. Furthermore, there's no indication of how special link args might be specified for unit tests (this is particularly relevant for running tests for PyO3 extension modules on MacOS).Also, if there are no tests
tests/
, thecargo test
comands fails with the message:Steps
1. Create a project with the following files:
Cargo.toml
:build.rs
:src/lib.rs
:2. Run tests
cargo test
(observe cargo "invalid instruction" error)
3. Add an integration test
tests/test_foo.rs
:4. Run tests
cargo test
(observe no cargo error)
5. Observe application of linker arguments
(observe linker arg only applied to
.../build-script-link-args-for-tests/target/debug/deps/test_foo-...
)Possible Solution(s)
a. Allow
cargo:rustc-link-arg-tests
to apply to unit (in-module) tests, or create a new flag specific to unit tests. 😄b. Update documentation to indicate unit tests aren't supported by this feature. 😞
Notes
May also apply to
#[bench]
functions.Version
The text was updated successfully, but these errors were encountered: