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