]>
Commit | Line | Data |
---|---|---|
ec86d4de PH |
1 | #[macro_use] |
2 | extern crate clap; | |
3 | ||
cd74e1d9 | 4 | use anyhow::Result; |
ec86d4de | 5 | use clap::{Arg, SubCommand}; |
446cb3f1 DG |
6 | use std::fs::File; |
7 | use std::io::Write; | |
ec86d4de PH |
8 | use std::path::PathBuf; |
9 | use wasi_headers::{generate, libc_wasi_api_header, snapshot_witx_files}; | |
10 | ||
11 | struct GenerateCommand { | |
12 | /// Input witx file | |
13 | inputs: Vec<PathBuf>, | |
14 | /// Output header file | |
15 | output: PathBuf, | |
16 | } | |
17 | ||
18 | impl GenerateCommand { | |
19 | pub fn execute(&self) -> Result<()> { | |
20 | let c_header = generate(&self.inputs)?; | |
21 | let mut file = File::create(&self.output)?; | |
22 | file.write_all(c_header.as_bytes())?; | |
23 | Ok(()) | |
24 | } | |
25 | } | |
26 | ||
27 | fn main() -> Result<()> { | |
28 | let matches = app_from_crate!() | |
29 | .arg(Arg::with_name("inputs").required(true).multiple(true)) | |
30 | .arg( | |
31 | Arg::with_name("output") | |
32 | .short("o") | |
33 | .long("output") | |
34 | .takes_value(true) | |
35 | .required(true), | |
36 | ) | |
37 | .subcommand( | |
38 | SubCommand::with_name("generate-libc") | |
39 | .about("generate libc api.h from current snapshot"), | |
40 | ) | |
41 | .get_matches(); | |
42 | ||
43 | let cmd = if matches.subcommand_matches("generate-libc").is_some() { | |
44 | let inputs = snapshot_witx_files()?; | |
45 | let output = libc_wasi_api_header(); | |
46 | GenerateCommand { inputs, output } | |
47 | } else { | |
48 | GenerateCommand { | |
49 | inputs: matches | |
50 | .values_of("inputs") | |
51 | .expect("inputs required") | |
52 | .map(PathBuf::from) | |
53 | .collect(), | |
54 | output: PathBuf::from(matches.value_of("output").expect("output required")), | |
55 | } | |
56 | }; | |
446cb3f1 | 57 | |
ec86d4de | 58 | cmd.execute()?; |
cd74e1d9 | 59 | Ok(()) |
446cb3f1 | 60 | } |