Skip to content

Commit

Permalink
Add rustc --print rustc-path
Browse files Browse the repository at this point in the history
  • Loading branch information
CAD97 committed Aug 21, 2022
1 parent 86c6ebe commit f5be8a4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
4 changes: 4 additions & 0 deletions compiler/rustc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,10 @@ fn print_crate_info(
| TargetFeatures => {
codegen_backend.print(*req, sess);
}
RustcPath => match env::current_exe() {
Ok(exe) => println!("{}", exe.display()),
Err(_) => early_error(ErrorOutputType::default(), "failed to get rustc path"),
},
// Any output here interferes with Cargo's parsing of other printed output
NativeStaticLibs => {}
LinkArgs => {}
Expand Down
28 changes: 17 additions & 11 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ pub enum PrintRequest {
NativeStaticLibs,
StackProtectorStrategies,
LinkArgs,
RustcPath,
}

pub enum Input {
Expand Down Expand Up @@ -1777,6 +1778,20 @@ fn collect_print_requests(
cg.target_feature = String::new();
}

let gate = |req, opt| {
if unstable_opts.unstable_options {
req
} else {
early_error(
error_format,
&format!(
"the `-Z unstable-options` flag must also be passed to \
enable the {opt} print option",
),
);
}
};

prints.extend(matches.opt_strs("print").into_iter().map(|s| match &*s {
"crate-name" => PrintRequest::CrateName,
"file-names" => PrintRequest::FileNames,
Expand All @@ -1791,18 +1806,9 @@ fn collect_print_requests(
"tls-models" => PrintRequest::TlsModels,
"native-static-libs" => PrintRequest::NativeStaticLibs,
"stack-protector-strategies" => PrintRequest::StackProtectorStrategies,
"target-spec-json" => {
if unstable_opts.unstable_options {
PrintRequest::TargetSpec
} else {
early_error(
error_format,
"the `-Z unstable-options` flag must also be passed to \
enable the target-spec-json print option",
);
}
}
"target-spec-json" => gate(PrintRequest::TargetSpec, "target-spec-json"),
"link-args" => PrintRequest::LinkArgs,
"rustc-path" => gate(PrintRequest::RustcPath, "rustc-path"),
req => early_error(error_format, &format!("unknown print request `{req}`")),
}));

Expand Down
9 changes: 9 additions & 0 deletions src/test/run-make/print-rustc-path/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-include ../../run-make-fulldeps/tools.mk

ifdef IS_WINDOWS
all:
$(RUSTC) -Zunstable-options --print rustc-path | $(CGREP) bin\rustc
else
all:
$(RUSTC) -Zunstable-options --print rustc-path | $(CGREP) bin/rustc
endif

0 comments on commit f5be8a4

Please sign in to comment.