Description
Problem
According to The Cargo Book, the build.rs
file can emit the cargo:rustc-link-arg-cdylib=FLAG
setting to have it passed to rustc as a -C link-arg=FLAG
but only when building a "cdylib library target". From the documentation, its not clear that building and running the test binaries for unittests in a cdylib crate will also apply -C link-arg=FLAG
.
In my specific case, I am passing (among other linker arguments) rustc-link-arg-cdylib=/INTEGRITYCHECK
to a cdylib crate via build.rs
. Doing so also passes it to the unit tests, so it makes it impossible to execute my unit tests.
This is sort of the opposite problem of #10937. In that issue, they want cargo:rustc-link-arg-tests=FLAG
to apply to unittests. In this issue, I want all the other rustc-link-arg
variants to not apply to unit tests (or at least have some option that lets me do this).
Steps
- Create project with files:
Cargo.toml
:
[package]
name = "rustc-link-arg-bug"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
build.rs
fn main() {
println!("cargo:rustc-link-arg-cdylib=/INTEGRITYCHECK");
}
src/lib.rs
:
empty file
- Run Cargo Test:
cargo test
Finished test [unoptimized + debuginfo] target(s) in 0.02s
Running unittests src\lib.rs (target\debug\deps\rustc_link_arg_bug-d9c1a47e9d03aa01.exe)
error: test failed, to rerun pass `--lib`
Caused by:
could not execute process `D:\rustc-link-arg-bug\target\debug\deps\rustc_link_arg_bug-d9c1a47e9d03aa01.exe` (never executed)
Caused by:
Windows cannot verify the digital signature for this file. A recent hardware or software change might have installed a file that is signed incorrectly or damaged, or that might be malicious software from an unknown source. (os error 577)
cargo test
fails to run because /INTEGRITYCHECK
was passed to the unittest "targets" (Aside: are these unit test binaries considered a separate target in the world of cargo?)
note: /INTEGRITYCHECK
is an msvc-specific linker argument
Possible Solution(s)
I think a way to control whether extra link args are passed or not passed to unit tests would be a good thing. The cargo documentation could also be improved as it pretty loose with the word "tests", and the way it is worded could confuse people (like me) as to what directives apply to tests(including unittests) and what applies only to integration tests(ie. test targets?).
Note: I would be very happy if anyone knows any workaround to this issue that allows me to pass linker arguments only to the cdylib and not to the cdylib's tests.
Notes
No response
Version
cargo 1.74.0-nightly (2fc85d15a 2023-09-09)
release: 1.74.0-nightly
commit-hash: 2fc85d15a542bfb610aff7682073412cf635352f
commit-date: 2023-09-09
host: x86_64-pc-windows-msvc
libgit2: 1.7.1 (sys:0.18.0 vendored)
libcurl: 8.2.1-DEV (sys:0.4.65+curl-8.2.1 vendored ssl:Schannel)
os: Windows 10.0.23541 (Windows 11 Enterprise) [64-bit]