Skip to content

Commit 6c9b60a

Browse files
committed
run-make-support: add clang and llvm-readobj command wrappers
1 parent c69b5b7 commit 6c9b60a

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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, you
8+
/// should check if you need `//@ needs-matching-clang` to make sure `clang` is available for
9+
/// a given target.
10+
pub fn clang() -> Clang {
11+
Clang::new()
12+
}
13+
14+
/// A `clang` invocation builder.
15+
#[derive(Debug)]
16+
pub struct Clang {
17+
cmd: Command,
18+
}
19+
20+
crate::impl_common_helpers!(Clang);
21+
22+
impl Clang {
23+
/// Construct a new `clang` invocation. `clang` is not always available for all targets, you
24+
/// should check if you need `//@ needs-matching-clang` to make sure `clang` is available for
25+
/// a given target.
26+
pub fn new() -> Self {
27+
let clang =
28+
env::var("CLANG").expect("`CLANG` not specified, but this is required to find `clang`");
29+
let cmd = Command::new(clang);
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+
/// Specify the name of the executable. The executable will be placed under `$TMPDIR`, and the
40+
/// extension will be determined by [`bin_name`].
41+
pub fn out_exe(&mut self, name: &str) -> &mut Self {
42+
self.cmd.arg("-o");
43+
self.cmd.arg(tmp_dir().join(bin_name(name)));
44+
self
45+
}
46+
47+
/// Specify which target triple clang should target.
48+
pub fn target(&mut self, target_triple: &str) -> &mut Self {
49+
self.cmd.arg("-target");
50+
self.cmd.arg(target_triple);
51+
self
52+
}
53+
54+
/// Pass `-nostdlib` to disable linking the C standard library.
55+
pub fn no_stdlib(&mut self) -> &mut Self {
56+
self.cmd.arg("-nostdlib");
57+
self
58+
}
59+
60+
/// Specify architecture.
61+
pub fn arch(&mut self, arch: &str) -> &mut Self {
62+
self.cmd.arg(format!("-march={arch}"));
63+
self
64+
}
65+
66+
/// Specify LTO settings.
67+
pub fn lto(&mut self, lto: &str) -> &mut Self {
68+
self.cmd.arg(format!("-flto={lto}"));
69+
self
70+
}
71+
72+
/// Specify which ld to use.
73+
pub fn use_ld(&mut self, ld: &str) -> &mut Self {
74+
self.cmd.arg(format!("-fuse-ld={ld}"));
75+
self
76+
}
77+
}

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;
@@ -16,6 +18,8 @@ pub use object;
1618
pub use wasmparser;
1719

1820
pub use cc::{cc, extra_c_flags, extra_cxx_flags, Cc};
21+
pub use clang::{clang, Clang};
22+
pub use llvm_readobj::{llvm_readobj, LlvmReadobj};
1923
pub use run::{run, run_fail};
2024
pub use rustc::{aux_build, rustc, Rustc};
2125
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)