Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ rustflags = [
"-Adead_code",
"-Awarnings",
]

[target.'cfg(unix)']
runner = 'scripts/test-runner.sh'
33 changes: 23 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members = [
"crates/containerd-shim-wasm",
"crates/containerd-shim-wasm-test",
"crates/wasi-demo-app",
"crates/oci-tar-builder",
"crates/containerd-shim-wasmedge",
Expand All @@ -20,6 +21,7 @@ homepage = "https://github.com/containerd/runwasi"
[workspace.dependencies]
anyhow = "1.0"
containerd-shim-wasm = { path = "crates/containerd-shim-wasm" }
containerd-shim-wasm-test = { path = "crates/containerd-shim-wasm-test" }
serde = "1.0"
serde_json = "1.0"
env_logger = "0.10"
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ check: check-wasm $(RUNTIMES:%=check-%);

check-common: check-wasm;
check-wasm:
cargo +nightly fmt -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -- --check
cargo clippy $(FEATURES_wasm) -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -- $(WARNINGS)
cargo +nightly fmt -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test -- --check
cargo clippy $(FEATURES_wasm) -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test -- $(WARNINGS)

check-%:
cargo +nightly fmt -p containerd-shim-$* -- --check
Expand All @@ -54,8 +54,8 @@ fix: fix-wasm $(RUNTIMES:%=fix-%);

fix-common: fix-wasm;
fix-wasm:
cargo +nightly fmt -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm
cargo clippy $(FEATURES_wasm) --fix -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -- $(WARNINGS)
cargo +nightly fmt -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test
cargo clippy $(FEATURES_wasm) --fix -p oci-tar-builder -p wasi-demo-app -p containerd-shim-wasm -p containerd-shim-wasm-test -- $(WARNINGS)

fix-%:
cargo +nightly fmt -p containerd-shim-$*
Expand Down
21 changes: 21 additions & 0 deletions crates/containerd-shim-wasm-test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "containerd-shim-wasm-test"
description = "Library for testing containerd shims for wasm"
version.workspace = true
edition.workspace = true
license.workspace = true

[dependencies]
anyhow = { workspace = true }
containerd-shim-wasm = { workspace = true }
env_logger = "0.10"
libc = { workspace = true }
log = { workspace = true }
oci-spec = { workspace = true }
serde_json = { workspace = true }
tempfile = "3.8"

[build-dependencies]
anyhow = { workspace = true }
lazy_static = { version = "1.4.0" }
wat = { version = "1.0.46" }
108 changes: 108 additions & 0 deletions crates/containerd-shim-wasm-test/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;

use anyhow::{bail, Context, Result};
use lazy_static::lazy_static;

fn env_path(key: impl AsRef<str>) -> Result<PathBuf> {
std::env::var_os(key.as_ref())
.map(Into::into)
.with_context(|| format!("failed to read env-var {}", key.as_ref()))
}

lazy_static! {
static ref OUT_DIR: PathBuf = env_path("OUT_DIR").unwrap();
static ref PKG_DIR: PathBuf = env_path("CARGO_MANIFEST_DIR").unwrap();
}

fn main() -> Result<()> {
let modules_file = OUT_DIR.join("modules.rs");
let modules_dir = PKG_DIR.join("src").join("modules");

let mut writer = std::fs::File::create(modules_file)?;

let paths = std::fs::read_dir(modules_dir)?;
for entry in paths.flatten() {
let src = entry.path();
let name = path_to_ident(&src)?.to_ascii_uppercase();

println!("cargo:rerun-if-changed={}", src.to_string_lossy());

let dst = match src
.extension()
.unwrap_or_default()
.to_str()
.unwrap_or_default()
{
"rs" => compile_rust(&src)?,
"wat" => compile_wat(&src)?,
_ => bail!("unrecognized file format for source file {src:?}"),
};

writeln!(writer, "pub const {name}: TestModule = TestModule {{")?;
writeln!(writer, " source: include_str!({src:?}),")?;
writeln!(writer, " bytes: include_bytes!({dst:?}),")?;
writeln!(writer, "}};")?;
}

Ok(())
}

fn compile_rust(src: impl AsRef<Path>) -> Result<PathBuf> {
let rustc = std::env::var_os("RUSTC").context("reading RUSTC")?;
let src = src.as_ref();
let dst = output_for(src)?;

Command::new(rustc)
.arg("--target=wasm32-wasi")
.arg("-Copt-level=z")
.arg("-Cstrip=symbols")
.arg("-o")
.arg(&dst)
.arg(src)
.spawn()?
.wait()?
.success()
.then_some(dst)
.context("running rustc")
}

fn compile_wat(src: impl AsRef<Path>) -> Result<PathBuf> {
let src = src.as_ref();
let dst = output_for(src)?;

let bytes = wat::parse_file(src)?;
std::fs::write(&dst, bytes)?;

Ok(dst)
}

fn output_for(src: impl AsRef<Path>) -> Result<PathBuf> {
let src = src.as_ref();
let filename = src
.file_name()
.with_context(|| format!("getting filename of {src:?}"))?;
Ok(OUT_DIR.join(filename).with_extension("wasm"))
}

fn path_to_ident(path: impl AsRef<Path>) -> Result<String> {
let path = path.as_ref();
let ident: String = path
.file_stem()
.with_context(|| format!("getting filename of {path:?}"))?
.to_str()
.context("converting filename to string")?
.chars()
.map(|c| match c {
'A'..='Z' | 'a'..='z' | '0'..='9' => c,
_ => '_',
})
.collect();

if !ident.starts_with(char::is_alphabetic) {
bail!("please start the filename with [a-zA-Z]")
}

Ok(ident)
}
Loading