From 898269bf721b2d593143c6afb10e4ae663d31431 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Ace=C3=B1a?= <40375118+j0nl1@users.noreply.github.com> Date: Tue, 23 Apr 2024 09:27:10 +0200 Subject: [PATCH] feat: feature support in workspace (#3) * feat: feature support in workspace under `metadata = { build_variants = [VARIANT] }` * fix: building script --- bob_the_builder/Cargo.lock | 20 +++++++------- bob_the_builder/src/cargo_toml.rs | 12 +++++++++ bob_the_builder/src/lib.rs | 45 ++++++++++++++++++++++++++++++- 3 files changed, 66 insertions(+), 11 deletions(-) diff --git a/bob_the_builder/Cargo.lock b/bob_the_builder/Cargo.lock index 3c34078..607bce1 100644 --- a/bob_the_builder/Cargo.lock +++ b/bob_the_builder/Cargo.lock @@ -19,36 +19,36 @@ checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" [[package]] name = "proc-macro2" -version = "1.0.69" +version = "1.0.80" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" +checksum = "a56dea16b0a29e94408b9aa5e2940a4eedbd128a1ba20e8f7ae60fd3d465af0e" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.33" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] [[package]] name = "serde" -version = "1.0.189" +version = "1.0.197" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e422a44e74ad4001bdc8eede9a4570ab52f71190e9c076d14369f38b9200537" +checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.189" +version = "1.0.197" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e48d1f918009ce3145511378cf68d613e3b3d9137d67272562080d68a2b32d5" +checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" dependencies = [ "proc-macro2", "quote", @@ -57,9 +57,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.38" +version = "2.0.59" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b" +checksum = "4a6531ffc7b071655e4ce2e04bd464c4830bb585a61cabb96cf808f05172615a" dependencies = [ "proc-macro2", "quote", diff --git a/bob_the_builder/src/cargo_toml.rs b/bob_the_builder/src/cargo_toml.rs index 2b6fcb3..8a3ff62 100644 --- a/bob_the_builder/src/cargo_toml.rs +++ b/bob_the_builder/src/cargo_toml.rs @@ -2,6 +2,7 @@ use serde::Deserialize; #[derive(Deserialize, Debug)] pub struct CargoToml { + pub package: Option, pub workspace: Option, } @@ -10,6 +11,17 @@ pub struct Workspace { pub members: Option>, } +#[derive(Deserialize, Debug)] +pub struct Package { + pub name: String, + pub metadata: Option, +} + +#[derive(Default, Deserialize, Debug)] +pub struct Metadata { + pub build_variants: Option>, +} + #[derive(Debug, PartialEq)] pub enum IsWorkspace { Yes { diff --git a/bob_the_builder/src/lib.rs b/bob_the_builder/src/lib.rs index 58c52bb..3b4ac15 100644 --- a/bob_the_builder/src/lib.rs +++ b/bob_the_builder/src/lib.rs @@ -7,7 +7,7 @@ use std::{ process::Command, }; -use cargo_toml::{is_workspace, IsWorkspace}; +use cargo_toml::{is_workspace, CargoToml, IsWorkspace}; const CARGO_PATH: &str = "cargo"; const PACKAGE_PREFIX: &str = "contracts/"; @@ -61,8 +61,51 @@ pub fn build_workspace(workspace_members: &[String]) { println!("Contracts to be built: {:?}", contract_packages); + let build_dir = PathBuf::from("/target/wasm32-unknown-unknown/release"); + for contract in contract_packages { println!("Building {:?} ...", contract); + let cargo_file = fs::read_to_string(contract.join("Cargo.toml")).unwrap(); + + let CargoToml { package, .. } = toml::from_str(&cargo_file).unwrap(); + + let package = package.expect("package is required in Cargo.toml"); + let name = package.name.replace("-", "_"); + let variants = package + .metadata + .unwrap_or_default() + .build_variants + .unwrap_or_default(); + + if variants.len() > 0 { + for variant in variants { + let mut child = Command::new(CARGO_PATH) + .args(&[ + "build", + "--target-dir=/target", + "--release", + "--features", + variant.as_str(), + "--lib", + "--target=wasm32-unknown-unknown", + "--locked", + ]) + .env("RUSTFLAGS", "-C link-arg=-s") + .current_dir(canonicalize(contract).unwrap()) + .spawn() + .unwrap(); + let error_code = child.wait().unwrap(); + assert!(error_code.success()); + + println!("Built variant: {}", variant); + + fs::rename( + build_dir.join(format!("{name}.wasm")), + build_dir.join(format!("{name}_{variant}.wasm")), + ) + .unwrap(); + } + } let mut child = Command::new(CARGO_PATH) .args(&[