|
| 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 | +} |
0 commit comments