Skip to content

Commit f5be8a4

Browse files
committed
Add rustc --print rustc-path
1 parent 86c6ebe commit f5be8a4

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

compiler/rustc_driver/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,10 @@ fn print_crate_info(
725725
| TargetFeatures => {
726726
codegen_backend.print(*req, sess);
727727
}
728+
RustcPath => match env::current_exe() {
729+
Ok(exe) => println!("{}", exe.display()),
730+
Err(_) => early_error(ErrorOutputType::default(), "failed to get rustc path"),
731+
},
728732
// Any output here interferes with Cargo's parsing of other printed output
729733
NativeStaticLibs => {}
730734
LinkArgs => {}

compiler/rustc_session/src/config.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ pub enum PrintRequest {
545545
NativeStaticLibs,
546546
StackProtectorStrategies,
547547
LinkArgs,
548+
RustcPath,
548549
}
549550

550551
pub enum Input {
@@ -1777,6 +1778,20 @@ fn collect_print_requests(
17771778
cg.target_feature = String::new();
17781779
}
17791780

1781+
let gate = |req, opt| {
1782+
if unstable_opts.unstable_options {
1783+
req
1784+
} else {
1785+
early_error(
1786+
error_format,
1787+
&format!(
1788+
"the `-Z unstable-options` flag must also be passed to \
1789+
enable the {opt} print option",
1790+
),
1791+
);
1792+
}
1793+
};
1794+
17801795
prints.extend(matches.opt_strs("print").into_iter().map(|s| match &*s {
17811796
"crate-name" => PrintRequest::CrateName,
17821797
"file-names" => PrintRequest::FileNames,
@@ -1791,18 +1806,9 @@ fn collect_print_requests(
17911806
"tls-models" => PrintRequest::TlsModels,
17921807
"native-static-libs" => PrintRequest::NativeStaticLibs,
17931808
"stack-protector-strategies" => PrintRequest::StackProtectorStrategies,
1794-
"target-spec-json" => {
1795-
if unstable_opts.unstable_options {
1796-
PrintRequest::TargetSpec
1797-
} else {
1798-
early_error(
1799-
error_format,
1800-
"the `-Z unstable-options` flag must also be passed to \
1801-
enable the target-spec-json print option",
1802-
);
1803-
}
1804-
}
1809+
"target-spec-json" => gate(PrintRequest::TargetSpec, "target-spec-json"),
18051810
"link-args" => PrintRequest::LinkArgs,
1811+
"rustc-path" => gate(PrintRequest::RustcPath, "rustc-path"),
18061812
req => early_error(error_format, &format!("unknown print request `{req}`")),
18071813
}));
18081814

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-include ../../run-make-fulldeps/tools.mk
2+
3+
ifdef IS_WINDOWS
4+
all:
5+
$(RUSTC) -Zunstable-options --print rustc-path | $(CGREP) bin\rustc
6+
else
7+
all:
8+
$(RUSTC) -Zunstable-options --print rustc-path | $(CGREP) bin/rustc
9+
endif

0 commit comments

Comments
 (0)