Skip to content

Commit 126cf18

Browse files
committed
set rustflags defaults early rather than during build_from_source
1 parent 19a179b commit 126cf18

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

src/lib.rs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,31 @@ impl SysrootBuilder {
159159
/// Prepare to create a new sysroot in the given folder (that folder should later be passed to
160160
/// rustc via `--sysroot`), for the given target.
161161
pub fn new(sysroot_dir: &Path, target: impl Into<OsString>) -> Self {
162+
let default_flags = &[
163+
// This is usually set by bootstrap via `RUSTC_FORCE_UNSTABLE`.
164+
"-Zforce-unstable-if-unmarked",
165+
// Don't fail when there are lints.
166+
// The whole point of this crate is to build the standard library in nonstandard
167+
// configurations, which may trip lints due to untested combinations of cfgs.
168+
// Cargo applies --cap-lints=allow or --cap-lints=warn when handling -Zbuild-std, which we
169+
// of course are not using:
170+
// https://github.com/rust-lang/cargo/blob/2ce45605d9db521b5fd6c1211ce8de6055fdb24e/src/cargo/core/compiler/mod.rs#L899
171+
// https://github.com/rust-lang/cargo/blob/2ce45605d9db521b5fd6c1211ce8de6055fdb24e/src/cargo/core/compiler/unit.rs#L102-L109
172+
// All the standard library crates are path dependencies, and they also sometimes pull in
173+
// separately-maintained crates like backtrace by treating their crate roots as module
174+
// roots. If we do not cap lints, we can get lint failures outside core or std.
175+
// We cannot set --cap-lints=allow because Cargo needs to parse warnings to understand the
176+
// output of --print=file-names for crate-types that the target does not support.
177+
"--cap-lints=warn",
178+
];
162179
SysrootBuilder {
163180
sysroot_dir: sysroot_dir.to_owned(),
164181
target: target.into(),
165182
config: SysrootConfig::WithStd {
166183
std_features: vec![],
167184
},
168185
mode: BuildMode::Build,
169-
rustflags: vec![],
186+
rustflags: default_flags.iter().map(Into::into).collect(),
170187
cargo: None,
171188
rustc_version: None,
172189
}
@@ -375,25 +392,6 @@ path = "lib.rs"
375392
None => rustc_version::version_meta()?,
376393
};
377394

378-
// The whole point of this crate is to build the standard library in nonstandard
379-
// configurations, which may trip lints due to untested combinations of cfgs.
380-
// Cargo applies --cap-lints=allow or --cap-lints=warn when handling -Zbuild-std, which we
381-
// of course are not using:
382-
// https://github.com/rust-lang/cargo/blob/2ce45605d9db521b5fd6c1211ce8de6055fdb24e/src/cargo/core/compiler/mod.rs#L899
383-
// https://github.com/rust-lang/cargo/blob/2ce45605d9db521b5fd6c1211ce8de6055fdb24e/src/cargo/core/compiler/unit.rs#L102-L109
384-
// All the standard library crates are path dependencies, and they also sometimes pull in
385-
// separately-maintained crates like backtrace by treating their crate roots as module
386-
// roots. If we do not cap lints, we can get lint failures outside core or std.
387-
// We cannot set --cap-lints=allow because Cargo needs to parse warnings to understand the
388-
// output of --print=file-names for crate-types that the target does not support.
389-
if !self.rustflags.iter().any(|flag| {
390-
// FIXME: OsStr::as_encoded_bytes is cleaner here
391-
flag.to_str()
392-
.map_or(false, |f| f.starts_with("--cap-lints"))
393-
}) {
394-
self.rustflags.push("--cap-lints=warn".into());
395-
}
396-
397395
// Check if we even need to do anything.
398396
let cur_hash = self.sysroot_compute_hash(src_dir, &rustc_version)?;
399397
if self.sysroot_read_hash() == Some(cur_hash) {
@@ -432,9 +430,7 @@ path = "lib.rs"
432430
cmd.arg("--target");
433431
cmd.arg(&self.target);
434432
// Set rustflags.
435-
let mut flags = self.rustflags;
436-
flags.push("-Zforce-unstable-if-unmarked".into());
437-
cmd.env("CARGO_ENCODED_RUSTFLAGS", encode_rustflags(&flags));
433+
cmd.env("CARGO_ENCODED_RUSTFLAGS", encode_rustflags(&self.rustflags));
438434
// Make sure the results end up where we expect them.
439435
let build_target_dir = build_dir.path().join("target");
440436
cmd.env("CARGO_TARGET_DIR", &build_target_dir);

0 commit comments

Comments
 (0)