Skip to content

Commit 8b9eaa5

Browse files
committed
chore: Merge build-rs into Cargo
2 parents fbc8ba8 + 0c7577d commit 8b9eaa5

File tree

10 files changed

+1230
-2
lines changed

10 files changed

+1230
-2
lines changed

Cargo.lock

Lines changed: 16 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "build-rs-test-lib"
3+
version = "0.2.0"
4+
edition = "2021"
5+
rust-version = "1.76"
6+
publish = false
7+
8+
[features]
9+
unstable = ["build-rs/unstable"]
10+
11+
[build-dependencies]
12+
build-rs = { path = "../build-rs" }

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

Lines changed: 78 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "build-rs"
3+
version = "0.2.0"
4+
edition = "2021"
5+
rust-version = "1.76"
6+
publish = false
7+
8+
[dependencies]
9+
unicode-ident = "1.0.13"
10+
11+
[features]
12+
13+
## Experimental API. This feature flag is **NOT** semver stable.
14+
unstable = []

crates/build-rs/src/allow_use.rs

Lines changed: 46 additions & 0 deletions
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

Lines changed: 27 additions & 0 deletions
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)