Skip to content

Commit cd74e1d

Browse files
authored
Correct the version of #136 on master (#141)
* Add the WASI repo as a submodule. Also, add the witx filenames to the generated output, and just have `cargo run` auto-generate the api.h header, rather than using clap. * Switch witx to a path dependency. * Add a test. * Add a test that the generated file is in sync with the generator. * Enable CI testing with Github Actions. * Fix the name of the wasi-headers directory. * Enable submodules. * Add a diff mechanism to help explain failures. * Sort the inputs for display. * More debugging. * More debugging. * Add a .gitattributes file forcing text files to be eol=lf. Most editors these days can deal with eof=lf files, even on Windows, and this avoids trouble with headers and other generated files differing in line endings.
1 parent 446cb3f commit cd74e1d

11 files changed

Lines changed: 183 additions & 76 deletions

File tree

.gitattributes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Se publish headers and other files from the repo, and we don't want
2+
# them differing depending on the line-ending style of the host they
3+
# were checked out on.
4+
* text eol=lf

.github/workflows/main.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: CI
2+
on: [push, pull_request]
3+
4+
jobs:
5+
test:
6+
name: Test
7+
runs-on: ${{ matrix.os }}
8+
strategy:
9+
matrix:
10+
os: [ubuntu-latest, macos-latest, windows-latest]
11+
steps:
12+
- uses: actions/checkout@master
13+
with:
14+
submodules: true
15+
- name: Install Rust (rustup)
16+
shell: bash
17+
run: rustup update stable --no-self-update && rustup default stable
18+
if: matrix.os != 'macos-latest'
19+
- name: Install Rust (macos)
20+
run: |
21+
curl https://sh.rustup.rs | sh -s -- -y
22+
echo "##[add-path]$HOME/.cargo/bin"
23+
if: matrix.os == 'macos-latest'
24+
- run: cargo fetch
25+
working-directory: tools/wasi-headers
26+
- run: cargo build
27+
working-directory: tools/wasi-headers
28+
- run: cargo test
29+
working-directory: tools/wasi-headers
30+
31+
rustfmt:
32+
name: Rustfmt
33+
runs-on: ubuntu-latest
34+
steps:
35+
- uses: actions/checkout@master
36+
with:
37+
submodules: true
38+
- name: Install Rust
39+
run: rustup update stable && rustup default stable && rustup component add rustfmt
40+
- run: cargo fmt -- --check
41+
working-directory: tools/wasi-headers

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "tools/wasi-headers/WASI"]
2+
path = tools/wasi-headers/WASI
3+
url = https://github.com/WebAssembly/WASI

libc-bottom-half/headers/public/wasi/api.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
/**
2-
* THIS FILE IS AUTO-GENERATED!
2+
* THIS FILE IS AUTO-GENERATED from the following files:
3+
* typenames.witx, wasi_snapshot_preview1.witx
34
*
45
* @file
5-
* This file describes the WASI interface, consisting of functions, types,
6+
* This file describes the [WASI] interface, consisting of functions, types,
67
* and defined values (macros).
78
*
89
* The interface described here is greatly inspired by [CloudABI]'s clean,
910
* thoughtfully-designed, cabability-oriented, POSIX-style API.
1011
*
1112
* [CloudABI]: https://github.com/NuxiNL/cloudlibc
13+
* [WASI]: https://github.com/WebAssembly/WASI/
1214
*/
1315

1416
#ifndef __wasi_api_h

tools/wasi-headers/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target
2+
Cargo.lock

tools/wasi-headers/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ edition = "2018"
77
publish = false
88

99
[dependencies]
10-
clap = "2"
1110
heck = "0.3.1"
12-
witx = "0.5.0"
11+
witx = { path = "WASI/tools/witx" }
12+
anyhow = "1.0.22"
13+
14+
[dev-dependencies]
15+
diff = "0.1.11"

tools/wasi-headers/WASI

Submodule WASI added at 9fc6370

tools/wasi-headers/src/c_header.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
use heck::ShoutySnakeCase;
22
use witx::*;
33

4-
const PROLOGUE: &str = r#"/**
5-
* THIS FILE IS AUTO-GENERATED!
4+
pub(crate) fn to_c_header(doc: &Document, inputs_str: &str) -> String {
5+
let mut ret = String::new();
6+
7+
ret.push_str(&format!(
8+
r#"/**
9+
* THIS FILE IS AUTO-GENERATED from the following files:
10+
* {}
611
*
712
* @file
8-
* This file describes the WASI interface, consisting of functions, types,
13+
* This file describes the [WASI] interface, consisting of functions, types,
914
* and defined values (macros).
1015
*
1116
* The interface described here is greatly inspired by [CloudABI]'s clean,
1217
* thoughtfully-designed, cabability-oriented, POSIX-style API.
1318
*
1419
* [CloudABI]: https://github.com/NuxiNL/cloudlibc
20+
* [WASI]: https://github.com/WebAssembly/WASI/
1521
*/
1622
1723
#ifndef __wasi_api_h
@@ -34,23 +40,14 @@ _Static_assert(_Alignof(int64_t) == 8, "non-wasi data layout");
3440
_Static_assert(_Alignof(uint64_t) == 8, "non-wasi data layout");
3541
3642
#ifdef __cplusplus
37-
extern "C" {
43+
extern "C" {{
3844
#endif
3945
4046
// TODO: Encoding this in witx.
4147
#define __WASI_DIRCOOKIE_START (UINT64_C(0))
42-
"#;
43-
44-
const EPILOGUE: &str = r#"#ifdef __cplusplus
45-
}
46-
#endif
47-
48-
#endif"#;
49-
50-
pub fn to_c_header(doc: &Document) -> String {
51-
let mut ret = String::new();
52-
53-
ret.push_str(PROLOGUE);
48+
"#,
49+
inputs_str,
50+
));
5451

5552
for d in doc.datatypes() {
5653
print_datatype(&mut ret, &*d);
@@ -60,7 +57,14 @@ pub fn to_c_header(doc: &Document) -> String {
6057
print_module(&mut ret, doc, &m);
6158
}
6259

63-
ret.push_str(EPILOGUE);
60+
ret.push_str(
61+
r#"#ifdef __cplusplus
62+
}
63+
#endif
64+
65+
#endif
66+
"#,
67+
);
6468

6569
ret
6670
}

tools/wasi-headers/src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
mod c_header;
2+
3+
use anyhow::{anyhow, Result};
4+
use c_header::to_c_header;
5+
use std::fs::read_dir;
6+
use std::io;
7+
use witx::load;
8+
9+
pub fn generate() -> Result<String> {
10+
let mut inputs = read_dir("WASI/phases/snapshot/witx")?
11+
.map(|res| res.map(|e| e.path()))
12+
.collect::<Result<Vec<_>, io::Error>>()?;
13+
14+
inputs.sort();
15+
16+
// TODO: drop the anyhow! part once witx switches to anyhow.
17+
let doc = load(&inputs).map_err(|e| anyhow!(e.to_string()))?;
18+
19+
let inputs_str = &inputs
20+
.iter()
21+
.map(|p| p.file_name().unwrap().to_str().unwrap().to_string())
22+
.collect::<Vec<_>>()
23+
.join(", ");
24+
25+
Ok(to_c_header(&doc, &inputs_str))
26+
}

tools/wasi-headers/src/main.rs

Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,11 @@
1-
mod c_header;
2-
3-
use crate::c_header::to_c_header;
4-
use clap::{App, Arg};
1+
use anyhow::Result;
52
use std::fs::File;
63
use std::io::Write;
7-
use std::path::PathBuf;
8-
use std::process;
9-
use witx::load;
10-
11-
pub fn main() {
12-
let app = App::new("wasi-headers")
13-
.version(env!("CARGO_PKG_VERSION"))
14-
.about("Generate C headers for WASI interfaces")
15-
.arg(
16-
Arg::with_name("input")
17-
.required(true)
18-
.multiple(true)
19-
.help("path to root of witx document"),
20-
)
21-
.arg(
22-
Arg::with_name("verbose")
23-
.short("v")
24-
.long("verbose")
25-
.takes_value(false)
26-
.required(false),
27-
)
28-
.get_matches();
29-
30-
let inputs = app
31-
.values_of("input")
32-
.expect("at least one input required")
33-
.map(PathBuf::from)
34-
.collect::<Vec<PathBuf>>();
35-
36-
match load(&inputs) {
37-
Ok(doc) => {
38-
if app.is_present("verbose") {
39-
println!("{:?}", doc)
40-
}
4+
use wasi_headers::generate;
415

42-
let c_header = to_c_header(&doc);
43-
if let Some(output) = app.value_of("output") {
44-
let mut file = File::create(output).expect("create output file");
45-
file.write_all(c_header.as_bytes())
46-
.expect("write output file");
47-
} else {
48-
println!("{}", c_header)
49-
}
50-
}
51-
Err(e) => {
52-
println!("{}", e.report());
53-
if app.is_present("verbose") {
54-
println!("{:?}", e);
55-
}
56-
process::exit(1)
57-
}
58-
}
6+
pub fn main() -> Result<()> {
7+
let c_header = generate()?;
8+
let mut file = File::create("../../libc-bottom-half/headers/public/wasi/api.h")?;
9+
file.write_all(c_header.as_bytes())?;
10+
Ok(())
5911
}

0 commit comments

Comments
 (0)