#!/usr/bin/env bash# libclang for the Rust lanes, because mupdf-sys runs bindgen over MuPDF's C# headers at BUILD time. Not a Rust dependency and not something rustup carries.## bash scripts/ci/setup-libclang.sh## Writes `LIBCLANG_PATH` and `BINDGEN_EXTRA_CLANG_ARGS` to `$GITHUB_ENV` when# there is one, so the cargo steps after it in the same job see them. Run by# `verify-main.yml`'s `rust` job and by `rust-deep.yml`'s `deep` job — ONE copy,# because two copies of a bootstrap is how one of them silently stops matching# the host it runs on.## The host has a stray libclang-18.so.18 (a bpftrace dependency) but NONE of# clang's own builtin headers, so a parse of `stddef.h` dies and the message# names a header rather than a missing toolchain. The runner user has no sudo,# so this is a download into the lane's HOME — the same shape as rustup, and# idempotent for the same reason.## THE DOWNLOADED LIBRARY DOES NOT LOAD ON EVERY RUNNER IN THE POOL. The only# x86_64 Linux asset LLVM publishes for 18.1.8 is built on Ubuntu 18.04, so its# libclang links `libtinfo.so.5` — a soname a modern box does not ship, and# #394's first run died on exactly that with a message naming ncurses rather# than anything about this repo. The tarball is still the only source of the# RESOURCE HEADERS, which are plain text and load nothing, so the two halves are# taken from wherever each one works: headers from the download, and the shared# library from the host when the host has one that loads.## If somebody root-installs `libclang-dev` on the box (a few MB, against ~100 MB# unpacked here) this script finds the resource headers, exports nothing, and# the download never happens again. That is the better end state; this is what# makes the lane work without waiting for it.set -euo pipefail# A hand run outside CI has neither file; `set -u` would die on the redirect# rather than on anything this script is about.GITHUB_ENV="${GITHUB_ENV:-/dev/null}"GITHUB_PATH="${GITHUB_PATH:-/dev/null}"# A MAC ALREADY HAS ONE, AND IT IS XCODE'S. Since `rust` became a `cotton-rust`# job this script runs on the Macs too, and everything below it is Linux: an# `/usr/lib/llvm-*` probe, and a `x86_64-linux-gnu` tarball that would not even# load here. The Macs have been building mupdf-sys for as long as the# XCFramework has existed — `scripts/build-xcframework.sh` runs the same bindgen# against the same headers — and it works because the toolchain in# `xcode-select -p` carries libclang AND its builtin headers together. So the# honest answer on Darwin is to name that copy and stop.## `libclang.dylib`, not `.so`. `xcode-select -p` is the selected developer# directory, and a machine with no Xcode selected is a machine that cannot build# the XCFramework either — so this refuses by name rather than falling through# into the Linux path and failing three steps later on a tarball.if [ "$(uname -s)" = "Darwin" ]; then developer="$(xcode-select -p 2>/dev/null || true)"if [ -z "$developer" ]; then echo "::error::libclang: no Xcode toolchain is selected (xcode-select -p answered nothing) — mupdf-sys runs bindgen and cannot be built here." >&2exit1fi lib="$developer/Toolchains/XcodeDefault.xctoolchain/usr/lib"if [ ! -e "$lib/libclang.dylib" ]; then echo "::error::libclang: $lib/libclang.dylib is not there — the selected toolchain ($developer) carries no libclang." >&2exit1fi echo "libclang: Xcode's own copy at $lib — nothing to fetch" echo "LIBCLANG_PATH=$lib" >> "$GITHUB_ENV"exit0fiif ls /usr/lib/llvm-*/lib/clang/*/include/stddef.h >/dev/null 2>&1; then echo "libclang: the system copy carries its builtin headers — nothing to fetch"exit0firoot="$HOME/.cache/cottondb/rust"llvm="$root/llvm"version=18.1.8if [ ! -e "$llvm/lib/libclang.so" ]; then mkdir -p "$llvm"# Only the two paths bindgen needs: the shared library, and the# resource headers it resolves relative to it. The whole tarball# unpacked would be gigabytes of compiler nobody here runs. curl -fsSL --proto '=https' --tlsv1.2 \"https://github.com/llvm/llvm-project/releases/download/llvmorg-$version/clang+llvm-$version-x86_64-linux-gnu-ubuntu-18.04.tar.xz" \ | tar xJ -C "$llvm" --strip-components=1 --wildcards \"clang+llvm-$version-x86_64-linux-gnu-ubuntu-18.04/lib/libclang.so*" \"clang+llvm-$version-x86_64-linux-gnu-ubuntu-18.04/lib/clang/*"fi# Does the library in our lib dir actually load? `ldd` answering# "not found" for anything is the whole question, and it is the# question the build error did not ask — bindgen only reports the# dlopen failure, several hundred lines of MuPDF cc output later.loads() { ! ldd "$1"2>/dev/null | grep -q 'not found'; }if ! loads "$llvm/lib/libclang.so"; then echo "libclang: the downloaded copy does not load here —" ldd "$llvm/lib/libclang.so"2>&1 | sed 's/^/ /' || true# The host's own libclang is packaged for the host, so it loads by# construction. Prefer one of the SAME major version as the headers# we unpacked: libclang resolves its resource headers as# `<its own dir>/../lib/clang/<its major>/include`, so a host# libclang-14 against 18's headers is the original stddef.h failure# wearing a different hat. major="${version%%.*}" host=""for pattern in"*-$major.so*""*.so.$major*""*.so*"; dofor cand in /usr/lib/x86_64-linux-gnu/libclang$pattern /usr/lib/llvm-$major/lib/libclang$pattern /usr/lib/llvm-*/lib/libclang$pattern /usr/lib64/libclang$pattern; do [ -f "$cand" ] || continueif loads "$cand"; then host="$cand"; break 2; fidonedoneif [ -z "$host" ]; then echo "libclang: and this host has no libclang of its own that loads." >&2 echo "libclang: root-install libclang-dev on this runner — it is a few MB and ends this whole step." >&2exit1fi echo "libclang: taking the shared library from the host ($host) and the resource headers from the download"# COPIED, never symlinked, and every unloadable copy the tarball# left goes first: libclang finds its headers relative to the path# it was loaded from, and clang-sys will happily pick the versioned# sibling over `libclang.so` if one is still lying there. rm -f "$llvm"/lib/libclang.so* cp "$host""$llvm/lib/libclang.so"fi# Loudly, before cargo: an unloadable library here is a MuPDF build# failure a thousand lines downstream, and the message names ncurses.if ! loads "$llvm/lib/libclang.so"; then echo "libclang: still unloadable after the fallback —" >&2 ldd "$llvm/lib/libclang.so" >&2 || trueexit1firesource="$(ls -d "$llvm"/lib/clang/*/include 2>/dev/null | head -1 || true)"if [ -z "$resource" ] || [ ! -f "$resource/stddef.h" ]; then echo "libclang: the resource headers are missing from $llvm/lib/clang — delete $llvm and re-run" >&2exit1fi# NAMED, not inferred. libclang normally finds its builtin headers# relative to its own file, and that is exactly the inference the# host's copy does not make here — it loaded fine and then could not# find `stddef.h`, which is the failure this whole step exists to# avoid. bindgen takes the directory directly, so it is given# directly, and nothing downstream depends on where the library# thinks it lives.echo "libclang: builtin headers at $resource"echo "LIBCLANG_PATH=$llvm/lib" >> "$GITHUB_ENV"echo "BINDGEN_EXTRA_CLANG_ARGS=-isystem $resource" >> "$GITHUB_ENV"ls -l "$llvm/lib/libclang.so"