Skip to content

Commit 82f8f63

Browse files
authored
Rollup merge of rust-lang#87320 - danakj:debug-compilation-dir, r=michaelwoerister
Introduce -Z remap-cwd-prefix switch This switch remaps any absolute paths rooted under the current working directory to a new value. This includes remapping the debug info in `DW_AT_comp_dir` and `DW_AT_decl_file`. Importantly, this flag does not require passing the current working directory to the compiler, such that the command line can be run on any machine (with the same input files) and produce the same results. This is critical property for debugging compiler issues that crop up on remote machines. This is based on adetaylor's rust-lang@dbc4ae7 Major Change Proposal: rust-lang/compiler-team#450 Discussed on rust-lang#38322. Would resolve issue rust-lang#87325.
2 parents c3c0f80 + 2a687de commit 82f8f63

File tree

5 files changed

+81
-3
lines changed

5 files changed

+81
-3
lines changed

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ fn test_debugging_options_tracking_hash() {
754754
tracked!(profiler_runtime, "abc".to_string());
755755
tracked!(relax_elf_relocations, Some(true));
756756
tracked!(relro_level, Some(RelroLevel::Full));
757+
tracked!(remap_cwd_prefix, Some(PathBuf::from("abc")));
757758
tracked!(simulate_remapped_rust_src_base, Some(PathBuf::from("/rustc/abc")));
758759
tracked!(report_delayed_bugs, true);
759760
tracked!(sanitizer, SanitizerSet::ADDRESS);

compiler/rustc_session/src/config.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -1920,9 +1920,10 @@ fn parse_extern_dep_specs(
19201920

19211921
fn parse_remap_path_prefix(
19221922
matches: &getopts::Matches,
1923+
debugging_opts: &DebuggingOptions,
19231924
error_format: ErrorOutputType,
19241925
) -> Vec<(PathBuf, PathBuf)> {
1925-
matches
1926+
let mut mapping: Vec<(PathBuf, PathBuf)> = matches
19261927
.opt_strs("remap-path-prefix")
19271928
.into_iter()
19281929
.map(|remap| match remap.rsplit_once('=') {
@@ -1932,7 +1933,15 @@ fn parse_remap_path_prefix(
19321933
),
19331934
Some((from, to)) => (PathBuf::from(from), PathBuf::from(to)),
19341935
})
1935-
.collect()
1936+
.collect();
1937+
match &debugging_opts.remap_cwd_prefix {
1938+
Some(to) => match std::env::current_dir() {
1939+
Ok(cwd) => mapping.push((cwd, to.clone())),
1940+
Err(_) => (),
1941+
},
1942+
None => (),
1943+
};
1944+
mapping
19361945
}
19371946

19381947
pub fn build_session_options(matches: &getopts::Matches) -> Options {
@@ -2077,7 +2086,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
20772086

20782087
let crate_name = matches.opt_str("crate-name");
20792088

2080-
let remap_path_prefix = parse_remap_path_prefix(matches, error_format);
2089+
let remap_path_prefix = parse_remap_path_prefix(matches, &debugging_opts, error_format);
20812090

20822091
let pretty = parse_pretty(&debugging_opts, error_format);
20832092

compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,8 @@ options! {
12501250
"whether ELF relocations can be relaxed"),
12511251
relro_level: Option<RelroLevel> = (None, parse_relro_level, [TRACKED],
12521252
"choose which RELRO level to use"),
1253+
remap_cwd_prefix: Option<PathBuf> = (None, parse_opt_pathbuf, [TRACKED],
1254+
"remap paths under the current working directory to this path prefix"),
12531255
simulate_remapped_rust_src_base: Option<PathBuf> = (None, parse_opt_pathbuf, [TRACKED],
12541256
"simulate the effect of remap-debuginfo = true at bootstrapping by remapping path \
12551257
to rust's source base directory. only meant for testing purposes"),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# `remap-cwd-prefix`
2+
3+
The tracking issue for this feature is: [#87325](https://github.com/rust-lang/rust/issues/87325).
4+
5+
------------------------
6+
7+
This flag will rewrite absolute paths under the current working directory,
8+
replacing the current working directory prefix with a specified value.
9+
10+
The given value may be absolute or relative, or empty. This switch takes
11+
precidence over `--remap-path-prefix` in case they would both match a given
12+
path.
13+
14+
This flag helps to produce deterministic output, by removing the current working
15+
directory from build output, while allowing the command line to be universally
16+
reproducible, such that the same execution will work on all machines, regardless
17+
of build environment.
18+
19+
## Example
20+
```sh
21+
# This would produce an absolute path to main.rs in build outputs of
22+
# "./main.rs".
23+
rustc -Z remap-cwd-prefix=. main.rs
24+
```

src/test/run-make-fulldeps/reproducible-build/Makefile

+42
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ all: \
1010
link_paths \
1111
remap_paths \
1212
different_source_dirs \
13+
remap_cwd_bin \
14+
remap_cwd_rlib \
15+
remap_cwd_to_empty \
1316
extern_flags
1417

1518
smoke:
@@ -64,6 +67,45 @@ different_source_dirs:
6467
--crate-type rlib)
6568
cmp "$(TMPDIR)/libreproducible_build.rlib" "$(TMPDIR)/libfoo.rlib" || exit 1
6669

70+
remap_cwd_bin:
71+
rm -rf $(TMPDIR) && mkdir $(TMPDIR)
72+
$(RUSTC) reproducible-build-aux.rs
73+
mkdir $(TMPDIR)/test
74+
cp reproducible-build.rs $(TMPDIR)/test
75+
$(RUSTC) reproducible-build.rs --crate-type bin -C debuginfo=2 \
76+
-Z remap-cwd-prefix=.
77+
cp $(TMPDIR)/reproducible-build $(TMPDIR)/first
78+
(cd $(TMPDIR)/test && \
79+
$(RUSTC) reproducible-build.rs --crate-type bin -C debuginfo=2 \
80+
-Z remap-cwd-prefix=.)
81+
cmp "$(TMPDIR)/first" "$(TMPDIR)/reproducible-build" || exit 1
82+
83+
remap_cwd_rlib:
84+
rm -rf $(TMPDIR) && mkdir $(TMPDIR)
85+
$(RUSTC) reproducible-build-aux.rs
86+
mkdir $(TMPDIR)/test
87+
cp reproducible-build.rs $(TMPDIR)/test
88+
$(RUSTC) reproducible-build.rs --crate-type rlib -C debuginfo=2 \
89+
-Z remap-cwd-prefix=.
90+
cp $(TMPDIR)/libreproducible_build.rlib $(TMPDIR)/libfirst.rlib
91+
(cd $(TMPDIR)/test && \
92+
$(RUSTC) reproducible-build.rs --crate-type rlib -C debuginfo=2 \
93+
-Z remap-cwd-prefix=.)
94+
cmp "$(TMPDIR)/libfirst.rlib" "$(TMPDIR)/libreproducible_build.rlib" || exit 1
95+
96+
remap_cwd_to_empty:
97+
rm -rf $(TMPDIR) && mkdir $(TMPDIR)
98+
$(RUSTC) reproducible-build-aux.rs
99+
mkdir $(TMPDIR)/test
100+
cp reproducible-build.rs $(TMPDIR)/test
101+
$(RUSTC) reproducible-build.rs --crate-type rlib -C debuginfo=2 \
102+
-Z remap-cwd-prefix=
103+
cp $(TMPDIR)/libreproducible_build.rlib $(TMPDIR)/libfirst.rlib
104+
(cd $(TMPDIR)/test && \
105+
$(RUSTC) reproducible-build.rs --crate-type rlib -C debuginfo=2 \
106+
-Z remap-cwd-prefix=)
107+
cmp "$(TMPDIR)/libfirst.rlib" "$(TMPDIR)/libreproducible_build.rlib" || exit 1
108+
67109
extern_flags:
68110
rm -rf $(TMPDIR) && mkdir $(TMPDIR)
69111
$(RUSTC) reproducible-build-aux.rs

0 commit comments

Comments
 (0)