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