How can I get cargo args in build.rs?

e.g
cargo test
cargo run
cargo clippy

If I don't pass custom parameters to cargo, how to get test/run/clippy in build.rs?

I have tried

for (key, value) in std::env::vars() {
    eprintln!("{key}: {value}");
}

in my build.rs, but can't find any env valued test/run/clippy.

I don't think you can, especially because cargo generally won't invoke your build script separately for each command. (Clippy does cause a rerun though because it adds a RUSTC_WORKSPACE_WRAPPER.)

As asked, this isn't possible; a build script's input is the project itself, not the specific Cargo command used against the program.

In the spirit of helping you find a workable alternative, what would you want your build.rs to do differently depending on the presence or absence of a test/run/clippy marker?

2 Likes

My crate uses hook technology, which requires Rust to be able to link .dylib/.so/.dll. Unlike other projects, dynamic link libraries are also compiled from Rust code(I built GitHub - loongs-zhang/link-example to show what I'm trying to do), the link can not works in ${arch}-pc-windows-msvc(the rest works fine). BTW, unfortunately, the problem was not reproduced in the minimal example.

There is a solution that can solve the problem of link failure, but it needs to recognize the cargo command parameters. If not recognized, there is no problem with cargo run/cargo test, but the other commands like cargo clippy will be blocked.

Sorry for providing the original complex project GitHub - loongs-zhang/open-coroutine-20240723 at dev-windows-msvc , you just need to focus on open-coroutine-20240723/open-coroutine/build.rs.

The CI(add CI · loongs-zhang/open-coroutine-20240723@b803a2a · GitHub) failed due to link problem.

If there is any other information you need to know, please let me know, thanks a lot for helping me.

Hi, may I ask if there is any progress?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.