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