Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
add to wasi target tinygo builder
Browse files Browse the repository at this point in the history
Signed-off-by: Jordan Rash <15827604+jordan-rash@users.noreply.github.com>
  • Loading branch information
jordan-rash authored and connorsmith256 committed Oct 5, 2023
1 parent 590159c commit 3d5517c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 27 deletions.
13 changes: 9 additions & 4 deletions crates/wash-lib/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
},
parser::{
ActorConfig, CommonConfig, InterfaceConfig, LanguageConfig, ProjectConfig, ProviderConfig,
RustConfig, TinyGoConfig, TypeConfig, WasmTarget,
RustConfig, RustWasmTarget, TinyGoConfig, TinyGoWasmTarget, TypeConfig,
},
};

Expand Down Expand Up @@ -94,7 +94,7 @@ pub fn build_actor(
}?;

// If the actor has been configured as WASI Preview2, adapt it
if actor_config.wasm_target == WasmTarget::WasiPreview2 {
if actor_config.wasm_target == RustWasmTarget::WasiPreview2 {
let adapter_wasm_bytes = get_wasi_preview2_adapter_bytes(actor_config)?;
// Adapt the component, using the adapter that is available locally
let wasm_bytes = adapt_wasi_preview1_component(&actor_wasm_path, adapter_wasm_bytes)
Expand Down Expand Up @@ -230,6 +230,11 @@ fn build_tinygo_actor(
None => process::Command::new("tinygo"),
};

let build_target = match &tinygo_config.wasm_target {
Some(t) => t.to_string(),
None => TinyGoWasmTarget::CoreModule.to_string(),
};

if let Some(p) = PathBuf::from(&filename).parent() {
fs::create_dir_all(p)?;
}
Expand All @@ -240,7 +245,7 @@ fn build_tinygo_actor(
"-o",
filename.as_str(),
"-target",
"wasm",
build_target.as_str(),
"-scheduler",
"none",
"-no-debug",
Expand Down Expand Up @@ -310,7 +315,7 @@ fn adapt_wasi_preview1_component(
/// if required by project configuration
pub(crate) fn get_wasi_preview2_adapter_bytes(config: &ActorConfig) -> Result<Vec<u8>> {
if let ActorConfig {
wasm_target: WasmTarget::WasiPreview2,
wasm_target: RustWasmTarget::WasiPreview2,
wasi_preview2_adapter_path: Some(path),
..
} = config
Expand Down
70 changes: 54 additions & 16 deletions crates/wash-lib/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub struct ActorConfig {
/// The filename of the signed wasm actor.
pub filename: Option<String>,
/// The target wasm target to build for. Defaults to "wasm32-unknown-unknown".
pub wasm_target: WasmTarget,
pub wasm_target: RustWasmTarget,
/// Path to a wasm adapter that can be used for preview2
pub wasi_preview2_adapter_path: Option<PathBuf>,
/// The call alias of the actor.
Expand Down Expand Up @@ -90,7 +90,7 @@ impl TryFrom<RawActorConfig> for ActorConfig {
filename: raw_config.filename,
wasm_target: raw_config
.wasm_target
.map(WasmTarget::from)
.map(RustWasmTarget::from)
.unwrap_or_default(),
wasi_preview2_adapter_path: raw_config.wasi_preview2_adapter_path,
call_alias: raw_config.call_alias,
Expand Down Expand Up @@ -163,12 +163,12 @@ pub struct RustConfig {
}

impl RustConfig {
pub fn build_target(&self, wasm_target: &WasmTarget) -> &'static str {
pub fn build_target(&self, wasm_target: &RustWasmTarget) -> &'static str {
match wasm_target {
WasmTarget::CoreModule => "wasm32-unknown-unknown",
RustWasmTarget::CoreModule => "wasm32-unknown-unknown",
// NOTE: eventually "wasm32-wasi" will be renamed to "wasm32-wasi-preview1"
// https://github.com/rust-lang/compiler-team/issues/607
WasmTarget::WasiPreview1 | WasmTarget::WasiPreview2 => "wasm32-wasi",
RustWasmTarget::WasiPreview1 | RustWasmTarget::WasiPreview2 => "wasm32-wasi",
}
}
}
Expand Down Expand Up @@ -207,7 +207,7 @@ pub struct CommonConfig {
}

#[derive(Debug, Deserialize, Default, Clone, Eq, PartialEq)]
pub enum WasmTarget {
pub enum RustWasmTarget {
#[default]
#[serde(alias = "wasm32-unknown-unknown")]
CoreModule,
Expand All @@ -217,29 +217,62 @@ pub enum WasmTarget {
WasiPreview2,
}

impl From<&str> for WasmTarget {
impl From<&str> for RustWasmTarget {
fn from(value: &str) -> Self {
match value {
"wasm32-wasi-preview1" => WasmTarget::WasiPreview1,
"wasm32-wasi" => WasmTarget::WasiPreview1,
"wasm32-wasi-preview2" => WasmTarget::WasiPreview2,
_ => WasmTarget::CoreModule,
"wasm32-wasi-preview1" => RustWasmTarget::WasiPreview1,
"wasm32-wasi" => RustWasmTarget::WasiPreview1,
"wasm32-wasi-preview2" => RustWasmTarget::WasiPreview2,
_ => RustWasmTarget::CoreModule,
}
}
}

impl From<String> for WasmTarget {
impl From<String> for RustWasmTarget {
fn from(value: String) -> Self {
value.as_str().into()
}
}

impl Display for WasmTarget {
impl Display for RustWasmTarget {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match &self {
WasmTarget::CoreModule => "wasm32-unknown-unknown",
WasmTarget::WasiPreview1 => "wasm32-wasi",
WasmTarget::WasiPreview2 => "wasm32-wasi-preview2",
RustWasmTarget::CoreModule => "wasm32-unknown-unknown",
RustWasmTarget::WasiPreview1 => "wasm32-wasi",
RustWasmTarget::WasiPreview2 => "wasm32-wasi-preview2",
})
}
}

#[derive(Debug, Deserialize, Default, Clone, Eq, PartialEq)]
pub enum TinyGoWasmTarget {
#[default]
#[serde(alias = "wasm")]
CoreModule,
#[serde(alias = "wasi", alias = "wasi")]
WasiPreview1,
}

impl From<&str> for TinyGoWasmTarget {
fn from(value: &str) -> Self {
match value {
"wasi" => TinyGoWasmTarget::WasiPreview1,
_ => TinyGoWasmTarget::CoreModule,
}
}
}

impl From<String> for TinyGoWasmTarget {
fn from(value: String) -> Self {
value.as_str().into()
}
}

impl Display for TinyGoWasmTarget {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match &self {
TinyGoWasmTarget::CoreModule => "wasm",
TinyGoWasmTarget::WasiPreview1 => "wasi",
})
}
}
Expand Down Expand Up @@ -267,12 +300,16 @@ struct RawProjectConfig {
pub struct TinyGoConfig {
/// The path to the tinygo binary. Optional, will default to `tinygo` if not specified.
pub tinygo_path: Option<PathBuf>,
/// The target wasm target to build for. Defaults to "wasm".
pub wasm_target: Option<TinyGoWasmTarget>,
}

#[derive(Deserialize, Debug, PartialEq, Default)]
struct RawTinyGoConfig {
/// The path to the tinygo binary. Optional, will default to `tinygo` if not specified.
pub tinygo_path: Option<PathBuf>,
/// The target wasm target to build for. Defaults to "wasm".
pub wasm_target: Option<TinyGoWasmTarget>,
}

impl TryFrom<RawTinyGoConfig> for TinyGoConfig {
Expand All @@ -281,6 +318,7 @@ impl TryFrom<RawTinyGoConfig> for TinyGoConfig {
fn try_from(raw_config: RawTinyGoConfig) -> Result<Self> {
Ok(Self {
tinygo_path: raw_config.tinygo_path,
wasm_target: raw_config.wasm_target,
})
}
}
Expand Down
15 changes: 8 additions & 7 deletions crates/wash-lib/tests/parser/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::{fs, path::PathBuf};
use claims::{assert_err, assert_ok};
use semver::Version;
use wash_lib::parser::{
get_config, ActorConfig, CommonConfig, LanguageConfig, RustConfig, TinyGoConfig, TypeConfig,
WasmTarget,
get_config, ActorConfig, CommonConfig, LanguageConfig, RustConfig, RustWasmTarget,
TinyGoConfig, TinyGoWasmTarget, TypeConfig,
};

#[test]
Expand Down Expand Up @@ -33,7 +33,7 @@ fn rust_actor() {
push_insecure: false,
key_directory: PathBuf::from("./keys"),
filename: Some("testactor.wasm".to_string()),
wasm_target: WasmTarget::CoreModule,
wasm_target: RustWasmTarget::CoreModule,
wasi_preview2_adapter_path: None,
call_alias: Some("testactor".to_string())
})
Expand Down Expand Up @@ -64,7 +64,8 @@ fn tinygo_actor() {
assert_eq!(
config.language,
LanguageConfig::TinyGo(TinyGoConfig {
tinygo_path: Some("path/to/tinygo".into())
tinygo_path: Some("path/to/tinygo".into()),
wasm_target: Some(TinyGoWasmTarget::CoreModule.into()),
})
);

Expand Down Expand Up @@ -339,7 +340,7 @@ fn minimal_rust_actor_preview2() {
assert!(matches!(
config.project_type,
TypeConfig::Actor(ActorConfig {
wasm_target: WasmTarget::WasiPreview2,
wasm_target: RustWasmTarget::WasiPreview2,
..
})
));
Expand All @@ -360,7 +361,7 @@ fn minimal_rust_actor_preview1() {
assert!(matches!(
config.project_type,
TypeConfig::Actor(ActorConfig {
wasm_target: WasmTarget::WasiPreview1,
wasm_target: RustWasmTarget::WasiPreview1,
..
})
));
Expand All @@ -381,7 +382,7 @@ fn minimal_rust_actor_core_module() {
assert!(matches!(
config.project_type,
TypeConfig::Actor(ActorConfig {
wasm_target: WasmTarget::CoreModule,
wasm_target: RustWasmTarget::CoreModule,
..
})
));
Expand Down

0 comments on commit 3d5517c

Please sign in to comment.