|
| 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]) |
0 commit comments