kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

run.sh (4151B)


      1 #!/bin/sh
      2 # Driver-level checks for `kit compile` — the kit-native, single-language
      3 # source compiler. Self-checking (no golden files): we assert exit status,
      4 # output existence, and a few symbol/text markers via the shared kit_* verbs.
      5 #
      6 # Coverage: per-language compile (C / toy / wasm), the emit modes
      7 # (-S / --emit=c / --emit=ir), check-only, default output naming, the target
      8 # feature flag path, and the negative paths that exercise the capability gate,
      9 # the target/frontend option parsers, and the no-link policy.
     10 
     11 set -u
     12 
     13 script_dir=$(cd "$(dirname "$0")" && pwd)
     14 repo_root=$(cd "$script_dir/../.." && pwd)
     15 
     16 KIT="${KIT:-$repo_root/build/kit}"
     17 
     18 if [ ! -x "$KIT" ]; then
     19     echo "compile: kit binary not found at $KIT" >&2
     20     exit 2
     21 fi
     22 
     23 work=$(mktemp -d "${TMPDIR:-/tmp}/kit-compile-test.XXXXXX")
     24 trap 'rm -rf "$work"' EXIT
     25 
     26 KIT_KIT_DIR="$repo_root/test/lib"
     27 . "$repo_root/test/lib/kit_sh_kit.sh"
     28 kit_report_init
     29 
     30 # ---- fixtures (all local to $work so default-name tests are isolated) -------
     31 mkdir -p "$work/inc"
     32 printf '#define ZERO 0\n' > "$work/inc/h.h"
     33 printf '#include "h.h"\nint main(void){return ZERO;}\n' > "$work/hello.c"
     34 printf '#include <stdint.h>\nint32_t f(void){return 1;}\n' > "$work/std.c"
     35 cp "$(ls "$repo_root"/test/toy/cases/*.toy | head -1)" "$work/prog.toy"
     36 cp "$repo_root/test/wasm/cases/if_return.wat" "$work/prog.wat"
     37 
     38 cd "$work"
     39 
     40 # ---- C: preprocessor flags + object output ---------------------------------
     41 run_ok c-compile-obj "$KIT" compile -c -Iinc -DUNUSED=1 hello.c -o hello.o
     42 assert_file_exists c-obj-exists hello.o
     43 "$KIT" nm hello.o > nm.out 2>/dev/null
     44 contains c-obj-has-main nm.out main
     45 
     46 # C freestanding system header (<stdint.h>) resolves via the rt include set.
     47 run_ok c-freestanding-header "$KIT" compile -c std.c -o std.o
     48 
     49 # Default output name: <basename>.o next to the cwd when -o is omitted.
     50 run_ok c-default-name "$KIT" compile -c -Iinc hello.c
     51 assert_file_exists c-default-name-file hello.o
     52 
     53 # ---- toy + wasm: non-C frontends through the generic driver -----------------
     54 run_ok toy-compile "$KIT" compile -c prog.toy -o prog_toy.o
     55 assert_file_exists toy-obj-exists prog_toy.o
     56 
     57 run_ok wasm-compile "$KIT" compile -c prog.wat -o prog_wat.o
     58 assert_file_exists wasm-obj-exists prog_wat.o
     59 
     60 # Target-owned option: the driver lowers feature flags into KitTargetOptions.
     61 run_ok wasm-feature-flag "$KIT" compile -target wasm32-none -c \
     62     -mattr=-tail-calls prog.wat -o prog_wat2.o
     63 
     64 # ---- emit modes ------------------------------------------------------------
     65 run_ok emit-asm "$KIT" compile -S -Iinc hello.c -o hello.s
     66 contains emit-asm-text hello.s .text
     67 run_ok emit-c "$KIT" compile --emit=c prog.toy -o prog.c
     68 assert_file_exists emit-c-file prog.c
     69 run_ok emit-ir "$KIT" compile --emit=ir -O1 prog.toy -o prog.ir
     70 assert_file_exists emit-ir-file prog.ir
     71 
     72 # ---- check-only writes no object -------------------------------------------
     73 rm -f check.o
     74 run_ok check-only "$KIT" compile -fsyntax-only -Iinc hello.c
     75 if [ ! -f check.o ] && [ ! -f hello_check.o ]; then ok check-only-no-output
     76 else not_ok check-only-no-output; fi
     77 
     78 # ---- negative paths --------------------------------------------------------
     79 # Preprocessor flag on a non-preprocessor frontend is rejected by the gate.
     80 run_fail neg-cpp-on-wasm "$KIT" compile -Iinc -c prog.wat -o x.o
     81 # A frontend with no option parser rejects any leftover flag.
     82 run_fail neg-unknown-toy-flag "$KIT" compile --bogus -c prog.toy -o x.o
     83 # The target parser rejects an unknown feature name.
     84 run_fail neg-bad-target-feature "$KIT" compile -target wasm32-none \
     85     -mattr=+nope -c prog.wat -o x.o
     86 # compile never links: object/archive inputs are refused.
     87 run_fail neg-link-input "$KIT" compile hello.o
     88 # One language per invocation.
     89 run_fail neg-mixed-language "$KIT" compile prog.toy prog.wat
     90 # --emit=c needs an explicit destination.
     91 run_fail neg-emit-c-needs-o "$KIT" compile --emit=c prog.toy
     92 # IR is only available with the optimizer on.
     93 run_fail neg-ir-needs-opt "$KIT" compile --emit=ir prog.toy -o x.ir
     94 # -o cannot fan out across multiple sources.
     95 run_fail neg-o-multi "$KIT" compile -c hello.c std.c -o both.o
     96 
     97 kit_summary compile-driver
     98 kit_exit