# shellcheck shell=bash # Portable resolver for the pinned LLVM build tree. # # The out-of-tree LLVM pass (-fpass-plugin) must load into the *exact* LLVM it # was built against (clang has no stable plugin ABI). That LLVM is the # from-source pin described in toolchain/llvm-from-source.md (commit 60513b8d, # clang 22), built with the dylib + plugin flags. This helper locates its build # directory (the one containing bin/clang and lib/cmake/llvm) without baking in # any machine-specific absolute path. # # Search order (first hit wins): # 1. $HWJS_LLVM_BUILD — explicit override # 2. /llvm-project/build — the LLVM this repo pins as a # submodule, and the default # libexec/config.sh resolves to # 3. $HOME/toolchains/llvm-pin/build — where llvm-from-source.md installs # 4. /../llvm-project/build — a sibling checkout, or the sibling # /../../toolchains/llvm-project/build submodule in a super-repo # # Candidate 2 was added when the LLVM fork became a submodule of this repo, and # it deliberately outranks the $HOME candidate: this repo pins its own LLVM, and # a build tree in someone's home directory is a DIFFERENT LLVM whose plugin load # fails as a crash or a miscompile rather than a clean version error. Everything # below the explicit override is a guess; prefer the guess the repo itself pins. # # Callers that go through bin/ never reach the guesses at all — # libexec/config.sh resolves BRINTIUM_LLVM and exports WASMLD_HWJS_LLVM_BIN, # which hwjs_llvm_bin honors first. # # Usage: # source libexec/find-llvm.sh # build="$(hwjs_find_llvm_build "$SOME_DIR_INSIDE_THE_REPO")" || exit 1 hwjs_find_llvm_build() { # $1: any directory inside this checkout (used to locate the repo root and # its sibling llvm-project). Falls back to this file's own location. local anchor="${1:-}" local self_dir self_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" # repo root local candidates=( "${HWJS_LLVM_BUILD:-}" "$self_dir/llvm-project/build" "$HOME/toolchains/llvm-pin/build" "$self_dir/../llvm-project/build" "$self_dir/../../toolchains/llvm-project/build" ) [ -n "$anchor" ] && candidates+=( "$anchor/../../../llvm-project/build" "$anchor/../../../../toolchains/llvm-project/build" ) local c for c in "${candidates[@]}"; do [ -n "$c" ] && [ -x "$c/bin/clang" ] && { (cd "$c" && pwd); return 0; } done return 1 } # hwjs_llvm_bin: echo the pinned LLVM `bin/` directory (clang, wasm-ld, opt, # llvm-nm, llvm-ar, llvm-link). Honors $WASMLD_HWJS_LLVM_BIN / $HWJS_LLVM_BIN # first (the driver override interface the distro sets), else resolves the build # tree via hwjs_find_llvm_build. Non-zero exit if nothing resolves. hwjs_llvm_bin() { local anchor="${1:-}" if [ -n "${WASMLD_HWJS_LLVM_BIN:-}" ]; then printf '%s\n' "$WASMLD_HWJS_LLVM_BIN"; return 0; fi if [ -n "${HWJS_LLVM_BIN:-}" ]; then printf '%s\n' "$HWJS_LLVM_BIN"; return 0; fi local build; build="$(hwjs_find_llvm_build "$anchor")" || return 1 printf '%s\n' "$build/bin" }