Skip to content

Introduce -Z remap-cwd-prefix switch #87320

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

Merged
merged 4 commits into from
Sep 16, 2021
Merged
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
1 change: 1 addition & 0 deletions compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
@@ -753,6 +753,7 @@ fn test_debugging_options_tracking_hash() {
tracked!(profiler_runtime, "abc".to_string());
tracked!(relax_elf_relocations, Some(true));
tracked!(relro_level, Some(RelroLevel::Full));
tracked!(remap_cwd_prefix, Some(PathBuf::from("abc")));
tracked!(simulate_remapped_rust_src_base, Some(PathBuf::from("/rustc/abc")));
tracked!(report_delayed_bugs, true);
tracked!(sanitizer, SanitizerSet::ADDRESS);
15 changes: 12 additions & 3 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
@@ -1920,9 +1920,10 @@ fn parse_extern_dep_specs(

fn parse_remap_path_prefix(
matches: &getopts::Matches,
debugging_opts: &DebuggingOptions,
error_format: ErrorOutputType,
) -> Vec<(PathBuf, PathBuf)> {
matches
let mut mapping: Vec<(PathBuf, PathBuf)> = matches
.opt_strs("remap-path-prefix")
.into_iter()
.map(|remap| match remap.rsplit_once('=') {
@@ -1932,7 +1933,15 @@ fn parse_remap_path_prefix(
),
Some((from, to)) => (PathBuf::from(from), PathBuf::from(to)),
})
.collect()
.collect();
match &debugging_opts.remap_cwd_prefix {
Some(to) => match std::env::current_dir() {
Ok(cwd) => mapping.push((cwd, to.clone())),
Err(_) => (),
},
None => (),
};
mapping
}

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

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

let remap_path_prefix = parse_remap_path_prefix(matches, error_format);
let remap_path_prefix = parse_remap_path_prefix(matches, &debugging_opts, error_format);

let pretty = parse_pretty(&debugging_opts, error_format);

2 changes: 2 additions & 0 deletions compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
@@ -1236,6 +1236,8 @@ options! {
"whether ELF relocations can be relaxed"),
relro_level: Option<RelroLevel> = (None, parse_relro_level, [TRACKED],
"choose which RELRO level to use"),
remap_cwd_prefix: Option<PathBuf> = (None, parse_opt_pathbuf, [TRACKED],
"remap paths under the current working directory to this path prefix"),
simulate_remapped_rust_src_base: Option<PathBuf> = (None, parse_opt_pathbuf, [TRACKED],
"simulate the effect of remap-debuginfo = true at bootstrapping by remapping path \
to rust's source base directory. only meant for testing purposes"),
24 changes: 24 additions & 0 deletions src/doc/unstable-book/src/compiler-flags/remap-cwd-prefix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# `remap-cwd-prefix`

The tracking issue for this feature is: [#87325](https://github.com/rust-lang/rust/issues/87325).

------------------------

This flag will rewrite absolute paths under the current working directory,
replacing the current working directory prefix with a specified value.

The given value may be absolute or relative, or empty. This switch takes
precidence over `--remap-path-prefix` in case they would both match a given
path.

This flag helps to produce deterministic output, by removing the current working
directory from build output, while allowing the command line to be universally
reproducible, such that the same execution will work on all machines, regardless
of build environment.

## Example
```sh
# This would produce an absolute path to main.rs in build outputs of
# "./main.rs".
rustc -Z remap-cwd-prefix=. main.rs
```
65 changes: 63 additions & 2 deletions src/test/run-make-fulldeps/reproducible-build/Makefile
Original file line number Diff line number Diff line change
@@ -9,9 +9,19 @@ all: \
opt \
link_paths \
remap_paths \
different_source_dirs \
different_source_dirs_rlib \
remap_cwd_rlib \
remap_cwd_to_empty \
extern_flags

# TODO: Builds of `bin` crate types are not deterministic with debuginfo=2 on
# Windows.
# See: https://github.com/rust-lang/rust/pull/87320#issuecomment-920105533
# Issue: https://github.com/rust-lang/rust/issues/88982
#
# different_source_dirs_bin \
# remap_cwd_bin \

smoke:
rm -rf $(TMPDIR) && mkdir $(TMPDIR)
$(RUSTC) linker.rs -O
@@ -52,7 +62,19 @@ remap_paths:
$(RUSTC) reproducible-build.rs --crate-type rlib --remap-path-prefix=/b=/c
cmp "$(TMPDIR)/libreproducible_build.rlib" "$(TMPDIR)/libfoo.rlib" || exit 1

different_source_dirs:
different_source_dirs_bin:
rm -rf $(TMPDIR) && mkdir $(TMPDIR)
$(RUSTC) reproducible-build-aux.rs
mkdir $(TMPDIR)/test
cp reproducible-build.rs $(TMPDIR)/test
$(RUSTC) reproducible-build.rs --crate-type bin --remap-path-prefix=$$PWD=/b
cp $(TMPDIR)/reproducible-build $(TMPDIR)/foo
(cd $(TMPDIR)/test && $(RUSTC) reproducible-build.rs \
--remap-path-prefix=$(TMPDIR)/test=/b \
--crate-type bin)
cmp "$(TMPDIR)/reproducible-build" "$(TMPDIR)/foo" || exit 1

different_source_dirs_rlib:
rm -rf $(TMPDIR) && mkdir $(TMPDIR)
$(RUSTC) reproducible-build-aux.rs
mkdir $(TMPDIR)/test
@@ -64,6 +86,45 @@ different_source_dirs:
--crate-type rlib)
cmp "$(TMPDIR)/libreproducible_build.rlib" "$(TMPDIR)/libfoo.rlib" || exit 1

remap_cwd_bin:
rm -rf $(TMPDIR) && mkdir $(TMPDIR)
$(RUSTC) reproducible-build-aux.rs
mkdir $(TMPDIR)/test
cp reproducible-build.rs $(TMPDIR)/test
$(RUSTC) reproducible-build.rs --crate-type bin -C debuginfo=2 \
-Z remap-cwd-prefix=.
cp $(TMPDIR)/reproducible-build $(TMPDIR)/first
(cd $(TMPDIR)/test && \
$(RUSTC) reproducible-build.rs --crate-type bin -C debuginfo=2 \
-Z remap-cwd-prefix=.)
cmp "$(TMPDIR)/first" "$(TMPDIR)/reproducible-build" || exit 1

remap_cwd_rlib:
rm -rf $(TMPDIR) && mkdir $(TMPDIR)
$(RUSTC) reproducible-build-aux.rs
mkdir $(TMPDIR)/test
cp reproducible-build.rs $(TMPDIR)/test
$(RUSTC) reproducible-build.rs --crate-type rlib -C debuginfo=2 \
-Z remap-cwd-prefix=.
cp $(TMPDIR)/libreproducible_build.rlib $(TMPDIR)/libfirst.rlib
(cd $(TMPDIR)/test && \
$(RUSTC) reproducible-build.rs --crate-type rlib -C debuginfo=2 \
-Z remap-cwd-prefix=.)
cmp "$(TMPDIR)/libfirst.rlib" "$(TMPDIR)/libreproducible_build.rlib" || exit 1

remap_cwd_to_empty:
rm -rf $(TMPDIR) && mkdir $(TMPDIR)
$(RUSTC) reproducible-build-aux.rs
mkdir $(TMPDIR)/test
cp reproducible-build.rs $(TMPDIR)/test
$(RUSTC) reproducible-build.rs --crate-type rlib -C debuginfo=2 \
-Z remap-cwd-prefix=
cp $(TMPDIR)/libreproducible_build.rlib $(TMPDIR)/libfirst.rlib
(cd $(TMPDIR)/test && \
$(RUSTC) reproducible-build.rs --crate-type rlib -C debuginfo=2 \
-Z remap-cwd-prefix=)
cmp "$(TMPDIR)/libfirst.rlib" "$(TMPDIR)/libreproducible_build.rlib" || exit 1

extern_flags:
rm -rf $(TMPDIR) && mkdir $(TMPDIR)
$(RUSTC) reproducible-build-aux.rs