bootstrap/core/config/toml/
target.rs

1//! This module defines the structures and logic for handling target-specific configuration
2//! within the `bootstrap.toml` file. This allows you to customize build settings, tools,
3//! and flags for individual compilation targets.
4//!
5//! It includes:
6//!
7//! * [`TomlTarget`]: This struct directly mirrors the `[target.<triple>]` sections in your
8//!   `bootstrap.toml`. It's used for deserializing raw TOML data for a specific target.
9//! * [`Target`]: This struct represents the processed and validated configuration for a
10//!   build target, which is is stored in the main `Config` structure.
11
12use std::collections::HashMap;
13
14use serde::de::Error;
15use serde::{Deserialize, Deserializer};
16
17use crate::core::config::{
18    CompilerBuiltins, LlvmLibunwind, Merge, ReplaceOpt, SplitDebuginfo, StringOrBool,
19};
20use crate::{CodegenBackendKind, HashSet, PathBuf, define_config, exit};
21
22define_config! {
23    /// TOML representation of how each build target is configured.
24    struct TomlTarget {
25        cc: Option<String> = "cc",
26        cxx: Option<String> = "cxx",
27        ar: Option<String> = "ar",
28        ranlib: Option<String> = "ranlib",
29        default_linker: Option<PathBuf> = "default-linker",
30        default_linker_linux_override: Option<DefaultLinuxLinkerOverride> = "default-linker-linux-override",
31        linker: Option<String> = "linker",
32        split_debuginfo: Option<String> = "split-debuginfo",
33        llvm_config: Option<String> = "llvm-config",
34        llvm_has_rust_patches: Option<bool> = "llvm-has-rust-patches",
35        llvm_filecheck: Option<String> = "llvm-filecheck",
36        llvm_libunwind: Option<String> = "llvm-libunwind",
37        sanitizers: Option<bool> = "sanitizers",
38        profiler: Option<StringOrBool> = "profiler",
39        rpath: Option<bool> = "rpath",
40        crt_static: Option<bool> = "crt-static",
41        musl_root: Option<String> = "musl-root",
42        musl_libdir: Option<String> = "musl-libdir",
43        wasi_root: Option<String> = "wasi-root",
44        qemu_rootfs: Option<String> = "qemu-rootfs",
45        no_std: Option<bool> = "no-std",
46        codegen_backends: Option<Vec<String>> = "codegen-backends",
47        runner: Option<String> = "runner",
48        optimized_compiler_builtins: Option<CompilerBuiltins> = "optimized-compiler-builtins",
49        jemalloc: Option<bool> = "jemalloc",
50    }
51}
52
53/// Per-target configuration stored in the global configuration structure.
54#[derive(Debug, Default, Clone, PartialEq, Eq)]
55pub struct Target {
56    /// Some(path to llvm-config) if using an external LLVM.
57    pub llvm_config: Option<PathBuf>,
58    pub llvm_has_rust_patches: Option<bool>,
59    /// Some(path to FileCheck) if one was specified.
60    pub llvm_filecheck: Option<PathBuf>,
61    pub llvm_libunwind: Option<LlvmLibunwind>,
62    pub cc: Option<PathBuf>,
63    pub cxx: Option<PathBuf>,
64    pub ar: Option<PathBuf>,
65    pub ranlib: Option<PathBuf>,
66    pub default_linker: Option<PathBuf>,
67    pub default_linker_linux_override: DefaultLinuxLinkerOverride,
68    pub linker: Option<PathBuf>,
69    pub split_debuginfo: Option<SplitDebuginfo>,
70    pub sanitizers: Option<bool>,
71    pub profiler: Option<StringOrBool>,
72    pub rpath: Option<bool>,
73    pub crt_static: Option<bool>,
74    pub musl_root: Option<PathBuf>,
75    pub musl_libdir: Option<PathBuf>,
76    pub wasi_root: Option<PathBuf>,
77    pub qemu_rootfs: Option<PathBuf>,
78    pub runner: Option<String>,
79    pub no_std: bool,
80    pub codegen_backends: Option<Vec<CodegenBackendKind>>,
81    pub optimized_compiler_builtins: Option<CompilerBuiltins>,
82    pub jemalloc: Option<bool>,
83}
84
85impl Target {
86    pub fn from_triple(triple: &str) -> Self {
87        let mut target: Self = Default::default();
88        if !build_helper::targets::target_supports_std(triple) {
89            target.no_std = true;
90        }
91        if triple.contains("emscripten") {
92            target.runner = Some("node".into());
93        }
94        target
95    }
96}
97
98/// Overrides the default linker used on a Linux target.
99/// On Linux, the linker is usually invoked through `cc`, therefore this exists as a separate
100/// configuration from simply setting `default-linker`, which corresponds to `-Clinker`.
101#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
102pub enum DefaultLinuxLinkerOverride {
103    /// Do not apply any override and use the default linker for the given target.
104    #[default]
105    Off,
106    /// Use the self-contained `rust-lld` linker, invoked through `cc`.
107    /// Corresponds to `-Clinker-features=+lld -Clink-self-contained=+linker`.
108    SelfContainedLldCc,
109}
110
111impl<'de> Deserialize<'de> for DefaultLinuxLinkerOverride {
112    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
113    where
114        D: Deserializer<'de>,
115    {
116        let name = String::deserialize(deserializer)?;
117        match name.as_str() {
118            "off" => Ok(Self::Off),
119            "self-contained-lld-cc" => Ok(Self::SelfContainedLldCc),
120            other => Err(D::Error::unknown_variant(other, &["off", "self-contained-lld-cc"])),
121        }
122    }
123}
124
125/// Set of linker overrides for selected Linux targets.
126#[cfg(not(test))]
127pub fn default_linux_linker_overrides() -> HashMap<String, DefaultLinuxLinkerOverride> {
128    [("x86_64-unknown-linux-gnu".to_string(), DefaultLinuxLinkerOverride::SelfContainedLldCc)]
129        .into()
130}
131
132#[cfg(test)]
133thread_local! {
134    static TEST_LINUX_LINKER_OVERRIDES: std::cell::RefCell<Option<HashMap<String, DefaultLinuxLinkerOverride>>> = std::cell::RefCell::new(None);
135}
136
137#[cfg(test)]
138pub fn default_linux_linker_overrides() -> HashMap<String, DefaultLinuxLinkerOverride> {
139    TEST_LINUX_LINKER_OVERRIDES.with(|cell| cell.borrow().clone()).unwrap_or_default()
140}
141
142#[cfg(test)]
143pub fn with_default_linux_linker_overrides<R>(
144    targets: HashMap<String, DefaultLinuxLinkerOverride>,
145    f: impl FnOnce() -> R,
146) -> R {
147    TEST_LINUX_LINKER_OVERRIDES.with(|cell| {
148        let prev = cell.replace(Some(targets));
149        let result = f();
150        cell.replace(prev);
151        result
152    })
153}