153 lines
7.9 KiB
Shell
153 lines
7.9 KiB
Shell
|
|
#!/bin/sh
|
||
|
|
# SPDX-License-Identifier: GPL-3.0-only
|
||
|
|
# SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
# make-flatpak-sysroot.sh — what the flatpak's aarch64 executable is
|
||
|
|
# cross-compiled against: the Freedesktop Sdk for aarch64 (glibc, GLib,
|
||
|
|
# Wayland, xkbcommon, udev and the Vulkan loader, the libraries the Platform
|
||
|
|
# runtime has at run time) plus LLVM's libc++ of the release crafter-build
|
||
|
|
# itself carries, built for aarch64, which the runtime does not have.
|
||
|
|
#
|
||
|
|
# packaging/make-flatpak-sysroot.sh
|
||
|
|
#
|
||
|
|
# Everything goes under $IMSD_MESSAGES_FLATPAK_CACHE (default
|
||
|
|
# ${XDG_CACHE_HOME:-~/.cache}/imsd-messages/flatpak), outside /tmp, which
|
||
|
|
# the crafter-build sandbox does not share:
|
||
|
|
# installation/ a flatpak installation of the aarch64 Sdk and Platform
|
||
|
|
# (packaging/build-flatpak.sh builds the app on it)
|
||
|
|
# sysroot-aarch64/ the Sdk's files as usr/, with libc++ added
|
||
|
|
# libcxx-aarch64/ libc++, libc++abi and libunwind as installed, which
|
||
|
|
# the flatpak bundles in /app/lib
|
||
|
|
# A sysroot made from the same Sdk commit, LLVM release and recipe is kept.
|
||
|
|
#
|
||
|
|
# Needs flatpak, flock, curl, tar, xz and sha256sum; crafter-build
|
||
|
|
# ($CRAFTER_REF, the Crafter.Build Flatpak) provides clang, cmake and ninja.
|
||
|
|
# Network: Flathub and the LLVM release on GitHub.
|
||
|
|
set -eu
|
||
|
|
|
||
|
|
ARCH=aarch64
|
||
|
|
TRIPLE=aarch64-unknown-linux-gnu
|
||
|
|
MULTIARCH=aarch64-linux-gnu
|
||
|
|
MARCH=armv8-a
|
||
|
|
RUNTIME_VERSION=26.08
|
||
|
|
# The LLVM release crafter-build itself carries, whose std module source it
|
||
|
|
# ships. The tarball and hash are the ones Crafter.Build's manifest builds
|
||
|
|
# that LLVM from.
|
||
|
|
LLVM_VERSION=23.1.2
|
||
|
|
LLVM_SHA256=c98bbef08a2b4c2613cd50e9aa9ae7b69b1fe6c16b2c40373bc0ab6116fdf78a
|
||
|
|
CRAFTER_REF=${CRAFTER_REF:-net.catcrafts.CrafterBuild//stable}
|
||
|
|
CACHE=${IMSD_MESSAGES_FLATPAK_CACHE:-${XDG_CACHE_HOME:-$HOME/.cache}/imsd-messages/flatpak}
|
||
|
|
|
||
|
|
INSTALLATION="$CACHE/installation"
|
||
|
|
SYSROOT="$CACHE/sysroot-$ARCH"
|
||
|
|
LIBCXX="$CACHE/libcxx-$ARCH"
|
||
|
|
# The Sdk and the Platform for aarch64 go into a flatpak installation of
|
||
|
|
# their own; crafter-build is run from wherever it is installed.
|
||
|
|
sdkpak() { FLATPAK_USER_DIR="$INSTALLATION" flatpak --user "$@"; }
|
||
|
|
# Flathub's CDN answers 502 now and then, and flatpak reports a summary it
|
||
|
|
# could not fetch as a runtime that does not exist.
|
||
|
|
retry() {
|
||
|
|
for attempt in 1 2 3 4 5; do
|
||
|
|
"$@" && return 0
|
||
|
|
echo "attempt $attempt of 5 failed: $*" >&2
|
||
|
|
[ "$attempt" = 5 ] || sleep 30
|
||
|
|
done
|
||
|
|
return 1
|
||
|
|
}
|
||
|
|
|
||
|
|
mkdir -p "$CACHE"
|
||
|
|
# One run at a time on this cache: the sysroot and the LLVM build are
|
||
|
|
# removed and made again by a run that finds them stale.
|
||
|
|
exec 9>"$CACHE/lock"; flock 9
|
||
|
|
echo ">> the $ARCH Sdk and Platform $RUNTIME_VERSION into $INSTALLATION"
|
||
|
|
retry sdkpak remote-add --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo
|
||
|
|
retry sdkpak install -y --noninteractive --no-related --arch="$ARCH" flathub \
|
||
|
|
"org.freedesktop.Sdk//$RUNTIME_VERSION" "org.freedesktop.Platform//$RUNTIME_VERSION"
|
||
|
|
retry sdkpak update -y --noninteractive --no-related --arch="$ARCH"
|
||
|
|
sdk=$(sdkpak info --show-location --arch="$ARCH" "org.freedesktop.Sdk//$RUNTIME_VERSION")
|
||
|
|
sdk_commit=$(sdkpak info --show-commit --arch="$ARCH" "org.freedesktop.Sdk//$RUNTIME_VERSION")
|
||
|
|
# A sysroot is kept while the Sdk commit, the LLVM release and this recipe
|
||
|
|
# stay the same.
|
||
|
|
stamp="sdk $sdk_commit llvm $LLVM_VERSION recipe $(sha256sum "$0" | cut -c1-16)"
|
||
|
|
if [ -f "$SYSROOT/.made-from" ] && [ "$(cat "$SYSROOT/.made-from")" = "$stamp" ] && [ -f "$LIBCXX/usr/lib/libc++.so.1" ]; then
|
||
|
|
echo ">> $SYSROOT is current ($stamp)"
|
||
|
|
exit 0
|
||
|
|
fi
|
||
|
|
|
||
|
|
echo ">> $SYSROOT from the Sdk at $sdk_commit"
|
||
|
|
rm -rf "$SYSROOT" "$LIBCXX"
|
||
|
|
mkdir -p "$SYSROOT"
|
||
|
|
# A copy: libc++ is installed into it, and the installation's files are
|
||
|
|
# hard links into its object store.
|
||
|
|
cp -a --reflink=auto "$sdk/files" "$SYSROOT/usr"
|
||
|
|
# Linker scripts and the dynamic linker's path name /lib.
|
||
|
|
ln -s usr/lib "$SYSROOT/lib"
|
||
|
|
# libc++'s linker script adds -lunwind, which would find the Sdk's
|
||
|
|
# libunwind 8 (in the multiarch directory, searched first) beside the LLVM
|
||
|
|
# libunwind its libc++abi uses: without the Sdk's link name it finds LLVM's.
|
||
|
|
rm "$SYSROOT/usr/lib/$MULTIARCH/libunwind.so"
|
||
|
|
|
||
|
|
src="$CACHE/llvm-project-$LLVM_VERSION.src"
|
||
|
|
tarball="$CACHE/llvm-project-$LLVM_VERSION.src.tar.xz"
|
||
|
|
if [ ! -d "$src/runtimes" ] || [ ! -d "$src/libc/shared" ]; then
|
||
|
|
echo ">> LLVM $LLVM_VERSION's runtimes source"
|
||
|
|
retry curl -fsSL -o "$tarball" "https://github.com/llvm/llvm-project/releases/download/llvmorg-$LLVM_VERSION/llvm-project-$LLVM_VERSION.src.tar.xz"
|
||
|
|
got=$(sha256sum "$tarball" | cut -d' ' -f1)
|
||
|
|
[ "$got" = "$LLVM_SHA256" ] || { echo "llvm-project-$LLVM_VERSION.src.tar.xz: sha256 $got, expected $LLVM_SHA256" >&2; exit 1; }
|
||
|
|
rm -rf "$src" "$src.part"
|
||
|
|
mkdir -p "$src.part"
|
||
|
|
# Only part of the tree is unpacked; libc is among it because libc++'s
|
||
|
|
# from_chars parses floating point with LLVM libc's shared headers. It
|
||
|
|
# lands under $src only once complete: a tar cut short leaves nothing
|
||
|
|
# a later run would take for the source.
|
||
|
|
tar -xJf "$tarball" -C "$src.part" --strip-components=1 \
|
||
|
|
"llvm-project-$LLVM_VERSION.src/runtimes" "llvm-project-$LLVM_VERSION.src/libcxx" \
|
||
|
|
"llvm-project-$LLVM_VERSION.src/libcxxabi" "llvm-project-$LLVM_VERSION.src/libunwind" \
|
||
|
|
"llvm-project-$LLVM_VERSION.src/cmake" "llvm-project-$LLVM_VERSION.src/llvm/cmake" \
|
||
|
|
"llvm-project-$LLVM_VERSION.src/llvm/utils" "llvm-project-$LLVM_VERSION.src/third-party" \
|
||
|
|
"llvm-project-$LLVM_VERSION.src/libc"
|
||
|
|
mv "$src.part" "$src"
|
||
|
|
rm -f "$tarball"
|
||
|
|
fi
|
||
|
|
|
||
|
|
echo ">> libc++, libc++abi and libunwind $LLVM_VERSION for $TRIPLE"
|
||
|
|
build="$CACHE/libcxx-build-$ARCH"
|
||
|
|
rm -rf "$build"
|
||
|
|
# The classic layout (include/c++/v1, lib/) is where crafter-build looks in
|
||
|
|
# a sysroot. libc++abi unwinds with LLVM's libunwind, as crafter-build's own
|
||
|
|
# libc++ does. Programs come from the sandbox, never from the sysroot: the
|
||
|
|
# Sdk's own ninja and tools are aarch64 binaries. The C++ compiler cannot
|
||
|
|
# link a test program before there is a libc++ to link (Crafter.Build's
|
||
|
|
# WASI sysroot build skips the same check); the C compiler's still runs.
|
||
|
|
flatpak run --command=sh "$CRAFTER_REF" -eu -c '
|
||
|
|
cmake -S "$1/runtimes" -B "$2" -G Ninja \
|
||
|
|
-DCMAKE_BUILD_TYPE=Release \
|
||
|
|
-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR="$3" \
|
||
|
|
-DCMAKE_SYSROOT="$4" \
|
||
|
|
-DCMAKE_MAKE_PROGRAM=/usr/bin/ninja \
|
||
|
|
-DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER -DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY \
|
||
|
|
-DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=ONLY -DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ONLY \
|
||
|
|
-DCMAKE_C_COMPILER=/app/bin/clang -DCMAKE_CXX_COMPILER=/app/bin/clang++ -DCMAKE_ASM_COMPILER=/app/bin/clang \
|
||
|
|
-DCMAKE_CXX_COMPILER_WORKS=ON \
|
||
|
|
-DCMAKE_C_COMPILER_TARGET="$5" -DCMAKE_CXX_COMPILER_TARGET="$5" -DCMAKE_ASM_COMPILER_TARGET="$5" \
|
||
|
|
-DCMAKE_AR=/app/bin/llvm-ar -DCMAKE_RANLIB=/app/bin/llvm-ranlib -DCMAKE_NM=/app/bin/llvm-nm \
|
||
|
|
-DCMAKE_C_FLAGS="-march=$6" -DCMAKE_CXX_FLAGS="-march=$6" \
|
||
|
|
-DCMAKE_INSTALL_PREFIX="$7/usr" \
|
||
|
|
"-DLLVM_ENABLE_RUNTIMES=libcxx;libcxxabi;libunwind" \
|
||
|
|
-DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF \
|
||
|
|
-DLIBCXXABI_USE_LLVM_UNWINDER=ON \
|
||
|
|
-DLIBCXX_INSTALL_MODULES=ON \
|
||
|
|
-DLLVM_INCLUDE_TESTS=OFF -DLIBCXX_INCLUDE_TESTS=OFF -DLIBCXXABI_INCLUDE_TESTS=OFF \
|
||
|
|
-DLIBUNWIND_INCLUDE_TESTS=OFF -DLIBCXX_INCLUDE_BENCHMARKS=OFF
|
||
|
|
ninja -C "$2" install
|
||
|
|
' sh "$src" "$build" "$ARCH" "$SYSROOT" "$TRIPLE" "$MARCH" "$LIBCXX"
|
||
|
|
rm -rf "$build"
|
||
|
|
|
||
|
|
grep -q "_LIBCPP_VERSION 230102" "$LIBCXX/usr/include/c++/v1/__config" || { echo "the installed libc++ headers are not $LLVM_VERSION" >&2; exit 1; }
|
||
|
|
# Their licences go with the runtime libraries into the flatpak.
|
||
|
|
for component in libcxx libcxxabi libunwind; do
|
||
|
|
install -Dm644 "$src/$component/LICENSE.TXT" "$LIBCXX/licenses/$component-LICENSE.TXT"
|
||
|
|
done
|
||
|
|
cp -a "$LIBCXX/usr/." "$SYSROOT/usr/"
|
||
|
|
echo "$stamp" > "$SYSROOT/.made-from"
|
||
|
|
echo ">> done: $SYSROOT ($stamp)"
|