summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJasper-Bekkers <bekkers@gmail.com>2019-12-10 16:12:16 +0100
committerEmilio Cobos Álvarez <emilio@crisal.io>2019-12-11 01:48:01 +0100
commit09f6c1d921a987ec2b20e0033d1bbbb36b0e22ef (patch)
tree94b7f1cb955ee63baccfee3beb66f686f9a5e50b
parent7d61f36a5bfcfe2ea533f5edcae8bb6991683432 (diff)
Add support for wasm_import_module
-rw-r--r--src/codegen/mod.rs18
-rw-r--r--src/lib.rs20
-rw-r--r--src/options.rs10
-rw-r--r--tests/expectations/tests/wasm-import-module.rs13
-rw-r--r--tests/headers/wasm-import-module.h3
5 files changed, 61 insertions, 3 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs
index d9ac4aa0..b1612c70 100644
--- a/src/codegen/mod.rs
+++ b/src/codegen/mod.rs
@@ -3611,10 +3611,22 @@ impl CodeGenerator for Function {
attributes.push(attributes::link_name(link_name));
}
+ // Unfortunately this can't piggyback on the `attributes` list because
+ // the #[link(wasm_import_module)] needs to happen before the `extern "C"` block.
+ // it doesn't get picked up properly otherwise
+ let wasm_link_attribute =
+ ctx.options().wasm_import_module_name.as_ref().map(|name| {
+ quote! {
+ #[link(wasm_import_module = #name)]
+ }
+ });
+
let ident = ctx.rust_ident(canonical_name);
- let tokens = quote!( extern #abi {
- #(#attributes)*
- pub fn #ident ( #( #args ),* ) #ret;
+ let tokens = quote!(
+ #wasm_link_attribute
+ extern #abi {
+ #(#attributes)*
+ pub fn #ident ( #( #args ),* ) #ret;
});
result.push(tokens);
}
diff --git a/src/lib.rs b/src/lib.rs
index 877dbd9a..a818409b 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -522,6 +522,13 @@ impl Builder {
output_vector.push("--use-array-pointers-in-arguments".into());
}
+ if let Some(ref wasm_import_module_name) =
+ self.options.wasm_import_module_name
+ {
+ output_vector.push("--wasm-import-module-name".into());
+ output_vector.push(wasm_import_module_name.clone());
+ }
+
self.options
.opaque_types
.get_items()
@@ -1491,6 +1498,15 @@ impl Builder {
self.options.array_pointers_in_arguments = doit;
self
}
+
+ /// Set the wasm import module name
+ pub fn wasm_import_module_name<T: Into<String>>(
+ mut self,
+ import_name: T,
+ ) -> Self {
+ self.options.wasm_import_module_name = Some(import_name.into());
+ self
+ }
}
/// Configuration options for generated bindings.
@@ -1750,6 +1766,9 @@ struct BindgenOptions {
/// Decide if C arrays should be regular pointers in rust or array pointers
array_pointers_in_arguments: bool,
+
+ /// Wasm import module name.
+ wasm_import_module_name: Option<String>,
}
/// TODO(emilio): This is sort of a lie (see the error message that results from
@@ -1875,6 +1894,7 @@ impl Default for BindgenOptions {
no_copy_types: Default::default(),
no_hash_types: Default::default(),
array_pointers_in_arguments: false,
+ wasm_import_module_name: None,
}
}
}
diff --git a/src/options.rs b/src/options.rs
index b09ba919..01982f13 100644
--- a/src/options.rs
+++ b/src/options.rs
@@ -434,6 +434,11 @@ where
Arg::with_name("use-array-pointers-in-arguments")
.long("use-array-pointers-in-arguments")
.help("Use `*const [T; size]` instead of `*const T` for C arrays"),
+ Arg::with_name("wasm-import-module-name")
+ .long("wasm-import-module-name")
+ .value_name("name")
+ .takes_value(true)
+ .help("The name to be used in a #[link(wasm_import_module = ...)] statement")
]) // .args()
.get_matches_from(args);
@@ -601,6 +606,11 @@ where
builder = builder.array_pointers_in_arguments(true);
}
+ if let Some(wasm_import_name) = matches.value_of("wasm-import-module-name")
+ {
+ builder = builder.wasm_import_module_name(wasm_import_name);
+ }
+
if let Some(prefix) = matches.value_of("ctypes-prefix") {
builder = builder.ctypes_prefix(prefix);
}
diff --git a/tests/expectations/tests/wasm-import-module.rs b/tests/expectations/tests/wasm-import-module.rs
new file mode 100644
index 00000000..afbc5846
--- /dev/null
+++ b/tests/expectations/tests/wasm-import-module.rs
@@ -0,0 +1,13 @@
+/* automatically generated by rust-bindgen */
+
+#![allow(
+ dead_code,
+ non_snake_case,
+ non_camel_case_types,
+ non_upper_case_globals
+)]
+
+#[link(wasm_import_module = "test-module")]
+extern "C" {
+ pub fn test_function();
+} \ No newline at end of file
diff --git a/tests/headers/wasm-import-module.h b/tests/headers/wasm-import-module.h
new file mode 100644
index 00000000..db4fe85c
--- /dev/null
+++ b/tests/headers/wasm-import-module.h
@@ -0,0 +1,3 @@
+// bindgen-flags: --wasm-import-module-name test-module
+
+void test_function(); \ No newline at end of file