|
| 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 | +for system in SYSTEMS: |
| 49 | + for version in VERSIONS: |
| 50 | + target = compute_target(system, version) |
| 51 | + generic_sources = list((BASE_DIR / "src").glob("*.c")) |
| 52 | + target_sources = list((BASE_DIR / "src" / target).glob("*.c")) |
| 53 | + |
| 54 | + target_dir = BASE_DIR / "testsuite" / target |
| 55 | + target_dir.mkdir(parents=True, exist_ok=True) |
| 56 | + target_args = [f"--target={compute_cc_target(system, version)}"] |
| 57 | + |
| 58 | + manifest = {'name': f"WASI C tests [{target}]"} |
| 59 | + Path(target_dir / "manifest.json").write_text(json.dumps(manifest)) |
| 60 | + |
| 61 | + for src in generic_sources + target_sources: |
| 62 | + dst = (target_dir / src.name).with_suffix(".wasm") |
| 63 | + if needs_rebuild(dst, src): |
| 64 | + print(f"building testsuite/{target}/{dst.name}") |
| 65 | + src_json = src.with_suffix(".json") |
| 66 | + if src_json.exists(): |
| 67 | + dst_json = dst.with_suffix(".json") |
| 68 | + with src_json.open() as f: |
| 69 | + for d in json.load(f).get('dirs', []): |
| 70 | + src_dir = src.parent / d |
| 71 | + dst_dir = dst.parent / d |
| 72 | + if args.verbose: |
| 73 | + print(f"cp --recursive {src_dir} {dst_dir}") |
| 74 | + if not args.dry_run: |
| 75 | + shutil.copytree(src_dir, dst_dir, |
| 76 | + dirs_exist_ok=True) |
| 77 | + if args.verbose: |
| 78 | + print(f"cp {src_json} {dst_json}") |
| 79 | + if not args.dry_run: |
| 80 | + shutil.copy(src_json, dst_json) |
| 81 | + build_cmd = CC + target_args + [src] + ['-o'] + [dst] |
| 82 | + command_line = shlex.join([str(x) for x in build_cmd]) |
| 83 | + if args.verbose: |
| 84 | + print(command_line) |
| 85 | + if not args.dry_run: |
| 86 | + r = subprocess.run(build_cmd) |
| 87 | + if r.returncode != 0: |
| 88 | + sys.exit(r.returncode) |
0 commit comments