Skip to content

Commit 1216a45

Browse files
authored
Reduce dependencies of wasm-metadata (#2090)
This crate is depended on by `wit-component` which shows up in a lot of places, but most of the dependencies of `wasm-metadata` are not needed for `wit-component`. This commit gates most of the OCI-related information behind a compile-time `oci` feature which is enabled by default. The workspace dependency now disables default features and only the CLI itself enables the default features of `wasm-metadata`.
1 parent ae18019 commit 1216a45

File tree

11 files changed

+180
-160
lines changed

11 files changed

+180
-160
lines changed

.github/workflows/main.yml

+3
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ jobs:
274274
- run: cargo check --no-default-features -p wasm-smith --features component-model
275275
- run: cargo check --no-default-features -p wasm-smith --features wasmparser
276276
- run: cargo check --no-default-features -p wasm-smith --features wasmparser,component-model
277+
- run: cargo check --no-default-features -p wasm-metadata
278+
- run: cargo check --no-default-features -p wasm-metadata --features serde
279+
- run: cargo check --no-default-features -p wasm-metadata --features oci
277280
- run: |
278281
if cargo tree -p wasm-smith --no-default-features -e no-dev | grep wasmparser; then
279282
echo wasm-smith without default features should not depend on wasmparser

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ wasmtime = { version = "29.0.1", default-features = false, features = ['cranelif
105105

106106
wasm-compose = { version = "0.227.1", path = "crates/wasm-compose" }
107107
wasm-encoder = { version = "0.227.1", path = "crates/wasm-encoder", default-features = false }
108-
wasm-metadata = { version = "0.227.1", path = "crates/wasm-metadata" }
108+
wasm-metadata = { version = "0.227.1", path = "crates/wasm-metadata", default-features = false }
109109
wasm-mutate = { version = "0.227.1", path = "crates/wasm-mutate" }
110110
wasm-shrink = { version = "0.227.1", path = "crates/wasm-shrink" }
111111
wasm-smith = { version = "0.227.1", path = "crates/wasm-smith" }
@@ -168,7 +168,7 @@ wit-encoder = { workspace = true, optional = true }
168168
wit-parser = { workspace = true, optional = true, features = ['decoding', 'wat', 'serde'] }
169169

170170
# Dependencies of `metadata`
171-
wasm-metadata = { workspace = true, features = ["clap"], optional = true }
171+
wasm-metadata = { workspace = true, features = ["clap", "default"], optional = true }
172172
bytesize = { workspace = true }
173173

174174
# Dependencies of `wit-smith`

crates/wasm-metadata/Cargo.toml

+21-12
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,25 @@ workspace = true
1414
anyhow = { workspace = true }
1515
clap = { workspace = true, optional = true }
1616
indexmap = { workspace = true, features = ["serde"] }
17-
serde = { workspace = true }
18-
serde_derive = { workspace = true }
19-
serde_json = { workspace = true }
20-
spdx = { workspace = true }
21-
url = { workspace = true }
17+
serde = { workspace = true, optional = true }
18+
serde_derive = { workspace = true, optional = true }
19+
serde_json = { workspace = true, optional = true }
20+
spdx = { workspace = true, optional = true }
21+
url = { workspace = true, optional = true }
2222
wasm-encoder = { workspace = true, features = ['std', 'component-model'] }
23-
wasmparser = { workspace = true, features = [
24-
'std',
25-
'component-model',
26-
'hash-collections',
27-
] }
28-
auditable-serde = "0.8.0"
29-
flate2 = "1.1.0"
23+
wasmparser = { workspace = true, features = ['std', 'component-model', 'hash-collections'] }
24+
auditable-serde = { version = "0.8.0", optional = true }
25+
flate2 = { version = "1.1.0", optional = true }
26+
27+
[features]
28+
default = ['oci', 'serde']
29+
oci = [
30+
'dep:auditable-serde',
31+
'dep:flate2',
32+
'dep:url',
33+
'dep:spdx',
34+
'dep:serde_json',
35+
'serde',
36+
]
37+
38+
serde = ['dep:serde_derive', 'dep:serde']
+18-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use crate::{
2-
rewrite_wasm, Authors, Description, Homepage, Licenses, Producers, Revision, Source, Version,
3-
};
4-
1+
use crate::{rewrite_wasm, Producers};
52
use anyhow::Result;
63

74
/// Add metadata (module name, producers) to a WebAssembly file.
@@ -10,6 +7,7 @@ use anyhow::Result;
107
/// metadata will be added to the outermost component.
118
#[cfg_attr(feature = "clap", derive(clap::Parser))]
129
#[derive(Debug, Clone, Default)]
10+
#[non_exhaustive]
1311
pub struct AddMetadata {
1412
/// Add a module or component name to the names section
1513
#[cfg_attr(feature = "clap", clap(long, value_name = "NAME"))]
@@ -30,31 +28,38 @@ pub struct AddMetadata {
3028
/// Contact details of the people or organization responsible,
3129
/// encoded as a freeform string.
3230
#[cfg_attr(feature = "clap", clap(long, value_name = "NAME"))]
33-
pub authors: Option<Authors>,
31+
#[cfg(feature = "oci")]
32+
pub authors: Option<crate::Authors>,
3433

3534
/// A human-readable description of the binary
3635
#[cfg_attr(feature = "clap", clap(long, value_name = "NAME"))]
37-
pub description: Option<Description>,
36+
#[cfg(feature = "oci")]
37+
pub description: Option<crate::Description>,
3838

3939
/// License(s) under which contained software is distributed as an SPDX License Expression.
4040
#[cfg_attr(feature = "clap", clap(long, value_name = "NAME"))]
41-
pub licenses: Option<Licenses>,
41+
#[cfg(feature = "oci")]
42+
pub licenses: Option<crate::Licenses>,
4243

4344
/// URL to get source code for building the image
4445
#[cfg_attr(feature = "clap", clap(long, value_name = "NAME"))]
45-
pub source: Option<Source>,
46+
#[cfg(feature = "oci")]
47+
pub source: Option<crate::Source>,
4648

4749
/// URL to find more information on the binary
4850
#[cfg_attr(feature = "clap", clap(long, value_name = "NAME"))]
49-
pub homepage: Option<Homepage>,
51+
#[cfg(feature = "oci")]
52+
pub homepage: Option<crate::Homepage>,
5053

5154
/// Source control revision identifier for the packaged software.
5255
#[cfg_attr(feature = "clap", clap(long, value_name = "NAME"))]
53-
pub revision: Option<Revision>,
56+
#[cfg(feature = "oci")]
57+
pub revision: Option<crate::Revision>,
5458

5559
/// Version of the packaged software
5660
#[cfg_attr(feature = "clap", clap(long, value_name = "NAME"))]
57-
pub version: Option<Version>,
61+
#[cfg(feature = "oci")]
62+
pub version: Option<crate::Version>,
5863
}
5964

6065
#[cfg(feature = "clap")]
@@ -69,17 +74,7 @@ impl AddMetadata {
6974
/// components. The module and component will have, at very least, an empty name and producers
7075
/// section created.
7176
pub fn to_wasm(&self, input: &[u8]) -> Result<Vec<u8>> {
72-
rewrite_wasm(
73-
&self.name,
74-
&Producers::from_meta(self),
75-
&self.authors,
76-
&self.description,
77-
&self.licenses,
78-
&self.source,
79-
&self.homepage,
80-
&self.revision,
81-
&self.version,
82-
input,
83-
)
77+
let add_producers = Producers::from_meta(self);
78+
rewrite_wasm(self, &add_producers, input)
8479
}
8580
}

crates/wasm-metadata/src/lib.rs

+31-22
Original file line numberDiff line numberDiff line change
@@ -25,44 +25,53 @@
2525
//!
2626
//! let wasm = fs::read("program.wasm")?;
2727
//!
28-
//! let metadata = AddMetadata {
29-
//! name: Some("program".to_owned()),
30-
//! language: vec![("tunalang".to_owned(), "1.0.0".to_owned())],
31-
//! processed_by: vec![("chashu-tools".to_owned(), "1.0.1".to_owned())],
32-
//! sdk: vec![],
33-
//! authors: Some(Authors::new("Chashu Cat")),
34-
//! description: Some(Description::new("Chashu likes tuna")),
35-
//! licenses: Some(Licenses::new("Apache-2.0 WITH LLVM-exception")?),
36-
//! source: Some(Source::new("https://github.com/chashu/chashu-tools")?),
37-
//! homepage: Some(Homepage::new("https://github.com/chashu/chashu-tools")?),
38-
//! revision: Some(Revision::new("de978e17a80c1118f606fce919ba9b7d5a04a5ad")),
39-
//! version: Some(Version::new("1.0.0")),
40-
//! };
28+
//! let mut add = AddMetadata ::default();
29+
//! add.name = Some("program".to_owned());
30+
//! add.language = vec![("tunalang".to_owned(), "1.0.0".to_owned())];
31+
//! add.processed_by = vec![("chashu-tools".to_owned(), "1.0.1".to_owned())];
32+
//! add.sdk = vec![];
33+
//! add.authors = Some(Authors::new("Chashu Cat"));
34+
//! add.description = Some(Description::new("Chashu likes tuna"));
35+
//! add.licenses = Some(Licenses::new("Apache-2.0 WITH LLVM-exception")?);
36+
//! add.source = Some(Source::new("https://github.com/chashu/chashu-tools")?);
37+
//! add.homepage = Some(Homepage::new("https://github.com/chashu/chashu-tools")?);
38+
//! add.revision = Some(Revision::new("de978e17a80c1118f606fce919ba9b7d5a04a5ad"));
39+
//! add.version = Some(Version::new("1.0.0"));
4140
//!
42-
//! let wasm = metadata.to_wasm(&wasm)?;
41+
//! let wasm = add.to_wasm(&wasm)?;
4342
//! fs::write("program.wasm", &wasm)?;
4443
//! # Ok(()) }
4544
//! ```
4645
46+
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
4747
#![warn(missing_debug_implementations, missing_docs)]
4848

4949
pub use add_metadata::AddMetadata;
50-
pub use dependencies::Dependencies;
51-
pub use metadata::Metadata;
5250
pub use names::{ComponentNames, ModuleNames};
53-
pub use oci_annotations::{Authors, Description, Homepage, Licenses, Revision, Source, Version};
54-
pub use payload::Payload;
5551
pub use producers::{Producers, ProducersField};
5652

5753
pub(crate) use rewrite::rewrite_wasm;
5854

5955
mod add_metadata;
60-
mod dependencies;
61-
mod metadata;
6256
mod names;
63-
mod oci_annotations;
64-
mod payload;
6557
mod producers;
6658
mod rewrite;
6759

6860
pub(crate) mod utils;
61+
62+
#[cfg(feature = "oci")]
63+
mod dependencies;
64+
#[cfg(feature = "oci")]
65+
pub use dependencies::Dependencies;
66+
#[cfg(feature = "oci")]
67+
mod oci_annotations;
68+
#[cfg(feature = "oci")]
69+
pub use oci_annotations::{Authors, Description, Homepage, Licenses, Revision, Source, Version};
70+
#[cfg(feature = "oci")]
71+
mod metadata;
72+
#[cfg(feature = "oci")]
73+
pub use metadata::Metadata;
74+
#[cfg(feature = "oci")]
75+
mod payload;
76+
#[cfg(feature = "oci")]
77+
pub use payload::Payload;

crates/wasm-metadata/src/payload.rs

+8
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ impl Payload {
9393
.metadata_mut()
9494
.producers = Some(producers);
9595
}
96+
#[cfg(feature = "oci")]
9697
KnownCustom::Unknown if c.name() == "authors" => {
9798
let a = Authors::parse_custom_section(&c)?;
9899
let Metadata {
@@ -103,6 +104,7 @@ impl Payload {
103104
.metadata_mut();
104105
*author = Some(a);
105106
}
107+
#[cfg(feature = "oci")]
106108
KnownCustom::Unknown if c.name() == "description" => {
107109
let a = Description::parse_custom_section(&c)?;
108110
let Metadata { description, .. } = output
@@ -111,6 +113,7 @@ impl Payload {
111113
.metadata_mut();
112114
*description = Some(a);
113115
}
116+
#[cfg(feature = "oci")]
114117
KnownCustom::Unknown if c.name() == "licenses" => {
115118
let a = Licenses::parse_custom_section(&c)?;
116119
let Metadata { licenses, .. } = output
@@ -119,6 +122,7 @@ impl Payload {
119122
.metadata_mut();
120123
*licenses = Some(a);
121124
}
125+
#[cfg(feature = "oci")]
122126
KnownCustom::Unknown if c.name() == "source" => {
123127
let a = Source::parse_custom_section(&c)?;
124128
let Metadata { source, .. } = output
@@ -127,6 +131,7 @@ impl Payload {
127131
.metadata_mut();
128132
*source = Some(a);
129133
}
134+
#[cfg(feature = "oci")]
130135
KnownCustom::Unknown if c.name() == "homepage" => {
131136
let a = Homepage::parse_custom_section(&c)?;
132137
let Metadata { homepage, .. } = output
@@ -135,6 +140,7 @@ impl Payload {
135140
.metadata_mut();
136141
*homepage = Some(a);
137142
}
143+
#[cfg(feature = "oci")]
138144
KnownCustom::Unknown if c.name() == "revision" => {
139145
let a = Revision::parse_custom_section(&c)?;
140146
let Metadata { revision, .. } = output
@@ -143,6 +149,7 @@ impl Payload {
143149
.metadata_mut();
144150
*revision = Some(a);
145151
}
152+
#[cfg(feature = "oci")]
146153
KnownCustom::Unknown if c.name() == "version" => {
147154
let a = crate::Version::parse_custom_section(&c)?;
148155
let Metadata { version, .. } = output
@@ -151,6 +158,7 @@ impl Payload {
151158
.metadata_mut();
152159
*version = Some(a);
153160
}
161+
#[cfg(feature = "oci")]
154162
KnownCustom::Unknown if c.name() == ".dep-v0" => {
155163
let a = crate::Dependencies::parse_custom_section(&c)?;
156164
let Metadata { dependencies, .. } = output

crates/wasm-metadata/src/producers.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
use anyhow::Result;
22
use indexmap::{map::Entry, IndexMap};
3-
use serde_derive::Serialize;
43
use wasm_encoder::Encode;
54
use wasmparser::{BinaryReader, KnownCustom, Parser, ProducersSectionReader};
65

76
use crate::{rewrite_wasm, AddMetadata};
87
/// A representation of a WebAssembly producers section.
98
///
109
/// Spec: <https://github.com/WebAssembly/tool-conventions/blob/main/ProducersSection.md>
11-
#[derive(Debug, Serialize)]
10+
#[derive(Debug)]
11+
#[cfg_attr(feature = "serde", derive(serde_derive::Serialize))]
1212
pub struct Producers(
13-
#[serde(serialize_with = "indexmap::map::serde_seq::serialize")]
13+
#[cfg_attr(
14+
feature = "serde",
15+
serde(serialize_with = "indexmap::map::serde_seq::serialize")
16+
)]
1417
IndexMap<String, IndexMap<String, String>>,
1518
);
1619

@@ -147,9 +150,7 @@ impl Producers {
147150
/// Merge into an existing wasm module. Rewrites the module with this producers section
148151
/// merged into its existing one, or adds this producers section if none is present.
149152
pub fn add_to_wasm(&self, input: &[u8]) -> Result<Vec<u8>> {
150-
rewrite_wasm(
151-
&None, self, &None, &None, &None, &None, &None, &None, &None, input,
152-
)
153+
rewrite_wasm(&Default::default(), self, input)
153154
}
154155
}
155156

0 commit comments

Comments
 (0)