Skip to content

Commit

Permalink
Make the procedural macros read files relative to $CARGO_MANIFEST_DIR (
Browse files Browse the repository at this point in the history
…#167)

(fixes #159)
  • Loading branch information
Michael-F-Bryan committed Mar 10, 2022
1 parent 8a27d70 commit 2739474
Show file tree
Hide file tree
Showing 41 changed files with 82 additions and 67 deletions.
10 changes: 8 additions & 2 deletions crates/rust-wasm-impl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::{Path, PathBuf};

use proc_macro::TokenStream;
use syn::parse::{Error, Parse, ParseStream, Result};
use syn::punctuated::Punctuated;
Expand Down Expand Up @@ -28,11 +30,11 @@ fn run(input: TokenStream, dir: Direction) -> TokenStream {

// Include a dummy `include_str!` for any files we read so rustc knows that
// we depend on the contents of those files.
let cwd = std::env::current_dir().unwrap();
let cwd = std::env::var("CARGO_MANIFEST_DIR").unwrap();
for file in input.files.iter() {
contents.push_str(&format!(
"const _: &str = include_str!(r#\"{}\"#);\n",
cwd.join(file).display()
Path::new(&cwd).join(file).display()
));
}

Expand Down Expand Up @@ -82,7 +84,9 @@ impl Parse for Opts {
files.push(s.value());
}
let mut interfaces = Vec::new();
let manifest_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
for path in files.iter() {
let path = manifest_dir.join(path);
let iface = Interface::parse_file(path).map_err(|e| Error::new(call_site, e))?;
interfaces.push(iface);
}
Expand Down Expand Up @@ -123,7 +127,9 @@ impl Parse for ConfigField {
let paths = Punctuated::<syn::LitStr, Token![,]>::parse_terminated(&paths)?;
let values = paths.iter().map(|s| s.value()).collect::<Vec<_>>();
let mut interfaces = Vec::new();
let manifest_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
for value in &values {
let value = manifest_dir.join(value);
let interface =
Interface::parse_file(value).map_err(|e| Error::new(bracket.span, e))?;
interfaces.push(interface);
Expand Down
2 changes: 1 addition & 1 deletion crates/test-modules/modules/crates/flags-main/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wit_bindgen_rust::import!("crates/flags/flags.wit");
wit_bindgen_rust::import!("../flags/flags.wit");

use flags::*;

Expand Down
2 changes: 1 addition & 1 deletion crates/test-modules/modules/crates/flags/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wit_bindgen_rust::export!("crates/flags/flags.wit");
wit_bindgen_rust::export!("flags.wit");

use flags::*;

Expand Down
2 changes: 1 addition & 1 deletion crates/test-modules/modules/crates/lists-main/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wit_bindgen_rust::import!("crates/lists/lists.wit");
wit_bindgen_rust::import!("../lists/lists.wit");

use lists::*;

Expand Down
2 changes: 1 addition & 1 deletion crates/test-modules/modules/crates/lists/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wit_bindgen_rust::export!("crates/lists/lists.wit");
wit_bindgen_rust::export!("lists.wit");

use lists::*;

Expand Down
2 changes: 1 addition & 1 deletion crates/test-modules/modules/crates/nested-main/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wit_bindgen_rust::import!("crates/nested_b/nested_b.wit");
wit_bindgen_rust::import!("../nested_b/nested_b.wit");

use nested_b::*;

Expand Down
2 changes: 1 addition & 1 deletion crates/test-modules/modules/crates/nested_a/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wit_bindgen_rust::export!("crates/nested_a/nested_a.wit");
wit_bindgen_rust::export!("nested_a.wit");

struct NestedA;

Expand Down
4 changes: 2 additions & 2 deletions crates/test-modules/modules/crates/nested_b/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
wit_bindgen_rust::import!("crates/nested_a/nested_a.wit");
wit_bindgen_rust::export!("crates/nested_b/nested_b.wit");
wit_bindgen_rust::import!("../nested_a/nested_a.wit");
wit_bindgen_rust::export!("nested_b.wit");

struct NestedB;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wit_bindgen_rust::import!("crates/records/records.wit");
wit_bindgen_rust::import!("../records/records.wit");

use records::*;

Expand Down
2 changes: 1 addition & 1 deletion crates/test-modules/modules/crates/records/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wit_bindgen_rust::export!("crates/records/records.wit");
wit_bindgen_rust::export!("records.wit");

use records::*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wit_bindgen_rust::import!("crates/resources/resources.wit");
wit_bindgen_rust::import!("../resources/resources.wit");

use resources::*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wit_bindgen_rust::import!("crates/resources/resources.wit");
wit_bindgen_rust::import!("../resources/resources.wit");

use resources::*;

Expand Down
2 changes: 1 addition & 1 deletion crates/test-modules/modules/crates/resources/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wit_bindgen_rust::export!("crates/resources/resources.wit");
wit_bindgen_rust::export!("resources.wit");

use std::sync::{Arc, Mutex};
use wit_bindgen_rust::Handle;
Expand Down
2 changes: 1 addition & 1 deletion crates/test-modules/modules/crates/types-main/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wit_bindgen_rust::import!("crates/types/types.wit");
wit_bindgen_rust::import!("../types/types.wit");

fn main() {
types::a();
Expand Down
2 changes: 1 addition & 1 deletion crates/test-modules/modules/crates/types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wit_bindgen_rust::export!("crates/types/types.wit");
wit_bindgen_rust::export!("types.wit");

struct Types;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wit_bindgen_rust::import!("crates/variants/variants.wit");
wit_bindgen_rust::import!("../variants/variants.wit");

use variants::*;

Expand Down
2 changes: 1 addition & 1 deletion crates/test-modules/modules/crates/variants/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wit_bindgen_rust::export!("crates/variants/variants.wit");
wit_bindgen_rust::export!("variants.wit");

use variants::*;

Expand Down
10 changes: 8 additions & 2 deletions crates/wasmtime-impl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::{Path, PathBuf};

use proc_macro::TokenStream;
use syn::parse::{Error, Parse, ParseStream, Result};
use syn::punctuated::Punctuated;
Expand Down Expand Up @@ -36,12 +38,12 @@ fn run(input: TokenStream, dir: Direction) -> TokenStream {

// Include a dummy `include_str!` for any files we read so rustc knows that
// we depend on the contents of those files.
let cwd = std::env::current_dir().unwrap();
let cwd = std::env::var("CARGO_MANIFEST_DIR").unwrap();
for file in input.files.iter() {
contents.extend(
format!(
"const _: &str = include_str!(r#\"{}\"#);\n",
cwd.join(file).display()
Path::new(&cwd).join(file).display()
)
.parse::<TokenStream>()
.unwrap(),
Expand Down Expand Up @@ -95,7 +97,9 @@ impl Parse for Opts {
files.push(s.value());
}
let mut interfaces = Vec::new();
let manifest_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
for path in files.iter() {
let path = manifest_dir.join(path);
let iface = Interface::parse_file(path).map_err(|e| Error::new(call_site, e))?;
interfaces.push(iface);
}
Expand Down Expand Up @@ -136,7 +140,9 @@ impl Parse for ConfigField {
let paths = Punctuated::<syn::LitStr, Token![,]>::parse_terminated(&paths)?;
let values = paths.iter().map(|s| s.value()).collect::<Vec<_>>();
let mut interfaces = Vec::new();
let manifest_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
for value in &values {
let value = manifest_dir.join(value);
let interface =
Interface::parse_file(value).map_err(|e| Error::new(bracket.span, e))?;
interfaces.push(interface);
Expand Down
4 changes: 2 additions & 2 deletions crates/wit-bindgen-demo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use wit_bindgen_gen_core::wit_parser::Interface;
use wit_bindgen_gen_core::Generator;
use wit_bindgen_rust::Handle;

wit_bindgen_rust::export!("./crates/wit-bindgen-demo/demo.wit");
wit_bindgen_rust::import!("./crates/wit-bindgen-demo/browser.wit");
wit_bindgen_rust::export!("demo.wit");
wit_bindgen_rust::import!("browser.wit");

struct Demo;

Expand Down
4 changes: 2 additions & 2 deletions tests/runtime/async_functions/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
wit_bindgen_rust::import!("./tests/runtime/async_functions/imports.wit");
wit_bindgen_rust::export!("./tests/runtime/async_functions/exports.wit");
wit_bindgen_rust::import!("../../tests/runtime/async_functions/imports.wit");
wit_bindgen_rust::export!("../../tests/runtime/async_functions/exports.wit");

struct Exports;

Expand Down
4 changes: 2 additions & 2 deletions tests/runtime/buffers/host.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wit_bindgen_wasmtime::export!("./tests/runtime/buffers/imports.wit");
wit_bindgen_wasmtime::export!("../../tests/runtime/buffers/imports.wit");

use anyhow::Result;
use imports::*;
Expand Down Expand Up @@ -115,7 +115,7 @@ impl Imports for MyImports {
}
}

wit_bindgen_wasmtime::import!("./tests/runtime/buffers/exports.wit");
wit_bindgen_wasmtime::import!("../../tests/runtime/buffers/exports.wit");

fn run(wasm: &str) -> Result<()> {
use exports::*;
Expand Down
4 changes: 2 additions & 2 deletions tests/runtime/buffers/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
wit_bindgen_rust::import!("./tests/runtime/buffers/imports.wit");
wit_bindgen_rust::export!("./tests/runtime/buffers/exports.wit");
wit_bindgen_rust::import!("../../tests/runtime/buffers/imports.wit");
wit_bindgen_rust::export!("../../tests/runtime/buffers/exports.wit");

use std::iter;

Expand Down
4 changes: 2 additions & 2 deletions tests/runtime/flavorful/host.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;

wit_bindgen_wasmtime::export!("./tests/runtime/flavorful/imports.wit");
wit_bindgen_wasmtime::export!("../../tests/runtime/flavorful/imports.wit");

use imports::*;

Expand Down Expand Up @@ -91,7 +91,7 @@ impl Imports for MyImports {
}
}

wit_bindgen_wasmtime::import!("./tests/runtime/flavorful/exports.wit");
wit_bindgen_wasmtime::import!("../../tests/runtime/flavorful/exports.wit");

fn run(wasm: &str) -> Result<()> {
use exports::*;
Expand Down
4 changes: 2 additions & 2 deletions tests/runtime/flavorful/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
wit_bindgen_rust::import!("./tests/runtime/flavorful/imports.wit");
wit_bindgen_rust::export!("./tests/runtime/flavorful/exports.wit");
wit_bindgen_rust::import!("../../tests/runtime/flavorful/imports.wit");
wit_bindgen_rust::export!("../../tests/runtime/flavorful/exports.wit");

use exports::*;

Expand Down
4 changes: 2 additions & 2 deletions tests/runtime/handles/host.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wit_bindgen_wasmtime::export!("./tests/runtime/handles/imports.wit");
wit_bindgen_wasmtime::export!("../../tests/runtime/handles/imports.wit");

use anyhow::Result;
use imports::*;
Expand Down Expand Up @@ -87,7 +87,7 @@ impl Imports for MyImports {
fn odd_name_frob_the_odd(&mut self, _: &()) {}
}

wit_bindgen_wasmtime::import!("./tests/runtime/handles/exports.wit");
wit_bindgen_wasmtime::import!("../../tests/runtime/handles/exports.wit");

fn run(wasm: &str) -> Result<()> {
use exports::*;
Expand Down
4 changes: 2 additions & 2 deletions tests/runtime/handles/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
wit_bindgen_rust::import!("./tests/runtime/handles/imports.wit");
wit_bindgen_rust::export!("./tests/runtime/handles/exports.wit");
wit_bindgen_rust::import!("../../tests/runtime/handles/imports.wit");
wit_bindgen_rust::export!("../../tests/runtime/handles/exports.wit");

use exports::*;
use std::cell::RefCell;
Expand Down
4 changes: 2 additions & 2 deletions tests/runtime/invalid/host.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wit_bindgen_wasmtime::export!("./tests/runtime/invalid/imports.wit");
wit_bindgen_wasmtime::export!("../../tests/runtime/invalid/imports.wit");

use anyhow::Result;
use imports::*;
Expand Down Expand Up @@ -36,7 +36,7 @@ impl Imports for MyImports {
}
}

wit_bindgen_wasmtime::import!("./tests/runtime/invalid/exports.wit");
wit_bindgen_wasmtime::import!("../../tests/runtime/invalid/exports.wit");

fn run(wasm: &str) -> Result<()> {
use exports::*;
Expand Down
2 changes: 1 addition & 1 deletion tests/runtime/invalid/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
wit_bindgen_rust::export!("./tests/runtime/invalid/exports.wit");
wit_bindgen_rust::export!("../../tests/runtime/invalid/exports.wit");

#[link(wasm_import_module = "imports")]
extern "C" {
Expand Down
4 changes: 2 additions & 2 deletions tests/runtime/lists/host.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;

wit_bindgen_wasmtime::export!("./tests/runtime/lists/imports.wit");
wit_bindgen_wasmtime::export!("../../tests/runtime/lists/imports.wit");

use imports::*;
use wit_bindgen_wasmtime::Le;
Expand Down Expand Up @@ -126,7 +126,7 @@ impl Imports for MyImports {
}
}

wit_bindgen_wasmtime::import!("./tests/runtime/lists/exports.wit");
wit_bindgen_wasmtime::import!("../../tests/runtime/lists/exports.wit");

fn run(wasm: &str) -> Result<()> {
use exports::*;
Expand Down
4 changes: 2 additions & 2 deletions tests/runtime/lists/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
wit_bindgen_rust::import!("./tests/runtime/lists/imports.wit");
wit_bindgen_rust::export!("./tests/runtime/lists/exports.wit");
wit_bindgen_rust::import!("../../tests/runtime/lists/imports.wit");
wit_bindgen_rust::export!("../../tests/runtime/lists/exports.wit");

use std::alloc::{self, Layout};
use std::mem;
Expand Down
4 changes: 2 additions & 2 deletions tests/runtime/numbers/host.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;

wit_bindgen_wasmtime::export!("./tests/runtime/numbers/imports.wit");
wit_bindgen_wasmtime::export!("../../tests/runtime/numbers/imports.wit");

#[derive(Default)]
pub struct MyImports {
Expand Down Expand Up @@ -61,7 +61,7 @@ impl imports::Imports for MyImports {
}
}

wit_bindgen_wasmtime::import!("./tests/runtime/numbers/exports.wit");
wit_bindgen_wasmtime::import!("../../tests/runtime/numbers/exports.wit");

fn run(wasm: &str) -> Result<()> {
let (exports, mut store) = crate::instantiate(
Expand Down
4 changes: 2 additions & 2 deletions tests/runtime/numbers/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
wit_bindgen_rust::import!("./tests/runtime/numbers/imports.wit");
wit_bindgen_rust::export!("./tests/runtime/numbers/exports.wit");
wit_bindgen_rust::import!("../../tests/runtime/numbers/imports.wit");
wit_bindgen_rust::export!("../../tests/runtime/numbers/exports.wit");

use imports::*;
use std::sync::atomic::{AtomicU32, Ordering::SeqCst};
Expand Down
4 changes: 2 additions & 2 deletions tests/runtime/records/host.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;

wit_bindgen_wasmtime::export!("./tests/runtime/records/imports.wit");
wit_bindgen_wasmtime::export!("../../tests/runtime/records/imports.wit");

use imports::*;

Expand Down Expand Up @@ -49,7 +49,7 @@ impl Imports for MyImports {
}
}

wit_bindgen_wasmtime::import!("./tests/runtime/records/exports.wit");
wit_bindgen_wasmtime::import!("../../tests/runtime/records/exports.wit");

fn run(wasm: &str) -> Result<()> {
use exports::*;
Expand Down
9 changes: 6 additions & 3 deletions tests/runtime/records/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
wit_bindgen_rust::import!("./tests/runtime/records/imports.wit");
wit_bindgen_rust::export!("./tests/runtime/records/exports.wit");
wit_bindgen_rust::import!("../../tests/runtime/records/imports.wit");
wit_bindgen_rust::export!("../../tests/runtime/records/exports.wit");

use exports::*;

Expand Down Expand Up @@ -27,7 +27,10 @@ impl exports::Exports for Exports {
(Flag8::B0, Flag16::B1, Flag32::B2, Flag64::B3)
);

let r = roundtrip_record1(R1 { a: 8, b: F1::empty() });
let r = roundtrip_record1(R1 {
a: 8,
b: F1::empty(),
});
assert_eq!(r.a, 8);
assert_eq!(r.b, F1::empty());

Expand Down
4 changes: 2 additions & 2 deletions tests/runtime/smoke/host.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;

wit_bindgen_wasmtime::export!("./tests/runtime/smoke/imports.wit");
wit_bindgen_wasmtime::export!("../../tests/runtime/smoke/imports.wit");

#[derive(Default)]
pub struct MyImports {
Expand All @@ -14,7 +14,7 @@ impl imports::Imports for MyImports {
}
}

wit_bindgen_wasmtime::import!("./tests/runtime/smoke/exports.wit");
wit_bindgen_wasmtime::import!("../../tests/runtime/smoke/exports.wit");

fn run(wasm: &str) -> Result<()> {
let (exports, mut store) = crate::instantiate(
Expand Down
Loading

0 comments on commit 2739474

Please sign in to comment.