Skip to content

Commit 255bee1

Browse files
committed
run-make-support: add clang and llvm-readobj command wrappers
1 parent 6c6b302 commit 255bee1

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use std::env;
2+
use std::path::Path;
3+
use std::process::Command;
4+
5+
use crate::{bin_name, handle_failed_output, tmp_dir};
6+
7+
/// Construct a new `clang` invocation. `clang` is not always available for all targets.
8+
pub fn clang() -> Clang {
9+
Clang::new()
10+
}
11+
12+
/// A `clang` invocation builder.
13+
#[derive(Debug)]
14+
pub struct Clang {
15+
cmd: Command,
16+
}
17+
18+
crate::impl_common_helpers!(Clang);
19+
20+
impl Clang {
21+
/// Construct a new `clang` invocation. `clang` is not always available for all targets.
22+
pub fn new() -> Self {
23+
let clang =
24+
env::var("CLANG").expect("`CLANG` not specified, but this is required to find `clang`");
25+
let cmd = Command::new(clang);
26+
Self { cmd }
27+
}
28+
29+
/// Provide an input file.
30+
pub fn input<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
31+
self.cmd.arg(path.as_ref());
32+
self
33+
}
34+
35+
/// Specify the name of the executable. The executable will be placed under `$TMPDIR`, and the
36+
/// extension will be determined by [`bin_name`].
37+
pub fn out_exe(&mut self, name: &str) -> &mut Self {
38+
self.cmd.arg("-o");
39+
self.cmd.arg(tmp_dir().join(bin_name(name)));
40+
self
41+
}
42+
43+
/// Specify which target triple clang should target.
44+
pub fn target(&mut self, target_triple: &str) -> &mut Self {
45+
self.cmd.arg("-target");
46+
self.cmd.arg(target_triple);
47+
self
48+
}
49+
50+
/// Pass `-nostdlib` to disable linking the C standard library.
51+
pub fn no_stdlib(&mut self) -> &mut Self {
52+
self.cmd.arg("-nostdlib");
53+
self
54+
}
55+
56+
/// Specify architecture.
57+
pub fn arch(&mut self, arch: &str) -> &mut Self {
58+
self.cmd.arg(format!("-march={arch}"));
59+
self
60+
}
61+
62+
/// Specify LTO settings.
63+
pub fn lto(&mut self, lto: &str) -> &mut Self {
64+
self.cmd.arg(format!("-flto={lto}"));
65+
self
66+
}
67+
68+
/// Specify which ld to use.
69+
pub fn use_ld(&mut self, ld: &str) -> &mut Self {
70+
self.cmd.arg(format!("-fuse-ld={ld}"));
71+
self
72+
}
73+
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
//! as `object` or `wasmparser`, they can be re-exported and be made available through this library.
55
66
pub mod cc;
7+
pub mod clang;
8+
pub mod llvm_readobj;
79
pub mod run;
810
pub mod rustc;
911
pub mod rustdoc;
@@ -17,6 +19,8 @@ pub use regex;
1719
pub use wasmparser;
1820

1921
pub use cc::{cc, extra_c_flags, extra_cxx_flags, Cc};
22+
pub use clang::{clang, Clang};
23+
pub use llvm_readobj::{llvm_readobj, LlvmReadobj};
2024
pub use run::{run, run_fail};
2125
pub use rustc::{aux_build, rustc, Rustc};
2226
pub use rustdoc::{bare_rustdoc, rustdoc, Rustdoc};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use std::env;
2+
use std::path::{Path, PathBuf};
3+
use std::process::Command;
4+
5+
use crate::handle_failed_output;
6+
7+
/// Construct a new `llvm-readobj` invocation. This assumes that `llvm-readobj` is available
8+
/// at `$LLVM_BIN_DIR/llvm-readobj`.
9+
pub fn llvm_readobj() -> LlvmReadobj {
10+
LlvmReadobj::new()
11+
}
12+
13+
/// A `llvm-readobj` invocation builder.
14+
#[derive(Debug)]
15+
pub struct LlvmReadobj {
16+
cmd: Command,
17+
}
18+
19+
crate::impl_common_helpers!(LlvmReadobj);
20+
21+
impl LlvmReadobj {
22+
/// Construct a new `llvm-readobj` invocation. This assumes that `llvm-readobj` is available
23+
/// at `$LLVM_BIN_DIR/llvm-readobj`.
24+
pub fn new() -> Self {
25+
let llvm_bin_dir = env::var("LLVM_BIN_DIR")
26+
.expect("`LLVM_BIN_DIR` not specified, but this is required to find `llvm-readobj`");
27+
let llvm_bin_dir = PathBuf::from(llvm_bin_dir);
28+
let llvm_readobj = llvm_bin_dir.join("llvm-readobj");
29+
let cmd = Command::new(llvm_readobj);
30+
Self { cmd }
31+
}
32+
33+
/// Provide an input file.
34+
pub fn input<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
35+
self.cmd.arg(path.as_ref());
36+
self
37+
}
38+
39+
/// Pass `--file-header` to display file headers.
40+
pub fn file_header(&mut self) -> &mut Self {
41+
self.cmd.arg("--file-header");
42+
self
43+
}
44+
}

0 commit comments

Comments
 (0)