Skip to content

Commit 89137b1

Browse files
committed
Auto merge of rust-lang#127825 - Oneirical:self-testeem, r=<try>
Migrate `macos-fat-archive`, `manual-link` and `archive-duplicate-names` `run-make` tests to rmake Part of rust-lang#121876 and the associated [Google Summer of Code project](https://blog.rust-lang.org/2024/05/01/gsoc-2024-selected-projects.html). Please try: try-job: x86_64-msvc try-job: aarch64-apple
2 parents 16b5690 + 3a14ef0 commit 89137b1

File tree

9 files changed

+132
-38
lines changed

9 files changed

+132
-38
lines changed

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

+22-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ pub use cc::{cc, extra_c_flags, extra_cxx_flags, Cc};
3030
pub use clang::{clang, Clang};
3131
pub use diff::{diff, Diff};
3232
pub use llvm::{
33-
llvm_filecheck, llvm_objdump, llvm_profdata, llvm_readobj, LlvmFilecheck, LlvmObjdump,
34-
LlvmProfdata, LlvmReadobj,
33+
llvm_ar, llvm_filecheck, llvm_objdump, llvm_profdata, llvm_readobj, LlvmAr, LlvmFilecheck,
34+
LlvmObjdump, LlvmProfdata, LlvmReadobj,
3535
};
3636
pub use run::{cmd, run, run_fail, run_with_args};
3737
pub use rustc::{aux_build, bare_rustc, rustc, Rustc};
@@ -321,6 +321,26 @@ pub fn count_regex_matches_in_files_with_extension(re: &regex::Regex, ext: &str)
321321
count
322322
}
323323

324+
/// Builds a static lib (`.lib` on Windows MSVC and `.a` for the rest) with the given name.
325+
#[track_caller]
326+
pub fn build_native_static_lib(lib_name: &str) -> PathBuf {
327+
let obj_file = if is_msvc() { format!("{lib_name}") } else { format!("{lib_name}.o") };
328+
let src = format!("{lib_name}.c");
329+
let lib_path = static_lib_name(lib_name);
330+
if is_msvc() {
331+
cc().arg("-c").out_exe(&obj_file).input(src).run();
332+
} else {
333+
cc().arg("-v").arg("-c").out_exe(&obj_file).input(src).run();
334+
};
335+
let mut obj_file = PathBuf::from(format!("{lib_name}.o"));
336+
if is_msvc() {
337+
obj_file.set_extension("");
338+
obj_file.set_extension("obj");
339+
}
340+
llvm_ar().obj_to_ar().output_input(&lib_path, &obj_file).run();
341+
path(lib_path)
342+
}
343+
324344
/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is
325345
/// available on the platform!
326346
#[track_caller]

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

+37
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ pub fn llvm_objdump() -> LlvmObjdump {
2929
LlvmObjdump::new()
3030
}
3131

32+
/// Construct a new `llvm-ar` invocation. This assumes that `llvm-ar` is available
33+
/// at `$LLVM_BIN_DIR/llvm-ar`.
34+
pub fn llvm_ar() -> LlvmAr {
35+
LlvmAr::new()
36+
}
37+
3238
/// A `llvm-readobj` invocation builder.
3339
#[derive(Debug)]
3440
#[must_use]
@@ -57,10 +63,18 @@ pub struct LlvmObjdump {
5763
cmd: Command,
5864
}
5965

66+
/// A `llvm-ar` invocation builder.
67+
#[derive(Debug)]
68+
#[must_use]
69+
pub struct LlvmAr {
70+
cmd: Command,
71+
}
72+
6073
crate::impl_common_helpers!(LlvmReadobj);
6174
crate::impl_common_helpers!(LlvmProfdata);
6275
crate::impl_common_helpers!(LlvmFilecheck);
6376
crate::impl_common_helpers!(LlvmObjdump);
77+
crate::impl_common_helpers!(LlvmAr);
6478

6579
/// Generate the path to the bin directory of LLVM.
6680
#[must_use]
@@ -204,3 +218,26 @@ impl LlvmObjdump {
204218
self
205219
}
206220
}
221+
222+
impl LlvmAr {
223+
/// Construct a new `llvm-ar` invocation. This assumes that `llvm-ar` is available
224+
/// at `$LLVM_BIN_DIR/llvm-ar`.
225+
pub fn new() -> Self {
226+
let llvm_ar = llvm_bin_dir().join("llvm-ar");
227+
let cmd = Command::new(llvm_ar);
228+
Self { cmd }
229+
}
230+
231+
pub fn obj_to_ar(&mut self) -> &mut Self {
232+
self.cmd.arg("rcus");
233+
self
234+
}
235+
236+
/// Provide an output, then an input file. Bundled in one function, as llvm-ar has
237+
/// no "--output"-style flag.
238+
pub fn output_input(&mut self, out: impl AsRef<Path>, input: impl AsRef<Path>) -> &mut Self {
239+
self.cmd.arg(out.as_ref());
240+
self.cmd.arg(input.as_ref());
241+
self
242+
}
243+
}

src/tools/tidy/src/allowed_run_make_makefiles.txt

-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
run-make/archive-duplicate-names/Makefile
21
run-make/atomic-lock-free/Makefile
32
run-make/branch-protection-check-IBT/Makefile
43
run-make/c-dynamic-dylib/Makefile
@@ -76,8 +75,6 @@ run-make/lto-linkage-used-attr/Makefile
7675
run-make/lto-no-link-whole-rlib/Makefile
7776
run-make/lto-smoke-c/Makefile
7877
run-make/macos-deployment-target/Makefile
79-
run-make/macos-fat-archive/Makefile
80-
run-make/manual-link/Makefile
8178
run-make/min-global-align/Makefile
8279
run-make/missing-crate-dependency/Makefile
8380
run-make/mixing-libs/Makefile

tests/run-make/archive-duplicate-names/Makefile

-16
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// When two object archives with the same filename are present, an iterator is supposed to
2+
// inspect each object, recognize the duplication and extract each one to a different directory.
3+
// This test checks that this duplicate handling behaviour has not been broken.
4+
// See https://github.com/rust-lang/rust/pull/24439
5+
6+
//@ ignore-cross-compile
7+
// Reason: the compiled binary is executed
8+
9+
use run_make_support::{cc, fs_wrapper, is_msvc, llvm_ar, run, rustc};
10+
11+
fn main() {
12+
fs_wrapper::create_dir("a");
13+
fs_wrapper::create_dir("b");
14+
compile_obj_force_foo("a", "foo");
15+
compile_obj_force_foo("b", "bar");
16+
let mut ar = llvm_ar();
17+
ar.obj_to_ar().arg("libfoo.a");
18+
if is_msvc() {
19+
ar.arg("a/foo.obj").arg("b/foo.obj").run();
20+
} else {
21+
ar.arg("a/foo.o").arg("b/foo.o").run();
22+
}
23+
rustc().input("foo.rs").run();
24+
rustc().input("bar.rs").run();
25+
run("bar");
26+
}
27+
28+
#[track_caller]
29+
pub fn compile_obj_force_foo(dir: &str, lib_name: &str) {
30+
let obj_file = if is_msvc() { format!("{dir}/foo") } else { format!("{dir}/foo.o") };
31+
let src = format!("{lib_name}.c");
32+
if is_msvc() {
33+
cc().arg("-c").out_exe(&obj_file).input(src).run();
34+
} else {
35+
cc().arg("-v").arg("-c").out_exe(&obj_file).input(src).run();
36+
};
37+
}

tests/run-make/macos-fat-archive/Makefile

-10
This file was deleted.
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// macOS (and iOS) has a concept of universal (fat) binaries which contain code for multiple CPU
2+
// architectures in the same file. Apple is migrating from x86_64 to aarch64 CPUs,
3+
// so for the next few years it will be important for macOS developers to
4+
// build "fat" binaries (executables and cdylibs).
5+
6+
// Rustc used to be unable to handle these special libraries, which was fixed in #98736. If
7+
// compilation in this test is successful, the native fat library was successfully linked to.
8+
// See https://github.com/rust-lang/rust/issues/55235
9+
10+
//@ only-apple
11+
12+
use run_make_support::{cc, llvm_ar, rustc};
13+
14+
fn main() {
15+
cc().args(&["-arch", "arm64", "-arch", "x86_64", "native-library.c", "-c"])
16+
.out_exe("native-library.o")
17+
.run();
18+
llvm_ar().obj_to_ar().output_input("libnative-library.a", "native-library.o").run();
19+
rustc().input("lib.rs").crate_type("lib").arg("-lstatic=native-library").run();
20+
}

tests/run-make/manual-link/Makefile

-7
This file was deleted.

tests/run-make/manual-link/rmake.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// A smoke test for the `-l` command line rustc flag, which manually links to the selected
2+
// library. Useful for native libraries, this is roughly equivalent to `#[link]` in Rust code.
3+
// If compilation succeeds, the flag successfully linked the native library.
4+
// See https://github.com/rust-lang/rust/pull/18470
5+
6+
//@ ignore-cross-compile
7+
// Reason: the compiled binary is executed
8+
9+
use run_make_support::{build_native_static_lib, run, rustc};
10+
11+
fn main() {
12+
build_native_static_lib("bar");
13+
rustc().input("foo.rs").arg("-lstatic=bar").run();
14+
rustc().input("main.rs").run();
15+
run("main");
16+
}

0 commit comments

Comments
 (0)