Skip to content

Commit

Permalink
feat(xtask): extract CargoBuild
Browse files Browse the repository at this point in the history
This improves generated commands.
  • Loading branch information
mkroening committed Sep 6, 2023
1 parent 4009249 commit a68b1e0
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 41 deletions.
1 change: 1 addition & 0 deletions xtask/src/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl Artifact {

pub fn builtins_archive(&self) -> Archive {
[
"hermit-builtins".as_ref(),
self.target_dir(),
self.arch.hermit_triple().as_ref(),
"release".as_ref(),
Expand Down
52 changes: 11 additions & 41 deletions xtask/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
use std::env::{self, VarError};
use std::ffi::OsStr;

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

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

/// Build the kernel.
#[derive(Args)]
pub struct Build {
#[command(flatten)]
artifact: Artifact,

/// Do not activate the `default` feature.
#[arg(long)]
pub no_default_features: bool,

/// Space or comma separated list of features to activate.
#[arg(long)]
pub features: Vec<String>,
cargo_build: CargoBuild,

/// Enable the `-Z instrument-mcount` flag.
#[arg(long)]
Expand All @@ -37,15 +28,12 @@ impl Build {
eprintln!("Building kernel");
cmd!(sh, "cargo build")
.env("CARGO_ENCODED_RUSTFLAGS", self.cargo_encoded_rustflags()?)
.args(&["--profile", self.artifact.profile()])
.args(self.artifact.arch.cargo_args())
.args(self.target_dir_args())
.args(self.no_default_features_args())
.args(self.features_args())
.args(self.cargo_build.artifact.arch.cargo_args())
.cargo_build_args(&self.cargo_build)
.run()?;

let build_archive = self.artifact.build_archive();
let dist_archive = self.artifact.dist_archive();
let build_archive = self.cargo_build.artifact.build_archive();
let dist_archive = self.cargo_build.artifact.dist_archive();
eprintln!(
"Copying {} to {}",
build_archive.as_ref().display(),
Expand All @@ -60,12 +48,12 @@ impl Build {
eprintln!("Building hermit-builtins");
cmd!(sh, "cargo build --release")
.arg("--manifest-path=hermit-builtins/Cargo.toml")
.args(self.artifact.arch.builtins_cargo_args())
.args(self.target_dir_args())
.args(self.cargo_build.artifact.arch.builtins_cargo_args())
.target_dir_args(&self.cargo_build)
.run()?;

eprintln!("Exporting hermit-builtins symbols");
let builtins = self.artifact.builtins_archive();
let builtins = self.cargo_build.artifact.builtins_archive();
let builtin_symbols = sh.read_file("hermit-builtins/exports")?;
builtins.retain_symbols(builtin_symbols.lines())?;

Expand Down Expand Up @@ -101,31 +89,13 @@ impl Build {
rustflags.push("-Zrandomize-layout")
}

rustflags.extend(self.artifact.arch.rustflags());
rustflags.extend(self.cargo_build.artifact.arch.rustflags());

Ok(rustflags.join("\x1f"))
}

fn target_dir_args(&self) -> [&OsStr; 2] {
["--target-dir".as_ref(), self.artifact.target_dir().as_ref()]
}

fn no_default_features_args(&self) -> &[&str] {
if self.no_default_features {
&["--no-default-features"]
} else {
&[]
}
}

fn features_args(&self) -> impl Iterator<Item = &str> {
self.features
.iter()
.flat_map(|feature| ["--features", feature.as_str()])
}

fn export_syms(&self) -> Result<()> {
let archive = self.artifact.dist_archive();
let archive = self.cargo_build.artifact.dist_archive();

let syscall_symbols = archive.syscall_symbols()?;
let explicit_exports = [
Expand Down
73 changes: 73 additions & 0 deletions xtask/src/cargo_build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use std::ffi::OsStr;

use clap::Args;
use xshell::Cmd;

use crate::artifact::Artifact;

#[derive(Args)]
pub struct CargoBuild {
#[command(flatten)]
pub artifact: Artifact,

/// Do not activate the `default` feature.
#[arg(long)]
no_default_features: bool,

/// Space or comma separated list of features to activate.
#[arg(long)]
features: Vec<String>,
}

pub trait CmdExt {
fn cargo_build_args(self, cargo_build: &CargoBuild) -> Self;
fn target_dir_args(self, cargo_build: &CargoBuild) -> Self;
}

impl CmdExt for Cmd<'_> {
fn cargo_build_args(self, cargo_build: &CargoBuild) -> Self {
let cmd = self
.target_dir_args(cargo_build)
.args(cargo_build.no_default_features_args())
.args(cargo_build.features_args())
.args(cargo_build.release_args());

if let Some(profile) = &cargo_build.artifact.profile {
cmd.args(&["--profile", profile])
} else {
cmd
}
}

fn target_dir_args(self, cargo_build: &CargoBuild) -> Self {
if let Some(target_dir) = &cargo_build.artifact.target_dir {
self.args::<&[&OsStr]>(&["--target-dir".as_ref(), target_dir.as_ref()])
} else {
self
}
}
}

impl CargoBuild {
fn release_args(&self) -> &'static [&'static str] {
if self.artifact.release {
&["--release"]
} else {
&[]
}
}

fn no_default_features_args(&self) -> &'static [&'static str] {
if self.no_default_features {
&["--no-default-features"]
} else {
&[]
}
}

fn features_args(&self) -> impl Iterator<Item = &str> {
self.features
.iter()
.flat_map(|feature| ["--features", feature.as_str()])
}
}
1 change: 1 addition & 0 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod arch;
mod archive;
mod artifact;
mod build;
mod cargo_build;
mod clippy;

use std::path::Path;
Expand Down

0 comments on commit a68b1e0

Please sign in to comment.