Skip to content

Commit

Permalink
feat(xtask): add ci subcommand
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <[email protected]>
  • Loading branch information
mkroening committed Sep 11, 2023
1 parent 7fbc7eb commit f91f321
Show file tree
Hide file tree
Showing 13 changed files with 969 additions and 205 deletions.
239 changes: 48 additions & 191 deletions .github/workflows/ci.yml

Large diffs are not rendered by default.

432 changes: 431 additions & 1 deletion Cargo.lock

Large diffs are not rendered by default.

13 changes: 0 additions & 13 deletions fc-config.json

This file was deleted.

3 changes: 3 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ anyhow = "1.0"
clap = { version = "4", features = ["derive"] }
goblin = { version = "0.7", default-features = false, features = ["archive", "elf32", "elf64", "std"] }
llvm-tools = "0.1"
sysinfo = "0.29"
ureq = "2"
wait-timeout = "0.2"
xshell = "0.2"
13 changes: 13 additions & 0 deletions xtask/src/arch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ impl Arch {
}
}

pub fn ci_cargo_args(&self) -> &'static [&'static str] {
match self {
Arch::X86_64 => &[
"--target=x86_64-unknown-hermit",
"-Zbuild-std=std,panic_abort",
],
Arch::Aarch64 => &[
"--target=aarch64-unknown-hermit",
"-Zbuild-std=std,panic_abort",
],
}
}

pub fn rustflags(&self) -> &'static [&'static str] {
match self {
Self::X86_64 => &[],
Expand Down
12 changes: 12 additions & 0 deletions xtask/src/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,16 @@ impl Artifact {
.collect::<PathBuf>()
.into()
}

pub fn ci_image(&self, package: &str) -> PathBuf {
[
"..".as_ref(),
self.target_dir(),
self.arch.hermit_triple().as_ref(),
self.profile_path_component().as_ref(),
package.as_ref(),
]
.iter()
.collect()
}
}
45 changes: 45 additions & 0 deletions xtask/src/ci/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::path::PathBuf;

use anyhow::Result;
use clap::Args;
use xshell::cmd;

use crate::cargo_build::{CargoBuild, CmdExt};

/// Build hermit-rs images.
#[derive(Args)]
#[command(next_help_heading = "Build options")]
pub struct Build {
#[command(flatten)]
pub cargo_build: CargoBuild,

/// Package to build (see `cargo help pkgid`)
#[arg(short, long, id = "SPEC")]
pub package: String,
}

impl Build {
pub fn run(&self) -> Result<()> {
if super::in_ci() {
eprintln!("::group::cargo build")
}

let sh = crate::sh()?;

cmd!(sh, "cargo build --manifest-path ../Cargo.toml")
.args(self.cargo_build.artifact.arch.ci_cargo_args())
.cargo_build_args(&self.cargo_build)
.args(&["--package", self.package.as_str()])
.run()?;

if super::in_ci() {
eprintln!("::endgroup::")
}

Ok(())
}

pub fn image(&self) -> PathBuf {
self.cargo_build.artifact.ci_image(&self.package)
}
}
42 changes: 42 additions & 0 deletions xtask/src/ci/firecracker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::path::Path;

use anyhow::Result;
use clap::Args;
use xshell::cmd;

use super::build::Build;

/// Run hermit-rs images on Firecracker.
#[derive(Args)]
pub struct Firecracker {
#[command(flatten)]
build: Build,
}

impl Firecracker {
pub fn run(self) -> Result<()> {
self.build.run()?;

let sh = crate::sh()?;

let config = format!(
include_str!("firecracker_vm_config.json"),
kernel_image_path = "../rusty-loader-x86_64-fc",
initrd_path = self.build.image().display()
);
eprintln!("firecracker config");
eprintln!("{config}");
let config_path = Path::new("firecracker_vm_config.json");
sh.write_file(config_path, config)?;

let log_path = Path::new("firecracker.log");
sh.write_file(log_path, "")?;
cmd!(sh, "firecracker --no-api --config-file {config_path} --log-path {log_path} --level Info --show-level --show-log-origin").run()?;
let log = sh.read_file(log_path)?;

eprintln!("firecracker log");
eprintln!("{log}");

Ok(())
}
}
13 changes: 13 additions & 0 deletions xtask/src/ci/firecracker_vm_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{
"boot-source": {{
"kernel_image_path": "{kernel_image_path}",
"initrd_path": "{initrd_path}",
"boot_args": ""
}},
"drives": [],
"machine-config": {{
"vcpu_count": 1,
"mem_size_mib": 256,
"smt": false
}}
}}
31 changes: 31 additions & 0 deletions xtask/src/ci/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use anyhow::Result;
use clap::Subcommand;

mod build;
mod firecracker;
mod qemu;
mod uhyve;

/// Run CI tasks.
#[derive(Subcommand)]
pub enum Ci {
Build(build::Build),
Firecracker(firecracker::Firecracker),
Qemu(qemu::Qemu),
Uhyve(uhyve::Uhyve),
}

impl Ci {
pub fn run(self) -> Result<()> {
match self {
Self::Build(build) => build.run(),
Self::Firecracker(firecracker) => firecracker.run(),
Self::Qemu(qemu) => qemu.run(),
Self::Uhyve(uhyve) => uhyve.run(),
}
}
}

fn in_ci() -> bool {
std::env::var_os("CI") == Some("true".into())
}
Loading

0 comments on commit f91f321

Please sign in to comment.