Skip to content

Commit 7db37c0

Browse files
committed
Build C test cases via Python script to target-specific dir
We move all the C sources from tests/c/testsuite to tests/c/src, somewhat like the Rust tests, then treat tests/c/testsuite as a built directory. The build script is rewritten to Python, as it is getting a bit more complicated. Tests are compiled into target-specific dirs, currently only tests/c/testsuite/wasm32-wasip1. The build script writes the version-specific manifest.json, so as to include the target in the test suite name. Workflows updated appropriately.
1 parent 9315be9 commit 7db37c0

34 files changed

+111
-22
lines changed

.github/workflows/compile-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ jobs:
121121
- name: Build tests
122122
shell: bash
123123
working-directory: tests/c
124-
run: CC="./wasi-sdk-${WASI_VERSION}.0-${SYSTEM_NAME}/bin/clang" ./build.sh
124+
run: CC="./wasi-sdk-${WASI_VERSION}.0-${SYSTEM_NAME}/bin/clang" ./build.py
125125

126126
- name: Upload precompiled tests
127127
if: matrix.os == 'ubuntu-latest'

.github/workflows/daily-runtime-validation.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ jobs:
7878
--json-output-location results.json \
7979
-t tests/assemblyscript/testsuite \
8080
tests/rust/testsuite \
81-
tests/c/testsuite
81+
tests/c/testsuite/wasm32-wasip1
8282
8383
- name: Configure git
8484
uses: ./.github/actions/git-config

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.wasm
2-
.DS_Store
2+
.DS_Store
3+
/tests/c/testsuite/

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ executor is quite simple; see the [specification] document for the details and t
4646
```bash
4747
python3 test-runner/wasi_test_runner.py \
4848
-t ./tests/assemblyscript/testsuite/ `# path to folders containing .wasm test files` \
49-
./tests/c/testsuite/ \
49+
./tests/c/testsuite/wasm32-wasip1 \
5050
./tests/rust/testsuite/ \
5151
-r adapters/wasmtime.py # path to a runtime adapter
5252
```
@@ -56,7 +56,7 @@ Optionally you can specify test cases to skip with the `--exclude-filter` option
5656
```bash
5757
python3 test-runner/wasi_test_runner.py \
5858
-t ./tests/assemblyscript/testsuite/ `# path to folders containing .wasm test files` \
59-
./tests/c/testsuite/ \
59+
./tests/c/testsuite/wasm32-wasip1 \
6060
./tests/rust/testsuite/ \
6161
--exclude-filter examples/skip.json \
6262
-r adapters/wasmtime.py # path to a runtime adapter
@@ -95,7 +95,7 @@ Here is some additional information for developers who are willing to contribute
9595

9696
### Cleaning up temporary resources
9797

98-
Some of the tests (e.g. [pwrite-with-access](./tests/c/testsuite/pwrite-with-access.c)) generate
98+
Some of the tests (e.g. [pwrite-with-access](./tests/c/src/pwrite-with-access.c)) generate
9999
output artifacts and their existence can affect consecutive test executions. Tests should clean up
100100
the artifacts they generate, but there might be cases where the test fails early. The test runner
101101
will automatically delete all the files and directories in the test suite directory with the

tests/c/build.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/usr/bin/env python3
2+
3+
import argparse
4+
import json
5+
import os
6+
import shlex
7+
import shutil
8+
import subprocess
9+
import sys
10+
from pathlib import Path
11+
from math import inf
12+
13+
# shlex.split() splits according to shell quoting rules
14+
CC = shlex.split(os.getenv("CC", "clang"))
15+
16+
parser = argparse.ArgumentParser()
17+
parser.add_argument("--dry-run", action="store_true")
18+
parser.add_argument("--verbose", action="store_true")
19+
20+
args = parser.parse_args()
21+
22+
SYSTEMS = ['wasm32']
23+
VERSIONS = ['wasip1'] # + ['wasip2', 'wasip3']
24+
25+
def compute_target(system, version):
26+
return f"{system}-{version}"
27+
28+
def compute_cc_target(system, version):
29+
if version == 'wasip3':
30+
# wasm32-wasip3 triple not yet supported.
31+
return compute_target(system, 'wasip2')
32+
return compute_target(system, version)
33+
34+
BASE_DIR = Path(__file__).parent
35+
36+
def maybe_stat(path, default):
37+
try:
38+
return path.stat().st_mtime
39+
except FileNotFoundError:
40+
return default
41+
42+
def needs_rebuild(dst, src):
43+
if maybe_stat(dst, 0) < src.stat().st_mtime:
44+
return True
45+
return (maybe_stat(dst.with_suffix(".json"), -1)
46+
< maybe_stat(src.with_suffix(".json"), -inf))
47+
48+
def run(argv):
49+
if args.verbose:
50+
print(shlex.join([str(x) for x in argv]))
51+
if not args.dry_run:
52+
r = subprocess.run(argv)
53+
if r.returncode != 0:
54+
sys.exit(r.returncode)
55+
56+
def cp(src, dst):
57+
if args.verbose:
58+
print(f"cp {src} {dst}")
59+
if not args.dry_run:
60+
shutil.copy(src, dst)
61+
62+
def cp_R(src, dst):
63+
if args.verbose:
64+
print(f"cp -R {src} {dst}")
65+
if not args.dry_run:
66+
shutil.copytree(src, dst, dirs_exist_ok=True)
67+
68+
def write_manifest(path, manifest):
69+
if args.verbose:
70+
print(f"writing {path}")
71+
if not args.dry_run:
72+
path.write_text(json.dumps(manifest))
73+
74+
def mkdir_p(path):
75+
if args.verbose:
76+
print(f"mkdir -p {path}")
77+
if not args.dry_run:
78+
path.mkdir(parents=True, exist_ok=True)
79+
80+
for system in SYSTEMS:
81+
for version in VERSIONS:
82+
target = compute_target(system, version)
83+
generic_sources = list((BASE_DIR / "src").glob("*.c"))
84+
target_sources = list((BASE_DIR / "src" / target).glob("*.c"))
85+
86+
target_dir = BASE_DIR / "testsuite" / target
87+
mkdir_p(target_dir)
88+
target_args = [f"--target={compute_cc_target(system, version)}"]
89+
90+
write_manifest(target_dir / "manifest.json",
91+
{'name': f"WASI C tests [{target}]"})
92+
93+
for src in generic_sources + target_sources:
94+
dst = (target_dir / src.name).with_suffix(".wasm")
95+
if needs_rebuild(dst, src):
96+
print(f"building testsuite/{target}/{dst.name}")
97+
src_json = src.with_suffix(".json")
98+
if src_json.exists():
99+
dst_json = dst.with_suffix(".json")
100+
with src_json.open() as f:
101+
for d in json.load(f).get('dirs', []):
102+
cp_R(src.parent / d, dst.parent / d)
103+
cp(src_json, dst_json)
104+
run(CC + target_args + [src] + ['-o'] + [dst])

tests/c/build.sh

Lines changed: 0 additions & 13 deletions
This file was deleted.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)