-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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)).
- Loading branch information
Showing
14 changed files
with
1,242 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "build-rs-test-lib" | ||
version = "0.0.0" | ||
edition.workspace = true | ||
publish = false | ||
|
||
[features] | ||
unstable = ["build-rs/unstable"] | ||
|
||
[build-dependencies] | ||
build-rs.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
fn main() { | ||
smoke_test_inputs(); | ||
|
||
build_rs::output::rerun_if_changed("build.rs"); | ||
build_rs::output::rustc_check_cfgs(&["did_run_build_script"]); | ||
build_rs::output::rustc_cfg("did_run_build_script"); | ||
} | ||
|
||
fn smoke_test_inputs() { | ||
use build_rs::input::*; | ||
dbg!(cargo()); | ||
dbg!(cargo_cfg("careful")); | ||
dbg!(cargo_cfg_debug_assertions()); | ||
#[cfg(feature = "unstable")] | ||
dbg!(cargo_cfg_fmt_debug()); | ||
#[cfg(feature = "unstable")] | ||
dbg!(cargo_cfg_overflow_checks()); | ||
dbg!(cargo_cfg_panic()); | ||
dbg!(cargo_cfg_proc_macro()); | ||
#[cfg(feature = "unstable")] | ||
dbg!(cargo_cfg_relocation_model()); | ||
#[cfg(feature = "unstable")] | ||
dbg!(cargo_cfg_sanitize()); | ||
#[cfg(feature = "unstable")] | ||
dbg!(cargo_cfg_sanitizer_cfi_generalize_pointers()); | ||
#[cfg(feature = "unstable")] | ||
dbg!(cargo_cfg_sanitizer_cfi_normalize_integers()); | ||
dbg!(cargo_cfg_target_abi()); | ||
dbg!(cargo_cfg_target_arch()); | ||
dbg!(cargo_cfg_target_endian()); | ||
dbg!(cargo_cfg_target_env()); | ||
dbg!(cargo_cfg_target_feature()); | ||
dbg!(cargo_cfg_target_has_atomic()); | ||
#[cfg(feature = "unstable")] | ||
dbg!(cargo_cfg_target_has_atomic_equal_alignment()); | ||
#[cfg(feature = "unstable")] | ||
dbg!(cargo_cfg_target_has_atomic_load_store()); | ||
dbg!(cargo_cfg_target_os()); | ||
dbg!(cargo_cfg_target_pointer_width()); | ||
#[cfg(feature = "unstable")] | ||
dbg!(cargo_cfg_target_thread_local()); | ||
dbg!(cargo_cfg_target_vendor()); | ||
#[cfg(feature = "unstable")] | ||
dbg!(cargo_cfg_ub_checks()); | ||
dbg!(cargo_cfg_unix()); | ||
dbg!(cargo_cfg_windows()); | ||
dbg!(cargo_encoded_rustflags()); | ||
dbg!(cargo_feature("unstable")); | ||
dbg!(cargo_manifest_dir()); | ||
dbg!(cargo_manifest_links()); | ||
dbg!(cargo_pkg_authors()); | ||
dbg!(cargo_pkg_description()); | ||
dbg!(cargo_pkg_homepage()); | ||
dbg!(cargo_pkg_license()); | ||
dbg!(cargo_pkg_license_file()); | ||
dbg!(cargo_pkg_name()); | ||
dbg!(cargo_pkg_readme()); | ||
dbg!(cargo_pkg_repository()); | ||
dbg!(cargo_pkg_rust_version()); | ||
dbg!(cargo_pkg_version()); | ||
dbg!(cargo_pkg_version_major()); | ||
dbg!(cargo_pkg_version_minor()); | ||
dbg!(cargo_pkg_version_patch()); | ||
dbg!(cargo_pkg_version_pre()); | ||
dbg!(debug()); | ||
dbg!(dep_metadata("z", "include")); | ||
dbg!(host()); | ||
dbg!(num_jobs()); | ||
dbg!(opt_level()); | ||
dbg!(out_dir()); | ||
dbg!(profile()); | ||
dbg!(rustc()); | ||
dbg!(rustc_linker()); | ||
dbg!(rustc_workspace_wrapper()); | ||
dbg!(rustc_wrapper()); | ||
dbg!(rustdoc()); | ||
dbg!(target()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#[test] | ||
fn test() { | ||
assert!(cfg!(did_run_build_script)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "build-rs" | ||
version = "0.2.0" | ||
rust-version.workspace = true | ||
edition.workspace = true | ||
license.workspace = true | ||
homepage.workspace = true | ||
repository.workspace = true | ||
description = "API for writing Cargo `build.rs` files" | ||
|
||
[features] | ||
## Experimental API. This feature flag is **NOT** semver stable. | ||
unstable = [] | ||
|
||
[dependencies] | ||
unicode-ident.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
> This crate is maintained by the Cargo team for use by the wider | ||
> ecosystem. This crate follows semver compatibility for its APIs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::{process::Command, sync::OnceLock}; | ||
|
||
fn rust_version_minor() -> u32 { | ||
static VERSION_MINOR: OnceLock<u32> = OnceLock::new(); | ||
*VERSION_MINOR.get_or_init(|| { | ||
crate::input::cargo_pkg_rust_version() | ||
.split('.') | ||
.nth(1) | ||
// assume build-rs's MSRV if none specified for the current package | ||
.unwrap_or(env!("CARGO_PKG_RUST_VERSION").split('.').nth(1).unwrap()) | ||
.parse() | ||
.unwrap() | ||
}) | ||
} | ||
|
||
fn cargo_version_minor() -> u32 { | ||
static VERSION_MINOR: OnceLock<u32> = OnceLock::new(); | ||
*VERSION_MINOR.get_or_init(|| { | ||
let out = Command::new(crate::input::cargo()) | ||
.arg("-V") | ||
.output() | ||
.expect("running `cargo -V` should succeed"); | ||
assert!(out.status.success(), "running `cargo -V` should succeed"); | ||
|
||
// > cargo -V # example output | ||
// cargo 1.82.0 (8f40fc59f 2024-08-21) | ||
|
||
String::from_utf8(out.stdout).expect("`cargo -V` should output valid UTF-8") | ||
["cargo 1.".len()..] | ||
.split('.') | ||
.next() | ||
.expect("`cargo -V` format should be stable") | ||
.parse() | ||
.unwrap() | ||
}) | ||
} | ||
|
||
pub(crate) fn double_colon_directives() -> bool { | ||
// cargo errors on `cargo::` directives with insufficient package.rust-version | ||
rust_version_minor() >= 77 | ||
} | ||
|
||
pub(crate) fn check_cfg() -> bool { | ||
// emit check-cfg if the toolchain being used supports it | ||
cargo_version_minor() >= 80 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use unicode_ident::{is_xid_continue, is_xid_start}; | ||
|
||
pub(crate) fn is_feature_name(s: &str) -> bool { | ||
s.chars() | ||
.all(|ch| is_xid_continue(ch) || matches!(ch, '-' | '+' | '.')) | ||
} | ||
|
||
pub(crate) fn is_ident(s: &str) -> bool { | ||
let mut cs = s.chars(); | ||
cs.next() | ||
.is_some_and(|ch| is_xid_start(ch) || matches!(ch, '_')) | ||
&& cs.all(is_xid_continue) | ||
} | ||
|
||
pub(crate) fn is_ascii_ident(s: &str) -> bool { | ||
let mut cs = s.chars(); | ||
cs.next() | ||
.is_some_and(|ch| ch.is_ascii_alphabetic() || matches!(ch, '_')) | ||
&& cs.all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '_')) | ||
} | ||
|
||
pub(crate) fn is_crate_name(s: &str) -> bool { | ||
let mut cs = s.chars(); | ||
cs.next() | ||
.is_some_and(|ch| is_xid_start(ch) || matches!(ch, '-' | '_')) | ||
&& cs.all(|ch| is_xid_continue(ch) || matches!(ch, '-')) | ||
} |
Oops, something went wrong.