make wiggle-generate ordinary lib, and wiggle the proc-macro lib

this allows us to reuse the code in wiggle-generate elsewhere, because
a proc-macro=true lib can only export a #[proc_macro] and not ordinary
functions.

In lucet, I will depend on wiggle-generate to define a proc macro that
glues wiggle to the specifics of the runtime.
This commit is contained in:
Pat Hickey
2020-02-28 11:43:43 -08:00
parent 0fe3f11194
commit bb6995ceaf
13 changed files with 32 additions and 30 deletions

View File

@@ -4,11 +4,15 @@ version = "0.1.0"
authors = ["Pat Hickey <phickey@fastly.com>", "Jakub Konka <kubkonk@jakubkonka.com>"] authors = ["Pat Hickey <phickey@fastly.com>", "Jakub Konka <kubkonk@jakubkonka.com>"]
edition = "2018" edition = "2018"
[lib]
proc-macro = true
[dependencies] [dependencies]
wiggle-generate = { path = "crates/generate" } wiggle-generate = { path = "crates/generate" }
wiggle-runtime = { path = "crates/runtime" } syn = { version = "1.0", features = ["full"] }
[dev-dependencies] [dev-dependencies]
wiggle-runtime = { path = "crates/runtime" }
wiggle-test = { path = "crates/test" } wiggle-test = { path = "crates/test" }
proptest = "0.9" proptest = "0.9"

View File

@@ -5,7 +5,6 @@ authors = ["Pat Hickey <phickey@fastly.com>", "Jakub Konka <kubkon@jakubkonka.co
edition = "2018" edition = "2018"
[lib] [lib]
proc-macro = true
[dependencies] [dependencies]
wiggle-runtime = { path = "../runtime" } wiggle-runtime = { path = "../runtime" }

View File

@@ -1,5 +1,3 @@
extern crate proc_macro;
mod config; mod config;
mod funcs; mod funcs;
mod lifetimes; mod lifetimes;
@@ -7,20 +5,16 @@ mod module_trait;
mod names; mod names;
mod types; mod types;
use proc_macro::TokenStream; use proc_macro2::TokenStream;
use quote::quote; use quote::quote;
use syn::parse_macro_input;
use config::Config; pub use config::Config;
use funcs::define_func; pub use funcs::define_func;
use module_trait::define_module_trait; pub use module_trait::define_module_trait;
use names::Names; pub use names::Names;
use types::define_datatype; pub use types::define_datatype;
#[proc_macro]
pub fn from_witx(args: TokenStream) -> TokenStream {
let config = parse_macro_input!(args as Config);
pub fn generate(config: Config) -> TokenStream {
let doc = witx::load(&config.witx.paths).expect("loading witx"); let doc = witx::load(&config.witx.paths).expect("loading witx");
let names = Names::new(config); // TODO parse the names from the invocation of the macro, or from a file? let names = Names::new(config); // TODO parse the names from the invocation of the macro, or from a file?
@@ -43,10 +37,10 @@ pub fn from_witx(args: TokenStream) -> TokenStream {
) )
}); });
TokenStream::from(quote!( quote!(
mod types { mod types {
#(#types)* #(#types)*
} }
#(#modules)* #(#modules)*
)) )
} }

View File

@@ -1,5 +1,10 @@
/* extern crate proc_macro;
pub mod wasi {
generate::from_witx!("crates/WASI/phases/snapshot/witx/wasi_snapshot_preview1.witx"); use proc_macro::TokenStream;
use syn::parse_macro_input;
#[proc_macro]
pub fn from_witx(args: TokenStream) -> TokenStream {
let config = parse_macro_input!(args as wiggle_generate::Config);
TokenStream::from(wiggle_generate::generate(config))
} }
*/

View File

@@ -2,7 +2,7 @@ use proptest::prelude::*;
use wiggle_runtime::{GuestArray, GuestError, GuestPtr, GuestPtrMut, GuestType}; use wiggle_runtime::{GuestArray, GuestError, GuestPtr, GuestPtrMut, GuestType};
use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx}; use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx};
wiggle_generate::from_witx!({ wiggle::from_witx!({
witx: ["tests/arrays.witx"], witx: ["tests/arrays.witx"],
ctx: WasiCtx, ctx: WasiCtx,
}); });

View File

@@ -2,7 +2,7 @@ use proptest::prelude::*;
use wiggle_runtime::{GuestError, GuestRef}; use wiggle_runtime::{GuestError, GuestRef};
use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx}; use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx};
wiggle_generate::from_witx!({ wiggle::from_witx!({
witx: ["tests/atoms.witx"], witx: ["tests/atoms.witx"],
ctx: WasiCtx, ctx: WasiCtx,
}); });

View File

@@ -3,7 +3,7 @@ use std::convert::TryFrom;
use wiggle_runtime::{GuestError, GuestPtr}; use wiggle_runtime::{GuestError, GuestPtr};
use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx}; use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx};
wiggle_generate::from_witx!({ wiggle::from_witx!({
witx: ["tests/flags.witx"], witx: ["tests/flags.witx"],
ctx: WasiCtx, ctx: WasiCtx,
}); });

View File

@@ -4,7 +4,7 @@ use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx};
const FD_VAL: u32 = 123; const FD_VAL: u32 = 123;
wiggle_generate::from_witx!({ wiggle::from_witx!({
witx: ["tests/handles.witx"], witx: ["tests/handles.witx"],
ctx: WasiCtx, ctx: WasiCtx,
}); });

View File

@@ -3,7 +3,7 @@ use std::convert::TryFrom;
use wiggle_runtime::GuestError; use wiggle_runtime::GuestError;
use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx}; use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx};
wiggle_generate::from_witx!({ wiggle::from_witx!({
witx: ["tests/ints.witx"], witx: ["tests/ints.witx"],
ctx: WasiCtx, ctx: WasiCtx,
}); });

View File

@@ -2,7 +2,7 @@ use proptest::prelude::*;
use wiggle_runtime::{GuestError, GuestPtr, GuestPtrMut, GuestRefMut, GuestType}; use wiggle_runtime::{GuestError, GuestPtr, GuestPtrMut, GuestRefMut, GuestType};
use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx}; use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx};
wiggle_generate::from_witx!({ wiggle::from_witx!({
witx: ["tests/pointers.witx"], witx: ["tests/pointers.witx"],
ctx: WasiCtx, ctx: WasiCtx,
}); });

View File

@@ -2,7 +2,7 @@ use proptest::prelude::*;
use wiggle_runtime::{GuestError, GuestPtrMut, GuestString}; use wiggle_runtime::{GuestError, GuestPtrMut, GuestString};
use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx}; use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx};
wiggle_generate::from_witx!({ wiggle::from_witx!({
witx: ["tests/strings.witx"], witx: ["tests/strings.witx"],
ctx: WasiCtx, ctx: WasiCtx,
}); });

View File

@@ -2,7 +2,7 @@ use proptest::prelude::*;
use wiggle_runtime::{GuestError, GuestPtr}; use wiggle_runtime::{GuestError, GuestPtr};
use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx}; use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx};
wiggle_generate::from_witx!({ wiggle::from_witx!({
witx: ["tests/structs.witx"], witx: ["tests/structs.witx"],
ctx: WasiCtx, ctx: WasiCtx,
}); });

View File

@@ -2,7 +2,7 @@ use proptest::prelude::*;
use wiggle_runtime::{GuestError, GuestType}; use wiggle_runtime::{GuestError, GuestType};
use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx}; use wiggle_test::{impl_errno, HostMemory, MemArea, WasiCtx};
wiggle_generate::from_witx!({ wiggle::from_witx!({
witx: ["tests/union.witx"], witx: ["tests/union.witx"],
ctx: WasiCtx, ctx: WasiCtx,
}); });