Skip to content

Commit a6e2f6b

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

File tree

3 files changed

+127
-0
lines changed

3 files changed

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

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

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

0 commit comments

Comments
 (0)