diff --git a/Cargo.lock b/Cargo.lock index 3a8362362e..ed0eeb7211 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1905,6 +1905,15 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "shellexpand" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2b22262a9aaf9464d356f656fea420634f78c881c5eebd5ef5e66d8b9bc603" +dependencies = [ + "dirs", +] + [[package]] name = "smallvec" version = "1.4.1" @@ -2700,6 +2709,7 @@ dependencies = [ "heck", "proc-macro2", "quote", + "shellexpand", "syn", "witx", ] diff --git a/crates/wasi-common/src/wasi.rs b/crates/wasi-common/src/wasi.rs index f35055ffa8..f8b4aab780 100644 --- a/crates/wasi-common/src/wasi.rs +++ b/crates/wasi-common/src/wasi.rs @@ -4,7 +4,7 @@ use crate::WasiCtx; wiggle::from_witx!({ - witx: ["WASI/phases/snapshot/witx/wasi_snapshot_preview1.witx"], + witx: ["$WASI_ROOT/phases/snapshot/witx/wasi_snapshot_preview1.witx"], ctx: WasiCtx, }); diff --git a/crates/wasi/src/lib.rs b/crates/wasi/src/lib.rs index bd12def83a..1e812ba033 100644 --- a/crates/wasi/src/lib.rs +++ b/crates/wasi/src/lib.rs @@ -9,8 +9,9 @@ pub use wasi_common::{WasiCtx, WasiCtxBuilder}; wasmtime_wiggle::wasmtime_integration!({ // The wiggle code to integrate with lives here: target: wasi_common::wasi, - // This must be the same witx document as used above: - witx: ["phases/snapshot/witx/wasi_snapshot_preview1.witx"], + // This must be the same witx document as used above. This should be ensured by + // the `WASI_ROOT` env variable, which is set in wasi-common's `build.rs`. + witx: ["$WASI_ROOT/phases/snapshot/witx/wasi_snapshot_preview1.witx"], // This must be the same ctx type as used for the target: ctx: WasiCtx, // This macro will emit a struct to represent the instance, diff --git a/crates/wiggle/generate/Cargo.toml b/crates/wiggle/generate/Cargo.toml index 632b649153..732fa34991 100644 --- a/crates/wiggle/generate/Cargo.toml +++ b/crates/wiggle/generate/Cargo.toml @@ -20,6 +20,7 @@ proc-macro2 = "1.0" heck = "0.3" anyhow = "1" syn = { version = "1.0", features = ["full"] } +shellexpand = "2.0" [badges] maintenance = { status = "actively-developed" } diff --git a/crates/wiggle/generate/src/config.rs b/crates/wiggle/generate/src/config.rs index 22ff88aa24..520532a3ec 100644 --- a/crates/wiggle/generate/src/config.rs +++ b/crates/wiggle/generate/src/config.rs @@ -1,10 +1,6 @@ use { proc_macro2::Span, - std::{ - collections::HashMap, - iter::FromIterator, - path::{Path, PathBuf}, - }, + std::{collections::HashMap, iter::FromIterator, path::PathBuf}, syn::{ braced, bracketed, parse::{Parse, ParseStream}, @@ -143,19 +139,6 @@ impl WitxConf { Self::Literal(doc) => witx::parse(doc.as_ref()).expect("parsing witx"), } } - - /// If using the [`Paths`][paths] syntax, make all paths relative to a root directory. - /// - /// [paths]: enum.WitxConf.html#variant.Paths - pub fn make_paths_relative_to>(&mut self, root: P) { - if let Self::Paths(paths) = self { - paths.as_mut().iter_mut().for_each(|p| { - if !p.is_absolute() { - *p = PathBuf::from(root.as_ref()).join(p.clone()); - } - }); - } - } } /// A collection of paths, pointing to witx documents. @@ -201,10 +184,19 @@ impl Parse for Paths { let content; let _ = bracketed!(content in input); let path_lits: Punctuated = content.parse_terminated(Parse::parse)?; - Ok(path_lits + + let expanded_paths = path_lits .iter() - .map(|lit| PathBuf::from(lit.value())) - .collect()) + .map(|lit| { + PathBuf::from( + shellexpand::env(&lit.value()) + .expect("shell expansion") + .as_ref(), + ) + }) + .collect::>(); + + Ok(Paths(expanded_paths)) } } diff --git a/crates/wiggle/macro/src/lib.rs b/crates/wiggle/macro/src/lib.rs index ed4a3dc6f0..48f0367312 100644 --- a/crates/wiggle/macro/src/lib.rs +++ b/crates/wiggle/macro/src/lib.rs @@ -90,10 +90,7 @@ use syn::parse_macro_input; /// ``` #[proc_macro] pub fn from_witx(args: TokenStream) -> TokenStream { - let mut config = parse_macro_input!(args as wiggle_generate::Config); - config.witx.make_paths_relative_to( - std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR env var"), - ); + let config = parse_macro_input!(args as wiggle_generate::Config); let doc = config.load_document(); let names = wiggle_generate::Names::new(&config.ctx.name, quote!(wiggle)); diff --git a/crates/wiggle/tests/arrays.rs b/crates/wiggle/tests/arrays.rs index 9142e9f01e..9bfad46e7b 100644 --- a/crates/wiggle/tests/arrays.rs +++ b/crates/wiggle/tests/arrays.rs @@ -3,7 +3,7 @@ use wiggle::{GuestMemory, GuestPtr}; use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx}; wiggle::from_witx!({ - witx: ["tests/arrays.witx"], + witx: ["$CARGO_MANIFEST_DIR/tests/arrays.witx"], ctx: WasiCtx, }); diff --git a/crates/wiggle/tests/atoms.rs b/crates/wiggle/tests/atoms.rs index 5407828fa9..b83468678b 100644 --- a/crates/wiggle/tests/atoms.rs +++ b/crates/wiggle/tests/atoms.rs @@ -3,7 +3,7 @@ use wiggle::GuestMemory; use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx}; wiggle::from_witx!({ - witx: ["tests/atoms.witx"], + witx: ["$CARGO_MANIFEST_DIR/tests/atoms.witx"], ctx: WasiCtx, }); diff --git a/crates/wiggle/tests/flags.rs b/crates/wiggle/tests/flags.rs index fcb07e7a7d..bca0a27bae 100644 --- a/crates/wiggle/tests/flags.rs +++ b/crates/wiggle/tests/flags.rs @@ -4,7 +4,7 @@ use wiggle::{GuestMemory, GuestPtr}; use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx}; wiggle::from_witx!({ - witx: ["tests/flags.witx"], + witx: ["$CARGO_MANIFEST_DIR/tests/flags.witx"], ctx: WasiCtx, }); diff --git a/crates/wiggle/tests/handles.rs b/crates/wiggle/tests/handles.rs index 93e3b54d18..cb441a910c 100644 --- a/crates/wiggle/tests/handles.rs +++ b/crates/wiggle/tests/handles.rs @@ -5,7 +5,7 @@ use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx}; const FD_VAL: u32 = 123; wiggle::from_witx!({ - witx: ["tests/handles.witx"], + witx: ["$CARGO_MANIFEST_DIR/tests/handles.witx"], ctx: WasiCtx, }); diff --git a/crates/wiggle/tests/ints.rs b/crates/wiggle/tests/ints.rs index 36d7933e62..a3d15c79d8 100644 --- a/crates/wiggle/tests/ints.rs +++ b/crates/wiggle/tests/ints.rs @@ -4,7 +4,7 @@ use wiggle::GuestMemory; use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx}; wiggle::from_witx!({ - witx: ["tests/ints.witx"], + witx: ["$CARGO_MANIFEST_DIR/tests/ints.witx"], ctx: WasiCtx, }); diff --git a/crates/wiggle/tests/keywords.rs b/crates/wiggle/tests/keywords.rs index ea002fcc6d..bc1ee4edce 100644 --- a/crates/wiggle/tests/keywords.rs +++ b/crates/wiggle/tests/keywords.rs @@ -58,7 +58,7 @@ mod struct_test { /// Test that a union variant that conflicts with a Rust keyword can be compiled properly. mod union_test { wiggle::from_witx!({ - witx: ["tests/keywords_union.witx"], + witx: ["$CARGO_MANIFEST_DIR/tests/keywords_union.witx"], ctx: DummyCtx, }); } diff --git a/crates/wiggle/tests/pointers.rs b/crates/wiggle/tests/pointers.rs index c39988d115..28ef3d107d 100644 --- a/crates/wiggle/tests/pointers.rs +++ b/crates/wiggle/tests/pointers.rs @@ -3,7 +3,7 @@ use wiggle::{GuestMemory, GuestPtr}; use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx}; wiggle::from_witx!({ - witx: ["tests/pointers.witx"], + witx: ["$CARGO_MANIFEST_DIR/tests/pointers.witx"], ctx: WasiCtx, }); diff --git a/crates/wiggle/tests/strings.rs b/crates/wiggle/tests/strings.rs index 3546fd1276..e68a569ed6 100644 --- a/crates/wiggle/tests/strings.rs +++ b/crates/wiggle/tests/strings.rs @@ -3,7 +3,7 @@ use wiggle::{GuestMemory, GuestPtr}; use wiggle_test::{impl_errno, HostMemory, MemArea, MemAreas, WasiCtx}; wiggle::from_witx!({ - witx: ["tests/strings.witx"], + witx: ["$CARGO_MANIFEST_DIR/tests/strings.witx"], ctx: WasiCtx, }); diff --git a/crates/wiggle/tests/structs.rs b/crates/wiggle/tests/structs.rs index f342d4487a..ad2e43a2db 100644 --- a/crates/wiggle/tests/structs.rs +++ b/crates/wiggle/tests/structs.rs @@ -3,7 +3,7 @@ use wiggle::{GuestMemory, GuestPtr}; use wiggle_test::{impl_errno, HostMemory, MemArea, MemAreas, WasiCtx}; wiggle::from_witx!({ - witx: ["tests/structs.witx"], + witx: ["$CARGO_MANIFEST_DIR/tests/structs.witx"], ctx: WasiCtx, }); diff --git a/crates/wiggle/tests/union.rs b/crates/wiggle/tests/union.rs index ab3dada99c..9763e488dd 100644 --- a/crates/wiggle/tests/union.rs +++ b/crates/wiggle/tests/union.rs @@ -3,7 +3,7 @@ use wiggle::{GuestMemory, GuestType}; use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx}; wiggle::from_witx!({ - witx: ["tests/union.witx"], + witx: ["$CARGO_MANIFEST_DIR/tests/union.witx"], ctx: WasiCtx, }); diff --git a/crates/wiggle/tests/wasi.rs b/crates/wiggle/tests/wasi.rs index 8847959aa2..07b56db62a 100644 --- a/crates/wiggle/tests/wasi.rs +++ b/crates/wiggle/tests/wasi.rs @@ -7,7 +7,7 @@ use wiggle_test::WasiCtx; // witx is exposed with the type signatures that we expect. wiggle::from_witx!({ - witx: ["tests/wasi.witx"], + witx: ["$CARGO_MANIFEST_DIR/tests/wasi.witx"], ctx: WasiCtx, }); @@ -16,7 +16,10 @@ wiggle::from_witx!({ #[test] fn document_equivalent() { let macro_doc = metadata::document(); - let disk_doc = witx::load(&["tests/wasi.witx"]).expect("load wasi.witx from disk"); + let mut path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")); + path.push("tests"); + path.push("wasi.witx"); + let disk_doc = witx::load(&[path]).expect("load wasi.witx from disk"); assert_eq!(macro_doc, disk_doc); } diff --git a/crates/wiggle/wasmtime/macro/src/lib.rs b/crates/wiggle/wasmtime/macro/src/lib.rs index ff1df0898c..4ba1c1b9fb 100644 --- a/crates/wiggle/wasmtime/macro/src/lib.rs +++ b/crates/wiggle/wasmtime/macro/src/lib.rs @@ -47,10 +47,7 @@ use config::{MissingMemoryConf, ModuleConf, TargetConf}; /// #[proc_macro] pub fn wasmtime_integration(args: TokenStream) -> TokenStream { - let mut config = parse_macro_input!(args as config::Config); - config - .witx - .make_paths_relative_to(std::env::var("WASI_ROOT").expect("WASI_ROOT env var")); + let config = parse_macro_input!(args as config::Config); let doc = config.load_document(); let names = Names::new(&config.ctx.name, quote!(wasmtime_wiggle));