Skip to content

Commit 8557bf0

Browse files
authored
Add bindgen version information to Producers in resulting components (#502)
* gen-guest-c: component type object uses the LinkingSection encoder rather than open coding the exact same thing * fixup * add producer metadata to gen-guest-c component-type section * add producer section to wit-component type in gen-guest-teavm-java * gen-guest-rust: add producers metadata to component type section * test-helpers: fix codegen test generator * cargo.lock fix * latest wasm-tools deps
1 parent 6add188 commit 8557bf0

File tree

9 files changed

+103
-56
lines changed

9 files changed

+103
-56
lines changed

Cargo.lock

Lines changed: 44 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ clap = { version = "4.0.9", features = ["derive"] }
2929
env_logger = "0.9.1"
3030
indexmap = "1.9.1"
3131

32-
wasm-encoder = "0.23.0"
33-
wat = "1.0.57"
32+
wasm-encoder = "0.24.0"
33+
wasm-metadata = "0.2.0"
34+
wat = "1.0.59"
3435
wit-parser = "0.6.0"
35-
wit-component = "0.6.0"
36+
wit-component = "0.7.0"
3637

3738
wit-bindgen-core = { path = 'crates/bindgen-core', version = '0.3.0' }
3839
wit-bindgen-gen-guest-c = { path = 'crates/gen-guest-c', version = '0.3.0' }

crates/gen-guest-c/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ test = false
1919
wit-bindgen-core = { workspace = true }
2020
wit-component = { workspace = true }
2121
wasm-encoder = { workspace = true }
22+
wasm-metadata = { workspace = true }
2223
anyhow = { workspace = true }
2324
heck = { workspace = true }
2425
clap = { workspace = true, optional = true }

crates/gen-guest-c/src/component_type_object.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use anyhow::Result;
22
use heck::ToSnakeCase;
33
use wasm_encoder::{
4-
CodeSection, CustomSection, Encode, Function, FunctionSection, Module, TypeSection,
4+
CodeSection, CustomSection, Function, FunctionSection, LinkingSection, Module, SymbolTable,
5+
TypeSection,
56
};
67
use wit_bindgen_core::wit_parser::{Resolve, WorldId};
78
use wit_component::StringEncoding;
@@ -25,7 +26,13 @@ pub fn object(resolve: &Resolve, world: WorldId, encoding: StringEncoding) -> Re
2526
code.function(&Function::new([]));
2627
module.section(&code);
2728

28-
let data = wit_component::metadata::encode(resolve, world, encoding).unwrap();
29+
let mut producers = wasm_metadata::Producers::empty();
30+
producers.add(
31+
"processed-by",
32+
env!("CARGO_PKG_NAME"),
33+
env!("CARGO_PKG_VERSION"),
34+
);
35+
let data = wit_component::metadata::encode(resolve, world, encoding, Some(&producers)).unwrap();
2936

3037
// The custom section name here must start with "component-type" but
3138
// otherwise is attempted to be unique here to ensure that this doesn't get
@@ -40,24 +47,12 @@ pub fn object(resolve: &Resolve, world: WorldId, encoding: StringEncoding) -> Re
4047
data: data.as_slice(),
4148
});
4249

43-
// Append the `.linking` section
44-
let mut data = Vec::new();
45-
data.push(0x02); // version 2
46-
{
47-
let mut subsection = Vec::<u8>::new();
48-
subsection.push(0x01); // syminfo count
49-
subsection.push(0x00); // SYMTAB_FUNCTION
50-
0u32.encode(&mut subsection); // flags
51-
0u32.encode(&mut subsection); // index
52-
linking_symbol(&world_name).encode(&mut subsection); // name
53-
54-
data.push(0x08); // `WASM_SYMBOL_TABLE`
55-
subsection.encode(&mut data);
56-
}
57-
module.section(&CustomSection {
58-
name: "linking",
59-
data: &data,
60-
});
50+
// Append the linking section, so that lld knows the custom section's symbol name
51+
let mut linking = LinkingSection::new();
52+
let mut symbols = SymbolTable::new();
53+
symbols.function(0, 0, Some(&linking_symbol(&world_name)));
54+
linking.symbol_table(&symbols);
55+
module.section(&linking);
6156

6257
Ok(module.finish())
6358
}

crates/gen-guest-rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ doctest = false
1919
wit-bindgen-core = { workspace = true }
2020
wit-bindgen-gen-rust-lib = { workspace = true }
2121
wit-component = { workspace = true }
22+
wasm-metadata = { workspace = true }
2223
heck = { workspace = true }
2324
clap = { workspace = true, optional = true }
2425

crates/gen-guest-rust/src/lib.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,21 @@ impl WorldGenerator for RustWasm {
252252
self.src
253253
.push_str(&format!("#[link_section = \"component-type:{}\"]\n", name,));
254254

255-
let component_type =
256-
wit_component::metadata::encode(resolve, world, wit_component::StringEncoding::UTF8)
257-
.unwrap();
255+
let mut producers = wasm_metadata::Producers::empty();
256+
producers.add(
257+
"processed-by",
258+
env!("CARGO_PKG_NAME"),
259+
env!("CARGO_PKG_VERSION"),
260+
);
261+
262+
let component_type = wit_component::metadata::encode(
263+
resolve,
264+
world,
265+
wit_component::StringEncoding::UTF8,
266+
Some(&producers),
267+
)
268+
.unwrap();
269+
258270
self.src.push_str("#[doc(hidden)]");
259271
self.src.push_str(&format!(
260272
"pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; {}] = ",

crates/gen-guest-teavm-java/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ through the `wit-bindgen-cli` crate.
1313
[dependencies]
1414
wit-bindgen-core = { workspace = true }
1515
wit-component = { workspace = true }
16+
wasm-metadata = { workspace = true }
1617
heck = { workspace = true }
1718
clap = { workspace = true, optional = true }
1819

crates/gen-guest-teavm-java/src/lib.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,26 @@ impl WorldGenerator for TeaVmJava {
202202
.join("\n"),
203203
);
204204

205-
let component_type =
206-
wit_component::metadata::encode(resolve, id, wit_component::StringEncoding::UTF8)
207-
.unwrap()
208-
.into_iter()
209-
.map(|byte| format!("{byte:02x}"))
210-
.collect::<Vec<_>>()
211-
.concat();
205+
let mut producers = wasm_metadata::Producers::empty();
206+
producers.add(
207+
"processed-by",
208+
env!("CARGO_PKG_NAME"),
209+
env!("CARGO_PKG_VERSION"),
210+
);
211+
212+
let component_type = wit_component::metadata::encode(
213+
resolve,
214+
id,
215+
wit_component::StringEncoding::UTF8,
216+
Some(&producers),
217+
)
218+
.unwrap();
219+
220+
let component_type = component_type
221+
.into_iter()
222+
.map(|byte| format!("{byte:02x}"))
223+
.collect::<Vec<_>>()
224+
.concat();
212225

213226
uwriteln!(
214227
src,

crates/test-helpers/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ pub fn run_component_codegen_test(
9090
let (resolve, world) = parse_wit(wit_path);
9191
let world_name = &resolve.worlds[world].name;
9292
let mut wasm = wit_component::dummy_module(&resolve, world);
93-
let encoded = wit_component::metadata::encode(&resolve, world, StringEncoding::UTF8).unwrap();
93+
let encoded =
94+
wit_component::metadata::encode(&resolve, world, StringEncoding::UTF8, None).unwrap();
9495
let section = wasm_encoder::CustomSection {
9596
name: "component-type",
9697
data: &encoded,

0 commit comments

Comments
 (0)