Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add(cdk): cloud build flags #4170

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion crates/cargo-builder/src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -28,6 +29,7 @@ pub enum CargoCommand {
#[default]
Build,
Clean,
ZigBuild,
}

impl Display for Profile {
Expand Down Expand Up @@ -95,7 +97,6 @@ impl Cargo {

fn make_cargo_cmd(&self) -> Result<Command> {
let cwd = std::env::current_dir()?;

let mut cargo = Command::new("cargo");

cargo.output().map_err(Error::from)?;
Expand All @@ -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 {
Expand Down
30 changes: 29 additions & 1 deletion crates/cdk/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::fmt::Debug;

use anyhow::Result;
Expand All @@ -8,28 +9,55 @@ 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<String>,
}

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::<Vec<String>>();
!args.iter().any(|arg| arg.contains("--target"))
}
5 changes: 5 additions & 0 deletions crates/cdk/src/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions crates/cdk/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down
10 changes: 7 additions & 3 deletions crates/cdk/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ 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<String>,
}

impl BuildOpts {
pub fn with_release(release: &str) -> Self {
Self {
cloud: false,
release: release.to_string(),
extra_arguments: Vec::default(),
}
Expand All @@ -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()
}
}
Loading