Skip to content

Commit

Permalink
set rustflags defaults early rather than during build_from_source
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Apr 13, 2024
1 parent 19a179b commit 126cf18
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,31 @@ impl SysrootBuilder {
/// Prepare to create a new sysroot in the given folder (that folder should later be passed to
/// rustc via `--sysroot`), for the given target.
pub fn new(sysroot_dir: &Path, target: impl Into<OsString>) -> Self {
let default_flags = &[
// This is usually set by bootstrap via `RUSTC_FORCE_UNSTABLE`.
"-Zforce-unstable-if-unmarked",
// Don't fail when there are lints.
// The whole point of this crate is to build the standard library in nonstandard
// configurations, which may trip lints due to untested combinations of cfgs.
// Cargo applies --cap-lints=allow or --cap-lints=warn when handling -Zbuild-std, which we
// of course are not using:
// https://github.com/rust-lang/cargo/blob/2ce45605d9db521b5fd6c1211ce8de6055fdb24e/src/cargo/core/compiler/mod.rs#L899
// https://github.com/rust-lang/cargo/blob/2ce45605d9db521b5fd6c1211ce8de6055fdb24e/src/cargo/core/compiler/unit.rs#L102-L109
// All the standard library crates are path dependencies, and they also sometimes pull in
// separately-maintained crates like backtrace by treating their crate roots as module
// roots. If we do not cap lints, we can get lint failures outside core or std.
// We cannot set --cap-lints=allow because Cargo needs to parse warnings to understand the
// output of --print=file-names for crate-types that the target does not support.
"--cap-lints=warn",
];
SysrootBuilder {
sysroot_dir: sysroot_dir.to_owned(),
target: target.into(),
config: SysrootConfig::WithStd {
std_features: vec![],
},
mode: BuildMode::Build,
rustflags: vec![],
rustflags: default_flags.iter().map(Into::into).collect(),
cargo: None,
rustc_version: None,
}
Expand Down Expand Up @@ -375,25 +392,6 @@ path = "lib.rs"
None => rustc_version::version_meta()?,
};

// The whole point of this crate is to build the standard library in nonstandard
// configurations, which may trip lints due to untested combinations of cfgs.
// Cargo applies --cap-lints=allow or --cap-lints=warn when handling -Zbuild-std, which we
// of course are not using:
// https://github.com/rust-lang/cargo/blob/2ce45605d9db521b5fd6c1211ce8de6055fdb24e/src/cargo/core/compiler/mod.rs#L899
// https://github.com/rust-lang/cargo/blob/2ce45605d9db521b5fd6c1211ce8de6055fdb24e/src/cargo/core/compiler/unit.rs#L102-L109
// All the standard library crates are path dependencies, and they also sometimes pull in
// separately-maintained crates like backtrace by treating their crate roots as module
// roots. If we do not cap lints, we can get lint failures outside core or std.
// We cannot set --cap-lints=allow because Cargo needs to parse warnings to understand the
// output of --print=file-names for crate-types that the target does not support.
if !self.rustflags.iter().any(|flag| {
// FIXME: OsStr::as_encoded_bytes is cleaner here
flag.to_str()
.map_or(false, |f| f.starts_with("--cap-lints"))
}) {
self.rustflags.push("--cap-lints=warn".into());
}

// Check if we even need to do anything.
let cur_hash = self.sysroot_compute_hash(src_dir, &rustc_version)?;
if self.sysroot_read_hash() == Some(cur_hash) {
Expand Down Expand Up @@ -432,9 +430,7 @@ path = "lib.rs"
cmd.arg("--target");
cmd.arg(&self.target);
// Set rustflags.
let mut flags = self.rustflags;
flags.push("-Zforce-unstable-if-unmarked".into());
cmd.env("CARGO_ENCODED_RUSTFLAGS", encode_rustflags(&flags));
cmd.env("CARGO_ENCODED_RUSTFLAGS", encode_rustflags(&self.rustflags));
// Make sure the results end up where we expect them.
let build_target_dir = build_dir.path().join("target");
cmd.env("CARGO_TARGET_DIR", &build_target_dir);
Expand Down

0 comments on commit 126cf18

Please sign in to comment.