From 51c5c2ec47cefafae9acfa240d2eb51c384ce09d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Tue, 5 Sep 2023 10:55:36 +0200 Subject: [PATCH] feat(xtask): extract CargoBuild This improves generated commands. --- xtask/src/artifact.rs | 1 + xtask/src/build.rs | 52 ++++++---------------------- xtask/src/cargo_build.rs | 73 ++++++++++++++++++++++++++++++++++++++++ xtask/src/main.rs | 1 + 4 files changed, 86 insertions(+), 41 deletions(-) create mode 100644 xtask/src/cargo_build.rs diff --git a/xtask/src/artifact.rs b/xtask/src/artifact.rs index 57ef29ed3b..6f4aa51019 100644 --- a/xtask/src/artifact.rs +++ b/xtask/src/artifact.rs @@ -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(), diff --git a/xtask/src/build.rs b/xtask/src/build.rs index 57bbe455dc..534c6c3290 100644 --- a/xtask/src/build.rs +++ b/xtask/src/build.rs @@ -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, + cargo_build: CargoBuild, /// Enable the `-Z instrument-mcount` flag. #[arg(long)] @@ -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(), @@ -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())?; @@ -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 { - 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 = [ diff --git a/xtask/src/cargo_build.rs b/xtask/src/cargo_build.rs new file mode 100644 index 0000000000..0570a51458 --- /dev/null +++ b/xtask/src/cargo_build.rs @@ -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, +} + +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 { + self.features + .iter() + .flat_map(|feature| ["--features", feature.as_str()]) + } +} diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 7c2ce1ce0c..0a089c9a4c 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -4,6 +4,7 @@ mod arch; mod archive; mod artifact; mod build; +mod cargo_build; mod clippy; use std::path::Path;