]> git.proxmox.com Git - wasi-libc.git/blame - tools/wasi-headers/src/main.rs
Improvements to wasi-headers tool (#160)
[wasi-libc.git] / tools / wasi-headers / src / main.rs
CommitLineData
ec86d4de
PH
1#[macro_use]
2extern crate clap;
3
cd74e1d9 4use anyhow::Result;
ec86d4de 5use clap::{Arg, SubCommand};
446cb3f1
DG
6use std::fs::File;
7use std::io::Write;
ec86d4de
PH
8use std::path::PathBuf;
9use wasi_headers::{generate, libc_wasi_api_header, snapshot_witx_files};
10
11struct GenerateCommand {
12 /// Input witx file
13 inputs: Vec<PathBuf>,
14 /// Output header file
15 output: PathBuf,
16}
17
18impl 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
27fn 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}