Skip to content

Commit c8455d2

Browse files
committed
rewrite cross-lang-lto to rmake
1 parent 4db3d12 commit c8455d2

File tree

5 files changed

+144
-60
lines changed

5 files changed

+144
-60
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-bcanalyzer` invocation. This assumes that `llvm-bcanalyzer` is available
40+
/// at `$LLVM_BIN_DIR/llvm-bcanalyzer`.
41+
pub fn llvm_bcanalyzer() -> LlvmBcanalyzer {
42+
LlvmBcanalyzer::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-bcanalyzer` invocation builder.
81+
#[derive(Debug)]
82+
#[must_use]
83+
pub struct LlvmBcanalyzer {
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!(LlvmBcanalyzer);
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 LlvmBcanalyzer {
263+
/// Construct a new `llvm-bcanalyzer` invocation. This assumes that `llvm-bcanalyzer` is available
264+
/// at `$LLVM_BIN_DIR/llvm-bcanalyzer`.
265+
pub fn new() -> Self {
266+
let llvm_bcanalyzer = llvm_bin_dir().join("llvm-bcanalyzer");
267+
let cmd = Command::new(llvm_bcanalyzer);
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_bcanalyzer, llvm_filecheck, llvm_objdump, llvm_profdata, llvm_readobj, LlvmAr,
52+
LlvmBcanalyzer, LlvmFilecheck, LlvmObjdump, LlvmProfdata, LlvmReadobj,
5353
};
5454
pub use python::python_command;
5555
pub use rustc::{aux_build, bare_rustc, rustc, Rustc};

src/tools/tidy/src/allowed_run_make_makefiles.txt

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ run-make/cdylib-dylib-linkage/Makefile
44
run-make/cross-lang-lto-clang/Makefile
55
run-make/cross-lang-lto-pgo-smoketest/Makefile
66
run-make/cross-lang-lto-upstream-rlibs/Makefile
7-
run-make/cross-lang-lto/Makefile
87
run-make/dep-info-doesnt-run-much/Makefile
98
run-make/dep-info-spaces/Makefile
109
run-make/dep-info/Makefile

tests/run-make/cross-lang-lto/Makefile

-57
This file was deleted.
+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// This test checks that the object files we generate are actually
2+
// LLVM bitcode files (as used by linker LTO plugins) when compiling with
3+
// -Clinker-plugin-lto.
4+
// See https://github.com/rust-lang/rust/pull/50000
5+
6+
//FIXME(Oneirical): try it on windows
7+
8+
#![feature(path_file_prefix)]
9+
10+
use std::path::PathBuf;
11+
12+
use run_make_support::{
13+
cwd, has_extension, has_prefix, llvm_ar, llvm_bcanalyzer, path, rfs, rust_lib_name, rustc,
14+
shallow_find_files, static_lib_name,
15+
};
16+
17+
fn main() {
18+
check_bitcode(LibBuild {
19+
source: path("lib.rs"),
20+
crate_type: Some("staticlib"),
21+
output: path(static_lib_name("liblib")),
22+
lto: None,
23+
emit_obj: false,
24+
});
25+
check_bitcode(LibBuild {
26+
source: path("lib.rs"),
27+
crate_type: Some("staticlib"),
28+
output: path(static_lib_name("liblib-fat-lto")),
29+
lto: Some("fat"),
30+
emit_obj: false,
31+
});
32+
check_bitcode(LibBuild {
33+
source: path("lib.rs"),
34+
crate_type: Some("staticlib"),
35+
output: path(static_lib_name("liblib-thin-lto")),
36+
lto: Some("thin"),
37+
emit_obj: false,
38+
});
39+
check_bitcode(LibBuild {
40+
source: path("lib.rs"),
41+
crate_type: Some("rlib"),
42+
output: path(rust_lib_name("liblib")),
43+
lto: None,
44+
emit_obj: false,
45+
});
46+
check_bitcode(LibBuild {
47+
source: path("lib.rs"),
48+
crate_type: Some("cdylib"),
49+
output: path("cdylib.o"),
50+
lto: None,
51+
emit_obj: true,
52+
});
53+
check_bitcode(LibBuild {
54+
source: path("lib.rs"),
55+
crate_type: Some("dylib"),
56+
output: path("rdylib.o"),
57+
lto: None,
58+
emit_obj: true,
59+
});
60+
check_bitcode(LibBuild {
61+
source: path("main.rs"),
62+
crate_type: None,
63+
output: path("exe.o"),
64+
lto: None,
65+
emit_obj: true,
66+
});
67+
}
68+
69+
#[track_caller]
70+
fn check_bitcode(instructions: LibBuild) {
71+
let mut rustc = rustc();
72+
rustc
73+
.input(instructions.source)
74+
.output(instructions.output.clone())
75+
.opt_level("2")
76+
.codegen_units(1)
77+
.arg("-Clinker-plugin-lto");
78+
if instructions.emit_obj {
79+
rustc.emit("obj");
80+
}
81+
if let Some(crate_type) = instructions.crate_type {
82+
rustc.crate_type(crate_type);
83+
}
84+
if let Some(lto) = instructions.lto {
85+
rustc.arg(format!("-Clto={lto}"));
86+
}
87+
rustc.run();
88+
89+
if instructions.output.clone().extension().unwrap().to_str().unwrap() != "o" {
90+
// Remove all potential leftover object files, then turn the output into an object file.
91+
for object in shallow_find_files(cwd(), |path| has_extension(path, "o")) {
92+
rfs::remove_file(object);
93+
}
94+
llvm_ar().arg("x").arg(instructions.output.clone()).run();
95+
}
96+
97+
for object in shallow_find_files(cwd(), |path| {
98+
has_prefix(path, instructions.output.file_prefix().unwrap().to_str().unwrap())
99+
&& has_extension(path, "o")
100+
}) {
101+
// All generated object files should be LLVM bitcode files - this will fail otherwise.
102+
llvm_bcanalyzer().input(object).run();
103+
}
104+
}
105+
106+
struct LibBuild {
107+
source: PathBuf,
108+
crate_type: Option<&'static str>,
109+
output: PathBuf,
110+
lto: Option<&'static str>,
111+
emit_obj: bool,
112+
}

0 commit comments

Comments
 (0)