Files
cthulhu/build-cthulhu.sh
T
2026-11-24 11:12:17 -05:00

157 lines
7.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# Build Cthulhu and its two helpers without replacing the system Go installation.
set -euo pipefail
projectDir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)
fail() {
echo "Error: $*" >&2
exit 1
}
usage() {
cat <<'EOF'
Usage: build-cthulhu.sh [-o DIR | --output-dir DIR] [-- GO_BUILD_FLAGS...]
Build cthulhu, cthulhu-braille and cthulhu-input-helper, obtaining a suitable Go toolchain if needed.
-o, --output-dir DIR Write all three binaries to DIR (default: checkout root).
Relative paths start at the caller's directory.
Missing directories are created.
-h, --help Show help without checking build dependencies.
-- Pass remaining flags to all three go build commands.
GOTOOLCHAIN defaults to auto. Set GOTOOLCHAIN=local to require installed Go
and prevent toolchain downloads. Other Go settings, compiler/linker flags,
and cache paths are inherited. CGO must be enabled (the default here).
If Go is absent or older than 1.21, the bootstrap toolchain is cached under
${XDG_CACHE_HOME:-$HOME/.cache}/cthulhu/toolchains; curl, tar and sha256sum
are needed only for this download. Installation belongs in the package recipe.
EOF
}
outputDir=$projectDir
buildFlags=()
while (( $# )); do
case $1 in
-o|--output-dir)
(( $# >= 2 )) || fail "$1 requires a directory."
[[ -n $2 && $2 != -* ]] || fail "$1 requires a directory (use ./ for a name starting with '-')."
outputDir=$2
shift 2
;;
-h|--help)
usage
exit 0
;;
--)
shift
buildFlags=("$@")
break
;;
*) fail "Unknown argument: $1. See --help." ;;
esac
done
if [[ $outputDir != /* ]]; then
outputDir="$PWD/$outputDir"
fi
[[ ! -e $outputDir || -d $outputDir ]] || fail "Output directory is an existing file: $outputDir"
for binary in cthulhu cthulhu-braille cthulhu-input-helper; do
[[ ! -d $outputDir/$binary ]] || fail "Binary output path is a directory: $outputDir/$binary"
done
cd -- "$projectDir"
[[ $(uname -s) == Linux ]] || fail "Cthulhu requires Linux."
export CGO_ENABLED=${CGO_ENABLED:-1}
[[ $CGO_ENABLED == 1 ]] || fail "Cthulhu requires CGO_ENABLED=1 and a C compiler."
command -v pkg-config >/dev/null || fail "Install pkg-config and the development libraries listed in CONTRIBUTING.md."
missingLibraries=()
for library in gtk+-3.0 atspi-2 gio-2.0 gstreamer-1.0 x11 xtst xi xfixes xres xkbcommon liblouis; do
if ! pkg-config --exists "$library"; then
missingLibraries+=("$library")
fi
done
if (( ${#missingLibraries[@]} )); then
fail "Missing development libraries (pkg-config names): ${missingLibraries[*]}. See CONTRIBUTING.md; BRLTTY/BrlAPI headers and library are also required."
fi
requiredVersion=$(awk '$1 == "go" { print $2; exit }' go.mod)
[[ $requiredVersion =~ ^1\.[0-9]+\.[0-9]+$ ]] || fail "Cannot read the required Go version from go.mod."
export GOTOOLCHAIN=${GOTOOLCHAIN:-auto}
goCommand=""
if command -v go >/dev/null; then
installedVersion=$(GOTOOLCHAIN=local go version 2>/dev/null || true)
if [[ $installedVersion =~ go1\.([0-9]+) ]] && (( 10#${BASH_REMATCH[1]} >= 21 )); then
goCommand=$(command -v go)
fi
fi
if [[ -z $goCommand ]]; then
[[ $GOTOOLCHAIN == auto ]] || fail "Install Go $requiredVersion or newer, or use GOTOOLCHAIN=auto to allow bootstrapping."
case $(uname -m) in
x86_64) archiveArch=amd64 ;;
aarch64|arm64) archiveArch=arm64 ;;
armv6l|armv7l) archiveArch=armv6l ;;
i386|i486|i586|i686) archiveArch=386 ;;
ppc64le|riscv64|s390x|loong64) archiveArch=$(uname -m) ;;
loongarch64) archiveArch=loong64 ;;
*) fail "No Go bootstrap archive configured for $(uname -m). Install Go 1.21 or newer."
esac
# Pinned SHA-256 digests of the official archives for the go.mod Go
# release. Never fetch a digest from the archive's own origin: a mirror
# that can replace the archive could replace its .sha256 file too.
# Update this table whenever the go directive in go.mod changes.
case "$requiredVersion:$archiveArch" in
1.27.2:amd64) expectedChecksum=ecbadb99091a3f46e31f5f934b068b1864eafa7995211b39eaddf76996045fe5 ;;
1.27.2:arm64) expectedChecksum=94f3e30b8e374bc285e7dadc11e0865726b9bc6e85b841ccceaabc0214c6b7c8 ;;
1.27.2:armv6l) expectedChecksum=e25a174051d8675f87ac720aedcd25bf89ac652c4b82f26aa47919524725ed95 ;;
1.27.2:386) expectedChecksum=93dae4b7f463ed1e9c65e05561ce9fc5f7cd5c4ffca5e336248d48f3a2eab5fc ;;
1.27.2:ppc64le) expectedChecksum=059d5f01b1607a724ea5ae87c5589041a51c577a6f3f20099e49d00f31c803fa ;;
1.27.2:riscv64) expectedChecksum=420f3b8dac8b5a9d11f51e12b496ac27f02ab31cbe495067e355ed43b9fe3aa1 ;;
1.27.2:s390x) expectedChecksum=dfd01d1e08e4dbece2cd91e259953f87b858752037fd151da2f674c4fa09aeef ;;
1.27.2:loong64) expectedChecksum=b49a3b2f090934ca588c7a897d64bc12f844348a2cd35e301d8f4f50eaf3bfdb ;;
*) fail "No pinned SHA-256 for Go $requiredVersion on Linux/$archiveArch. Install Go $requiredVersion or newer, or add its official digest to build-cthulhu.sh."
esac
toolchainDir="${XDG_CACHE_HOME:-$HOME/.cache}/cthulhu/toolchains/go${requiredVersion}.linux-${archiveArch}"
goCommand="$toolchainDir/go/bin/go"
digestFile="$toolchainDir/.archive.sha256"
if [[ -x $goCommand ]]; then
# A cached toolchain is trusted only if a previous verified download
# recorded the pinned digest for it.
cachedChecksum=""
if [[ -f $digestFile ]]; then
IFS= read -r cachedChecksum < "$digestFile" || true
fi
[[ $cachedChecksum == "$expectedChecksum" ]] || fail "Cached Go toolchain at $toolchainDir is unverified. Remove it and rerun to download a verified copy."
else
for dependency in curl tar sha256sum; do
command -v "$dependency" >/dev/null || fail "Install $dependency to bootstrap Go, or install Go $requiredVersion or newer."
done
mkdir -p -- "$(dirname -- "$toolchainDir")"
downloadDir=$(mktemp -d "${toolchainDir}.download.XXXXXX")
trap 'rm -rf -- "$downloadDir"' EXIT
archiveURL="https://dl.google.com/go/go${requiredVersion}.linux-${archiveArch}.tar.gz"
echo "Downloading Go $requiredVersion for Linux/$archiveArch..."
curlFlags=(--fail --location --silent --show-error --proto '=https' --proto-redir '=https')
curl "${curlFlags[@]}" --output "$downloadDir/go.tar.gz" "$archiveURL"
printf '%s %s\n' "$expectedChecksum" "$downloadDir/go.tar.gz" | sha256sum --check --status || fail "Go archive checksum mismatch."
tar -xzf "$downloadDir/go.tar.gz" -C "$downloadDir"
[[ -x $downloadDir/go/bin/go ]] || fail "The downloaded archive does not contain Go."
# Publish a complete tree; another build may have populated the cache.
if [[ ! -x $goCommand ]]; then
mkdir -p -- "$toolchainDir"
mv -T -- "$downloadDir/go" "$toolchainDir/go" || [[ -x $goCommand ]]
fi
printf '%s\n' "$expectedChecksum" > "$digestFile"
fi
unset GOROOT
fi
goVersion=$("$goCommand" version) || fail "The selected Go toolchain could not run."
echo "Using $goVersion"
mkdir -p -- "$outputDir"
"$goCommand" build "${buildFlags[@]}" -o "$outputDir/cthulhu" ./cmd/cthulhu
"$goCommand" build "${buildFlags[@]}" -tags brlapi -o "$outputDir/cthulhu-braille" ./cmd/cthulhu-braille
"$goCommand" build "${buildFlags[@]}" -o "$outputDir/cthulhu-input-helper" ./cmd/cthulhu-input-helper
echo "Cthulhu has been built: $outputDir/cthulhu, $outputDir/cthulhu-braille and $outputDir/cthulhu-input-helper"