kit

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

x64_macos.sh (5030B)


      1 #!/usr/bin/env bash
      2 # test/smoke/x64_macos.sh — end-to-end smoke test for kit's x86_64-macos
      3 # (Mach-O) compile→link→execute path, on the shared Type-K mode-P kit
      4 # (test/lib/kit_sh_kit.sh).
      5 #
      6 # Unlike test/smoke/x64.sh (which proves the harness can run an x86_64-LINUX
      7 # ELF built by clang), this exercises kit's OWN x86_64 Mach-O codegen and
      8 # linker: kit compiles a hello-world to a CPU_TYPE_X86_64 Mach-O object, kit
      9 # links it into a Mach-O executable (driving the __stubs/__got import path —
     10 # x64_emit_macho_stub), and the binary runs via Rosetta 2 on an Apple-silicon
     11 # host. It asserts both stdout content and exit code, on the synchronous
     12 # (exec_target_run) and batched (exec_target_queue+flush) paths.
     13 #
     14 # Skipped unless: host is Darwin, kit is built, Rosetta 2 is installed (so an
     15 # arm64 host can run x86_64), and a macOS SDK is discoverable via xcrun (kit's
     16 # Darwin hosted profile needs a sysroot). Skip is a failure unless
     17 # KIT_TEST_ALLOW_SKIP=1 (the shared kit_exit honors it).
     18 
     19 set -u
     20 
     21 ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
     22 KIT="${KIT:-$ROOT/build/kit}"
     23 BUILD_DIR="$ROOT/build/test/smoke-x64-macos"
     24 mkdir -p "$BUILD_DIR"
     25 
     26 KIT_KIT_DIR="$ROOT/test/lib"
     27 # shellcheck source=../lib/kit_sh_kit.sh
     28 . "$ROOT/test/lib/kit_sh_kit.sh"
     29 kit_report_init
     30 
     31 # A SKIP is a failure unless KIT_TEST_ALLOW_SKIP=1, so CI catches a degraded run.
     32 KIT_SKIP_IS_FAILURE=1
     33 
     34 work="$BUILD_DIR"
     35 
     36 # ---- detect prerequisites --------------------------------------------------
     37 
     38 # Variables consumed by exec_target.sh; none of qemu/podman apply to a native
     39 # Mach-O run, but the helper reads them unconditionally.
     40 have_qemu=0
     41 QEMU_BIN=""
     42 have_podman=0
     43 arch_raw="$(uname -m 2>/dev/null || true)"
     44 is_aarch64=0
     45 { [ "$arch_raw" = "aarch64" ] || [ "$arch_raw" = "arm64" ]; } && is_aarch64=1
     46 export have_qemu QEMU_BIN have_podman is_aarch64
     47 EXEC_TARGET_MOUNT_ROOT="$BUILD_DIR"
     48 export EXEC_TARGET_MOUNT_ROOT
     49 # shellcheck source=../lib/exec_target.sh
     50 . "$ROOT/test/lib/exec_target.sh"
     51 
     52 if [ "$(uname -s 2>/dev/null)" != "Darwin" ]; then
     53     skip_test "exec" "x86_64-macos exec requires a Darwin host"
     54     kit_summary test-smoke-x64-macos; kit_exit
     55 fi
     56 if [ ! -x "$KIT" ]; then
     57     skip_test "build" "$KIT not built (run 'make bin')"
     58     kit_summary test-smoke-x64-macos; kit_exit
     59 fi
     60 # exec_target_supported x64-macos is true on an x86_64 host, or on an arm64
     61 # host with Rosetta 2 (the case this lane is built for).
     62 if ! exec_target_supported x64-macos; then
     63     skip_test "exec" "no x86_64-macos runner (need x86_64 host or Rosetta 2)"
     64     kit_summary test-smoke-x64-macos; kit_exit
     65 fi
     66 SDK="$(xcrun --sdk macosx --show-sdk-path 2>/dev/null || true)"
     67 if [ -z "$SDK" ]; then
     68     skip_test "build" "no macOS SDK (xcrun --show-sdk-path empty)"
     69     kit_summary test-smoke-x64-macos; kit_exit
     70 fi
     71 
     72 # ---- build: kit -> x86_64 Mach-O executable --------------------------------
     73 
     74 # Exercises the import path (printf → __stubs → __got → dyld bind on libSystem)
     75 # plus a non-trivial exit code. Returning 42 mirrors the other smoke lanes.
     76 SRC="$BUILD_DIR/smoke.c"
     77 cat >"$SRC" <<'EOF'
     78 #include <stdio.h>
     79 int main(void) {
     80   printf("kit-x64-macos-ok\n");
     81   return 42;
     82 }
     83 EOF
     84 
     85 EXE="$BUILD_DIR/smoke"
     86 if ! "$KIT" cc -target x86_64-macos --sysroot "$SDK" -lc \
     87         "$SRC" -o "$EXE" 2>"$BUILD_DIR/build.err"; then
     88     not_ok "build" "$BUILD_DIR/build.err"
     89     kit_summary test-smoke-x64-macos; kit_exit
     90 fi
     91 # Confirm we actually produced an x86_64 Mach-O, not a host-arch binary.
     92 if ! file "$EXE" 2>/dev/null | grep -q "Mach-O.*x86_64"; then
     93     file "$EXE" > "$BUILD_DIR/file.diag" 2>&1
     94     not_ok "build (x86_64 Mach-O)" "$BUILD_DIR/file.diag"
     95     kit_summary test-smoke-x64-macos; kit_exit
     96 fi
     97 ok "build (x86_64 Mach-O executable)"
     98 
     99 # ---- exec_target_run --------------------------------------------------------
    100 
    101 exec_target_run x64-macos "$EXE" "$BUILD_DIR/run.out" "$BUILD_DIR/run.err"
    102 if [ "$RUN_RC" -eq 42 ] && grep -q "kit-x64-macos-ok" "$BUILD_DIR/run.out"; then
    103     ok "exec_target_run x64-macos (rc=42, stdout)"
    104 else
    105     echo "expected rc=42 + stdout marker; got rc=$RUN_RC out=[$(cat "$BUILD_DIR/run.out" 2>/dev/null)] err=$BUILD_DIR/run.err" \
    106         > "$BUILD_DIR/run.diag"
    107     not_ok "exec_target_run x64-macos" "$BUILD_DIR/run.diag"
    108 fi
    109 
    110 # ---- exec_target_queue + flush ----------------------------------------------
    111 
    112 exec_target_queue x64-macos smoke "$EXE" \
    113     "$BUILD_DIR/q.out" "$BUILD_DIR/q.err" "$BUILD_DIR/q.rc"
    114 exec_target_flush
    115 if [ ! -f "$BUILD_DIR/q.rc" ]; then
    116     echo "no rc file produced" > "$BUILD_DIR/q.diag"
    117     not_ok "exec_target_queue+flush x64-macos" "$BUILD_DIR/q.diag"
    118 else
    119     Q_RC="$(cat "$BUILD_DIR/q.rc")"
    120     if [ "$Q_RC" -eq 42 ] && grep -q "kit-x64-macos-ok" "$BUILD_DIR/q.out"; then
    121         ok "exec_target_queue+flush x64-macos (rc=42, stdout)"
    122     else
    123         echo "expected rc=42 + stdout marker; got rc=$Q_RC; see $BUILD_DIR/q.err" \
    124             > "$BUILD_DIR/q.diag"
    125         not_ok "exec_target_queue+flush x64-macos" "$BUILD_DIR/q.diag"
    126     fi
    127 fi
    128 
    129 kit_summary test-smoke-x64-macos
    130 kit_exit