rust-impl-build-scripts
rust-impl-build-scripts
The Cargo build script skill: how to write a build.rs that runs before the package compiles, generates code into OUT_DIR, links native libraries, sets cfg flags and environment variables for the crate, and triggers correct incremental rebuilds via rerun-if-changed / rerun-if-env-changed directives.
Cross-references: [[rust-impl-cargo-project]] (where [build-dependencies] and the links key live in Cargo.toml), [[rust-impl-ffi-bindgen]] (running bindgen inside build.rs to wrap a C library), [[rust-impl-cross-compile]] (HOST vs TARGET, CARGO_CFG_* for cross builds).
When to use this skill
- User adds a
build.rsto a crate (compiling C, generating code, detecting features, embedding git commit). - User asks why their build script seems "cached" or never re-runs after editing a file.
- User links a system library (
-lfoo) and asks where to put the directive. - User writes generated Rust code and asks where to put it so
cargo publishandcargo cleanstill work. - User wants
#[cfg(has_feature_x)]driven by runtime detection at build time. - User runs
bindgento wrap a C header and asks how to wire it into the build. - User sees
cargo:warning=in build output and asks how to emit one.
For toolchain setup, [build-dependencies] syntax, the links manifest key, and package.links semantics see [[rust-impl-cargo-project]]. For the full FFI / bindgen wrapping flow see [[rust-impl-ffi-bindgen]]. For cross-compilation specifics in build.rs (HOST vs TARGET, CARGO_CFG_TARGET_*) see [[rust-impl-cross-compile]].