Skip to content

Commit

Permalink
Implement --remap-path-prefix
Browse files Browse the repository at this point in the history
Remove experimental -Zremap-path-prefix-from/to, and replace it with
the stabilized --remap-path-prefix=from=to variant.

This is an implementation for issue of #41555.
  • Loading branch information
jsgf committed Feb 22, 2018
1 parent b1f8e6f commit 56a6828
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 70 deletions.
10 changes: 10 additions & 0 deletions src/doc/man/rustc.1
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ Print version info and exit.
\fB\-v\fR, \fB\-\-verbose\fR
Use verbose output.
.TP
\fB\-\-remap\-path\-prefix\fR \fIfrom\fR=\fIto\fR
Remap source path prefixes in all output, including compiler diagnostics, debug information,
macro expansions, etc. The \fIfrom\fR=\fIto\fR parameter is scanned from right to left, so \fIfrom\fR
may contain '=', but \fIto\fR may not.

This is useful for normalizing build products, for example by removing the current directory out of
pathnames emitted into the object files. The replacement is purely textual, with no consideration of
the current system's pathname syntax. For example \fI\-\-remap\-path\-prefix foo=bar\fR will
match \fBfoo/lib.rs\fR but not \fB./foo/lib.rs\fR.
.TP
\fB\-\-extern\fR \fINAME\fR=\fIPATH\fR
Specify where an external rust library is located. These should match
\fIextern\fR declarations in the crate's source code.
Expand Down
37 changes: 0 additions & 37 deletions src/doc/unstable-book/src/compiler-flags/remap-path-prefix.md

This file was deleted.

2 changes: 1 addition & 1 deletion src/libproc_macro/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ impl SourceFile {
/// If the code span associated with this `SourceFile` was generated by an external macro, this
/// may not be an actual path on the filesystem. Use [`is_real`] to check.
///
/// Also note that even if `is_real` returns `true`, if `-Z remap-path-prefix-*` was passed on
/// Also note that even if `is_real` returns `true`, if `--remap-path-prefix` was passed on
/// the command line, the path as given may not actually be valid.
///
/// [`is_real`]: #method.is_real
Expand Down
47 changes: 21 additions & 26 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,9 @@ top_level_options!(
// if we otherwise use the defaults of rustc.
cli_forced_codegen_units: Option<usize> [UNTRACKED],
cli_forced_thinlto_off: bool [UNTRACKED],

// Remap source path prefixes in all output (messages, object files, debug, etc)
remap_path_prefix: Vec<(PathBuf, PathBuf)> [UNTRACKED],
}
);

Expand Down Expand Up @@ -617,6 +620,7 @@ pub fn basic_options() -> Options {
actually_rustdoc: false,
cli_forced_codegen_units: None,
cli_forced_thinlto_off: false,
remap_path_prefix: Vec::new(),
}
}

Expand All @@ -635,11 +639,7 @@ impl Options {
}

pub fn file_path_mapping(&self) -> FilePathMapping {
FilePathMapping::new(
self.debugging_opts.remap_path_prefix_from.iter().zip(
self.debugging_opts.remap_path_prefix_to.iter()
).map(|(src, dst)| (src.clone(), dst.clone())).collect()
)
FilePathMapping::new(self.remap_path_prefix.clone())
}

/// True if there will be an output file generated
Expand Down Expand Up @@ -1269,10 +1269,6 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
"set the optimization fuel quota for a crate"),
print_fuel: Option<String> = (None, parse_opt_string, [TRACKED],
"make Rustc print the total optimization fuel used by a crate"),
remap_path_prefix_from: Vec<PathBuf> = (vec![], parse_pathbuf_push, [UNTRACKED],
"add a source pattern to the file path remapping config"),
remap_path_prefix_to: Vec<PathBuf> = (vec![], parse_pathbuf_push, [UNTRACKED],
"add a mapping target to the file path remapping config"),
force_unstable_if_unmarked: bool = (false, parse_bool, [TRACKED],
"force all crates to be `rustc_private` unstable"),
pre_link_arg: Vec<String> = (vec![], parse_string_push, [UNTRACKED],
Expand Down Expand Up @@ -1595,6 +1591,7 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
`expanded` (crates expanded), or
`expanded,identified` (fully parenthesized, AST nodes with IDs).",
"TYPE"),
opt::multi_s("", "remap-path-prefix", "remap source names in output", "FROM=TO"),
]);
opts
}
Expand Down Expand Up @@ -1718,23 +1715,6 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
output_types.insert(OutputType::Exe, None);
}

let remap_path_prefix_sources = debugging_opts.remap_path_prefix_from.len();
let remap_path_prefix_targets = debugging_opts.remap_path_prefix_to.len();

if remap_path_prefix_targets < remap_path_prefix_sources {
for source in &debugging_opts.remap_path_prefix_from[remap_path_prefix_targets..] {
early_error(error_format,
&format!("option `-Zremap-path-prefix-from='{}'` does not have \
a corresponding `-Zremap-path-prefix-to`", source.display()))
}
} else if remap_path_prefix_targets > remap_path_prefix_sources {
for target in &debugging_opts.remap_path_prefix_to[remap_path_prefix_sources..] {
early_error(error_format,
&format!("option `-Zremap-path-prefix-to='{}'` does not have \
a corresponding `-Zremap-path-prefix-from`", target.display()))
}
}

let mut cg = build_codegen_options(matches, error_format);
let mut codegen_units = cg.codegen_units;
let mut disable_thinlto = false;
Expand Down Expand Up @@ -1968,6 +1948,20 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)

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

let remap_path_prefix = matches.opt_strs("remap-path-prefix")
.into_iter()
.map(|remap| {
let mut parts = remap.rsplitn(2, '='); // reverse iterator
let to = parts.next();
let from = parts.next();
match (from, to) {
(Some(from), Some(to)) => (PathBuf::from(from), PathBuf::from(to)),
_ => early_error(error_format,
"--remap-path-prefix must contain '=' between FROM and TO"),
}
})
.collect();

(Options {
crate_types,
optimize: opt_level,
Expand Down Expand Up @@ -1995,6 +1989,7 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
actually_rustdoc: false,
cli_forced_codegen_units: codegen_units,
cli_forced_thinlto_off: disable_thinlto,
remap_path_prefix,
},
cfg)
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
// paths because any relative paths are potentially relative to
// a wrong directory.
// However, if a path has been modified via
// `-Zremap-path-prefix` we assume the user has already set
// `--remap-path-prefix` we assume the user has already set
// things up the way they want and don't touch the path values
// anymore.
match filemap.name {
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/codemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub struct CodeMap {
pub(super) files: RefCell<Vec<Rc<FileMap>>>,
file_loader: Box<FileLoader>,
// This is used to apply the file path remapping as specified via
// -Zremap-path-prefix to all FileMaps allocated within this CodeMap.
// --remap-path-prefix to all FileMaps allocated within this CodeMap.
path_mapping: FilePathMapping,
stable_id_to_filemap: RefCell<FxHashMap<StableFilemapId, Rc<FileMap>>>,
/// In case we are in a doctest, replace all file names with the PathBuf,
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax_pos/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ pub struct FileMap {
/// originate from files has names between angle brackets by convention,
/// e.g. `<anon>`
pub name: FileName,
/// True if the `name` field above has been modified by -Zremap-path-prefix
/// True if the `name` field above has been modified by --remap-path-prefix
pub name_was_remapped: bool,
/// The unmapped path of the file that the source came from.
/// Set to `None` if the FileMap was imported from an external crate.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

// ignore-tidy-linelength

// compile-flags: -g -Zremap-path-prefix-from={{cwd}} -Zremap-path-prefix-to=/the/aux-cwd -Zremap-path-prefix-from={{src-base}}/remap_path_prefix/auxiliary -Zremap-path-prefix-to=/the/aux-src
// compile-flags: -g --remap-path-prefix={{cwd}}=/the/aux-cwd --remap-path-prefix={{src-base}}/remap_path_prefix/auxiliary=/the/aux-src

#[inline]
pub fn some_aux_function() -> i32 {
Expand Down
2 changes: 1 addition & 1 deletion src/test/codegen/remap_path_prefix/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// ignore-windows
// ignore-tidy-linelength

// compile-flags: -g -C no-prepopulate-passes -Zremap-path-prefix-from={{cwd}} -Zremap-path-prefix-to=/the/cwd -Zremap-path-prefix-from={{src-base}} -Zremap-path-prefix-to=/the/src
// compile-flags: -g -C no-prepopulate-passes --remap-path-prefix={{cwd}}=/the/cwd --remap-path-prefix={{src-base}}=/the/src
// aux-build:remap_path_prefix_aux.rs

extern crate remap_path_prefix_aux;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

//[rpass1] compile-flags: -g
//[rpass2] compile-flags: -g
//[rpass3] compile-flags: -g -Zremap-path-prefix-from={{src-base}} -Zremap-path-prefix-to=/the/src
//[rpass3] compile-flags: -g --remap-path-prefix={{src-base}}=/the/src

#![feature(rustc_attrs)]
#![crate_type="rlib"]
Expand Down

0 comments on commit 56a6828

Please sign in to comment.