Detect 'cargo check' in build.rs script?

I have a build.rs script that compiles a C library which takes a while. Is there a way that I can detect when the user has invoked cargo check (or cargo doc or anything else that implies cargo check) so that I can avoid compiling the C library? (cargo check doesn’t perform linking, so there’s no need to make object files available)

1 Like

Maybe use a feature flag and conditional compilation in the build script?

https://doc.rust-lang.org/book/first-edition/conditional-compilation.html

Though you will have to use something like cargo check --features "dontcompilelibrary" instead of just cargo check.

1 Like

Yeah, I’m primarily concerned about this for people other than myself who use my library (or use a library which uses my library, or…) who wouldn’t know to do that.

Could incremental compilation be used to cache the C library?

Luckily, yes. This is only an issue for the first run.