-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathwit-bindgen.rs
223 lines (203 loc) · 7.21 KB
/
wit-bindgen.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use anyhow::{bail, Context, Error, Result};
use clap::Parser;
use std::path::PathBuf;
use std::str;
use wit_bindgen_core::{wit_parser, Files, WorldGenerator};
use wit_parser::Resolve;
/// Helper for passing VERSION to opt.
/// If CARGO_VERSION_INFO is set, use it, otherwise use CARGO_PKG_VERSION.
fn version() -> &'static str {
option_env!("CARGO_VERSION_INFO").unwrap_or(env!("CARGO_PKG_VERSION"))
}
#[derive(Debug, Parser)]
#[command(version = version())]
enum Opt {
/// This generator outputs a Markdown file describing an interface.
#[cfg(feature = "markdown")]
Markdown {
#[clap(flatten)]
opts: wit_bindgen_markdown::Opts,
#[clap(flatten)]
args: Common,
},
/// Generates bindings for MoonBit guest modules.
#[cfg(feature = "moonbit")]
Moonbit {
#[clap(flatten)]
opts: wit_bindgen_moonbit::Opts,
#[clap(flatten)]
args: Common,
},
/// Generates bindings for Rust guest modules.
#[cfg(feature = "rust")]
Rust {
#[clap(flatten)]
opts: wit_bindgen_rust::Opts,
#[clap(flatten)]
args: Common,
},
/// Generates bindings for C/CPP guest modules.
#[cfg(feature = "c")]
C {
#[clap(flatten)]
opts: wit_bindgen_c::Opts,
#[clap(flatten)]
args: Common,
},
/// Generates bindings for TeaVM-based Java guest modules.
#[cfg(feature = "teavm-java")]
TeavmJava {
#[clap(flatten)]
opts: wit_bindgen_teavm_java::Opts,
#[clap(flatten)]
args: Common,
},
/// Generates bindings for TinyGo-based Go guest modules.
#[cfg(feature = "go")]
TinyGo {
#[clap(flatten)]
opts: wit_bindgen_go::Opts,
#[clap(flatten)]
args: Common,
},
/// Generates bindings for C# guest modules.
#[cfg(feature = "csharp")]
CSharp {
#[clap(flatten)]
opts: wit_bindgen_csharp::Opts,
#[clap(flatten)]
args: Common,
},
}
#[derive(Debug, Parser)]
struct Common {
/// Where to place output files
#[clap(long = "out-dir")]
out_dir: Option<PathBuf>,
/// Location of WIT file(s) to generate bindings for.
///
/// This path can be either a directory containing `*.wit` files, a `*.wit`
/// file itself, or a `*.wasm` file which is a wasm-encoded WIT package.
/// Most of the time it's likely to be a directory containing `*.wit` files
/// with an optional `deps` folder inside of it.
#[clap(value_name = "WIT", index = 1)]
wit: PathBuf,
/// Optionally specified world that bindings are generated for.
///
/// Bindings are always generated for a world but this option can be omitted
/// when the WIT package pointed to by the `WIT` option only has a single
/// world. If there's more than one world in the package then this option
/// must be specified to name the world that bindings are generated for.
/// This option can also use the fully qualified syntax such as
/// `wasi:http/proxy` to select a world from a dependency of the main WIT
/// package.
#[clap(short, long)]
world: Option<String>,
/// Indicates that no files are written and instead files are checked if
/// they're up-to-date with the source files.
#[clap(long)]
check: bool,
/// Comma-separated list of features that should be enabled when processing
/// WIT files.
///
/// This enables using `@unstable` annotations in WIT files.
#[clap(long)]
features: Vec<String>,
/// Whether or not to activate all WIT features when processing WIT files.
///
/// This enables using `@unstable` annotations in WIT files.
#[clap(long)]
all_features: bool,
}
fn main() -> Result<()> {
let mut files = Files::default();
let (generator, opt) = match Opt::parse() {
#[cfg(feature = "markdown")]
Opt::Markdown { opts, args } => (opts.build(), args),
#[cfg(feature = "moonbit")]
Opt::Moonbit { opts, args } => (opts.build(), args),
#[cfg(feature = "c")]
Opt::C { opts, args } => (opts.build(), args),
#[cfg(feature = "rust")]
Opt::Rust { opts, args } => (opts.build(), args),
#[cfg(feature = "teavm-java")]
Opt::TeavmJava { opts, args } => (opts.build(), args),
#[cfg(feature = "go")]
Opt::TinyGo { opts, args } => (opts.build(), args),
#[cfg(feature = "csharp")]
Opt::CSharp { opts, args } => (opts.build(), args),
};
gen_world(generator, &opt, &mut files).map_err(attach_with_context)?;
for (name, contents) in files.iter() {
let dst = match &opt.out_dir {
Some(path) => path.join(name),
None => name.into(),
};
eprintln!("Generating {:?}", dst);
if opt.check {
let prev = std::fs::read(&dst).with_context(|| format!("failed to read {:?}", dst))?;
if prev != contents {
// The contents differ. If it looks like textual contents, do a
// line-by-line comparison so that we can tell users what the
// problem is directly.
if let (Ok(utf8_prev), Ok(utf8_contents)) =
(str::from_utf8(&prev), str::from_utf8(contents))
{
if !utf8_prev
.chars()
.any(|c| c.is_control() && !matches!(c, '\n' | '\r' | '\t'))
&& utf8_prev.lines().eq(utf8_contents.lines())
{
bail!("{} differs only in line endings (CRLF vs. LF). If this is a text file, configure git to mark the file as `text eol=lf`.", dst.display());
}
}
// The contents are binary or there are other differences; just
// issue a generic error.
bail!("not up to date: {}", dst.display());
}
continue;
}
if let Some(parent) = dst.parent() {
std::fs::create_dir_all(parent)
.with_context(|| format!("failed to create {:?}", parent))?;
}
std::fs::write(&dst, contents).with_context(|| format!("failed to write {:?}", dst))?;
}
Ok(())
}
fn attach_with_context(err: Error) -> Error {
#[cfg(feature = "rust")]
if let Some(e) = err.downcast_ref::<wit_bindgen_rust::MissingWith>() {
let option = e.0.clone();
return err.context(format!(
"missing either `--generate-all` or `--with {option}=(...|generate)`"
));
}
err
}
fn gen_world(
mut generator: Box<dyn WorldGenerator>,
opts: &Common,
files: &mut Files,
) -> Result<()> {
let mut resolve = Resolve::default();
resolve.all_features = opts.all_features;
for features in opts.features.iter() {
for feature in features
.split(',')
.flat_map(|s| s.split_whitespace())
.filter(|f| !f.is_empty())
{
resolve.features.insert(feature.to_string());
}
}
let (pkg, _files) = resolve.push_path(&opts.wit)?;
let world = resolve.select_world(pkg, opts.world.as_deref())?;
generator.generate(&resolve, world, files)?;
Ok(())
}
#[test]
fn verify_cli() {
use clap::CommandFactory;
Opt::command().debug_assert()
}