Skip to content

Commit c8c5fa4

Browse files
authored
Rollup merge of #135330 - bjorn3:respect_sysroot_in_version_printing, r=lqd
Respect --sysroot for rustc -vV and -Cpasses=list This is necessary when the specified codegen backend is in a custom sysroot. Fixes #135165
2 parents bbec151 + 056a9ce commit c8c5fa4

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

compiler/rustc_driver_impl/src/lib.rs

+22-15
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use rustc_middle::ty::TyCtxt;
5353
use rustc_parse::{new_parser_from_file, new_parser_from_source_str, unwrap_or_emit_fatal};
5454
use rustc_session::config::{
5555
CG_OPTIONS, ErrorOutputType, Input, OptionDesc, OutFileName, OutputType, UnstableOptions,
56-
Z_OPTIONS, nightly_options,
56+
Z_OPTIONS, nightly_options, parse_target_triple,
5757
};
5858
use rustc_session::getopts::{self, Matches};
5959
use rustc_session::lint::{Lint, LintId};
@@ -916,13 +916,7 @@ pub fn version_at_macro_invocation(
916916
safe_println!("host: {}", config::host_tuple());
917917
safe_println!("release: {release}");
918918

919-
let debug_flags = matches.opt_strs("Z");
920-
let backend_name = debug_flags.iter().find_map(|x| x.strip_prefix("codegen-backend="));
921-
let opts = config::Options::default();
922-
let sysroot = filesearch::materialize_sysroot(opts.maybe_sysroot.clone());
923-
let target = config::build_target_config(early_dcx, &opts, &sysroot);
924-
925-
get_codegen_backend(early_dcx, &sysroot, backend_name, &target).print_version();
919+
get_backend_from_raw_matches(early_dcx, matches).print_version();
926920
}
927921
}
928922

@@ -1125,19 +1119,32 @@ pub fn describe_flag_categories(early_dcx: &EarlyDiagCtxt, matches: &Matches) ->
11251119
}
11261120

11271121
if cg_flags.iter().any(|x| *x == "passes=list") {
1128-
let backend_name = debug_flags.iter().find_map(|x| x.strip_prefix("codegen-backend="));
1129-
1130-
let opts = config::Options::default();
1131-
let sysroot = filesearch::materialize_sysroot(opts.maybe_sysroot.clone());
1132-
let target = config::build_target_config(early_dcx, &opts, &sysroot);
1133-
1134-
get_codegen_backend(early_dcx, &sysroot, backend_name, &target).print_passes();
1122+
get_backend_from_raw_matches(early_dcx, matches).print_passes();
11351123
return true;
11361124
}
11371125

11381126
false
11391127
}
11401128

1129+
/// Get the codegen backend based on the raw [`Matches`].
1130+
///
1131+
/// `rustc -vV` and `rustc -Cpasses=list` need to get the codegen backend before we have parsed all
1132+
/// arguments and created a [`Session`]. This function reads `-Zcodegen-backend`, `--target` and
1133+
/// `--sysroot` without validating any other arguments and loads the codegen backend based on these
1134+
/// arguments.
1135+
fn get_backend_from_raw_matches(
1136+
early_dcx: &EarlyDiagCtxt,
1137+
matches: &Matches,
1138+
) -> Box<dyn CodegenBackend> {
1139+
let debug_flags = matches.opt_strs("Z");
1140+
let backend_name = debug_flags.iter().find_map(|x| x.strip_prefix("codegen-backend="));
1141+
let target = parse_target_triple(early_dcx, matches);
1142+
let sysroot = filesearch::materialize_sysroot(matches.opt_str("sysroot").map(PathBuf::from));
1143+
let target = config::build_target_config(early_dcx, &target, &sysroot);
1144+
1145+
get_codegen_backend(early_dcx, &sysroot, backend_name, &target)
1146+
}
1147+
11411148
fn describe_debug_flags() {
11421149
safe_println!("\nAvailable options:\n");
11431150
print_flag_list("-Z", config::Z_OPTIONS);

compiler/rustc_interface/src/interface.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
383383
crate::callbacks::setup_callbacks();
384384

385385
let sysroot = filesearch::materialize_sysroot(config.opts.maybe_sysroot.clone());
386-
let target = config::build_target_config(&early_dcx, &config.opts, &sysroot);
386+
let target = config::build_target_config(&early_dcx, &config.opts.target_triple, &sysroot);
387387
let file_loader = config.file_loader.unwrap_or_else(|| Box::new(RealFileLoader));
388388
let path_mapping = config.opts.file_path_mapping();
389389
let hash_kind = config.opts.unstable_opts.src_hash_algorithm(&target);

compiler/rustc_interface/src/tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ where
4242
let matches = optgroups().parse(args).unwrap();
4343
let sessopts = build_session_options(&mut early_dcx, &matches);
4444
let sysroot = filesearch::materialize_sysroot(sessopts.maybe_sysroot.clone());
45-
let target = rustc_session::config::build_target_config(&early_dcx, &sessopts, &sysroot);
45+
let target =
46+
rustc_session::config::build_target_config(&early_dcx, &sessopts.target_triple, &sysroot);
4647
let hash_kind = sessopts.unstable_opts.src_hash_algorithm(&target);
4748
let checksum_hash_kind = sessopts.unstable_opts.checksum_hash_algorithm();
4849
let sm_inputs = Some(SourceMapInputs {

compiler/rustc_session/src/config.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1346,8 +1346,12 @@ pub fn build_configuration(sess: &Session, mut user_cfg: Cfg) -> Cfg {
13461346
user_cfg
13471347
}
13481348

1349-
pub fn build_target_config(early_dcx: &EarlyDiagCtxt, opts: &Options, sysroot: &Path) -> Target {
1350-
match Target::search(&opts.target_triple, sysroot) {
1349+
pub fn build_target_config(
1350+
early_dcx: &EarlyDiagCtxt,
1351+
target: &TargetTuple,
1352+
sysroot: &Path,
1353+
) -> Target {
1354+
match Target::search(target, sysroot) {
13511355
Ok((target, warnings)) => {
13521356
for warning in warnings.warning_messages() {
13531357
early_dcx.early_warn(warning)

0 commit comments

Comments
 (0)