Executes the command.
(mut self)
| 50 | impl CompileCommand { |
| 51 | /// Executes the command. |
| 52 | pub fn execute(mut self) -> Result<()> { |
| 53 | self.common.init_logging()?; |
| 54 | |
| 55 | let mut config = self.common.config(None)?; |
| 56 | |
| 57 | if let Some(path) = self.emit_clif { |
| 58 | if !path.exists() { |
| 59 | std::fs::create_dir(&path)?; |
| 60 | } |
| 61 | |
| 62 | if !path.is_dir() { |
| 63 | bail!( |
| 64 | "the path passed for '--emit-clif' ({}) must be a directory", |
| 65 | path.display() |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | config.emit_clif(&path); |
| 70 | } |
| 71 | |
| 72 | let engine = Engine::new(&config)?; |
| 73 | |
| 74 | if self.module.file_name().is_none() { |
| 75 | bail!( |
| 76 | "'{}' is not a valid input module path", |
| 77 | self.module.display() |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | let mut code = CodeBuilder::new(&engine); |
| 82 | code.wasm_binary_or_text_file(&self.module)?; |
| 83 | |
| 84 | let output = self.output.take().unwrap_or_else(|| { |
| 85 | let mut output: PathBuf = self.module.file_name().unwrap().into(); |
| 86 | output.set_extension("cwasm"); |
| 87 | output |
| 88 | }); |
| 89 | |
| 90 | let output_bytes = match code.hint() { |
| 91 | #[cfg(feature = "component-model")] |
| 92 | Some(CodeHint::Component) => code.compile_component_serialized()?, |
| 93 | #[cfg(not(feature = "component-model"))] |
| 94 | Some(CodeHint::Component) => { |
| 95 | bail!("component model support was disabled at compile time") |
| 96 | } |
| 97 | Some(CodeHint::Module) | None => code.compile_module_serialized()?, |
| 98 | }; |
| 99 | fs::write(&output, output_bytes) |
| 100 | .with_context(|| format!("failed to write output: {}", output.display()))?; |
| 101 | |
| 102 | Ok(()) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | #[cfg(all(test, not(miri)))] |