Skip to content

Commit

Permalink
Rollup merge of rust-lang#100681 - CAD97:rustc-print-rustc, r=petroch…
Browse files Browse the repository at this point in the history
…enkov

Add `rustc --print rustc-path`

Related:

- rust-lang/cargo#10986
- rust-lang/rustup#3035

Goal:

Like the original rust-lang/rustup#2958, the goal is to enable `cargo` to call `rustc` directly, rather than through the `rustup` shim.

Solution:

`cargo` asks `rustc` to tell it what executable to run. Sometime early in compilation, `cargo` will run `$(RUSTC_WRAPPER) $(RUSTC ?: rustc) --print rustc-path`[^1]. Further calls to `rustc` to do execution will use the resolved printed executable path rather than continuing to call the "input `rustc` path," which will allow it to bypass the `rustup` shim.

The cargo side is rust-lang/cargo#10998.

[^1]: This can potentially be combined with other `--print`s, as well!

This is a tiny patch, so I've implemented it as insta-stable; this will need signoff probably from both the compiler and cargo teams. I'm working on the cargo side of the patch currently, but wanted to get this up ASAP.

cc `@davidlattimore` `@bjorn3` `@rust-lang/cargo` `@rust-lang/compiler` (sorry for the big ping list 😅)
  • Loading branch information
ChrisDenton authored Aug 20, 2022
2 parents dedae00 + 6cdf192 commit 745bfe3
Show file tree
Hide file tree
Showing 3 changed files with 25 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
4 changes: 4 additions & 0 deletions src/test/run-make/print-rustc-path/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-include ../../run-make-fulldeps/tools.mk

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

0 comments on commit 745bfe3

Please sign in to comment.