Skip to content

Commit e5ce5e5

Browse files
authored
Migrate build-rs to the Cargo repo (#14786)
### What does this PR try to resolve? Fixes #12432 ### How should we test and review this PR? This pulls in https://github.com/cad97/build-rs at eb389d1 with the following changes: - `Cargo.toml` metadata - Removal of `.github`, `.gitignore`, `Cargo.lock` We'll need to integrate `test-lib` into our processes but that seemed more invasive, so I wanted to leave that for a future PR. ### Additional information Infra changes are being coordinated in https://rust-lang.zulipchat.com/#narrow/channel/242791-t-infra/topic/Transfering.20.60build-rs.60.20crate.20to.20rust-lang/near/480779960 Context: per [Cargo's charter](https://doc.crates.io/contrib/team.html#decision-process), we approved this transfer in an [FCP](#12432 (comment)).
2 parents 2e7fc43 + 9814045 commit e5ce5e5

File tree

14 files changed

+1242
-2
lines changed

14 files changed

+1242
-2
lines changed

Cargo.lock

+16-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ anstyle = "1.0.8"
2424
anyhow = "1.0.86"
2525
base64 = "0.22.1"
2626
blake3 = "1.5.2"
27+
build-rs = { version = "0.2.0", path = "crates/build-rs" }
2728
bytesize = "1.3"
2829
cargo = { path = "" }
2930
cargo-credential = { version = "0.4.2", path = "credential/cargo-credential" }
@@ -107,6 +108,7 @@ tracing = { version = "0.1.40", default-features = false, features = ["std"] } #
107108
tracing-chrome = "0.7.2"
108109
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
109110
unicase = "2.7.0"
111+
unicode-ident = "1.0.13"
110112
unicode-width = "0.2.0"
111113
unicode-xid = "0.2.4"
112114
url = "2.5.2"

crates/build-rs-test-lib/Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "build-rs-test-lib"
3+
version = "0.0.0"
4+
edition.workspace = true
5+
publish = false
6+
7+
[features]
8+
unstable = ["build-rs/unstable"]
9+
10+
[build-dependencies]
11+
build-rs.workspace = true

crates/build-rs-test-lib/build.rs

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
fn main() {
2+
smoke_test_inputs();
3+
4+
build_rs::output::rerun_if_changed("build.rs");
5+
build_rs::output::rustc_check_cfgs(&["did_run_build_script"]);
6+
build_rs::output::rustc_cfg("did_run_build_script");
7+
}
8+
9+
fn smoke_test_inputs() {
10+
use build_rs::input::*;
11+
dbg!(cargo());
12+
dbg!(cargo_cfg("careful"));
13+
dbg!(cargo_cfg_debug_assertions());
14+
#[cfg(feature = "unstable")]
15+
dbg!(cargo_cfg_fmt_debug());
16+
#[cfg(feature = "unstable")]
17+
dbg!(cargo_cfg_overflow_checks());
18+
dbg!(cargo_cfg_panic());
19+
dbg!(cargo_cfg_proc_macro());
20+
#[cfg(feature = "unstable")]
21+
dbg!(cargo_cfg_relocation_model());
22+
#[cfg(feature = "unstable")]
23+
dbg!(cargo_cfg_sanitize());
24+
#[cfg(feature = "unstable")]
25+
dbg!(cargo_cfg_sanitizer_cfi_generalize_pointers());
26+
#[cfg(feature = "unstable")]
27+
dbg!(cargo_cfg_sanitizer_cfi_normalize_integers());
28+
dbg!(cargo_cfg_target_abi());
29+
dbg!(cargo_cfg_target_arch());
30+
dbg!(cargo_cfg_target_endian());
31+
dbg!(cargo_cfg_target_env());
32+
dbg!(cargo_cfg_target_feature());
33+
dbg!(cargo_cfg_target_has_atomic());
34+
#[cfg(feature = "unstable")]
35+
dbg!(cargo_cfg_target_has_atomic_equal_alignment());
36+
#[cfg(feature = "unstable")]
37+
dbg!(cargo_cfg_target_has_atomic_load_store());
38+
dbg!(cargo_cfg_target_os());
39+
dbg!(cargo_cfg_target_pointer_width());
40+
#[cfg(feature = "unstable")]
41+
dbg!(cargo_cfg_target_thread_local());
42+
dbg!(cargo_cfg_target_vendor());
43+
#[cfg(feature = "unstable")]
44+
dbg!(cargo_cfg_ub_checks());
45+
dbg!(cargo_cfg_unix());
46+
dbg!(cargo_cfg_windows());
47+
dbg!(cargo_encoded_rustflags());
48+
dbg!(cargo_feature("unstable"));
49+
dbg!(cargo_manifest_dir());
50+
dbg!(cargo_manifest_links());
51+
dbg!(cargo_pkg_authors());
52+
dbg!(cargo_pkg_description());
53+
dbg!(cargo_pkg_homepage());
54+
dbg!(cargo_pkg_license());
55+
dbg!(cargo_pkg_license_file());
56+
dbg!(cargo_pkg_name());
57+
dbg!(cargo_pkg_readme());
58+
dbg!(cargo_pkg_repository());
59+
dbg!(cargo_pkg_rust_version());
60+
dbg!(cargo_pkg_version());
61+
dbg!(cargo_pkg_version_major());
62+
dbg!(cargo_pkg_version_minor());
63+
dbg!(cargo_pkg_version_patch());
64+
dbg!(cargo_pkg_version_pre());
65+
dbg!(debug());
66+
dbg!(dep_metadata("z", "include"));
67+
dbg!(host());
68+
dbg!(num_jobs());
69+
dbg!(opt_level());
70+
dbg!(out_dir());
71+
dbg!(profile());
72+
dbg!(rustc());
73+
dbg!(rustc_linker());
74+
dbg!(rustc_workspace_wrapper());
75+
dbg!(rustc_wrapper());
76+
dbg!(rustdoc());
77+
dbg!(target());
78+
}

crates/build-rs-test-lib/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[test]
2+
fn test() {
3+
assert!(cfg!(did_run_build_script));
4+
}

crates/build-rs/Cargo.toml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[package]
2+
name = "build-rs"
3+
version = "0.2.0"
4+
rust-version.workspace = true
5+
edition.workspace = true
6+
license.workspace = true
7+
homepage.workspace = true
8+
repository.workspace = true
9+
description = "API for writing Cargo `build.rs` files"
10+
11+
[features]
12+
## Experimental API. This feature flag is **NOT** semver stable.
13+
unstable = []
14+
15+
[dependencies]
16+
unicode-ident.workspace = true

crates/build-rs/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
> This crate is maintained by the Cargo team for use by the wider
2+
> ecosystem. This crate follows semver compatibility for its APIs.

crates/build-rs/src/allow_use.rs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use std::{process::Command, sync::OnceLock};
2+
3+
fn rust_version_minor() -> u32 {
4+
static VERSION_MINOR: OnceLock<u32> = OnceLock::new();
5+
*VERSION_MINOR.get_or_init(|| {
6+
crate::input::cargo_pkg_rust_version()
7+
.split('.')
8+
.nth(1)
9+
// assume build-rs's MSRV if none specified for the current package
10+
.unwrap_or(env!("CARGO_PKG_RUST_VERSION").split('.').nth(1).unwrap())
11+
.parse()
12+
.unwrap()
13+
})
14+
}
15+
16+
fn cargo_version_minor() -> u32 {
17+
static VERSION_MINOR: OnceLock<u32> = OnceLock::new();
18+
*VERSION_MINOR.get_or_init(|| {
19+
let out = Command::new(crate::input::cargo())
20+
.arg("-V")
21+
.output()
22+
.expect("running `cargo -V` should succeed");
23+
assert!(out.status.success(), "running `cargo -V` should succeed");
24+
25+
// > cargo -V # example output
26+
// cargo 1.82.0 (8f40fc59f 2024-08-21)
27+
28+
String::from_utf8(out.stdout).expect("`cargo -V` should output valid UTF-8")
29+
["cargo 1.".len()..]
30+
.split('.')
31+
.next()
32+
.expect("`cargo -V` format should be stable")
33+
.parse()
34+
.unwrap()
35+
})
36+
}
37+
38+
pub(crate) fn double_colon_directives() -> bool {
39+
// cargo errors on `cargo::` directives with insufficient package.rust-version
40+
rust_version_minor() >= 77
41+
}
42+
43+
pub(crate) fn check_cfg() -> bool {
44+
// emit check-cfg if the toolchain being used supports it
45+
cargo_version_minor() >= 80
46+
}

crates/build-rs/src/ident.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use unicode_ident::{is_xid_continue, is_xid_start};
2+
3+
pub(crate) fn is_feature_name(s: &str) -> bool {
4+
s.chars()
5+
.all(|ch| is_xid_continue(ch) || matches!(ch, '-' | '+' | '.'))
6+
}
7+
8+
pub(crate) fn is_ident(s: &str) -> bool {
9+
let mut cs = s.chars();
10+
cs.next()
11+
.is_some_and(|ch| is_xid_start(ch) || matches!(ch, '_'))
12+
&& cs.all(is_xid_continue)
13+
}
14+
15+
pub(crate) fn is_ascii_ident(s: &str) -> bool {
16+
let mut cs = s.chars();
17+
cs.next()
18+
.is_some_and(|ch| ch.is_ascii_alphabetic() || matches!(ch, '_'))
19+
&& cs.all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '_'))
20+
}
21+
22+
pub(crate) fn is_crate_name(s: &str) -> bool {
23+
let mut cs = s.chars();
24+
cs.next()
25+
.is_some_and(|ch| is_xid_start(ch) || matches!(ch, '-' | '_'))
26+
&& cs.all(|ch| is_xid_continue(ch) || matches!(ch, '-'))
27+
}

0 commit comments

Comments
 (0)