axbuild 0.4.9

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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
use std::{
    fs,
    path::{Path, PathBuf},
};

use anyhow::{Context, bail};
use clap::{Args, Subcommand};

#[derive(Args, Debug, Clone)]
pub struct ArgsQuickStart {
    #[command(subcommand)]
    pub command: QuickStartCommand,
}

#[derive(Subcommand, Debug, Clone)]
pub enum QuickStartCommand {
    /// List supported quick-start platforms and templates
    List,
    #[command(name = "qemu-aarch64")]
    QemuAarch64(ArgsQuickQemu),
    #[command(name = "qemu-riscv64")]
    QemuRiscv64(ArgsQuickQemu),
    #[command(name = "qemu-loongarch64")]
    QemuLoongarch64(ArgsQuickQemu),
    #[command(name = "qemu-x86_64")]
    QemuX8664(ArgsQuickQemu),
    #[command(name = "orangepi-5-plus")]
    Orangepi5Plus(ArgsQuickOrange),
    #[command(name = "licheerv-nano-sg2002")]
    LicheervNanoSg2002(ArgsQuickSg2002),
}

#[derive(Args, Debug, Clone)]
pub struct ArgsQuickQemu {
    #[command(subcommand)]
    pub action: QuickQemuAction,
}

#[derive(Subcommand, Debug, Clone, Copy)]
pub enum QuickQemuAction {
    /// Prepare rootfs and build StarryOS for the selected QEMU platform
    Build,
    /// Build and run StarryOS for the selected QEMU platform
    Run,
}

#[derive(Args, Debug, Clone)]
pub struct ArgsQuickOrange {
    #[command(subcommand)]
    pub action: QuickOrangeAction,
}

#[derive(Subcommand, Debug, Clone)]
pub enum QuickOrangeAction {
    /// Build StarryOS for Orange Pi 5 Plus
    Build(QuickOrangeConfigArgs),
    /// Build and run StarryOS via U-Boot for Orange Pi 5 Plus
    Run(QuickOrangeRunArgs),
}

#[derive(Args, Debug, Clone, Default)]
pub struct QuickOrangeConfigArgs {
    /// Override serial device in the tmp U-Boot config
    #[arg(long)]
    pub serial: Option<String>,
    /// Override baud rate in the tmp U-Boot config
    #[arg(long)]
    pub baud: Option<String>,
    /// Override DTB path in the tmp U-Boot config
    #[arg(long)]
    pub dtb: Option<PathBuf>,
}

pub type QuickOrangeRunArgs = QuickOrangeConfigArgs;

impl QuickOrangeConfigArgs {
    fn has_overrides(&self) -> bool {
        self.serial.is_some() || self.baud.is_some() || self.dtb.is_some()
    }
}

#[derive(Args, Debug, Clone)]
pub struct ArgsQuickSg2002 {
    #[command(subcommand)]
    pub action: QuickSg2002Action,
}

#[derive(Subcommand, Debug, Clone)]
pub enum QuickSg2002Action {
    /// Build StarryOS for SG2002
    Build,
    /// Build and run StarryOS on a local SG2002 serial console
    Run(QuickSg2002RunArgs),
}

#[derive(Args, Debug, Clone, Default)]
pub struct QuickSg2002RunArgs {
    /// Override serial device in the tmp SG2002 U-Boot config
    #[arg(long)]
    pub serial: Option<String>,
    /// Override baud rate in the tmp SG2002 U-Boot config
    #[arg(long)]
    pub baud: Option<String>,
}

#[derive(Debug, Clone, Copy)]
pub enum QuickQemuPlatform {
    Aarch64,
    Riscv64,
    Loongarch64,
    X8664,
}

impl QuickQemuPlatform {
    pub const fn arch(self) -> &'static str {
        match self {
            Self::Aarch64 => "aarch64",
            Self::Riscv64 => "riscv64",
            Self::Loongarch64 => "loongarch64",
            Self::X8664 => "x86_64",
        }
    }

    pub const fn cli_name(self) -> &'static str {
        match self {
            Self::Aarch64 => "qemu-aarch64",
            Self::Riscv64 => "qemu-riscv64",
            Self::Loongarch64 => "qemu-loongarch64",
            Self::X8664 => "qemu-x86_64",
        }
    }
}

pub fn print_supported_platforms(workspace_root: &Path) {
    println!("Supported platforms:");
    for platform in [
        QuickQemuPlatform::Aarch64,
        QuickQemuPlatform::Riscv64,
        QuickQemuPlatform::Loongarch64,
        QuickQemuPlatform::X8664,
    ] {
        let build = qemu_build_config_path(workspace_root, platform);
        let tmp_build = tmp_qemu_build_config_path(workspace_root, platform);
        println!("  {}", platform.cli_name());
        println!("    build template: {}", build.display());
        println!(
            "    run template:   {}",
            super::default_qemu_config_template_path(workspace_root, platform.arch()).display()
        );
        println!("    tmp build cfg:  {}", tmp_build.display());
        println!(
            "    commands:\n      cargo xtask starry quick-start {} build\n      cargo xtask \
             starry quick-start {} run",
            platform.cli_name(),
            platform.cli_name()
        );
    }

    let build = orangepi_build_config_path(workspace_root);
    let run = orangepi_uboot_config_path(workspace_root);
    let tmp_build = tmp_orangepi_build_config_path(workspace_root);
    let tmp_run = tmp_orangepi_uboot_config_path(workspace_root);
    println!("  orangepi-5-plus");
    println!("    build template: {}", build.display());
    println!("    run template:   {}", run.display());
    println!("    tmp build cfg:  {}", tmp_build.display());
    println!("    tmp run cfg:    {}", tmp_run.display());
    println!(
        "    commands:\n      cargo xtask starry quick-start orangepi-5-plus build\n      cargo \
         xtask starry quick-start orangepi-5-plus run --serial /dev/ttyUSB0"
    );

    let build = sg2002_build_config_path(workspace_root);
    let board = sg2002_board_config_path(workspace_root);
    let tmp_build = tmp_sg2002_build_config_path(workspace_root);
    println!("  licheerv-nano-sg2002");
    println!("    build template: {}", build.display());
    println!("    board template: {}", board.display());
    println!(
        "    local run template: {}",
        sg2002_uboot_config_path(workspace_root).display()
    );
    println!("    tmp build cfg:  {}", tmp_build.display());
    println!(
        "    commands:\n      cargo xtask starry quick-start licheerv-nano-sg2002 build\n      \
         cargo xtask starry quick-start licheerv-nano-sg2002 run --serial /dev/ttyUSB0"
    );
}

pub fn qemu_build_config_path(workspace_root: &Path, platform: QuickQemuPlatform) -> PathBuf {
    workspace_root
        .join("os/StarryOS/configs/board")
        .join(match platform {
            QuickQemuPlatform::Aarch64 => "qemu-aarch64.toml",
            QuickQemuPlatform::Riscv64 => "qemu-riscv64.toml",
            QuickQemuPlatform::Loongarch64 => "qemu-loongarch64.toml",
            QuickQemuPlatform::X8664 => "qemu-x86_64.toml",
        })
}

pub fn orangepi_build_config_path(workspace_root: &Path) -> PathBuf {
    workspace_root.join("os/StarryOS/configs/board/orangepi-5-plus.toml")
}

pub fn orangepi_uboot_config_path(workspace_root: &Path) -> PathBuf {
    workspace_root.join("os/StarryOS/configs/board/orangepi-5-plus-uboot.toml")
}

pub fn sg2002_build_config_path(workspace_root: &Path) -> PathBuf {
    workspace_root.join("os/StarryOS/configs/board/licheerv-nano-sg2002.toml")
}

pub fn sg2002_board_config_path(workspace_root: &Path) -> PathBuf {
    workspace_root.join("os/StarryOS/configs/board/licheerv-nano-sg2002-board.toml")
}

pub fn sg2002_uboot_config_path(workspace_root: &Path) -> PathBuf {
    workspace_root.join("os/StarryOS/configs/board/licheerv-nano-sg2002-uboot.toml")
}

pub fn tmp_config_dir(workspace_root: &Path) -> PathBuf {
    crate::context::axbuild_tmp_dir(workspace_root)
        .join("config")
        .join("starryos")
        .join("quick-start")
}

pub fn tmp_qemu_build_config_path(workspace_root: &Path, platform: QuickQemuPlatform) -> PathBuf {
    tmp_config_dir(workspace_root).join(match platform {
        QuickQemuPlatform::Aarch64 => "build-aarch64.toml",
        QuickQemuPlatform::Riscv64 => "build-riscv64.toml",
        QuickQemuPlatform::Loongarch64 => "build-loongarch64.toml",
        QuickQemuPlatform::X8664 => "build-x86_64.toml",
    })
}

pub fn tmp_orangepi_build_config_path(workspace_root: &Path) -> PathBuf {
    tmp_config_dir(workspace_root).join("orangepi-5-plus.toml")
}

pub fn tmp_orangepi_uboot_config_path(workspace_root: &Path) -> PathBuf {
    tmp_config_dir(workspace_root).join("orangepi-5-plus-uboot.toml")
}

pub fn tmp_sg2002_build_config_path(workspace_root: &Path) -> PathBuf {
    tmp_config_dir(workspace_root).join("licheerv-nano-sg2002.toml")
}

pub fn tmp_sg2002_uboot_config_path(workspace_root: &Path) -> PathBuf {
    tmp_config_dir(workspace_root).join("licheerv-nano-sg2002-uboot.toml")
}

pub fn default_orangepi_dtb_path(workspace_root: &Path) -> PathBuf {
    workspace_root.join("os/StarryOS/configs/board/orangepi-5-plus.dtb")
}

fn copy_template(src: &Path, dst: &Path) -> anyhow::Result<()> {
    if let Some(parent) = dst.parent() {
        fs::create_dir_all(parent)
            .with_context(|| format!("failed to create {}", parent.display()))?;
    }
    fs::copy(src, dst)
        .with_context(|| format!("failed to copy {} to {}", src.display(), dst.display()))?;
    Ok(())
}

pub fn refresh_qemu_build_config(
    workspace_root: &Path,
    platform: QuickQemuPlatform,
) -> anyhow::Result<()> {
    copy_template(
        &qemu_build_config_path(workspace_root, platform),
        &tmp_qemu_build_config_path(workspace_root, platform),
    )?;
    Ok(())
}

pub fn ensure_qemu_build_config(
    workspace_root: &Path,
    platform: QuickQemuPlatform,
) -> anyhow::Result<()> {
    let build_cfg = tmp_qemu_build_config_path(workspace_root, platform);
    if !build_cfg.exists() {
        refresh_qemu_build_config(workspace_root, platform)?;
    }
    Ok(())
}

pub fn refresh_orangepi_configs(workspace_root: &Path) -> anyhow::Result<()> {
    copy_template(
        &orangepi_build_config_path(workspace_root),
        &tmp_orangepi_build_config_path(workspace_root),
    )?;
    copy_template(
        &orangepi_uboot_config_path(workspace_root),
        &tmp_orangepi_uboot_config_path(workspace_root),
    )?;
    Ok(())
}

pub fn refresh_sg2002_config(workspace_root: &Path) -> anyhow::Result<()> {
    copy_template(
        &sg2002_build_config_path(workspace_root),
        &tmp_sg2002_build_config_path(workspace_root),
    )?;
    copy_template(
        &sg2002_uboot_config_path(workspace_root),
        &tmp_sg2002_uboot_config_path(workspace_root),
    )?;
    Ok(())
}

pub fn ensure_sg2002_config(workspace_root: &Path) -> anyhow::Result<()> {
    let build_cfg = tmp_sg2002_build_config_path(workspace_root);
    if !build_cfg.exists() {
        copy_template(&sg2002_build_config_path(workspace_root), &build_cfg)?;
    }
    let run_cfg = tmp_sg2002_uboot_config_path(workspace_root);
    if !run_cfg.exists() {
        copy_template(&sg2002_uboot_config_path(workspace_root), &run_cfg)?;
    }
    Ok(())
}

pub fn ensure_orangepi_configs(workspace_root: &Path) -> anyhow::Result<()> {
    let build_cfg = tmp_orangepi_build_config_path(workspace_root);
    let run_cfg = tmp_orangepi_uboot_config_path(workspace_root);
    if !build_cfg.exists() {
        copy_template(&orangepi_build_config_path(workspace_root), &build_cfg)?;
    }
    if !run_cfg.exists() {
        copy_template(&orangepi_uboot_config_path(workspace_root), &run_cfg)?;
    }
    Ok(())
}

pub fn prepare_orangepi_uboot_config(
    workspace_root: &Path,
    args: &QuickOrangeConfigArgs,
) -> anyhow::Result<PathBuf> {
    let tmp_path = tmp_orangepi_uboot_config_path(workspace_root);
    let tmp_exists = tmp_path.exists();
    if tmp_exists && !args.has_overrides() {
        return Ok(tmp_path);
    }

    if !tmp_exists {
        copy_template(&orangepi_uboot_config_path(workspace_root), &tmp_path)?;
    }

    let mut value: toml::Value = toml::from_str(
        &fs::read_to_string(&tmp_path)
            .with_context(|| format!("failed to read {}", tmp_path.display()))?,
    )
    .with_context(|| format!("failed to parse {}", tmp_path.display()))?;

    let table = value
        .as_table_mut()
        .ok_or_else(|| anyhow::anyhow!("expected top-level TOML table"))?;
    if let Some(serial) = &args.serial {
        table.insert("serial".into(), toml::Value::String(serial.clone()));
    }
    if let Some(baud) = &args.baud {
        table.insert("baud_rate".into(), toml::Value::String(baud.clone()));
    }

    if let Some(dtb_path) = args
        .dtb
        .clone()
        .or_else(|| (!tmp_exists).then(|| default_orangepi_dtb_path(workspace_root)))
    {
        if !dtb_path.exists() {
            bail!("DTB path does not exist: {}", dtb_path.display());
        }
        table.insert(
            "dtb_file".into(),
            toml::Value::String(dtb_path.display().to_string()),
        );
    }

    fs::write(&tmp_path, toml::to_string_pretty(&value)?)
        .with_context(|| format!("failed to write {}", tmp_path.display()))?;
    Ok(tmp_path)
}

pub fn prepare_sg2002_uboot_config(
    workspace_root: &Path,
    args: &QuickSg2002RunArgs,
) -> anyhow::Result<PathBuf> {
    let tmp_path = tmp_sg2002_uboot_config_path(workspace_root);
    let tmp_exists = tmp_path.exists();

    if !tmp_exists {
        copy_template(&sg2002_uboot_config_path(workspace_root), &tmp_path)?;
    }

    let template: toml::Value = toml::from_str(
        &fs::read_to_string(sg2002_uboot_config_path(workspace_root)).with_context(|| {
            format!(
                "failed to read {}",
                sg2002_uboot_config_path(workspace_root).display()
            )
        })?,
    )
    .with_context(|| {
        format!(
            "failed to parse {}",
            sg2002_uboot_config_path(workspace_root).display()
        )
    })?;
    let mut value: toml::Value = toml::from_str(
        &fs::read_to_string(&tmp_path)
            .with_context(|| format!("failed to read {}", tmp_path.display()))?,
    )
    .with_context(|| format!("failed to parse {}", tmp_path.display()))?;

    let table = value
        .as_table_mut()
        .ok_or_else(|| anyhow::anyhow!("expected top-level TOML table"))?;
    let template_table = template
        .as_table()
        .ok_or_else(|| anyhow::anyhow!("expected top-level TOML table"))?;
    for key in [
        "kernel_load_addr",
        "fit_load_addr",
        "dtb_file",
        "shell_prefix",
        "shell_init_cmd",
        "success_regex",
        "fail_regex",
        "timeout",
    ] {
        if !table.contains_key(key)
            && let Some(value) = template_table.get(key)
        {
            table.insert(key.to_string(), value.clone());
        }
    }
    if let Some(serial) = &args.serial {
        table.insert("serial".into(), toml::Value::String(serial.clone()));
    }
    if let Some(baud) = &args.baud {
        table.insert("baud_rate".into(), toml::Value::String(baud.clone()));
    }

    fs::write(&tmp_path, toml::to_string_pretty(&value)?)
        .with_context(|| format!("failed to write {}", tmp_path.display()))?;
    Ok(tmp_path)
}

#[cfg(test)]
mod tests {
    use std::{fs, path::Path};

    use tempfile::tempdir;

    use super::*;

    fn write_orangepi_uboot_template(root: &Path) {
        let path = orangepi_uboot_config_path(root);
        fs::create_dir_all(path.parent().unwrap()).unwrap();
        fs::write(
            path,
            "serial = \"/dev/template\"\nbaud_rate = \"1500000\"\ndtb_file = \"template.dtb\"\n",
        )
        .unwrap();
    }

    fn write_sg2002_uboot_template(root: &Path) {
        let path = sg2002_uboot_config_path(root);
        fs::create_dir_all(path.parent().unwrap()).unwrap();
        fs::write(
            path,
            "serial = \"/dev/template\"\nbaud_rate = \"115200\"\nkernel_load_addr = \
             \"0x80200000\"\nfit_load_addr = \"0x82200000\"\ndtb_file = \"sg2002.dtb\"\n",
        )
        .unwrap();
    }

    #[test]
    fn prepare_orangepi_uboot_config_keeps_existing_tmp_without_overrides() {
        let root = tempdir().unwrap();
        write_orangepi_uboot_template(root.path());
        let tmp_path = tmp_orangepi_uboot_config_path(root.path());
        fs::create_dir_all(tmp_path.parent().unwrap()).unwrap();
        fs::write(
            &tmp_path,
            "serial = \"/dev/existing\"\nbaud_rate = \"9600\"\ndtb_file = \"existing.dtb\"\n",
        )
        .unwrap();

        prepare_orangepi_uboot_config(root.path(), &QuickOrangeConfigArgs::default()).unwrap();

        let value: toml::Value = toml::from_str(&fs::read_to_string(tmp_path).unwrap()).unwrap();
        assert_eq!(value["serial"].as_str(), Some("/dev/existing"));
        assert_eq!(value["baud_rate"].as_str(), Some("9600"));
        assert_eq!(value["dtb_file"].as_str(), Some("existing.dtb"));
    }

    #[test]
    fn prepare_orangepi_uboot_config_updates_existing_tmp_overrides() {
        let root = tempdir().unwrap();
        write_orangepi_uboot_template(root.path());
        let tmp_path = tmp_orangepi_uboot_config_path(root.path());
        fs::create_dir_all(tmp_path.parent().unwrap()).unwrap();
        fs::write(
            &tmp_path,
            "serial = \"/dev/existing\"\nbaud_rate = \"9600\"\ndtb_file = \"existing.dtb\"\n",
        )
        .unwrap();
        let dtb_path = root.path().join("custom.dtb");
        fs::write(&dtb_path, "").unwrap();

        prepare_orangepi_uboot_config(
            root.path(),
            &QuickOrangeConfigArgs {
                serial: Some("/dev/ttyUSB1".to_string()),
                baud: Some("115200".to_string()),
                dtb: Some(dtb_path.clone()),
            },
        )
        .unwrap();

        let value: toml::Value = toml::from_str(&fs::read_to_string(tmp_path).unwrap()).unwrap();
        assert_eq!(value["serial"].as_str(), Some("/dev/ttyUSB1"));
        assert_eq!(value["baud_rate"].as_str(), Some("115200"));
        let expected_dtb = dtb_path.display().to_string();
        assert_eq!(value["dtb_file"].as_str(), Some(expected_dtb.as_str()));
    }

    #[test]
    fn prepare_sg2002_uboot_config_updates_existing_tmp_overrides() {
        let root = tempdir().unwrap();
        write_sg2002_uboot_template(root.path());
        let tmp_path = tmp_sg2002_uboot_config_path(root.path());
        fs::create_dir_all(tmp_path.parent().unwrap()).unwrap();
        fs::write(
            &tmp_path,
            "serial = \"/dev/existing\"\nbaud_rate = \"9600\"\n",
        )
        .unwrap();

        prepare_sg2002_uboot_config(
            root.path(),
            &QuickSg2002RunArgs {
                serial: Some("/dev/ttyUSB2".to_string()),
                baud: Some("115200".to_string()),
            },
        )
        .unwrap();

        let value: toml::Value = toml::from_str(&fs::read_to_string(tmp_path).unwrap()).unwrap();
        assert_eq!(value["serial"].as_str(), Some("/dev/ttyUSB2"));
        assert_eq!(value["baud_rate"].as_str(), Some("115200"));
        assert_eq!(value["kernel_load_addr"].as_str(), Some("0x80200000"));
        assert_eq!(value["fit_load_addr"].as_str(), Some("0x82200000"));
        assert_eq!(value["dtb_file"].as_str(), Some("sg2002.dtb"));
    }
}