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 uses macOS.
32#[must_use]
33pub fn is_darwin() -> bool {
34    target().contains("darwin")
35}
36
37/// Check if target uses AIX.
38#[must_use]
39pub fn is_aix() -> bool {
40    target().contains("aix")
41}
42
43/// Get the target OS on Apple operating systems.
44#[must_use]
45pub fn apple_os() -> &'static str {
46    if target().contains("darwin") {
47        "macos"
48    } else if target().contains("ios") {
49        "ios"
50    } else if target().contains("tvos") {
51        "tvos"
52    } else if target().contains("watchos") {
53        "watchos"
54    } else if target().contains("visionos") {
55        "visionos"
56    } else {
57        panic!("not an Apple OS")
58    }
59}
60
61/// Check if `component` is within `LLVM_COMPONENTS`
62#[must_use]
63pub fn llvm_components_contain(component: &str) -> bool {
64    // `LLVM_COMPONENTS` is a space-separated list of words
65    env_var("LLVM_COMPONENTS").split_whitespace().find(|s| s == &component).is_some()
66}
67
68/// Run `uname`. This assumes that `uname` is available on the platform!
69#[track_caller]
70#[must_use]
71pub fn uname() -> String {
72    let caller = panic::Location::caller();
73    let mut uname = Command::new("uname");
74    let output = uname.run();
75    if !output.status().success() {
76        handle_failed_output(&uname, output, caller.line());
77    }
78    output.stdout_utf8()
79}