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?
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.