Skip to content

doc: Update build script examples to use build-rs#16592

Open
hksama wants to merge 1 commit intorust-lang:masterfrom
hksama:build-script-docs
Open

doc: Update build script examples to use build-rs#16592
hksama wants to merge 1 commit intorust-lang:masterfrom
hksama:build-script-docs

Conversation

@hksama
Copy link

@hksama hksama commented Feb 5, 2026

Title: doc: Update build script examples to use build-rs
Description:
Updates the build script documentation to recommend the build-rs crate, as discussed in #15567.
Changes

Added a Note recommending build-rs for type safety in build-scripts.md.

Updated examples in build-script-examples.md to use build_rs::output::... functions instead of raw println! macros.

Preserved raw protocol strings in section headers for specification reference.

Relates to #15567.
Ready for FCP review.

@rustbot rustbot added A-documenting-cargo-itself Area: Cargo's documentation S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 5, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 5, 2026

r? @epage

rustbot has assigned @epage.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

"
).unwrap();
println!("cargo::rerun-if-changed=build.rs");
build_rs::output::rerun_if_changed("build.rs");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple lines above OUT_DIR is read and could instead use out_dir()

edition = "2024"

[build-dependencies]
build-rs= "0.3.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
build-rs= "0.3.0"
build-rs = "0.3.0"

edition = "2024"

[build-dependencies]
build-rs= "0.3.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this being in the docs, how are we going to keep it up-to-date?

println!("cargo::rustc-link-search=native={}", out_dir);
println!("cargo::rustc-link-lib=static=hello");
println!("cargo::rerun-if-changed=src/hello.c");
build_rs::output::rustc_link_search(out_dir);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be rustc_link_search_kind?

```toml
[build-dependencies]
cc = "1.0"
build-rs= "0.3.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
build-rs= "0.3.0"
build-rs = "0.3.0"

println!("cargo::rustc-link-search=native={}", out_dir);
println!("cargo::rustc-link-lib=static=hello");
println!("cargo::rerun-if-changed=src/hello.c");
build_rs::output::rustc_link_search(out_dir);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OUT_DIR again

}
cfg.compile("z_user");
println!("cargo::rerun-if-changed=src/z_user.c");
build_rs::output::rerun_if_changed("src/z_user.c");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we use dep_metadata for DEP_Z_INCLUDE?

Comment on lines +477 to +478
build_rs::output::rustc_check_cfgs(&["ossl101","ossl102",]);
build_rs::output::rustc_check_cfgs(&["ossl110","ossl110g","ossl111",]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please clean up the code style

For historical reasons, the `cargo::rustc-cdylib-link-arg` form is an alias
for `cargo::rustc-link-arg-cdylib`, and has the same meaning.

**Recommended:** Use [`build_rs::output::rustc_cdylib_link_arg`](https://docs.rs/build-rs/0.3.3/build_rs/output/fn.rustc_cdylib_link_arg.html) from the `build-rs` crate.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to recommend this under every entry

Comment on lines +27 to +34
> **Note**
> Build scripts communicate with Cargo by printing specially formatted lines to stdout.
> While you can print these lines manually (e.g., `println!("cargo::rerun-if-changed=build.rs")`),
> we recommend using the [`build-rs`](https://crates.io/crates/build-rs) crate.
> It provides a type-safe API that ensures your build instructions are formatted correctly.
>
> The examples in this reference guide use `build-rs` to demonstrate best practices,
> while the section headers preserve the raw protocol strings for specification purposes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is partly covered under " Life Cycle of a Build Script". Seems like adding a note about build-rs being an API for this to that would be cleaner

fn main() {
// Tell Cargo that if the given file changes, to rerun this build script.
println!("cargo::rerun-if-changed=src/hello.c");
//recommended method(using the build_rs crate)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment doesn't quite read correctly to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-documenting-cargo-itself Area: Cargo's documentation S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants