Skip to content

Commit 2e240c5

Browse files
committed
rewrite native-link-modifier-whole-archive to rmake
1 parent b889321 commit 2e240c5

File tree

5 files changed

+109
-54
lines changed

5 files changed

+109
-54
lines changed

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

+23
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ pub fn cc() -> Cc {
1515
Cc::new()
1616
}
1717

18+
/// Construct a new platform-specific CXX compiler invocation.
19+
/// CXX_DEFAULT_FLAGS is passed from compiletest.
20+
#[track_caller]
21+
pub fn cxx() -> Cc {
22+
Cc::new_cxx()
23+
}
24+
1825
/// A platform-specific C compiler invocation builder. The specific C compiler used is
1926
/// passed down from compiletest.
2027
#[derive(Debug)]
@@ -44,6 +51,22 @@ impl Cc {
4451
Self { cmd }
4552
}
4653

54+
/// Construct a new platform-specific CXX compiler invocation.
55+
/// CXX_DEFAULT_FLAGS is passed from compiletest.
56+
#[track_caller]
57+
pub fn new_cxx() -> Self {
58+
let compiler = env_var("CXX");
59+
60+
let mut cmd = Command::new(compiler);
61+
62+
let default_cflags = env_var("CXX_DEFAULT_FLAGS");
63+
for flag in default_cflags.split(char::is_whitespace) {
64+
cmd.arg(flag);
65+
}
66+
67+
Self { cmd }
68+
}
69+
4770
/// Specify path of the input file.
4871
pub fn input<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
4972
self.cmd.arg(path.as_ref());

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub use external_deps::{c_build, cc, clang, htmldocck, llvm, python, rustc, rust
4444

4545
// These rely on external dependencies.
4646
pub use c_build::build_native_static_lib;
47-
pub use cc::{cc, extra_c_flags, extra_cxx_flags, Cc};
47+
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::{

src/tools/tidy/src/allowed_run_make_makefiles.txt

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ run-make/lto-linkage-used-attr/Makefile
4747
run-make/macos-deployment-target/Makefile
4848
run-make/min-global-align/Makefile
4949
run-make/native-link-modifier-bundle/Makefile
50-
run-make/native-link-modifier-whole-archive/Makefile
5150
run-make/no-alloc-shim/Makefile
5251
run-make/no-builtins-attribute/Makefile
5352
run-make/no-duplicate-libs/Makefile

tests/run-make/native-link-modifier-whole-archive/Makefile

-52
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// This test case makes sure that native libraries are linked with appropriate semantics
2+
// when the `[+-]bundle,[+-]whole-archive` modifiers are applied to them.
3+
// The test works by checking that the resulting executables produce the expected output,
4+
// part of which is emitted by otherwise unreferenced C code. If +whole-archive didn't work
5+
// that code would never make it into the final executable and we'd thus be missing some
6+
// of the output.
7+
// See https://github.com/rust-lang/rust/issues/88085
8+
9+
//@ ignore-cross-compile
10+
// Reason: compiling C++ code does not work well when cross-compiling
11+
// plus, the compiled binary is executed
12+
13+
use run_make_support::{cxx, is_msvc, llvm_ar, run, run_with_args, rustc, static_lib_name};
14+
15+
fn main() {
16+
let mut cxx = cxx();
17+
if is_msvc() {
18+
cxx.arg("-EHs");
19+
}
20+
cxx.input("c_static_lib_with_constructor.cpp")
21+
.arg("-c")
22+
.out_exe("libc_static_lib_with_constructor")
23+
.run();
24+
25+
let mut llvm_ar = llvm_ar();
26+
llvm_ar.obj_to_ar();
27+
if is_msvc() {
28+
llvm_ar
29+
.output_input(
30+
static_lib_name("c_static_lib_with_constructor"),
31+
"libc_static_lib_with_constructor.obj",
32+
)
33+
.run();
34+
} else {
35+
llvm_ar
36+
.output_input(
37+
static_lib_name("c_static_lib_with_constructor"),
38+
"libc_static_lib_with_constructor",
39+
)
40+
.run();
41+
}
42+
43+
// Native lib linked directly into executable
44+
rustc()
45+
.input("directly_linked.rs")
46+
.arg("-lstatic:+whole-archive=c_static_lib_with_constructor")
47+
.run();
48+
49+
// Native lib linked into test executable, +whole-archive
50+
rustc()
51+
.input("directly_linked_test_plus_whole_archive.rs")
52+
.arg("--test")
53+
.arg("-lstatic:+whole-archive=c_static_lib_with_constructor")
54+
.run();
55+
56+
// Native lib linked into test executable, -whole-archive
57+
rustc()
58+
.input("directly_linked_test_minus_whole_archive.rs")
59+
.arg("--test")
60+
.arg("-lstatic:-whole-archive=c_static_lib_with_constructor")
61+
.run();
62+
63+
// Native lib linked into rlib with via commandline
64+
rustc()
65+
.input("rlib_with_cmdline_native_lib.rs")
66+
.crate_type("rlib")
67+
.arg("-lstatic:-bundle,+whole-archive=c_static_lib_with_constructor")
68+
.run();
69+
// Native lib linked into RLIB via `-l static:-bundle,+whole-archive`, RLIB linked into executable
70+
rustc().input("indirectly_linked.rs").run();
71+
72+
// Native lib linked into rlib via `#[link()]` attribute on extern block.
73+
rustc().input("native_lib_in_src.rs").crate_type("rlib").run();
74+
// Native lib linked into RLIB via #[link] attribute, RLIB linked into executable
75+
rustc().input("indirectly_linked_via_attr.rs").run();
76+
77+
run("directly_linked").assert_stdout_contains("static-initializer.directly_linked.");
78+
run_with_args("directly_linked_test_plus_whole_archive", &["--nocapture"])
79+
.assert_stdout_contains("static-initializer.");
80+
run_with_args("directly_linked_test_minus_whole_archive", &["--nocapture"])
81+
.assert_stdout_not_contains("static-initializer.");
82+
run("indirectly_linked").assert_stdout_contains("static-initializer.indirectly_linked.");
83+
run("indirectly_linked_via_attr")
84+
.assert_stdout_contains("static-initializer.native_lib_in_src.");
85+
}

0 commit comments

Comments
 (0)