Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate build-rs to the Cargo repo #14786

Merged
merged 22 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
epage marked this conversation as resolved.
Show resolved Hide resolved
epage marked this conversation as resolved.
Show resolved Hide resolved
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
Copy link
Member

Choose a reason for hiding this comment

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

Just want to call out that this MSRV seems way too high for this crate.
We may want to lower this in the future.

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.
Copy link
Contributor

Choose a reason for hiding this comment

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

We probably want to explicitly exempt the unstable feature here, and/or migrate it to using a cfg (like procmacro2_semver_exempt) instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My assumption is that its fine as that feature is documented as unstable. I'm not even sure why that line is encouraged in the policy. Pretty much every lib is assumed to follow semver.

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