Skip to content

Commit

Permalink
Migrate build-rs to the Cargo repo (#14786)
Browse files Browse the repository at this point in the history
### 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
weihanglo authored Nov 13, 2024
2 parents 2e7fc43 + 9814045 commit e5ce5e5
Show file tree
Hide file tree
Showing 14 changed files with 1,242 additions and 2 deletions.
18 changes: 16 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ anstyle = "1.0.8"
anyhow = "1.0.86"
base64 = "0.22.1"
blake3 = "1.5.2"
build-rs = { version = "0.2.0", path = "crates/build-rs" }
bytesize = "1.3"
cargo = { path = "" }
cargo-credential = { version = "0.4.2", path = "credential/cargo-credential" }
Expand Down Expand Up @@ -107,6 +108,7 @@ tracing = { version = "0.1.40", default-features = false, features = ["std"] } #
tracing-chrome = "0.7.2"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
unicase = "2.7.0"
unicode-ident = "1.0.13"
unicode-width = "0.2.0"
unicode-xid = "0.2.4"
url = "2.5.2"
Expand Down
11 changes: 11 additions & 0 deletions crates/build-rs-test-lib/Cargo.toml
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
78 changes: 78 additions & 0 deletions crates/build-rs-test-lib/build.rs
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());
}
4 changes: 4 additions & 0 deletions crates/build-rs-test-lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[test]
fn test() {
assert!(cfg!(did_run_build_script));
}
16 changes: 16 additions & 0 deletions crates/build-rs/Cargo.toml
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
2 changes: 2 additions & 0 deletions crates/build-rs/README.md
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.
46 changes: 46 additions & 0 deletions crates/build-rs/src/allow_use.rs
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
}
27 changes: 27 additions & 0 deletions crates/build-rs/src/ident.rs
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, '-'))
}
Loading

0 comments on commit e5ce5e5

Please sign in to comment.