-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Migrate build-rs to the Cargo repo #14786
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
Changes from all commits
5448ed7
41ea720
22c1e9e
4a6d465
6655e48
401f77a
e5516ae
7a50053
66a2544
657b2aa
85896ca
9b627d6
25f8bd7
e243d71
4f60fe0
b62c598
0c7577d
8b9eaa5
54660c6
82a912a
80c117a
9814045
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
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()); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#[test] | ||
fn test() { | ||
assert!(cfg!(did_run_build_script)); | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
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 |
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We probably want to explicitly exempt the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
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 | ||
} |
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, '-')) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.