Skip to content

Commit e8d5bc4

Browse files
committed
get rid of custom linker setup
1 parent 1275bab commit e8d5bc4

File tree

2 files changed

+30
-123
lines changed

2 files changed

+30
-123
lines changed

crates/intrinsic-test/src/arm/compile.rs

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,12 @@ pub fn build_cpp_compilation(config: &ProcessedCli) -> Option<CppCompilation> {
1818
command = command.add_arch_flags(vec!["faminmax", "lut", "sha3"]);
1919
}
2020

21-
/*
22-
* clang++ cannot link an aarch64_be object file, so we invoke
23-
* aarch64_be-unknown-linux-gnu's C++ linker. This ensures that we
24-
* are testing the intrinsics against LLVM.
25-
*
26-
* Note: setting `--sysroot=<...>` which is the obvious thing to do
27-
* does not work as it gets caught up with `#include_next <stdlib.h>`
28-
* not existing...
29-
*/
21+
if !cpp_compiler.contains("clang") {
22+
command = command.add_extra_flag("-flax-vector-conversions");
23+
}
24+
25+
let mut cpp_compiler = command.into_cpp_compilation();
26+
3027
if config.target.contains("aarch64_be") {
3128
let Some(ref cxx_toolchain_dir) = config.cxx_toolchain_dir else {
3229
panic!(
@@ -35,27 +32,20 @@ pub fn build_cpp_compilation(config: &ProcessedCli) -> Option<CppCompilation> {
3532
)
3633
};
3734

38-
let linker = if let Some(ref linker) = config.linker {
39-
linker.to_owned()
40-
} else {
41-
format!("{cxx_toolchain_dir}/bin/aarch64_be-none-linux-gnu-g++")
42-
};
43-
44-
trace!("using linker: {linker}");
45-
46-
command = command.set_linker(linker).set_include_paths(vec![
47-
"/include",
48-
"/aarch64_be-none-linux-gnu/include",
49-
"/aarch64_be-none-linux-gnu/include/c++/14.2.1",
50-
"/aarch64_be-none-linux-gnu/include/c++/14.2.1/aarch64_be-none-linux-gnu",
51-
"/aarch64_be-none-linux-gnu/include/c++/14.2.1/backward",
52-
"/aarch64_be-none-linux-gnu/libc/usr/include",
35+
cpp_compiler.command_mut().args([
36+
&format!("--sysroot={cxx_toolchain_dir}/aarch64_be-none-linux-gnu/libc"),
37+
"--include-directory",
38+
&format!("{cxx_toolchain_dir}/aarch64_be-none-linux-gnu/include/c++/14.2.1"),
39+
"--include-directory",
40+
&format!("{cxx_toolchain_dir}/aarch64_be-none-linux-gnu/include/c++/14.2.1/aarch64_be-none-linux-gnu"),
41+
"-L",
42+
&format!("{cxx_toolchain_dir}/lib/gcc/aarch64_be-none-linux-gnu/14.2.1"),
43+
"-L",
44+
&format!("{cxx_toolchain_dir}/aarch64_be-none-linux-gnu/libc/usr/lib"),
45+
"-B",
46+
&format!("{cxx_toolchain_dir}/lib/gcc/aarch64_be-none-linux-gnu/14.2.1"),
5347
]);
5448
}
5549

56-
if !cpp_compiler.contains("clang") {
57-
command = command.add_extra_flag("-flax-vector-conversions");
58-
}
59-
60-
Some(command.into_cpp_compilation())
50+
Some(cpp_compiler)
6151
}

crates/intrinsic-test/src/common/compile_c.rs

Lines changed: 11 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ pub struct CompilationCommandBuilder {
55
cxx_toolchain_dir: Option<String>,
66
arch_flags: Vec<String>,
77
optimization: String,
8-
include_paths: Vec<String>,
98
project_root: Option<String>,
10-
linker: Option<String>,
119
extra_flags: Vec<String>,
1210
}
1311

@@ -19,9 +17,7 @@ impl CompilationCommandBuilder {
1917
cxx_toolchain_dir: None,
2018
arch_flags: Vec::new(),
2119
optimization: "2".to_string(),
22-
include_paths: Vec::new(),
2320
project_root: None,
24-
linker: None,
2521
extra_flags: Vec::new(),
2622
}
2723
}
@@ -53,25 +49,12 @@ impl CompilationCommandBuilder {
5349
self
5450
}
5551

56-
/// Sets a list of include paths for compilation.
57-
/// The paths that are passed must be relative to the
58-
/// "cxx_toolchain_dir" directory path.
59-
pub fn set_include_paths(mut self, paths: Vec<&str>) -> Self {
60-
self.include_paths = paths.into_iter().map(|path| path.to_string()).collect();
61-
self
62-
}
63-
6452
/// Sets the root path of all the generated test files.
6553
pub fn set_project_root(mut self, path: &str) -> Self {
6654
self.project_root = Some(path.to_string());
6755
self
6856
}
6957

70-
pub fn set_linker(mut self, linker: String) -> Self {
71-
self.linker = Some(linker);
72-
self
73-
}
74-
7558
pub fn add_extra_flags(mut self, flags: Vec<&str>) -> Self {
7659
let mut flags: Vec<String> = flags.into_iter().map(|f| f.to_string()).collect();
7760
self.extra_flags.append(&mut flags);
@@ -104,30 +87,11 @@ impl CompilationCommandBuilder {
10487
cpp_compiler.arg(format!("--target={target}"));
10588
}
10689

107-
if let (Some(linker), Some(cxx_toolchain_dir)) = (&self.linker, &self.cxx_toolchain_dir) {
108-
cpp_compiler.args(
109-
self.include_paths
110-
.iter()
111-
.map(|path| "--include-directory=".to_string() + cxx_toolchain_dir + path),
112-
);
113-
114-
CppCompilation::CustomLinker {
115-
cpp_compiler,
116-
linker: linker.to_owned(),
117-
}
118-
} else {
119-
CppCompilation::Simple(cpp_compiler)
120-
}
90+
CppCompilation(cpp_compiler)
12191
}
12292
}
12393

124-
pub enum CppCompilation {
125-
Simple(std::process::Command),
126-
CustomLinker {
127-
cpp_compiler: std::process::Command,
128-
linker: String,
129-
},
130-
}
94+
pub struct CppCompilation(std::process::Command);
13195

13296
fn clone_command(command: &std::process::Command) -> std::process::Command {
13397
let mut cmd = std::process::Command::new(command.get_program());
@@ -144,62 +108,15 @@ fn clone_command(command: &std::process::Command) -> std::process::Command {
144108
}
145109

146110
impl CppCompilation {
111+
pub fn command_mut(&mut self) -> &mut std::process::Command {
112+
&mut self.0
113+
}
114+
147115
pub fn run(&self, inputs: &[String], output: &str) -> std::io::Result<std::process::Output> {
148-
match self {
149-
CppCompilation::Simple(command) => {
150-
let mut cmd = clone_command(command);
151-
cmd.args(inputs);
152-
cmd.args(["-o", output]);
153-
154-
cmd.output()
155-
}
156-
CppCompilation::CustomLinker {
157-
cpp_compiler,
158-
linker,
159-
} => {
160-
let object_file = &format!("{output}.o");
161-
162-
// Build an object file using the cpp compiler.
163-
let mut cmd = clone_command(cpp_compiler);
164-
cmd.args(inputs);
165-
cmd.args(["-c", "-o", object_file]);
166-
167-
let cpp_output = cmd.output()?;
168-
if !cpp_output.status.success() {
169-
error!("c++ compilaton failed");
170-
return Ok(cpp_output);
171-
}
172-
173-
trace!("using custom linker");
174-
175-
// Use the custom linker to turn the object file into an executable.
176-
let mut cmd = std::process::Command::new(linker);
177-
cmd.args([object_file, "-o", output]);
178-
179-
if let Some(current_dir) = cpp_compiler.get_current_dir() {
180-
cmd.current_dir(current_dir);
181-
}
182-
183-
for (key, val) in cpp_compiler.get_envs() {
184-
cmd.env(key, val.unwrap_or_default());
185-
}
186-
187-
let linker_output = cmd.output()?;
188-
if !linker_output.status.success() {
189-
error!("custom linker failed");
190-
return Ok(linker_output);
191-
}
192-
193-
trace!("removing {object_file}");
194-
let object_file_path = match cpp_compiler.get_current_dir() {
195-
Some(current_dir) => &format!("{}/{object_file}", current_dir.display()),
196-
None => object_file,
197-
};
198-
199-
std::fs::remove_file(object_file_path)?;
200-
201-
Ok(cpp_output)
202-
}
203-
}
116+
let mut cmd = clone_command(&self.0);
117+
cmd.args(inputs);
118+
cmd.args(["-o", output]);
119+
120+
cmd.output()
204121
}
205122
}

0 commit comments

Comments
 (0)