MCPcopy Create free account
hub / github.com/bytecodealliance/wit-bindgen / compile

Method compile

crates/test/src/rust.rs:166–223  ·  view source on GitHub ↗
(&self, runner: &Runner, compile: &Compile)

Source from the content-addressed store, hash-verified

164 }
165
166 fn compile(&self, runner: &Runner, compile: &Compile) -> Result<()> {
167 let config = compile.component.deserialize_lang_config::<RustConfig>()?;
168
169 // If this rust target doesn't natively produce a component then place
170 // the compiler output in a temporary location which is componentized
171 // later on.
172 let output = compile.output.with_extension("core.wasm");
173
174 // Compile all extern crates, if any
175 let mut externs = Vec::new();
176 let manifest_dir = compile.component.path.parent().unwrap();
177
178 let rustc = |path: &Path, output: &Path| {
179 // Compile the main crate, passing `--extern` for all upstream crates.
180 let mut cmd = runner.rustc(Edition::E2021);
181 cmd.env("CARGO_MANIFEST_DIR", manifest_dir)
182 .arg(path)
183 .arg("-o")
184 .arg(&output);
185 for flag in Vec::from(config.rustflags.clone()) {
186 cmd.arg(flag);
187 }
188 cmd
189 };
190
191 for file in config.externs.iter() {
192 let file = manifest_dir.join(file);
193 let stem = file.file_stem().unwrap().to_str().unwrap();
194 let output = compile.artifacts_dir.join(format!("lib{stem}.rlib"));
195 runner.run_command(rustc(&file, &output).arg("--crate-type=rlib"))?;
196 externs.push((stem.to_string(), output));
197 }
198
199 // Compile the main crate, passing `--extern` for all upstream crates.
200 let mut cmd = rustc(&compile.component.path, &output);
201 cmd.env(
202 "BINDINGS",
203 compile.bindings_dir.join(format!(
204 "{}.rs",
205 compile.component.bindgen.world.replace('-', "_")
206 )),
207 );
208 for (name, path) in externs {
209 let arg = format!("--extern={name}={}", path.display());
210 cmd.arg(arg);
211 }
212 cmd.arg("--crate-type=cdylib");
213 if runner.produces_component() {
214 cmd.arg("-Clink-arg=--skip-wit-component");
215 }
216 runner.run_command(&mut cmd)?;
217
218 runner
219 .convert_p1_to_component(&output, compile)
220 .with_context(|| format!("failed to convert {output:?}"))?;
221
222 Ok(())
223 }

Callers

nothing calls this directly

Calls 9

newFunction · 0.85
rustcMethod · 0.80
joinMethod · 0.80
run_commandMethod · 0.80
produces_componentMethod · 0.80
iterMethod · 0.45
pushMethod · 0.45
to_stringMethod · 0.45

Tested by

no test coverage detected