run_make_support/
targets.rs

1use std::panic;
2
3use crate::command::Command;
4use crate::env_var;
5use crate::util::handle_failed_output;
6
7/// `TARGET`
8#[must_use]
9pub fn target() -> String {
10    env_var("TARGET")
11}
12
13/// Check if target is windows-like.
14#[must_use]
15pub fn is_windows() -> bool {
16    target().contains("windows")
17}
18
19/// Check if target uses msvc.
20#[must_use]
21pub fn is_msvc() -> bool {
22    target().contains("msvc")
23}
24
25/// Check if target is windows-gnu.
26#[must_use]
27pub fn is_windows_gnu() -> bool {
28    target().ends_with("windows-gnu")
29}
30
31/// Check if target is win7.
32#[must_use]
33pub fn is_win7() -> bool {
34    target().contains("win7")
35}
36
37/// Check if target uses macOS.
38#[must_use]
39pub fn is_darwin() -> bool {
40    target().contains("darwin")
41}
42
43/// Check if target uses AIX.
44#[must_use]
45pub fn is_aix() -> bool {
46    target().contains("aix")
47}
48
49/// Get the target OS on Apple operating systems.
50#[must_use]
51pub fn apple_os() -> &'static str {
52    if target().contains("darwin") {
53        "macos"
54    } else if target().contains("ios") {
55        "ios"
56    } else if target().contains("tvos") {
57        "tvos"
58    } else if target().contains("watchos") {
59        "watchos"
60    } else if target().contains("visionos") {
61        "visionos"
62    } else {
63        panic!("not an Apple OS")
64    }
65}
66
67/// Check if `component` is within `LLVM_COMPONENTS`
68#[must_use]
69pub fn llvm_components_contain(component: &str) -> bool {
70    // `LLVM_COMPONENTS` is a space-separated list of words
71    env_var("LLVM_COMPONENTS").split_whitespace().find(|s| s == &component).is_some()
72}
73
74/// Run `uname`. This assumes that `uname` is available on the platform!
75#[track_caller]
76#[must_use]
77pub fn uname() -> String {
78    let caller = panic::Location::caller();
79    let mut uname = Command::new("uname");
80    let output = uname.run();
81    if !output.status().success() {
82        handle_failed_output(&uname, output, caller.line());
83    }
84    output.stdout_utf8()
85}