diff --git a/crates/cargo-builder/src/cargo.rs b/crates/cargo-builder/src/cargo.rs index d9eb60d2e0..09379b1c13 100644 --- a/crates/cargo-builder/src/cargo.rs +++ b/crates/cargo-builder/src/cargo.rs @@ -15,6 +15,7 @@ const AMBIGUOUS_TARGET_ERR_MSG: &str = const BUILD_CMD: &str = "build"; const CLEAN_CMD: &str = "clean"; +const ZIGBUILD_CMD: &str = "zigbuild"; #[derive(Default)] pub enum Profile { @@ -28,6 +29,7 @@ pub enum CargoCommand { #[default] Build, Clean, + ZigBuild, } impl Display for Profile { @@ -95,7 +97,6 @@ impl Cargo { fn make_cargo_cmd(&self) -> Result { let cwd = std::env::current_dir()?; - let mut cargo = Command::new("cargo"); cargo.output().map_err(Error::from)?; @@ -113,6 +114,13 @@ impl Cargo { CargoCommand::Clean => { cargo.current_dir(&cwd).arg(CLEAN_CMD); } + CargoCommand::ZigBuild => { + cargo + .current_dir(&cwd) + .arg(ZIGBUILD_CMD) + .arg("--profile") + .arg(&self.profile); + } } if self.lib { diff --git a/crates/cdk/src/build.rs b/crates/cdk/src/build.rs index 9d5cf51861..90e7e5e8a9 100644 --- a/crates/cdk/src/build.rs +++ b/crates/cdk/src/build.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; use std::fmt::Debug; use anyhow::Result; @@ -8,12 +9,18 @@ use cargo_builder::package::PackageInfo; use crate::cmd::PackageCmd; use crate::utils::build::{BuildOpts, build_connector}; +const CLOUD_ARCH_TARGET: &str = "aarch64-unknown-linux-musl"; + /// Build the Connector in the current working directory #[derive(Debug, Parser)] pub struct BuildCmd { #[clap(flatten)] package: PackageCmd, + /// build optimized for running in the Infinyon Cloud environment + #[arg(long, default_value_t = false)] + cloud: bool, + /// Extra arguments to be passed to cargo #[arg(raw = true)] extra_arguments: Vec, @@ -21,15 +28,36 @@ pub struct BuildCmd { impl BuildCmd { pub(crate) fn process(self) -> Result<()> { - let opt = self.package.as_opt(); + let mut opt = self.package.as_opt(); + if self.cloud { + opt.target = CLOUD_ARCH_TARGET.to_owned(); + } else if target_not_specified() { + let tmap = Self::target_map(); + if let Some(tgt) = tmap.get(&opt.target.as_str()) { + opt.target = tgt.to_string(); + } + } let package_info = PackageInfo::from_options(&opt)?; build_connector( &package_info, BuildOpts { + cloud: self.cloud, release: opt.release, extra_arguments: self.extra_arguments, }, ) } + + /// Map to most supported native target + fn target_map() -> HashMap<&'static str, &'static str> { + let mut map = HashMap::new(); + map.insert("x86_64-unknown-linux-musl", "x86_64-unknown-linux-gnu"); + map + } +} + +fn target_not_specified() -> bool { + let args = std::env::args().collect::>(); + !args.iter().any(|arg| arg.contains("--target")) } diff --git a/crates/cdk/src/publish.rs b/crates/cdk/src/publish.rs index ed8cff10bf..ef1f0c75ee 100644 --- a/crates/cdk/src/publish.rs +++ b/crates/cdk/src/publish.rs @@ -45,6 +45,11 @@ pub struct PublishCmd { #[arg(long, default_value = "false")] pub no_build: bool, + /// publish a build for running in the Infinyon Cloud environment + /// + #[arg(long, default_value_t = false)] + cloud: bool, + /// do only the pack portion #[arg(long, hide_short_help = true)] pack: bool, diff --git a/crates/cdk/src/test.rs b/crates/cdk/src/test.rs index a1ce865e7b..72f3ec0018 100644 --- a/crates/cdk/src/test.rs +++ b/crates/cdk/src/test.rs @@ -34,6 +34,7 @@ impl TestCmd { let opt = self.package.as_opt(); let package_info = PackageInfo::from_options(&opt)?; let build_options = BuildOpts { + cloud: false, release: opt.release, extra_arguments: self.extra_arguments, }; diff --git a/crates/cdk/src/utils.rs b/crates/cdk/src/utils.rs index 47c722f7ab..6f6ebd5397 100644 --- a/crates/cdk/src/utils.rs +++ b/crates/cdk/src/utils.rs @@ -2,9 +2,10 @@ pub mod build { use anyhow::Result; use cargo_builder::package::PackageInfo; - use cargo_builder::cargo::Cargo; + use cargo_builder::cargo::{Cargo, CargoCommand}; pub struct BuildOpts { + pub cloud: bool, pub(crate) release: String, pub(crate) extra_arguments: Vec, } @@ -12,6 +13,7 @@ pub mod build { impl BuildOpts { pub fn with_release(release: &str) -> Self { Self { + cloud: false, release: release.to_string(), extra_arguments: Vec::default(), } @@ -20,14 +22,16 @@ pub mod build { /// Builds a Connector given it's package info and Cargo Build options pub fn build_connector(package_info: &PackageInfo, opts: BuildOpts) -> Result<()> { - let cargo = Cargo::build() + let mut cargo = Cargo::build() .profile(opts.release) .lib(false) .package(package_info.package_name()) .target(package_info.arch_target()) .extra_arguments(opts.extra_arguments) .build()?; - + if opts.cloud { + cargo.cmd = CargoCommand::ZigBuild; + } cargo.run() } }