native_macos_sdk.sh (6192B)
1 #!/bin/sh 2 # Native macOS hosted-SDK policy and precedence acceptance (KIT-P0-002). 3 4 set -u 5 6 script_dir=$(CDPATH= cd -- "$(dirname "$0")" && pwd) 7 repo_root=$(CDPATH= cd -- "$script_dir/../.." && pwd) 8 KIT=${KIT:-$repo_root/build/kit} 9 KIT_KIT_DIR=$repo_root/test/lib 10 . "$repo_root/test/lib/kit_sh_kit.sh" 11 12 kit_require_kit native-macos-sdk 13 kit_report_init 14 kit_workdir native-macos-sdk 15 work=$KIT_WORK 16 17 if [ "$(uname -s 2>/dev/null || printf unknown)" != Darwin ]; then 18 skip_test native-macos-sdk "requires a Darwin host with a macOS SDK" 19 kit_summary native-macos-sdk 20 kit_exit 21 fi 22 23 mkdir -p "$work/sentinel-bin" 24 cat > "$work/sentinel-bin/xcrun" <<'EOF' 25 #!/bin/sh 26 printf 'xcrun invoked: %s\n' "$*" >> "$KIT_XCRUN_SENTINEL_LOG" 27 exit 99 28 EOF 29 chmod +x "$work/sentinel-bin/xcrun" 30 : > "$work/xcrun.log" 31 isolated_path=$work/sentinel-bin:/usr/bin:/bin 32 33 cat > "$work/hosted.c" <<'EOF' 34 #include <stdlib.h> 35 #ifndef __APPLE__ 36 #error native macOS hosted predefines are missing 37 #endif 38 int kit_native_sdk_marker = 2026060; 39 int main(void) { 40 void *p = malloc(8); 41 if (!p) return 1; 42 free(p); 43 return 0; 44 } 45 EOF 46 cat > "$work/freestanding.c" <<'EOF' 47 int kit_freestanding_probe(void) { return 0; } 48 EOF 49 cat > "$work/start.c" <<'EOF' 50 void _start(void) {} 51 EOF 52 53 run_ok print-internal /usr/bin/env -u KIT_SYSROOT \ 54 PATH="$isolated_path" KIT_XCRUN_SENTINEL_LOG="$work/xcrun.log" \ 55 "$KIT" cc -print-sysroot 56 internal_sdk=$(sed -n '1p' "$work/print-internal.out") 57 if [ -n "$internal_sdk" ] && [ -d "$internal_sdk" ]; then 58 ok internal-sdk-exists 59 else 60 printf 'reported SDK: %s\n' "$internal_sdk" > "$work/internal-sdk.diag" 61 not_ok internal-sdk-exists "$work/internal-sdk.diag" 62 fi 63 64 run_ok print-env /usr/bin/env KIT_SYSROOT="$internal_sdk" \ 65 PATH="$isolated_path" KIT_XCRUN_SENTINEL_LOG="$work/xcrun.log" \ 66 "$KIT" cc -print-sysroot 67 if [ "$(sed -n '1p' "$work/print-env.out")" = "$internal_sdk" ]; then 68 ok env-sysroot-precedence 69 else 70 not_ok env-sysroot-precedence "$work/print-env.out" 71 fi 72 73 explicit_sdk="$work/Explicit SDK With Spaces" 74 ln -s "$internal_sdk" "$explicit_sdk" 75 run_ok print-explicit /usr/bin/env KIT_SYSROOT="$internal_sdk" \ 76 PATH="$isolated_path" KIT_XCRUN_SENTINEL_LOG="$work/xcrun.log" \ 77 "$KIT" cc --sysroot "$explicit_sdk" -print-sysroot 78 if [ "$(sed -n '1p' "$work/print-explicit.out")" = "$explicit_sdk" ]; then 79 ok explicit-sysroot-wins 80 else 81 not_ok explicit-sysroot-wins "$work/print-explicit.out" 82 fi 83 run_ok explicit-root-compile-link /usr/bin/env KIT_SYSROOT="$internal_sdk" \ 84 PATH="$isolated_path" KIT_XCRUN_SENTINEL_LOG="$work/xcrun.log" \ 85 "$KIT" cc --sysroot "$explicit_sdk" "$work/hosted.c" \ 86 -o "$work/explicit-root-program" 87 run_ok explicit-root-run "$work/explicit-root-program" 88 89 run_fail invalid-explicit-no-fallback /usr/bin/env KIT_SYSROOT="$internal_sdk" \ 90 PATH="$isolated_path" KIT_XCRUN_SENTINEL_LOG="$work/xcrun.log" \ 91 "$KIT" cc --sysroot "$work/missing-sdk" -print-sysroot 92 contains invalid-explicit-diagnostic "$work/invalid-explicit-no-fallback.err" \ 93 "sysroot not found: $work/missing-sdk" 94 95 for row in 'aa64 aarch64-apple-darwin' 'x64 x86_64-apple-darwin'; do 96 set -- $row 97 tag=$1 98 target=$2 99 common_env="PATH=$isolated_path" 100 : "$common_env" 101 102 run_ok "$tag-cc" /usr/bin/env -u KIT_SYSROOT \ 103 PATH="$isolated_path" KIT_XCRUN_SENTINEL_LOG="$work/xcrun.log" \ 104 "$KIT" cc -target "$target" "$work/hosted.c" \ 105 -o "$work/$tag-cc-program" 106 run_ok "$tag-check" /usr/bin/env -u KIT_SYSROOT \ 107 PATH="$isolated_path" KIT_XCRUN_SENTINEL_LOG="$work/xcrun.log" \ 108 "$KIT" check -target "$target" "$work/hosted.c" 109 run_ok "$tag-cpp" /usr/bin/env -u KIT_SYSROOT \ 110 PATH="$isolated_path" KIT_XCRUN_SENTINEL_LOG="$work/xcrun.log" \ 111 "$KIT" cpp -target "$target" "$work/hosted.c" \ 112 -o "$work/$tag-hosted.i" 113 contains "$tag-cpp-marker" "$work/$tag-hosted.i" \ 114 "kit_native_sdk_marker" 115 run_ok "$tag-build-obj" /usr/bin/env -u KIT_SYSROOT \ 116 PATH="$isolated_path" KIT_XCRUN_SENTINEL_LOG="$work/xcrun.log" \ 117 "$KIT" build-obj -target "$target" "$work/hosted.c" \ 118 -o "$work/$tag-build.o" 119 run_ok "$tag-build-lib" /usr/bin/env -u KIT_SYSROOT \ 120 PATH="$isolated_path" KIT_XCRUN_SENTINEL_LOG="$work/xcrun.log" \ 121 "$KIT" build-lib -target "$target" "$work/hosted.c" \ 122 -o "$work/lib$tag.a" 123 run_ok "$tag-build-exe" /usr/bin/env -u KIT_SYSROOT \ 124 PATH="$isolated_path" KIT_XCRUN_SENTINEL_LOG="$work/xcrun.log" \ 125 "$KIT" build-exe -target "$target" "$work/hosted.c" \ 126 -o "$work/$tag-build-program" 127 done 128 129 run_ok native-cc-run "$work/aa64-cc-program" 130 run_ok native-build-exe-run "$work/aa64-build-program" 131 run_ok x64-cc-run "$work/x64-cc-program" 132 run_ok x64-build-exe-run "$work/x64-build-program" 133 134 # Both freestanding opt-outs must avoid SDK resolution even when KIT_SYSROOT 135 # is an invalid authoritative value. 136 run_ok freestanding-opt-out /usr/bin/env KIT_SYSROOT="$work/missing-sdk" \ 137 PATH="$isolated_path" KIT_XCRUN_SENTINEL_LOG="$work/xcrun.log" \ 138 "$KIT" cc -target aarch64-apple-darwin -ffreestanding -nostdinc \ 139 -c "$work/freestanding.c" -o "$work/freestanding.o" 140 141 run_ok direct-ld-object /usr/bin/env -u KIT_SYSROOT \ 142 PATH="$isolated_path" KIT_XCRUN_SENTINEL_LOG="$work/xcrun.log" \ 143 "$KIT" cc -target aarch64-apple-darwin -ffreestanding -nostdinc \ 144 -c "$work/start.c" -o "$work/start.o" 145 run_ok direct-ld-unhosted /usr/bin/env KIT_SYSROOT="$work/missing-sdk" \ 146 PATH="$isolated_path" KIT_XCRUN_SENTINEL_LOG="$work/xcrun.log" \ 147 "$KIT" ld -e _start "$work/start.o" -o "$work/direct-ld" 148 run_fail direct-ld-hosted-needs-sdk /usr/bin/env \ 149 KIT_SYSROOT="$work/missing-sdk" PATH="$isolated_path" \ 150 KIT_XCRUN_SENTINEL_LOG="$work/xcrun.log" \ 151 "$KIT" ld -lc -e _start "$work/start.o" -o "$work/direct-ld-hosted" 152 contains direct-ld-hosted-sdk-diagnostic \ 153 "$work/direct-ld-hosted-needs-sdk.err" "sysroot not found" 154 155 run_fail cross-os-does-not-borrow-sdk /usr/bin/env -u KIT_SYSROOT \ 156 PATH="$isolated_path" KIT_XCRUN_SENTINEL_LOG="$work/xcrun.log" \ 157 "$KIT" cc -target aarch64-linux-gnu -lc "$work/hosted.c" \ 158 -o "$work/cross-linux" 159 160 if [ ! -s "$work/xcrun.log" ]; then 161 ok no-xcrun-dependency 162 else 163 not_ok no-xcrun-dependency "$work/xcrun.log" 164 fi 165 166 kit_summary native-macos-sdk 167 kit_exit