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

Commit bb454cb

Browse files
vados-cosmonicbrooksmtownsend
authored andcommitted
feat: add p2 target to wasmcloud.toml
Signed-off-by: Victor Adossi <[email protected]>
1 parent 7265d9e commit bb454cb

File tree

11 files changed

+326
-69
lines changed

11 files changed

+326
-69
lines changed

.github/workflows/rust_ci.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,23 @@ jobs:
3535
shared-key: "${{ matrix.os }}-shared-cache"
3636
- name: Install nextest
3737
uses: taiki-e/install-action@nextest
38+
39+
# Sometimes, unit tests will get stuck executing wash during the
40+
# loading CA certificates from the :otp store step. All you see is:
41+
#
42+
# > stderr log: 14:38:03.656 [info] Loading 467 CA(s) from :otp store
43+
#
44+
# when this happens, tests like can_download_and_start_wasmcloud get stuck and fail.
45+
# to avoid this, we can call wash up/down and at least cause this loading as early as possible,
46+
# hoping it does not happen again (or completes near instantaneously) during a test.
47+
- name: Build wash
48+
run: make build
49+
- name: Cycle wash
50+
run: |
51+
cargo run -- up --detached;
52+
sleep 10;
53+
cargo run -- down;
54+
3855
- name: Run all wash & wash-lib unit tests
3956
run: make test-wash-ci
4057

crates/wash-lib/src/build.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ use std::{fs, io::ErrorKind, path::PathBuf, process, str::FromStr};
44

55
use anyhow::{anyhow, bail, Result};
66

7-
use crate::cli::{
8-
claims::{sign_file, ActorMetadata, SignCommand},
9-
OutputKind,
10-
};
11-
use crate::parser::{
12-
ActorConfig, CommonConfig, InterfaceConfig, LanguageConfig, ProjectConfig, ProviderConfig,
13-
RustConfig, TinyGoConfig, TypeConfig,
7+
use crate::{
8+
cli::{
9+
claims::{sign_file, ActorMetadata, SignCommand},
10+
OutputKind,
11+
},
12+
parser::{
13+
ActorConfig, CommonConfig, InterfaceConfig, LanguageConfig, ProjectConfig, ProviderConfig,
14+
RustConfig, TinyGoConfig, TypeConfig, WasmTarget,
15+
},
1416
};
1517

1618
/// Configuration for signing an artifact (actor or provider) including issuer and subject key, the path to where keys can be found, and an option to
@@ -126,19 +128,31 @@ fn build_rust_actor(
126128
None => process::Command::new("cargo"),
127129
};
128130

131+
if actor_config.wasm_target == WasmTarget::WasiPreview2 {
132+
bail!("Building projects targeting WASI preview 2 is not yet supported");
133+
}
134+
129135
// Change directory into the project directory
130136
std::env::set_current_dir(&common_config.path)?;
131137

132138
let metadata = cargo_metadata::MetadataCommand::new().exec()?;
133139
let target_path = metadata.target_directory.as_path();
134140

135-
let result = command.args(["build", "--release"]).status().map_err(|e| {
136-
if e.kind() == ErrorKind::NotFound {
137-
anyhow!("{:?} command is not found", command.get_program())
138-
} else {
139-
anyhow!(e)
140-
}
141-
})?;
141+
let result = command
142+
.args([
143+
"build",
144+
"--release",
145+
"--target",
146+
rust_config.build_target(&actor_config.wasm_target),
147+
])
148+
.status()
149+
.map_err(|e| {
150+
if e.kind() == ErrorKind::NotFound {
151+
anyhow!("{:?} command is not found", command.get_program())
152+
} else {
153+
anyhow!(e)
154+
}
155+
})?;
142156

143157
if !result.success() {
144158
bail!("Compiling actor failed: {}", result.to_string())

crates/wash-lib/src/parser/mod.rs

Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,26 @@ use anyhow::{anyhow, bail, Result};
55
use cargo_toml::{Manifest, Product};
66
use config::Config;
77
use semver::Version;
8-
use std::{fs, path::PathBuf};
8+
use serde::Deserialize;
9+
use std::{fmt::Display, fs, path::PathBuf};
910

10-
#[derive(serde::Deserialize, Debug, PartialEq, Eq, Clone)]
11+
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
1112
#[serde(rename_all = "snake_case")]
1213
pub enum LanguageConfig {
1314
Rust(RustConfig),
1415
TinyGo(TinyGoConfig),
1516
}
1617

17-
#[derive(serde::Deserialize, Debug, PartialEq, Eq, Clone)]
18+
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
1819
#[serde(rename_all = "snake_case")]
1920
pub enum TypeConfig {
2021
Actor(ActorConfig),
2122
Provider(ProviderConfig),
2223
Interface(InterfaceConfig),
2324
}
2425

25-
#[derive(serde::Deserialize, Debug, Clone)]
26+
/// Project configuration, normally specified in the root keys of a wasmcloud.toml file
27+
#[derive(Deserialize, Debug, Clone)]
2628
pub struct ProjectConfig {
2729
/// The language of the project, e.g. rust, tinygo. Contains specific configuration for that language.
2830
pub language: LanguageConfig,
@@ -34,7 +36,7 @@ pub struct ProjectConfig {
3436
pub common: CommonConfig,
3537
}
3638

37-
#[derive(serde::Deserialize, Debug, PartialEq, Eq, Clone, Default)]
39+
#[derive(Deserialize, Debug, PartialEq, Eq, Clone, Default)]
3840
pub struct ActorConfig {
3941
/// The list of provider claims that this actor requires. eg. ["wasmcloud:httpserver", "wasmcloud:blobstore"]
4042
pub claims: Vec<String>,
@@ -47,11 +49,12 @@ pub struct ActorConfig {
4749
/// The filename of the signed wasm actor.
4850
pub filename: Option<String>,
4951
/// The target wasm target to build for. Defaults to "wasm32-unknown-unknown".
50-
pub wasm_target: String,
52+
pub wasm_target: WasmTarget,
5153
/// The call alias of the actor.
5254
pub call_alias: Option<String>,
5355
}
54-
#[derive(serde::Deserialize, Debug, PartialEq)]
56+
57+
#[derive(Deserialize, Debug, PartialEq)]
5558
struct RawActorConfig {
5659
/// The list of provider claims that this actor requires. eg. ["wasmcloud:httpserver", "wasmcloud:blobstore"]
5760
pub claims: Option<Vec<String>>,
@@ -83,19 +86,20 @@ impl TryFrom<RawActorConfig> for ActorConfig {
8386
filename: raw_config.filename,
8487
wasm_target: raw_config
8588
.wasm_target
86-
.unwrap_or_else(|| "wasm32-unknown-unknown".to_string()),
89+
.map(WasmTarget::from)
90+
.unwrap_or_default(),
8791
call_alias: raw_config.call_alias,
8892
})
8993
}
9094
}
91-
#[derive(serde::Deserialize, Debug, PartialEq, Eq, Clone, Default)]
95+
#[derive(Deserialize, Debug, PartialEq, Eq, Clone, Default)]
9296
pub struct ProviderConfig {
9397
/// The capability ID of the provider.
9498
pub capability_id: String,
9599
/// The vendor name of the provider.
96100
pub vendor: String,
97101
}
98-
#[derive(serde::Deserialize, Debug, PartialEq)]
102+
#[derive(Deserialize, Debug, PartialEq)]
99103
struct RawProviderConfig {
100104
/// The capability ID of the provider.
101105
pub capability_id: String,
@@ -114,14 +118,14 @@ impl TryFrom<RawProviderConfig> for ProviderConfig {
114118
}
115119
}
116120

117-
#[derive(serde::Deserialize, Debug, PartialEq, Eq, Clone, Default)]
121+
#[derive(Deserialize, Debug, PartialEq, Eq, Clone, Default)]
118122
pub struct InterfaceConfig {
119123
/// Directory to output HTML.
120124
pub html_target: PathBuf,
121125
/// Path to codegen.toml file.
122126
pub codegen_config: PathBuf,
123127
}
124-
#[derive(serde::Deserialize, Debug, PartialEq)]
128+
#[derive(Deserialize, Debug, PartialEq)]
125129

126130
struct RawInterfaceConfig {
127131
/// Directory to output HTML. Defaults to "./html".
@@ -145,15 +149,27 @@ impl TryFrom<RawInterfaceConfig> for InterfaceConfig {
145149
}
146150
}
147151

148-
#[derive(serde::Deserialize, Debug, PartialEq, Eq, Clone, Default)]
152+
#[derive(Deserialize, Debug, PartialEq, Eq, Clone, Default)]
149153
pub struct RustConfig {
150154
/// The path to the cargo binary. Optional, will default to search the user's `PATH` for `cargo` if not specified.
151155
pub cargo_path: Option<PathBuf>,
152156
/// Path to cargo/rust's `target` directory. Optional, defaults to the cargo target directory for the workspace or project.
153157
pub target_path: Option<PathBuf>,
154158
}
155159

156-
#[derive(serde::Deserialize, Debug, PartialEq, Default, Clone)]
160+
impl RustConfig {
161+
pub fn build_target(&self, wasm_target: &WasmTarget) -> &'static str {
162+
match wasm_target {
163+
WasmTarget::CoreModule => "wasm32-unknown-unknown",
164+
// NOTE: eventually "wasm32-wasi" will be renamed to "wasm32-wasi-preview1"
165+
// https://github.com/rust-lang/compiler-team/issues/607
166+
WasmTarget::WasiPreview1 => "wasm32-wasi",
167+
WasmTarget::WasiPreview2 => "wasm32-wasi-preview2",
168+
}
169+
}
170+
}
171+
172+
#[derive(Deserialize, Debug, PartialEq, Default, Clone)]
157173
struct RawRustConfig {
158174
/// The path to the cargo binary. Optional, will default to search the user's `PATH` for `cargo` if not specified.
159175
pub cargo_path: Option<PathBuf>,
@@ -173,7 +189,7 @@ impl TryFrom<RawRustConfig> for RustConfig {
173189
}
174190

175191
/// Configuration common amoung all project types & languages.
176-
#[derive(serde::Deserialize, Debug, PartialEq, Eq, Clone)]
192+
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
177193
pub struct CommonConfig {
178194
/// Name of the project.
179195
pub name: String,
@@ -186,7 +202,45 @@ pub struct CommonConfig {
186202
pub wasm_bin_name: Option<String>,
187203
}
188204

189-
#[derive(serde::Deserialize, Debug)]
205+
#[derive(Debug, Deserialize, Default, Clone, Eq, PartialEq)]
206+
pub enum WasmTarget {
207+
#[default]
208+
#[serde(alias = "wasm32-unknown-unknown")]
209+
CoreModule,
210+
#[serde(alias = "wasm32-wasi", alias = "wasm32-wasi-preview1")]
211+
WasiPreview1,
212+
#[serde(alias = "wasm32-wasi-preview2")]
213+
WasiPreview2,
214+
}
215+
216+
impl From<&str> for WasmTarget {
217+
fn from(value: &str) -> Self {
218+
match value {
219+
"wasm32-wasi-preview1" => WasmTarget::WasiPreview1,
220+
"wasm32-wasi" => WasmTarget::WasiPreview1,
221+
"wasm32-wasi-preview2" => WasmTarget::WasiPreview2,
222+
_ => WasmTarget::CoreModule,
223+
}
224+
}
225+
}
226+
227+
impl From<String> for WasmTarget {
228+
fn from(value: String) -> Self {
229+
value.as_str().into()
230+
}
231+
}
232+
233+
impl Display for WasmTarget {
234+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
235+
f.write_str(match &self {
236+
WasmTarget::CoreModule => "wasm32-unknown-unknown",
237+
WasmTarget::WasiPreview1 => "wasm32-wasi",
238+
WasmTarget::WasiPreview2 => "wasm32-wasi-preview2",
239+
})
240+
}
241+
}
242+
243+
#[derive(Deserialize, Debug)]
190244
struct RawProjectConfig {
191245
/// The language of the project, e.g. rust, tinygo. This is used to determine which config to parse.
192246
pub language: String,
@@ -205,13 +259,13 @@ struct RawProjectConfig {
205259
pub tinygo: Option<RawTinyGoConfig>,
206260
}
207261

208-
#[derive(serde::Deserialize, Debug, PartialEq, Eq, Clone, Default)]
262+
#[derive(Deserialize, Debug, PartialEq, Eq, Clone, Default)]
209263
pub struct TinyGoConfig {
210264
/// The path to the tinygo binary. Optional, will default to `tinygo` if not specified.
211265
pub tinygo_path: Option<PathBuf>,
212266
}
213267

214-
#[derive(serde::Deserialize, Debug, PartialEq, Default)]
268+
#[derive(Deserialize, Debug, PartialEq, Default)]
215269
struct RawTinyGoConfig {
216270
/// The path to the tinygo binary. Optional, will default to `tinygo` if not specified.
217271
pub tinygo_path: Option<PathBuf>,

0 commit comments

Comments
 (0)