aboutsummaryrefslogtreecommitdiff
path: root/src/tools/findcc.sh
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/findcc.sh')
-rwxr-xr-xsrc/tools/findcc.sh33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/tools/findcc.sh b/src/tools/findcc.sh
new file mode 100755
index 0000000..caa3af3
--- /dev/null
+++ b/src/tools/findcc.sh
@@ -0,0 +1,33 @@
+#!/bin/sh
+# prints the first c compiler that accepts c23 #embed, which every core needs to
+# take in its genome (src/core/genome.h). clang 19 and gcc 15 are the first
+# releases with it, and both find #embed files through --embed-dir. a CC
+# already in the environment is tried first.
+# src/tools/findcc.sh a native compiler
+# src/tools/findcc.sh --wasm a clang that also targets wasm32
+# exits 1, naming what was tried, when nothing fits.
+case "${1:-}" in
+ --help|-h) printf '%s\n' 'usage: src/tools/findcc.sh [--wasm]' 'prints a c compiler that supports #embed; --wasm also requires the wasm32 target.' 'environment: CC is tried first.'; exit 0 ;;
+ --wasm|'') ;;
+ *) echo "findcc: unknown option $1; use --help for usage" >&2; exit 2 ;;
+esac
+wasm=${1:-}
+dir=$(mktemp -d "${TMPDIR:-/tmp}/findcc.XXXXXX") || exit 1
+trap 'rm -rf "$dir"' EXIT
+mkdir "$dir/data"
+printf 'ab' > "$dir/data/probe.bin"
+printf 'static const unsigned char probe[] = {\n#embed "probe.bin"\n};\nint main(void) { return sizeof probe == 2 ? 0 : 1; }\n' > "$dir/probe.c"
+tried=""
+for cc in ${CC:-} cc clang gcc clang-22 clang-21 clang-20 clang-19 gcc-16 gcc-15 /opt/homebrew/opt/llvm/bin/clang /usr/local/opt/llvm/bin/clang; do
+ command -v "$cc" >/dev/null 2>&1 || continue
+ tried="$tried $cc"
+ if [ -n "$wasm" ]; then
+ "$cc" --target=wasm32 -nostdlib -w --embed-dir="$dir/data" -c "$dir/probe.c" -o "$dir/probe.o" >/dev/null 2>&1 || continue
+ else
+ "$cc" -w --embed-dir="$dir/data" "$dir/probe.c" -o "$dir/probe" >/dev/null 2>&1 || continue
+ fi
+ echo "$cc"
+ exit 0
+done
+echo "findcc: no c compiler with #embed${wasm:+ and wasm32} (tried:${tried:- nothing}); install clang 19 or gcc 15, or set CC" >&2
+exit 1