axbuild 0.4.21

An OS build lib toolkit used by arceos
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
use anyhow::bail;

use super::*;

pub(crate) fn env_truthy(env: &HashMap<String, String>, key: &str) -> bool {
    env.get(key).is_some_and(|value| {
        matches!(
            value.trim().to_ascii_lowercase().as_str(),
            "y" | "yes" | "1" | "true" | "on"
        )
    })
}

pub(crate) fn toolchain_rustflags(env: &HashMap<String, String>) -> Vec<String> {
    let mut flags = Vec::new();
    let dwarf = env_truthy(env, "DWARF");
    let backtrace = env_truthy(env, "BACKTRACE") || dwarf;

    if dwarf {
        flags.push("-Cdebuginfo=2".to_string());
        flags.push("-Cstrip=none".to_string());
    }

    if backtrace {
        flags.push("-Cforce-frame-pointers=yes".to_string());
    }

    flags
}

pub(super) fn features_enable_stack_protector(features: &[String]) -> bool {
    features.iter().any(|feature| {
        matches!(
            feature.as_str(),
            "stack-protector" | "ax-std/stack-protector" | "starry-kernel/stack-protector"
        )
    })
}

pub(crate) fn toolchain_rustflags_for_features(
    env: &HashMap<String, String>,
    features: &[String],
) -> Vec<String> {
    let mut flags = toolchain_rustflags(env);
    if features_enable_stack_protector(features) {
        flags.push("-Zstack-protector=strong".to_string());
    }
    flags
}

pub(crate) fn append_encoded_rustflags(cargo: &mut Cargo, flags: &[&str]) {
    const KEY: &str = "CARGO_ENCODED_RUSTFLAGS";
    let encoded = flags.join("\x1f");
    if encoded.is_empty() {
        return;
    }

    // Cargo selects exactly one rustflags source. An encoded environment value
    // would therefore shadow the target-specific linker contract generated by
    // `build_cargo_args`. Keep later test/coverage flags in that same inline
    // source whenever it is present.
    if !cargo.env.contains_key(KEY) && append_inline_target_rustflags(cargo, flags) {
        return;
    }

    let value = cargo.env.entry(KEY.to_string()).or_default();
    if encoded_rustflags_contains_sequence(value, &encoded) {
        return;
    }
    if !value.is_empty() {
        value.push('\x1f');
    }
    value.push_str(&encoded);
}

fn append_inline_target_rustflags(cargo: &mut Cargo, flags: &[&str]) -> bool {
    let target_key = Path::new(&cargo.target)
        .file_stem()
        .and_then(|stem| stem.to_str())
        .unwrap_or(&cargo.target);
    let rustflags_key = format!("target.{target_key}.rustflags");

    for index in 1..cargo.args.len() {
        if cargo.args[index - 1] != "--config" {
            continue;
        }
        let Some((key, value)) = cargo.args[index].split_once('=') else {
            continue;
        };
        if key != rustflags_key {
            continue;
        }
        let Ok(table) = toml::from_str::<toml::Table>(&format!("rustflags = {value}")) else {
            continue;
        };
        let Some(mut rustflags) = table
            .get("rustflags")
            .and_then(toml::Value::as_array)
            .and_then(|rustflags| {
                rustflags
                    .iter()
                    .map(|flag| flag.as_str().map(ToOwned::to_owned))
                    .collect::<Option<Vec<_>>>()
            })
        else {
            continue;
        };
        if !rustflags
            .windows(flags.len())
            .any(|window| window.iter().map(String::as_str).eq(flags.iter().copied()))
        {
            rustflags.extend(flags.iter().map(|flag| (*flag).to_string()));
        }
        let rustflags =
            toml::Value::Array(rustflags.into_iter().map(toml::Value::String).collect());
        cargo.args[index] = format!("{rustflags_key}={rustflags}");
        return true;
    }

    false
}

fn encoded_rustflags_contains_sequence(value: &str, encoded: &str) -> bool {
    let needle: Vec<_> = encoded.split('\x1f').collect();
    if needle.is_empty() {
        return true;
    }
    value
        .split('\x1f')
        .collect::<Vec<_>>()
        .windows(needle.len())
        .any(|window| window == needle.as_slice())
}

/// Whether the build config enables target backtrace support (frame pointers / unwind).
///
/// Matches [`toolchain_rustflags`]: `BACKTRACE=y` or `DWARF=y` in `[env]`.
pub(crate) fn build_info_enables_backtrace(info: &BuildInfo) -> bool {
    let dwarf = env_truthy(&info.env, "DWARF");
    env_truthy(&info.env, "BACKTRACE") || dwarf
}

/// Read a per-target `build-*.toml` and check [`build_info_enables_backtrace`].
pub(crate) fn build_info_enables_backtrace_path(path: &Path) -> bool {
    load_build_info::<BuildInfo>(path)
        .ok()
        .is_some_and(|info| build_info_enables_backtrace(&info))
}

pub(super) const TARGET_JSON_ROOT: &str = "scripts/targets";
pub(super) const PIE_TARGET_DIR: &str = "pie";
pub(crate) const ARCEOS_LINKER_SCRIPT: &str = "linker.x";
pub(super) const STD_TARGET_DIR: &str = "std";
pub(super) const AXSTD_STD_PACKAGE: &str = "ax-std";

/// Link contract for freestanding kernels built without Rust `std`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum BareKernelLinkMode {
    /// Use the target's default relocation and linker policy.
    Default,
    /// Produce a position-independent executable with the kernel linker script.
    Pie,
}

impl BareKernelLinkMode {
    fn rustflags(self, target: &str) -> Vec<String> {
        match self {
            Self::Default => Vec::new(),
            Self::Pie => {
                let mut flags = vec![
                    "-Crelocation-model=pic".to_string(),
                    "-Clink-args=-pie".to_string(),
                ];
                if target.starts_with("riscv64") {
                    flags.push("-Clink-args=--no-relax".to_string());
                }
                flags.extend([
                    "-Clink-args=--gc-sections".to_string(),
                    "-Clink-args=-znorelro".to_string(),
                    "-Clink-args=-znostart-stop-gc".to_string(),
                    "-Clink-args=-Tlinker.x".to_string(),
                    "-Clink-args=-u _head".to_string(),
                ]);
                flags
            }
        }
    }
}

#[derive(Debug, Clone, JsonSchema, Deserialize, Serialize, PartialEq)]
pub struct BuildInfo {
    /// Environment variables to set during the build.
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub env: HashMap<String, String>,
    /// Cargo features to enable.
    pub features: Vec<String>,
    /// Log level feature to automatically enable.
    pub log: LogLevel,
    /// Maximum number of CPUs to expose to the build.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub max_cpu_num: Option<usize>,
}

impl BuildInfo {
    pub fn with_features<T: AsRef<str>>(mut self, features: impl AsRef<[T]>) -> Self {
        let features = features
            .as_ref()
            .iter()
            .map(|feature| feature.as_ref().to_string())
            .collect();
        self.features = features;
        self
    }

    pub(crate) fn prepare_log_env(&mut self) {
        self.env
            .insert("AX_LOG".into(), format!("{:?}", self.log).to_lowercase());
    }

    pub(crate) fn prepare_max_cpu_num_env(&mut self) -> anyhow::Result<()> {
        if let Some(max_cpu_num) = self.validated_max_cpu_num()? {
            self.env.insert("SMP".into(), max_cpu_num.to_string());
        }
        Ok(())
    }

    pub(crate) fn into_base_cargo_config(
        self,
        package: String,
        target: String,
        args: Vec<String>,
    ) -> Cargo {
        // Keep the Cargo artifact as ELF by default. BIN conversion is an
        // explicit runner/config concern and must not be inferred from target.
        self.into_base_cargo_config_with_to_bin(package, target, args, false)
    }

    pub(crate) fn into_base_cargo_config_with_to_bin(
        self,
        package: String,
        target: String,
        args: Vec<String>,
        to_bin: bool,
    ) -> Cargo {
        Cargo {
            env: self.env,
            target,
            package,
            features: self.features,
            log: Some(self.log),
            extra_config: None,
            profile: None,
            disable_someboot_build_config: true,
            args,
            pre_build_cmds: vec![],
            post_build_cmds: vec![],
            to_bin,
            bin: None,
            test: None,
        }
    }

    pub(crate) fn into_base_cargo_config_with_log(
        mut self,
        package: String,
        target: String,
        args: Vec<String>,
    ) -> Cargo {
        self.prepare_log_env();
        self.prepare_max_cpu_num_env()
            .expect("max_cpu_num validation should run before cargo config generation");
        self.into_base_cargo_config(package, target, args)
    }

    pub(crate) fn into_prepared_base_cargo_config_with_metadata(
        mut self,
        package: &str,
        target: &str,
        metadata: &Metadata,
    ) -> anyhow::Result<Cargo> {
        self.validated_max_cpu_num()?;
        self.validate_features()?;
        self.resolve_std_features();
        // `max_cpu_num` is an explicit build setting. Propagate SMP only when
        // the caller requested more than one CPU; package metadata never adds
        // features implicitly.
        if self.max_cpu_num.is_some_and(|max_cpu_num| max_cpu_num > 1) {
            self.features.push("smp".to_string());
            self.resolve_std_features();
        }
        let std_target = std_build_target_for(target)?;
        let fake_lib_dir = std_fake_lib_dir(&std_target.target_name)?;
        let wrapper = std_linker_wrapper_path(&std_target.target_name, &fake_lib_dir)?;
        let mut cargo = self.into_base_cargo_config_with_log(
            package.to_string(),
            std_target.target.clone(),
            std_target.cargo_args,
        );
        cargo.env.extend(std_target.env);
        // The std target wrapper needs the original kernel target. This is
        // build context, not a Cargo feature or platform selection.
        cargo
            .env
            .insert("AX_TARGET".to_string(), target.to_string());
        let app_features = package_feature_names(package, metadata)?;
        let axstd_features = package_feature_names(AXSTD_STD_PACKAGE, metadata)?;
        pass_std_build_nested_features(&mut cargo.features, &app_features, &axstd_features);
        cargo.pre_build_cmds.push(
            std_fake_lib_prebuild_script_path(&std_target.target_name, &fake_lib_dir, &cargo.env)?
                .display()
                .to_string(),
        );
        let rustflags = toolchain_rustflags_for_features(&cargo.env, &cargo.features);
        cargo.extra_config = Some(
            std_cargo_config_path(&std_target.target_name, &wrapper, &rustflags)?
                .display()
                .to_string(),
        );
        Ok(cargo)
    }

    /// Builds a Rust-`std` kernel through the musl PIE target and linker wrapper.
    pub(crate) fn into_prepared_std_cargo_config_with_metadata(
        self,
        package: &str,
        target: &str,
        metadata: &Metadata,
    ) -> anyhow::Result<Cargo> {
        self.into_prepared_base_cargo_config_with_metadata(package, target, metadata)
    }

    /// Builds a freestanding kernel against only `core` and `alloc`.
    pub(crate) fn into_prepared_no_std_cargo_config_with_metadata(
        mut self,
        package: &str,
        target: &str,
        metadata: &Metadata,
        link_mode: BareKernelLinkMode,
    ) -> anyhow::Result<Cargo> {
        self.validated_max_cpu_num()?;
        self.validate_features()?;
        self.reject_freestanding_std_compat()?;
        self.enable_package_smp_feature(package, metadata)?;

        let mut rustflags = toolchain_rustflags_for_features(&self.env, &self.features);
        rustflags.extend(link_mode.rustflags(target));
        let args = Self::build_cargo_args(target, &rustflags);
        let mut cargo =
            self.into_base_cargo_config_with_log(package.to_string(), target.to_string(), args);
        cargo.to_bin = bare_target_requires_bin(target);
        Ok(cargo)
    }

    fn reject_freestanding_std_compat(&self) -> anyhow::Result<()> {
        if let Some(feature) = self
            .features
            .iter()
            .find(|feature| feature.rsplit('/').next() == Some("std-compat"))
        {
            bail!("freestanding no_std build cannot enable `{feature}`");
        }
        Ok(())
    }

    fn enable_package_smp_feature(
        &mut self,
        package: &str,
        metadata: &Metadata,
    ) -> anyhow::Result<()> {
        if !self.max_cpu_num.is_some_and(|max_cpu_num| max_cpu_num > 1) {
            return Ok(());
        }
        if package_feature_names(package, metadata)?
            .iter()
            .any(|feature| feature == "smp")
        {
            self.features.push("smp".to_string());
            self.features.sort();
            self.features.dedup();
        }
        Ok(())
    }

    pub(super) fn resolve_std_features(&mut self) {
        self.features = self
            .features
            .iter()
            .map(|feature| normalize_std_feature(feature))
            .collect();
        self.features.sort();
        self.features.dedup();
    }

    pub(crate) fn resolve_c_app_features(&mut self) -> anyhow::Result<()> {
        self.validate_features()?;
        // `max_cpu_num` is an explicit C build setting; expose the matching ax-std
        // capability only when the caller requested more than one CPU.
        if self.max_cpu_num.is_some_and(|max_cpu_num| max_cpu_num > 1) {
            self.features.push("ax-std/smp".to_string());
        }
        self.features.sort();
        self.features.dedup();
        Ok(())
    }

    /// Reject compatibility aliases and removed platform controls instead of silently changing
    /// the build contract selected by the caller.
    pub(crate) fn validate_features(&self) -> anyhow::Result<()> {
        let selects_mode = |mode: &str| {
            self.features
                .iter()
                .any(|feature| feature.rsplit('/').next() == Some(mode))
        };
        if selects_mode("uspace") && selects_mode("tls") {
            bail!(
                "features `uspace` and `tls` select incompatible CPU-local register ownership \
                 modes"
            );
        }
        for feature in &self.features {
            self.validate_feature(feature)?;
        }
        Ok(())
    }

    pub(crate) fn validate_feature(&self, feature: &str) -> anyhow::Result<()> {
        if feature == "axstd" || feature.starts_with("axstd/") {
            bail!(
                "feature `{feature}` uses the removed `axstd` alias; use the declared Cargo \
                 feature name instead"
            );
        }
        if is_removed_dynamic_platform_feature(feature) {
            bail!(
                "feature `{feature}` is no longer supported; dynamic platform selection is \
                 automatic, remove the feature from the selected configuration"
            );
        }
        Ok(())
    }

    pub(crate) fn validated_max_cpu_num(&self) -> anyhow::Result<Option<usize>> {
        match self.max_cpu_num {
            Some(0) => bail!("max_cpu_num must be greater than 0"),
            Some(max_cpu_num) => Ok(Some(max_cpu_num)),
            None => Ok(None),
        }
    }

    pub(crate) fn build_cargo_args(target: &str, extra_rustflags: &[String]) -> Vec<String> {
        let mut args = vec!["-Z".to_string(), "build-std=core,alloc".to_string()];
        let target_key = Path::new(target)
            .file_stem()
            .and_then(|stem| stem.to_str())
            .unwrap_or(target);

        let mut rustflags = extra_rustflags.to_vec();
        if target_key.starts_with("loongarch64-") {
            rustflags.push("-Ctarget-feature=-ual".to_string());
        }

        if !rustflags.is_empty() {
            args.push("--config".to_string());
            let rustflags_toml =
                toml::Value::Array(rustflags.into_iter().map(toml::Value::String).collect())
                    .to_string();
            args.push(format!("target.{target_key}.rustflags={rustflags_toml}"));
        }
        args
    }
}

fn bare_target_requires_bin(target: &str) -> bool {
    target.starts_with("aarch64-") || target.starts_with("riscv64")
}

impl Default for BuildInfo {
    fn default() -> Self {
        Self {
            env: HashMap::new(),
            log: LogLevel::Warn,
            features: Vec::new(),
            max_cpu_num: None,
        }
    }
}