Skip to content

Commit

Permalink
refactor(xtask): extract artifact args
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 1, 2023
1 parent 33c6493 commit 7264319
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 67 deletions.
67 changes: 67 additions & 0 deletions xtask/src/artifact.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::path::{Path, PathBuf};

use clap::Args;

use crate::arch::Arch;
use crate::archive::Archive;

#[derive(Args)]
pub struct Artifact {
/// Target architecture.
#[arg(value_enum, long)]
pub arch: Arch,

/// Directory for all generated artifacts.
#[arg(long)]
pub target_dir: Option<PathBuf>,

/// Build artifacts in release mode, with optimizations.
#[arg(short, long)]
pub release: bool,

/// Build artifacts with the specified profile.
#[arg(long)]
pub profile: Option<String>,
}

impl Artifact {
pub fn profile(&self) -> &str {
self.profile
.as_deref()
.unwrap_or(if self.release { "release" } else { "dev" })
}

pub fn target_dir(&self) -> &Path {
self.target_dir
.as_deref()
.unwrap_or_else(|| Path::new("target"))
}

pub fn out_dir(&self, triple: impl AsRef<Path>) -> PathBuf {
let mut out_dir = self.target_dir().to_path_buf();
out_dir.push(triple);
out_dir.push(match self.profile() {
"dev" => "debug",
profile => profile,
});
out_dir
}

pub fn builtins_archive(&self) -> Archive {
let mut builtins_archive = self.out_dir(self.arch.hermit_triple());
builtins_archive.push("libhermit_builtins.a");
builtins_archive.into()
}

pub fn build_archive(&self) -> Archive {
let mut built_archive = self.out_dir(self.arch.triple());
built_archive.push("libhermit.a");
built_archive.into()
}

pub fn dist_archive(&self) -> Archive {
let mut dist_archive = self.out_dir(self.arch.name());
dist_archive.push("libhermit.a");
dist_archive.into()
}
}
79 changes: 12 additions & 67 deletions xtask/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
use std::env::{self, VarError};
use std::ffi::OsStr;
use std::path::{Path, PathBuf};

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

use crate::arch::Arch;
use crate::archive::Archive;
use crate::artifact::Artifact;

/// Build the kernel.
#[derive(Args)]
pub struct Build {
/// Build for the architecture.
#[arg(value_enum, long)]
pub arch: Arch,

/// Directory for all generated artifacts.
#[arg(long)]
pub target_dir: Option<PathBuf>,
#[command(flatten)]
artifact: Artifact,

/// Do not activate the `default` feature.
#[arg(long)]
Expand All @@ -28,14 +21,6 @@ pub struct Build {
#[arg(long)]
pub features: Vec<String>,

/// Build artifacts in release mode, with optimizations.
#[arg(short, long)]
pub release: bool,

/// Build artifacts with the specified profile.
#[arg(long)]
pub profile: Option<String>,

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

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

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

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

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

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

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

fn no_default_features_args(&self) -> &[&str] {
Expand All @@ -141,11 +126,11 @@ impl Build {
}

fn profile_args(&self) -> [&str; 2] {
["--profile", self.profile()]
["--profile", self.artifact.profile()]
}

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

let syscall_symbols = archive.syscall_symbols()?;
let explicit_exports = [
Expand All @@ -170,44 +155,4 @@ impl Build {

Ok(())
}

fn profile(&self) -> &str {
self.profile
.as_deref()
.unwrap_or(if self.release { "release" } else { "dev" })
}

fn target_dir(&self) -> &Path {
self.target_dir
.as_deref()
.unwrap_or_else(|| Path::new("target"))
}

fn out_dir(&self, triple: impl AsRef<Path>) -> PathBuf {
let mut out_dir = self.target_dir().to_path_buf();
out_dir.push(triple);
out_dir.push(match self.profile() {
"dev" => "debug",
profile => profile,
});
out_dir
}

fn builtins_archive(&self) -> Archive {
let mut builtins_archive = self.out_dir(self.arch.hermit_triple());
builtins_archive.push("libhermit_builtins.a");
builtins_archive.into()
}

fn build_archive(&self) -> Archive {
let mut built_archive = self.out_dir(self.arch.triple());
built_archive.push("libhermit.a");
built_archive.into()
}

fn dist_archive(&self) -> Archive {
let mut dist_archive = self.out_dir(self.arch.name());
dist_archive.push("libhermit.a");
dist_archive.into()
}
}
1 change: 1 addition & 0 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
mod arch;
mod archive;
mod artifact;
mod build;
mod clippy;

Expand Down

0 comments on commit 7264319

Please sign in to comment.