Skip to content

Commit

Permalink
don't show the full linker args unless --verbose is passed
Browse files Browse the repository at this point in the history
the linker arguments can be *very* long, especially for crates with many dependencies. often they are not useful. omit them unless the user specifically requests them.
  • Loading branch information
jyn514 committed Dec 25, 2023
1 parent 8faed20 commit 43edbe0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,12 +934,12 @@ fn link_natively<'a>(
let mut output = prog.stderr.clone();
output.extend_from_slice(&prog.stdout);
let escaped_output = escape_linker_output(&output, flavor);
// FIXME: Add UI tests for this error.
let err = errors::LinkingFailed {
linker_path: &linker_path,
exit_status: prog.status,
command: &cmd,
escaped_output,
verbose: sess.opts.verbose,
};
sess.dcx().emit_err(err);
// If MSVC's `link.exe` was expected but the return code
Expand Down
9 changes: 8 additions & 1 deletion compiler/rustc_codegen_ssa/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ pub struct LinkingFailed<'a> {
pub exit_status: ExitStatus,
pub command: &'a Command,
pub escaped_output: String,
pub verbose: bool,
}

impl IntoDiagnostic<'_> for LinkingFailed<'_> {
Expand All @@ -418,7 +419,13 @@ impl IntoDiagnostic<'_> for LinkingFailed<'_> {

let contains_undefined_ref = self.escaped_output.contains("undefined reference to");

diag.note(format!("{:?}", self.command)).note(self.escaped_output);
if self.verbose {
diag.note(format!("{:?}", self.command));
} else {
diag.note("use `--verbose` to show all linker arguments");
}

diag.note(self.escaped_output);

// Trying to match an error from OS linkers
// which by now we have no way to translate.
Expand Down
4 changes: 2 additions & 2 deletions tests/run-make/link-args-order/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ RUSTC_FLAGS = -C linker-flavor=ld -C link-arg=a -C link-args="b c" -C link-args=
RUSTC_FLAGS_PRE = -C linker-flavor=ld -Z pre-link-arg=a -Z pre-link-args="b c" -Z pre-link-args="d e" -Z pre-link-arg=f

all:
$(RUSTC) $(RUSTC_FLAGS) empty.rs 2>&1 | $(CGREP) '"a" "b" "c" "d" "e" "f"'
$(RUSTC) $(RUSTC_FLAGS_PRE) empty.rs 2>&1 | $(CGREP) '"a" "b" "c" "d" "e" "f"'
$(RUSTC) $(RUSTC_FLAGS) empty.rs --print=link-args | $(CGREP) '"a" "b" "c" "d" "e" "f"'
$(RUSTC) $(RUSTC_FLAGS_PRE) empty.rs --print=link-args | $(CGREP) '"a" "b" "c" "d" "e" "f"'
8 changes: 4 additions & 4 deletions tests/run-make/link-dedup/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ all:
$(RUSTC) depa.rs
$(RUSTC) depb.rs
$(RUSTC) depc.rs
$(RUSTC) empty.rs --cfg bar 2>&1 | $(CGREP) '"-ltesta" "-ltestb" "-ltesta"'
$(RUSTC) empty.rs 2>&1 | $(CGREP) '"-ltesta"'
$(RUSTC) empty.rs 2>&1 | $(CGREP) -v '"-ltestb"'
$(RUSTC) empty.rs 2>&1 | $(CGREP) -v '"-ltesta" "-ltesta" "-ltesta"'
$(RUSTC) empty.rs --cfg bar --print=link-args | $(CGREP) '"-ltesta" "-ltestb" "-ltesta"'
$(RUSTC) empty.rs --print=link-args | $(CGREP) '"-ltesta"'
$(RUSTC) empty.rs --print=link-args | $(CGREP) -v '"-ltestb"'
$(RUSTC) empty.rs --print=link-args | $(CGREP) -v '"-ltesta" "-ltesta" "-ltesta"'
10 changes: 9 additions & 1 deletion tests/run-make/linker-warning/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ include ../tools.mk

RUN_RUSTC := $(RUSTC_ORIGINAL) main.rs -o $(TMPDIR)/main -C linker=./fake-linker.sh

all:
all: succeeds_with_warnings errors linker_args

succeeds_with_warnings:
# Run rustc with our fake linker, and make sure it shows warnings
$(RUN_RUSTC) -C link-arg=run_make_warn 2>&1 | $(CGREP) "warning: linker stderr: bar"

# Make sure it shows stdout, but only when --verbose is passed
$(RUN_RUSTC) -C link-arg=run_make_info --verbose 2>&1 | $(CGREP) "warning: linker stdout: foo"
$(RUN_RUSTC) -C link-arg=run_make_info 2>&1 | $(CGREP) -v "warning: linker stdout: foo"

errors:
# Make sure we short-circuit this new path if the linker exits with an error (so the diagnostic is less verbose)
rm -f $(TMPDIR)/main
$(RUN_RUSTC) -C link-arg=run_make_error 2>&1 | $(CGREP) "note: error: baz"
! [ -e $(TMPDIR)/main ]

linker_args:
# Make sure we don't show the linker args unless `--verbose` is passed
$(RUN_RUSTC) --verbose -C link-arg=run_make_error 2>&1 | $(CGREP) -e "PATH=.*fake-linker.sh.*run_make_error"
$(RUN_RUSTC) -C link-arg=run_make_error 2>&1 | $(CGREP) -v -e "PATH=.*fake-linker.sh.*run_make_error"

0 comments on commit 43edbe0

Please sign in to comment.