Skip to main content

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