Skip to content

Commit 84646e9

Browse files
authored
Rollup merge of #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 dbc4ae7 Major Change Proposal: rust-lang/compiler-team#450 Discussed on #38322. Would resolve issue #87325.
2 parents 2c7bc5e + c011828 commit 84646e9

File tree

5 files changed

+102
-5
lines changed

5 files changed

+102
-5
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

+63-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,19 @@ all: \
99
opt \
1010
link_paths \
1111
remap_paths \
12-
different_source_dirs \
12+
different_source_dirs_rlib \
13+
remap_cwd_rlib \
14+
remap_cwd_to_empty \
1315
extern_flags
1416

17+
# TODO: Builds of `bin` crate types are not deterministic with debuginfo=2 on
18+
# Windows.
19+
# See: https://github.com/rust-lang/rust/pull/87320#issuecomment-920105533
20+
# Issue: https://github.com/rust-lang/rust/issues/88982
21+
#
22+
# different_source_dirs_bin \
23+
# remap_cwd_bin \
24+
1525
smoke:
1626
rm -rf $(TMPDIR) && mkdir $(TMPDIR)
1727
$(RUSTC) linker.rs -O
@@ -52,7 +62,19 @@ remap_paths:
5262
$(RUSTC) reproducible-build.rs --crate-type rlib --remap-path-prefix=/b=/c
5363
cmp "$(TMPDIR)/libreproducible_build.rlib" "$(TMPDIR)/libfoo.rlib" || exit 1
5464

55-
different_source_dirs:
65+
different_source_dirs_bin:
66+
rm -rf $(TMPDIR) && mkdir $(TMPDIR)
67+
$(RUSTC) reproducible-build-aux.rs
68+
mkdir $(TMPDIR)/test
69+
cp reproducible-build.rs $(TMPDIR)/test
70+
$(RUSTC) reproducible-build.rs --crate-type bin --remap-path-prefix=$$PWD=/b
71+
cp $(TMPDIR)/reproducible-build $(TMPDIR)/foo
72+
(cd $(TMPDIR)/test && $(RUSTC) reproducible-build.rs \
73+
--remap-path-prefix=$(TMPDIR)/test=/b \
74+
--crate-type bin)
75+
cmp "$(TMPDIR)/reproducible-build" "$(TMPDIR)/foo" || exit 1
76+
77+
different_source_dirs_rlib:
5678
rm -rf $(TMPDIR) && mkdir $(TMPDIR)
5779
$(RUSTC) reproducible-build-aux.rs
5880
mkdir $(TMPDIR)/test
@@ -64,6 +86,45 @@ different_source_dirs:
6486
--crate-type rlib)
6587
cmp "$(TMPDIR)/libreproducible_build.rlib" "$(TMPDIR)/libfoo.rlib" || exit 1
6688

89+
remap_cwd_bin:
90+
rm -rf $(TMPDIR) && mkdir $(TMPDIR)
91+
$(RUSTC) reproducible-build-aux.rs
92+
mkdir $(TMPDIR)/test
93+
cp reproducible-build.rs $(TMPDIR)/test
94+
$(RUSTC) reproducible-build.rs --crate-type bin -C debuginfo=2 \
95+
-Z remap-cwd-prefix=.
96+
cp $(TMPDIR)/reproducible-build $(TMPDIR)/first
97+
(cd $(TMPDIR)/test && \
98+
$(RUSTC) reproducible-build.rs --crate-type bin -C debuginfo=2 \
99+
-Z remap-cwd-prefix=.)
100+
cmp "$(TMPDIR)/first" "$(TMPDIR)/reproducible-build" || exit 1
101+
102+
remap_cwd_rlib:
103+
rm -rf $(TMPDIR) && mkdir $(TMPDIR)
104+
$(RUSTC) reproducible-build-aux.rs
105+
mkdir $(TMPDIR)/test
106+
cp reproducible-build.rs $(TMPDIR)/test
107+
$(RUSTC) reproducible-build.rs --crate-type rlib -C debuginfo=2 \
108+
-Z remap-cwd-prefix=.
109+
cp $(TMPDIR)/libreproducible_build.rlib $(TMPDIR)/libfirst.rlib
110+
(cd $(TMPDIR)/test && \
111+
$(RUSTC) reproducible-build.rs --crate-type rlib -C debuginfo=2 \
112+
-Z remap-cwd-prefix=.)
113+
cmp "$(TMPDIR)/libfirst.rlib" "$(TMPDIR)/libreproducible_build.rlib" || exit 1
114+
115+
remap_cwd_to_empty:
116+
rm -rf $(TMPDIR) && mkdir $(TMPDIR)
117+
$(RUSTC) reproducible-build-aux.rs
118+
mkdir $(TMPDIR)/test
119+
cp reproducible-build.rs $(TMPDIR)/test
120+
$(RUSTC) reproducible-build.rs --crate-type rlib -C debuginfo=2 \
121+
-Z remap-cwd-prefix=
122+
cp $(TMPDIR)/libreproducible_build.rlib $(TMPDIR)/libfirst.rlib
123+
(cd $(TMPDIR)/test && \
124+
$(RUSTC) reproducible-build.rs --crate-type rlib -C debuginfo=2 \
125+
-Z remap-cwd-prefix=)
126+
cmp "$(TMPDIR)/libfirst.rlib" "$(TMPDIR)/libreproducible_build.rlib" || exit 1
127+
67128
extern_flags:
68129
rm -rf $(TMPDIR) && mkdir $(TMPDIR)
69130
$(RUSTC) reproducible-build-aux.rs

0 commit comments

Comments
 (0)