Skip to content
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

Migrate remap-path-prefix-dwarf run-make test to rmake #128410

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
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
30 changes: 30 additions & 0 deletions src/tools/run-make-support/src/external_deps/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ pub fn llvm_bcanalyzer() -> LlvmBcanalyzer {
LlvmBcanalyzer::new()
}

/// Construct a new `llvm-dwarfdump` invocation. This assumes that `llvm-dwarfdump` is available
/// at `$LLVM_BIN_DIR/llvm-dwarfdump`.
pub fn llvm_dwarfdump() -> LlvmDwarfdump {
LlvmDwarfdump::new()
}

/// A `llvm-readobj` invocation builder.
#[derive(Debug)]
#[must_use]
Expand Down Expand Up @@ -97,13 +103,21 @@ pub struct LlvmBcanalyzer {
cmd: Command,
}

/// A `llvm-dwarfdump` invocation builder.
#[derive(Debug)]
#[must_use]
pub struct LlvmDwarfdump {
cmd: Command,
}

crate::macros::impl_common_helpers!(LlvmReadobj);
crate::macros::impl_common_helpers!(LlvmProfdata);
crate::macros::impl_common_helpers!(LlvmFilecheck);
crate::macros::impl_common_helpers!(LlvmObjdump);
crate::macros::impl_common_helpers!(LlvmAr);
crate::macros::impl_common_helpers!(LlvmNm);
crate::macros::impl_common_helpers!(LlvmBcanalyzer);
crate::macros::impl_common_helpers!(LlvmDwarfdump);

/// Generate the path to the bin directory of LLVM.
#[must_use]
Expand Down Expand Up @@ -317,3 +331,19 @@ impl LlvmBcanalyzer {
self
}
}

impl LlvmDwarfdump {
/// Construct a new `llvm-dwarfdump` invocation. This assumes that `llvm-dwarfdump` is available
/// at `$LLVM_BIN_DIR/llvm-dwarfdump`.
pub fn new() -> Self {
let llvm_dwarfdump = llvm_bin_dir().join("llvm-dwarfdump");
let cmd = Command::new(llvm_dwarfdump);
Self { cmd }
}

/// Provide an input file.
pub fn input<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
self.cmd.arg(path.as_ref());
self
}
}
5 changes: 3 additions & 2 deletions src/tools/run-make-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ pub use c_build::{build_native_dynamic_lib, build_native_static_lib, build_nativ
pub use clang::{clang, Clang};
pub use htmldocck::htmldocck;
pub use llvm::{
llvm_ar, llvm_bcanalyzer, llvm_filecheck, llvm_nm, llvm_objdump, llvm_profdata, llvm_readobj,
LlvmAr, LlvmBcanalyzer, LlvmFilecheck, LlvmNm, LlvmObjdump, LlvmProfdata, LlvmReadobj,
llvm_ar, llvm_bcanalyzer, llvm_dwarfdump, llvm_filecheck, llvm_nm, llvm_objdump, llvm_profdata,
llvm_readobj, LlvmAr, LlvmBcanalyzer, LlvmDwarfdump, LlvmFilecheck, LlvmNm, LlvmObjdump,
LlvmProfdata, LlvmReadobj,
};
pub use python::python_command;
pub use rustc::{aux_build, bare_rustc, rustc, Rustc};
Expand Down
1 change: 0 additions & 1 deletion src/tools/tidy/src/allowed_run_make_makefiles.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ run-make/macos-deployment-target/Makefile
run-make/min-global-align/Makefile
run-make/native-link-modifier-bundle/Makefile
run-make/no-alloc-shim/Makefile
run-make/remap-path-prefix-dwarf/Makefile
run-make/reproducible-build/Makefile
run-make/rlib-format-packed-bundled-libs/Makefile
run-make/split-debuginfo/Makefile
Expand Down
112 changes: 0 additions & 112 deletions tests/run-make/remap-path-prefix-dwarf/Makefile

This file was deleted.

202 changes: 202 additions & 0 deletions tests/run-make/remap-path-prefix-dwarf/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
// This test makes sure that --remap-path-prefix has the expected effects on paths in debuginfo.
// We explicitly switch to a directory that *is* a prefix of the directory our
// source code is contained in.
// It tests several cases, each of them has a detailed description attached to it.
// See https://github.com/rust-lang/rust/pull/96867

//@ ignore-windows
// Reason: the remap path prefix is not printed in the dwarf dump.

use run_make_support::{cwd, is_darwin, llvm_dwarfdump, rust_lib_name, rustc};

fn main() {
// The compiler is called with an *ABSOLUTE PATH* as input, and that absolute path *is* within
// the working directory of the compiler. We are remapping the path that contains `src`.
check_dwarf(DwarfTest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remark: this organization is very nice 👍

lib_name: "abs_input_inside_working_dir",
input_path: PathType::Absolute,
scope: None,
remap_path_prefix: PrefixType::Regular(format!("{}=REMAPPED", cwd().display())),
dwarf_test: DwarfDump::ContainsSrcPath,
});
check_dwarf(DwarfTest {
lib_name: "abs_input_inside_working_dir_scope",
input_path: PathType::Absolute,
scope: Some(ScopeType::Object),
remap_path_prefix: PrefixType::Regular(format!("{}=REMAPPED", cwd().display())),
dwarf_test: DwarfDump::ContainsSrcPath,
});
// The compiler is called with an *ABSOLUTE PATH* as input, and that absolute path is *not*
// within the working directory of the compiler. We are remapping both the path that contains
// `src` and the working directory to the same thing. This setup corresponds to a workaround
// that is needed when trying to remap everything to something that looks like a local
// path. Relative paths are interpreted as relative to the compiler's working directory (e.g.
// in debuginfo). If we also remap the working directory, the compiler strip it from other
// paths so that the final outcome is the desired one again.
check_dwarf(DwarfTest {
lib_name: "abs_input_outside_working_dir",
input_path: PathType::Absolute,
scope: None,
remap_path_prefix: PrefixType::Dual((
format!("{}=REMAPPED", cwd().display()),
"rmake_out=REMAPPED".to_owned(),
)),
dwarf_test: DwarfDump::ContainsSrcPath,
});
// The compiler is called with a *RELATIVE PATH* as input. We are remapping the working
// directory of the compiler, which naturally is an implicit prefix of our relative input path.
// Debuginfo will expand the relative path to an absolute path and we expect the working
// directory to be remapped in that expansion.
check_dwarf(DwarfTest {
lib_name: "rel_input_remap_working_dir",
input_path: PathType::Relative,
scope: None,
remap_path_prefix: PrefixType::Regular(format!("{}=REMAPPED", cwd().display())),
dwarf_test: DwarfDump::ContainsSrcPath,
});
check_dwarf(DwarfTest {
lib_name: "rel_input_remap_working_dir_scope",
input_path: PathType::Relative,
scope: Some(ScopeType::Object),
remap_path_prefix: PrefixType::Regular(format!("{}=REMAPPED", cwd().display())),
dwarf_test: DwarfDump::ContainsSrcPath,
});
check_dwarf(DwarfTest {
lib_name: "rel_input_remap_working_dir_scope",
input_path: PathType::Relative,
scope: Some(ScopeType::Diagnostics),
remap_path_prefix: PrefixType::Regular(format!("{}=REMAPPED", cwd().display())),
dwarf_test: DwarfDump::AvoidSrcPath,
});
// The compiler is called with a *RELATIVE PATH* as input. We are remapping a *SUB-DIRECTORY*
// of the compiler's working directory. This test makes sure that that directory is remapped
// even though it won't actually show up in this form in the compiler's SourceMap and instead
// is only constructed on demand during debuginfo generation.
check_dwarf(DwarfTest {
lib_name: "rel_input_remap_working_dir_child",
input_path: PathType::Relative,
scope: None,
remap_path_prefix: PrefixType::Regular(format!("{}=REMAPPED", cwd().join("src").display())),
dwarf_test: DwarfDump::ChildTest,
});
// The compiler is called with a *RELATIVE PATH* as input. We are remapping a
// *PARENT DIRECTORY* of the compiler's working directory.
check_dwarf(DwarfTest {
lib_name: "rel_input_remap_working_dir_parent",
input_path: PathType::Relative,
scope: None,
remap_path_prefix: PrefixType::Regular(format!(
"{}=REMAPPED",
cwd().parent().unwrap().display()
)),
dwarf_test: DwarfDump::ParentTest,
});
}

#[track_caller]
fn check_dwarf(test: DwarfTest) {
let mut rustc = rustc();
match test.input_path {
PathType::Absolute => rustc.input(cwd().join("src/quux.rs")),
PathType::Relative => rustc.input("src/quux.rs"),
};
rustc.output(rust_lib_name(test.lib_name));
rustc.arg("-Cdebuginfo=2");
if let Some(scope) = test.scope {
match scope {
ScopeType::Object => rustc.arg("-Zremap-path-scope=object"),
ScopeType::Diagnostics => rustc.arg("-Zremap-path-scope=diagnostics"),
};
if is_darwin() {
rustc.arg("-Csplit-debuginfo=off");
}
}
match test.remap_path_prefix {
PrefixType::Regular(prefix) => {
// We explicitly switch to a directory that *is* a prefix of the directory our
// source code is contained in.
rustc.arg("--remap-path-prefix");
rustc.arg(prefix);
}
PrefixType::Dual((prefix1, prefix2)) => {
// We explicitly switch to a directory that is *not* a prefix of the directory our
// source code is contained in.
rustc.arg("--remap-path-prefix");
rustc.arg(prefix1);
rustc.arg("--remap-path-prefix");
rustc.arg(prefix2);
}
}
rustc.run();
match test.dwarf_test {
DwarfDump::ContainsSrcPath => {
llvm_dwarfdump()
.input(rust_lib_name(test.lib_name))
.run()
// We expect the path to the main source file to be remapped.
.assert_stdout_contains("REMAPPED/src/quux.rs")
// No weird duplication of remapped components (see #78479)
.assert_stdout_not_contains("REMAPPED/REMAPPED");
}
DwarfDump::AvoidSrcPath => {
llvm_dwarfdump()
.input(rust_lib_name(test.lib_name))
.run()
.assert_stdout_not_contains("REMAPPED/src/quux.rs")
.assert_stdout_not_contains("REMAPPED/REMAPPED");
}
DwarfDump::ChildTest => {
llvm_dwarfdump()
.input(rust_lib_name(test.lib_name))
.run()
// We expect `src/quux.rs` to have been remapped to `REMAPPED/quux.rs`.
.assert_stdout_contains("REMAPPED/quux.rs")
// We don't want to find the path that we just remapped anywhere in the DWARF
.assert_stdout_not_contains(cwd().join("src").to_str().unwrap())
// No weird duplication of remapped components (see #78479)
.assert_stdout_not_contains("REMAPPED/REMAPPED");
}
DwarfDump::ParentTest => {
llvm_dwarfdump()
.input(rust_lib_name(test.lib_name))
.run()
// We expect `src/quux.rs` to have been remapped to
// `REMAPPED/remap-path-prefix-dwarf/src/quux.rs`.
.assert_stdout_contains("REMAPPED/rmake_out/src/quux.rs")
// We don't want to find the path that we just remapped anywhere in the DWARF
.assert_stdout_not_contains(cwd().parent().unwrap().to_str().unwrap())
// No weird duplication of remapped components (see #78479)
.assert_stdout_not_contains("REMAPPED/REMAPPED");
}
};
}

struct DwarfTest {
lib_name: &'static str,
input_path: PathType,
scope: Option<ScopeType>,
remap_path_prefix: PrefixType,
dwarf_test: DwarfDump,
}

enum PathType {
Absolute,
Relative,
}

enum ScopeType {
Object,
Diagnostics,
}

enum DwarfDump {
ContainsSrcPath,
AvoidSrcPath,
ChildTest,
ParentTest,
}

enum PrefixType {
Regular(String),
Dual((String, String)),
}
Loading