Skip to content

Commit ea04b0a

Browse files
committed
use llvm-nm in symbol-visibility rmake test
1 parent dcaa17a commit ea04b0a

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

src/tools/run-make-support/src/external_deps/llvm.rs

+30
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ pub fn llvm_ar() -> LlvmAr {
3636
LlvmAr::new()
3737
}
3838

39+
/// Construct a new `llvm-nm` invocation. This assumes that `llvm-nm` is available
40+
/// at `$LLVM_BIN_DIR/llvm-nm`.
41+
pub fn llvm_nm() -> LlvmNm {
42+
LlvmNm::new()
43+
}
44+
3945
/// A `llvm-readobj` invocation builder.
4046
#[derive(Debug)]
4147
#[must_use]
@@ -71,11 +77,19 @@ pub struct LlvmAr {
7177
cmd: Command,
7278
}
7379

80+
/// A `llvm-nm` invocation builder.
81+
#[derive(Debug)]
82+
#[must_use]
83+
pub struct LlvmNm {
84+
cmd: Command,
85+
}
86+
7487
crate::macros::impl_common_helpers!(LlvmReadobj);
7588
crate::macros::impl_common_helpers!(LlvmProfdata);
7689
crate::macros::impl_common_helpers!(LlvmFilecheck);
7790
crate::macros::impl_common_helpers!(LlvmObjdump);
7891
crate::macros::impl_common_helpers!(LlvmAr);
92+
crate::macros::impl_common_helpers!(LlvmNm);
7993

8094
/// Generate the path to the bin directory of LLVM.
8195
#[must_use]
@@ -244,3 +258,19 @@ impl LlvmAr {
244258
self
245259
}
246260
}
261+
262+
impl LlvmNm {
263+
/// Construct a new `llvm-nm` invocation. This assumes that `llvm-nm` is available
264+
/// at `$LLVM_BIN_DIR/llvm-nm`.
265+
pub fn new() -> Self {
266+
let llvm_nm = llvm_bin_dir().join("llvm-nm");
267+
let cmd = Command::new(llvm_nm);
268+
Self { cmd }
269+
}
270+
271+
/// Provide an input file.
272+
pub fn input<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
273+
self.cmd.arg(path.as_ref());
274+
self
275+
}
276+
}

src/tools/run-make-support/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ pub use cc::{cc, cxx, extra_c_flags, extra_cxx_flags, Cc};
4848
pub use clang::{clang, Clang};
4949
pub use htmldocck::htmldocck;
5050
pub use llvm::{
51-
llvm_ar, llvm_filecheck, llvm_objdump, llvm_profdata, llvm_readobj, LlvmAr, LlvmFilecheck,
52-
LlvmObjdump, LlvmProfdata, LlvmReadobj,
51+
llvm_ar, llvm_filecheck, llvm_nm, llvm_objdump, llvm_profdata, llvm_readobj, LlvmAr,
52+
LlvmFilecheck, LlvmNm, LlvmObjdump, LlvmProfdata, LlvmReadobj,
5353
};
5454
pub use python::python_command;
5555
pub use rustc::{aux_build, bare_rustc, rustc, Rustc};

tests/run-make/symbol-visibility/rmake.rs

+20-9
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66

77
//@ ignore-windows-msvc
88

9-
use run_make_support::{bin_name, dynamic_lib_name, is_windows, llvm_readobj, regex, rustc};
9+
//FIXME(Oneirical): This currently uses llvm-nm for symbol detection. However,
10+
// the custom Rust-based solution of #128314 may prove to be an interesting alternative.
11+
12+
use run_make_support::{bin_name, dynamic_lib_name, is_darwin, is_windows, llvm_nm, regex, rustc};
1013

1114
fn main() {
12-
let mut cdylib_name = dynamic_lib_name("a_cdylib");
13-
let mut rdylib_name = dynamic_lib_name("a_rust_dylib");
15+
let cdylib_name = dynamic_lib_name("a_cdylib");
16+
let rdylib_name = dynamic_lib_name("a_rust_dylib");
1417
let exe_name = bin_name("an_executable");
15-
let mut combined_cdylib_name = dynamic_lib_name("combined_rlib_dylib");
18+
let combined_cdylib_name = dynamic_lib_name("combined_rlib_dylib");
1619
rustc().arg("-Zshare-generics=no").input("an_rlib.rs").run();
1720
rustc().arg("-Zshare-generics=no").input("a_cdylib.rs").run();
1821
rustc().arg("-Zshare-generics=no").input("a_rust_dylib.rs").run();
@@ -74,13 +77,13 @@ fn main() {
7477

7578
// Check the combined case, where we generate a cdylib and an rlib in the same
7679
// compilation session:
77-
// Check that a cdylib exports its public //[no_mangle] functions
80+
// Check that a cdylib exports its public #[no_mangle] functions
7881
symbols_check(
7982
&combined_cdylib_name,
8083
SymbolCheckType::StrSymbol("public_c_function_from_cdylib"),
8184
true,
8285
);
83-
// Check that a cdylib exports the public //[no_mangle] functions of dependencies
86+
// Check that a cdylib exports the public #[no_mangle] functions of dependencies
8487
symbols_check(
8588
&combined_cdylib_name,
8689
SymbolCheckType::StrSymbol("public_c_function_from_rlib"),
@@ -94,9 +97,9 @@ fn main() {
9497
rustc().arg("-Zshare-generics=yes").input("a_rust_dylib.rs").run();
9598
rustc().arg("-Zshare-generics=yes").input("an_executable.rs").run();
9699

97-
// Check that a cdylib exports its public //[no_mangle] functions
100+
// Check that a cdylib exports its public #[no_mangle] functions
98101
symbols_check(&cdylib_name, SymbolCheckType::StrSymbol("public_c_function_from_cdylib"), true);
99-
// Check that a cdylib exports the public //[no_mangle] functions of dependencies
102+
// Check that a cdylib exports the public #[no_mangle] functions of dependencies
100103
symbols_check(&cdylib_name, SymbolCheckType::StrSymbol("public_c_function_from_rlib"), true);
101104
// Check that a cdylib DOES NOT export any public Rust functions
102105
symbols_check(&cdylib_name, SymbolCheckType::AnyRustSymbol, false);
@@ -142,7 +145,15 @@ fn main() {
142145

143146
#[track_caller]
144147
fn symbols_check(path: &str, symbol_check_type: SymbolCheckType, exists_once: bool) {
145-
let out = llvm_readobj().arg("--dyn-symbols").input(path).run().invalid_stdout_utf8();
148+
let mut nm = llvm_nm();
149+
if is_windows() {
150+
nm.arg("--extern-only");
151+
} else if is_darwin() {
152+
nm.arg("--extern-only").arg("--defined-only");
153+
} else {
154+
nm.arg("--dynamic");
155+
}
156+
let out = nm.input(path).run().stdout_utf8();
146157
assert_eq!(
147158
out.lines()
148159
.filter(|&line| !line.contains("__imp_") && has_symbol(line, symbol_check_type))

0 commit comments

Comments
 (0)