axbuild 0.4.15

An OS build lib toolkit used by arceos
Documentation
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
use std::{
    collections::BTreeSet,
    path::{Path, PathBuf},
    process::Command as StdCommand,
};

use anyhow::Context;
use clap::{Args, Subcommand};
use log::warn;
use ostool::{build::config::Cargo, run::qemu::QemuConfig};

use crate::{
    context::{AppContext, BuildCliArgs, ResolvedBuildRequest, SnapshotPersistence},
    test::host_http::HostHttpServerGuard,
};

const DEFAULT_TEST_DISK_IMAGE_SIZE: &str = "64M";

/// Prepare runtime disk images referenced by QEMU configs.
pub(super) fn ensure_qemu_runtime_assets(
    workspace_root: &Path,
    qemu: &QemuConfig,
) -> anyhow::Result<()> {
    let mut seen = BTreeSet::new();
    for image in qemu_runtime_disk_images(qemu) {
        if !seen.insert(image.clone()) {
            continue;
        }
        ensure_fat32_image(
            &image,
            DEFAULT_TEST_DISK_IMAGE_SIZE,
            should_recreate_runtime_image(workspace_root, &image),
        )?;
    }
    Ok(())
}

/// Create a FAT32 disk image at `path` with the given `size` if it does not
/// already exist.
fn ensure_fat32_image(image: &Path, size: &str, recreate: bool) -> anyhow::Result<()> {
    if image.exists() && !recreate {
        return Ok(());
    }
    let msg = format!("generating {}", image.display());
    println!("{msg} ...");
    if let Some(parent) = image.parent() {
        std::fs::create_dir_all(parent)?;
    }
    if image.exists() {
        std::fs::remove_file(image)?;
    }
    let ran = |cmd: &mut StdCommand| -> anyhow::Result<()> {
        let name = cmd.get_program().to_string_lossy().to_string();
        cmd.status()
            .with_context(|| format!("failed to run `{name}`"))?
            .success()
            .then_some(())
            .ok_or_else(|| anyhow::anyhow!("`{name}` exited with non-zero status"))
    };
    ran(StdCommand::new("truncate").args(["-s", size]).arg(image))?;
    ran(StdCommand::new("mkfs.fat")
        .args(["-F", "32"])
        .arg(image)
        .stdout(std::process::Stdio::null()))?;
    println!("{msg} ... done");
    Ok(())
}

fn qemu_runtime_disk_images(qemu: &QemuConfig) -> Vec<PathBuf> {
    crate::rootfs::qemu::drive_file_paths(qemu)
        .into_iter()
        .filter(|path| path.file_name().and_then(|name| name.to_str()) == Some("disk.img"))
        .collect()
}

fn should_recreate_runtime_image(workspace_root: &Path, image: &Path) -> bool {
    image.starts_with(crate::context::axbuild_tmp_dir(workspace_root).join("runtime-assets"))
}

pub mod build;
pub mod cbuild;
pub mod rootfs;
pub mod test;

fn start_qemu_host_http_server(
    request: &ResolvedBuildRequest,
) -> anyhow::Result<Option<HostHttpServerGuard>> {
    request
        .qemu_config
        .as_deref()
        .map(crate::test::qemu::load_qemu_case_host_http_server)
        .transpose()?
        .flatten()
        .map(|config| HostHttpServerGuard::start(&config, &request.package))
        .transpose()
}

// ---------------------------------------------------------------------------
// CLI types
// ---------------------------------------------------------------------------

/// ArceOS subcommands
#[derive(Subcommand)]
pub enum Command {
    /// Build ArceOS application
    Build(ArgsBuild),
    /// Build and run ArceOS application in QEMU
    Qemu(ArgsQemu),
    /// Run ArceOS test suites
    Test(test::ArgsTest),
    /// Build and run ArceOS application with U-Boot
    Uboot(ArgsUboot),
}

#[derive(Args)]
pub struct ArgsBuild {
    #[arg(short, long)]
    pub config: Option<PathBuf>,
    #[arg(short, long)]
    pub package: Option<String>,
    #[arg(long)]
    pub arch: Option<String>,
    #[arg(short, long)]
    pub target: Option<String>,
    #[arg(long = "plat_dyn", alias = "plat-dyn")]
    pub plat_dyn: Option<bool>,

    #[arg(long, value_name = "CPUS")]
    pub smp: Option<usize>,

    #[arg(long)]
    pub debug: bool,
}

#[derive(Args)]
pub struct ArgsQemu {
    #[command(flatten)]
    pub build: ArgsBuild,

    #[arg(long)]
    pub qemu_config: Option<PathBuf>,

    /// Override the rootfs disk image path (skips auto-download).
    #[arg(long, value_name = "IMAGE")]
    pub rootfs: Option<PathBuf>,
}

#[derive(Args)]
pub struct ArgsUboot {
    #[command(flatten)]
    pub build: ArgsBuild,

    #[arg(long)]
    pub uboot_config: Option<PathBuf>,
}

// ---------------------------------------------------------------------------
// ArceOS executor
// ---------------------------------------------------------------------------

pub struct ArceOS {
    pub(super) app: AppContext,
}

impl From<&ArgsBuild> for BuildCliArgs {
    fn from(args: &ArgsBuild) -> Self {
        Self {
            config: args.config.clone(),
            package: args.package.clone(),
            arch: args.arch.clone(),
            target: args.target.clone(),
            plat_dyn: args.plat_dyn,
            smp: args.smp,
            debug: args.debug,
        }
    }
}

impl ArceOS {
    pub fn new() -> anyhow::Result<Self> {
        let app = AppContext::new()?;
        Ok(Self { app })
    }

    pub async fn execute(&mut self, command: Command) -> anyhow::Result<()> {
        match command {
            Command::Build(args) => self.build(args).await,
            Command::Qemu(args) => self.qemu(args).await,
            Command::Uboot(args) => self.uboot(args).await,
            Command::Test(args) => self.test(args).await,
        }
    }

    async fn build(&mut self, args: ArgsBuild) -> anyhow::Result<()> {
        let request =
            self.prepare_request((&args).into(), None, None, SnapshotPersistence::Store)?;
        self.run_build_request(request).await
    }

    async fn qemu(&mut self, args: ArgsQemu) -> anyhow::Result<()> {
        let request = self.prepare_request(
            (&args.build).into(),
            args.qemu_config,
            None,
            SnapshotPersistence::Store,
        )?;
        if let Some(rootfs) = args.rootfs {
            rootfs::qemu_with_explicit_rootfs(self, request, rootfs).await
        } else {
            self.run_qemu_request(request).await
        }
    }

    async fn uboot(&mut self, args: ArgsUboot) -> anyhow::Result<()> {
        let request = self.prepare_request(
            (&args.build).into(),
            None,
            args.uboot_config,
            SnapshotPersistence::Store,
        )?;
        self.run_uboot_request(request).await
    }

    // ---- test dispatch ----

    async fn test(&mut self, args: test::ArgsTest) -> anyhow::Result<()> {
        test::test(self, args).await
    }

    // ---- internal helpers ----

    pub(super) fn prepare_request(
        &self,
        args: BuildCliArgs,
        qemu_config: Option<PathBuf>,
        uboot_config: Option<PathBuf>,
        persistence: SnapshotPersistence,
    ) -> anyhow::Result<ResolvedBuildRequest> {
        let (request, snapshot) = self.app.prepare_arceos_request(
            args,
            qemu_config,
            uboot_config,
            build::resolve_build_info_path,
        )?;
        if persistence.should_store() {
            self.app.store_arceos_snapshot(&snapshot)?;
        }
        Ok(request)
    }

    pub(super) async fn load_qemu_config(
        &mut self,
        request: &ResolvedBuildRequest,
        cargo: &Cargo,
    ) -> anyhow::Result<Option<ostool::run::qemu::QemuConfig>> {
        let mut qemu = match request.qemu_config.as_deref() {
            Some(path) => self
                .app
                .read_qemu_config_from_path_for_cargo(cargo, path)
                .await
                .map(Some)?,
            None => None,
        };
        if let Some(qemu) = qemu.as_mut() {
            crate::test::qemu::apply_dynamic_platform_qemu_boot(qemu, cargo);
        }
        Ok(qemu)
    }

    async fn load_uboot_config(
        &mut self,
        request: &ResolvedBuildRequest,
        cargo: &Cargo,
    ) -> anyhow::Result<Option<ostool::run::uboot::UbootConfig>> {
        match request.uboot_config.as_deref() {
            Some(path) => self
                .app
                .read_uboot_config_from_path_for_cargo(cargo, path)
                .await
                .map(Some),
            None => Ok(None),
        }
    }

    async fn run_qemu_request(&mut self, request: ResolvedBuildRequest) -> anyhow::Result<()> {
        match build::load_arceos_build_mode(&request.build_info_path)? {
            build::ArceosBuildMode::RustStd => {
                let cargo = build::load_cargo_config(&request)?;
                self.run_qemu_request_with_cargo(request, cargo).await
            }
            build::ArceosBuildMode::AppC { app_dir, app_name } => {
                self.run_c_app_qemu_request(request, app_dir, app_name)
                    .await
            }
        }
    }

    async fn run_qemu_request_with_cargo(
        &mut self,
        request: ResolvedBuildRequest,
        cargo: Cargo,
    ) -> anyhow::Result<()> {
        self.app.set_debug_mode(request.debug)?;
        let qemu = self.load_qemu_config(&request, &cargo).await?;
        if let Some(qemu) = qemu.as_ref() {
            ensure_qemu_runtime_assets(self.app.workspace_root(), qemu)?;
        }
        let _host_http_server = start_qemu_host_http_server(&request)?;
        self.app.qemu(cargo, request.build_info_path, qemu).await
    }

    async fn run_build_request(&mut self, request: ResolvedBuildRequest) -> anyhow::Result<()> {
        self.app.set_debug_mode(request.debug)?;
        match build::load_arceos_build_mode(&request.build_info_path)? {
            build::ArceosBuildMode::RustStd => {
                let cargo = build::load_cargo_config(&request)?;
                self.app
                    .build(cargo, request.build_info_path)
                    .await
                    .map(|_| ())
            }
            build::ArceosBuildMode::AppC { app_dir, app_name } => {
                let request = c_app_internal_request(&request);
                let output = self.build_c_app_request(&request, app_dir, app_name)?;
                println!("Built ArceOS C app ELF: {}", output.elf_path.display());
                Ok(())
            }
        }
    }

    async fn run_uboot_request(&mut self, request: ResolvedBuildRequest) -> anyhow::Result<()> {
        self.app.set_debug_mode(request.debug)?;
        match build::load_arceos_build_mode(&request.build_info_path)? {
            build::ArceosBuildMode::RustStd => {
                let cargo = build::load_cargo_config(&request)?;
                let uboot = self.load_uboot_config(&request, &cargo).await?;
                self.app.uboot(cargo, request.build_info_path, uboot).await
            }
            build::ArceosBuildMode::AppC { app_dir, app_name } => {
                self.run_c_app_uboot_request(request, app_dir, app_name)
                    .await
            }
        }
    }

    fn build_c_app_request(
        &mut self,
        request: &ResolvedBuildRequest,
        app_dir: PathBuf,
        app_name: String,
    ) -> anyhow::Result<cbuild::ArceosCBuildOutput> {
        let workspace_root = self.app.workspace_root();
        let config = build::load_arceos_build_config(&request.build_info_path)?;
        let paths = cbuild::default_c_app_artifact_paths(workspace_root, &app_name);
        let input = cbuild::ArceosCBuildInput {
            app_dir,
            app_name,
            target_dir: paths.target_dir,
            out_dir: paths.out_dir,
            features: config.build_info.features,
        };

        cbuild::build_c_app(workspace_root, request, &input)
    }

    async fn run_c_app_qemu_request(
        &mut self,
        request: ResolvedBuildRequest,
        app_dir: PathBuf,
        app_name: String,
    ) -> anyhow::Result<()> {
        self.app.set_debug_mode(request.debug)?;
        let request = c_app_internal_request(&request);
        let cargo = build::load_c_app_cargo_config(&request)?;
        let mut qemu = self
            .load_qemu_config(&request, &cargo)
            .await?
            .with_context(|| {
                format!(
                    "ArceOS C app config {} requires an explicit qemu config",
                    request.build_info_path.display()
                )
            })?;
        let output = self.build_c_app_request(&request, app_dir, app_name)?;
        crate::test::qemu::apply_dynamic_platform_qemu_boot(&mut qemu, &cargo);
        ensure_qemu_runtime_assets(self.app.workspace_root(), &qemu)?;
        self.app
            .prepare_elf_artifact(output.elf_path, qemu.to_bin)
            .await?;
        let _host_http_server = start_qemu_host_http_server(&request)?;
        self.app.run_prepared_qemu(qemu, None).await
    }

    async fn run_c_app_uboot_request(
        &mut self,
        request: ResolvedBuildRequest,
        app_dir: PathBuf,
        app_name: String,
    ) -> anyhow::Result<()> {
        self.app.set_debug_mode(request.debug)?;
        let request = c_app_internal_request(&request);
        let cargo = build::load_c_app_cargo_config(&request)?;
        let uboot = self
            .load_uboot_config(&request, &cargo)
            .await?
            .with_context(|| {
                format!(
                    "ArceOS C app config {} requires an explicit uboot config",
                    request.build_info_path.display()
                )
            })?;
        let output = self.build_c_app_request(&request, app_dir, app_name)?;
        self.app.prepare_elf_artifact(output.elf_path, true).await?;
        self.app.run_prepared_uboot(uboot).await
    }
}

fn warn_if_c_app_package_override(request: &ResolvedBuildRequest) {
    if request.package != "ax-libc" {
        warn!(
            "ArceOS C app build ignores --package {}; using ax-libc internally",
            request.package
        );
    }
}

fn c_app_internal_request(request: &ResolvedBuildRequest) -> ResolvedBuildRequest {
    warn_if_c_app_package_override(request);
    let mut request = request.clone();
    request.package = "ax-libc".to_string();
    request
}

#[cfg(test)]
mod tests {
    use tempfile::tempdir;

    use super::*;

    #[test]
    fn qemu_runtime_disk_images_finds_disk_img_drive_paths() {
        let qemu = QemuConfig {
            args: vec![
                "-drive".to_string(),
                "id=disk0,if=none,format=raw,file=/tmp/case/disk.img".to_string(),
                "-drive".to_string(),
                "id=rootfs,if=none,format=raw,file=/tmp/rootfs.img".to_string(),
            ],
            ..Default::default()
        };

        assert_eq!(
            qemu_runtime_disk_images(&qemu),
            vec![PathBuf::from("/tmp/case/disk.img")]
        );
    }

    #[test]
    fn should_recreate_only_tmp_runtime_asset_images() {
        let workspace = Path::new("/workspace");

        assert!(should_recreate_runtime_image(
            workspace,
            Path::new("/workspace/tmp/axbuild/runtime-assets/apps/arceos/std/case/disk.img")
        ));
        assert!(!should_recreate_runtime_image(
            workspace,
            Path::new("/workspace/test-suit/arceos/rust/fs/shell/disk.img")
        ));
    }

    #[test]
    fn qemu_request_starts_host_http_server_from_config() {
        let root = tempdir().unwrap();
        let qemu_config = root.path().join("qemu-x86_64.toml");
        std::fs::write(
            &qemu_config,
            r#"
args = []

[host_http_server]
port = 0
body = "fixture"
"#,
        )
        .unwrap();
        let request = ResolvedBuildRequest {
            package: "arceos-httpclient".to_string(),
            arch: "x86_64".to_string(),
            target: "x86_64-unknown-none".to_string(),
            plat_dyn: Some(true),
            smp: Some(1),
            debug: false,
            build_info_path: root.path().join("build.toml"),
            qemu_config: Some(qemu_config),
            uboot_config: None,
        };

        let guard = start_qemu_host_http_server(&request).unwrap();

        assert!(guard.is_some());
    }
}