Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rustc --print rustc-path #100681

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Looks like \ needs to be escaped here.
  2. FWIW, CGREP supports regexes to match on both \ and / together.

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