Skip to content

Commit 5a2e69c

Browse files
committed
Pass certain flags when building all compiler/ crates.
Currently they are passed to the ones satisfying `Mode::Rustc` via RUSTFLAGS. But RUSTFLAGS is ignored for proc macro crates. This commit makes those flags get added directly to the command for proc macro crates.
1 parent f9e0239 commit 5a2e69c

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

src/bootstrap/src/bin/rustc.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use std::process::{Child, Command};
2121
use std::time::Instant;
2222

2323
use shared_helpers::{
24-
dylib_path, dylib_path_var, exe, maybe_dump, parse_rustc_stage, parse_rustc_verbose,
25-
parse_value_from_args,
24+
FLAGS_FOR_RUSTC, dylib_path, dylib_path_var, exe, maybe_dump, parse_rustc_stage,
25+
parse_rustc_verbose, parse_value_from_args,
2626
};
2727

2828
#[path = "../utils/shared_helpers.rs"]
@@ -145,6 +145,35 @@ fn main() {
145145
cmd.arg("-Z").arg("on-broken-pipe=kill");
146146
}
147147

148+
// In `cargo.rs` we add the `FLAGS_FOR_RUSTC` flags for all `compiler/`
149+
// crates to RUSTFLAGS. However, RUSTFLAGS is ignored for `compiler/` proc
150+
// macro crates, because RUSTFLAGS is ignored when `--target` is given. So,
151+
// we add the `FLAGS_FOR_RUSTC` flags directly to the command here if they
152+
// are also in RUSTFLAGS.
153+
let crate_type = parse_value_from_args(&orig_args, "--crate-type");
154+
if crate_type == Some("proc-macro") {
155+
if let Ok(rustflags) = env::var("RUSTFLAGS") {
156+
// Count how many of flags from `FLAGS_FOR_RUSTC` are in RUSTFLAGS.
157+
let n = FLAGS_FOR_RUSTC.iter().filter(|&flag| rustflags.contains(flag)).count();
158+
if n == FLAGS_FOR_RUSTC.len() {
159+
// All the FLAGS_FOR_RUSTC flags are in RUSTFLAGS, which means
160+
// this is a `compiler/` proc macro crate. Insert the flags
161+
// directly into the command.
162+
for flag in shared_helpers::FLAGS_FOR_RUSTC {
163+
cmd.arg(flag);
164+
}
165+
} else if n == 0 {
166+
// None of the FLAGS_FOR_RUSTC flags are in RUSTFLAGS.
167+
// Therefore it's a non-`compiler/` proc macro crate:
168+
// third-party, clippy, etc. Do nothing.
169+
} else {
170+
// Some but not all of the FLAGS_FOR_RUSTC flags are in
171+
// RUSTFLAGS. Huh?
172+
panic!("RUSTFLAGS is missing some expected flags");
173+
}
174+
}
175+
}
176+
148177
if target.is_some() {
149178
// The stage0 compiler has a special sysroot distinct from what we
150179
// actually downloaded, so we just always pass the `--sysroot` option,
@@ -165,7 +194,6 @@ fn main() {
165194
cmd.arg("-C").arg("panic=abort");
166195
}
167196

168-
let crate_type = parse_value_from_args(&orig_args, "--crate-type");
169197
// `-Ztls-model=initial-exec` must not be applied to proc-macros, see
170198
// issue https://github.com/rust-lang/rust/issues/100530
171199
if env::var("RUSTC_TLS_MODEL_INITIAL_EXEC").is_ok()

src/bootstrap/src/core/builder/cargo.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,13 +1071,13 @@ impl Builder<'_> {
10711071
rustdocflags.arg("-Wrustdoc::invalid_codeblock_attributes");
10721072
}
10731073

1074+
// Add some flags to RUSTFLAGS for `compiler/` crates. Note: proc macro
1075+
// crates need additional special handling. See the `FLAGS_FOR_RUSTC`
1076+
// use in `rustc.rs` for details.
10741077
if mode == Mode::Rustc {
1075-
rustflags.arg("-Wrustc::internal");
1076-
rustflags.arg("-Drustc::symbol_intern_string_literal");
1077-
// FIXME(edition_2024): Change this to `-Wrust_2024_idioms` when all
1078-
// of the individual lints are satisfied.
1079-
rustflags.arg("-Wkeyword_idents_2024");
1080-
rustflags.arg("-Wunsafe_op_in_unsafe_fn");
1078+
for flag in crate::utils::shared_helpers::FLAGS_FOR_RUSTC {
1079+
rustflags.arg(flag);
1080+
}
10811081
}
10821082

10831083
if self.config.rust_frame_pointers {

src/bootstrap/src/utils/shared_helpers.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,13 @@ pub fn parse_value_from_args<'a>(args: &'a [OsString], key: &str) -> Option<&'a
113113

114114
None
115115
}
116+
117+
// Flags used to compile all crates in `compiler/`.
118+
pub const FLAGS_FOR_RUSTC: &[&'static str] = &[
119+
"-Wrustc::internal",
120+
"-Drustc::symbol_intern_string_literal",
121+
// FIXME(edition_2024): Change this to `-Wrust_2024_idioms` when all of the
122+
// individual lints are satisfied.
123+
"-Wkeyword_idents_2024",
124+
"-Wunsafe_op_in_unsafe_fn",
125+
];

0 commit comments

Comments
 (0)