#!/usr/bin/env bash # Copyright (C) 2026 Zaher Salman # SPDX-License-Identifier: GPL-3.0-or-later # # Build a relocatable ROOT + musrfit runtime for muSRFlow. Runs inside the # container built from packaging/runtime/Dockerfile (see build.sh for the host # wrapper). The repo is mounted at /work, so the finished tree lands directly in # /work/runtime where electron-builder ships it as extraResources. # # Environment overrides: # ROOT_VERSION ROOT release to build (default 6.38.00) # ROOT_SOURCE "build" (default) or "binary" # ROOT_BINARY_URL source tarball URL for ROOT_SOURCE=binary # MUSRFIT_REPO path to the musrfit checkout (default /src/musrfit) # PREFIX install prefix (default /work/runtime) # JOBS build parallelism (default: nproc) set -euo pipefail ROOT_VERSION="${ROOT_VERSION:-6.38.00}" ROOT_SOURCE="${ROOT_SOURCE:-build}" MUSRFIT_REPO="${MUSRFIT_REPO:-/src/musrfit}" PREFIX="${PREFIX:-/work/runtime}" JOBS="${JOBS:-$(nproc)}" ROOT_TARBALL_URL="https://root.cern/download/root_v${ROOT_VERSION}.source.tar.gz" case "$ROOT_SOURCE" in build|binary) ;; *) echo "ERROR: ROOT_SOURCE must be build or binary" >&2; exit 1 ;; esac # ROOT 6.38 predates CMake 4 and its CMakeLists uses a construct that CMake 4 # rejects ("Unknown arguments specified"). The manylinux base image ships a # CMake 4 in /usr/local/bin that shadows the baseos CMake 3.x in /usr/bin, so # prefer a 3.x CMake when the default is 4.x. CMAKE_BIN="${CMAKE_BIN:-cmake}" if "$CMAKE_BIN" --version 2>/dev/null | head -n1 | grep -q 'version 4\.'; then for cand in /usr/bin/cmake /bin/cmake; do if [ -x "$cand" ] && "$cand" --version | head -n1 | grep -q 'version 3\.'; then CMAKE_BIN="$cand" break fi done fi echo "Using CMake: ${CMAKE_BIN} ($("$CMAKE_BIN" --version | head -n1))" # Host-provided libraries that must NOT be bundled: the AppImage contract keeps # the C/C++ runtime, the dynamic loader, and the low-level system calls on the # host so the bundle works across distributions. SYSTEM_LIB_RE='^(libc|libm|libmvec|libdl|libpthread|librt|libgcc_s|libstdc\+\+|ld-linux[^/]*|libresolv|libutil|libnsl|libcrypt|libanl|libasound|libdl)\.so' log() { printf '\n=== %s ===\n' "$*"; } log "Building ROOT ${ROOT_VERSION} (${ROOT_SOURCE}) into ${PREFIX}" mkdir -p "$PREFIX" if [ "$ROOT_SOURCE" = "binary" ]; then : "${ROOT_BINARY_URL:?ROOT_BINARY_URL is required when ROOT_SOURCE=binary}" wget -q -O /tmp/root.tar.gz "$ROOT_BINARY_URL" mkdir -p /tmp/root-bin tar -C /tmp/root-bin --strip-components=1 -xzf /tmp/root.tar.gz cp -a /tmp/root-bin/. "$PREFIX/" else wget -q -O /tmp/root.tar.gz "$ROOT_TARBALL_URL" mkdir -p /tmp/root-src tar -C /tmp/root-src --strip-components=1 -xzf /tmp/root.tar.gz "$CMAKE_BIN" -S /tmp/root-src -B /tmp/root-build \ -DCMAKE_INSTALL_PREFIX="$PREFIX" \ -Dgnuinstall=OFF \ -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_INSTALL_RPATH='$ORIGIN/../lib' \ -Droofit=OFF \ -Dtmva=OFF \ -Dhttp=OFF \ -Dsqlite=OFF \ -Dbuiltin_xrootd=OFF \ -Dmathmore=ON \ -Dx11=ON \ -Dopengl=OFF \ -Dpython=OFF \ -Dpythia8=OFF "$CMAKE_BIN" --build /tmp/root-build -j "$JOBS" "$CMAKE_BIN" --install /tmp/root-build fi log "Building musrfit against ${PREFIX}" # Select the ROOT we just built, not a system installation. The source is a # read-only mount and may have a different owner inside the CI container. export ROOTSYS="$PREFIX" export PATH="$PREFIX/bin:$PATH" export LD_LIBRARY_PATH="$PREFIX/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" git config --global --add safe.directory "$MUSRFIT_REPO" "$CMAKE_BIN" -S "$MUSRFIT_REPO" -B /tmp/musrfit-build \ -DCMAKE_INSTALL_PREFIX="$PREFIX" \ -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_INSTALL_RPATH='$ORIGIN/../lib' \ -DROOT_DIR="$PREFIX/cmake" \ -Dqt_based_tools=OFF \ -Dtry_OpenMP=ON "$CMAKE_BIN" --build /tmp/musrfit-build -j "$JOBS" "$CMAKE_BIN" --install /tmp/musrfit-build # musrfit resolves its default startup file via MUSRFITPATH (set by muSRFlow to # the runtime root); ship a sane template so a fresh install has one. if [ -f /work/packaging/runtime/musrfit_startup.xml ] && [ ! -f "$PREFIX/musrfit_startup.xml" ]; then cp /work/packaging/runtime/musrfit_startup.xml "$PREFIX/musrfit_startup.xml" fi log "Patching runpaths to \$ORIGIN" if command -v patchelf >/dev/null 2>&1; then # Patch each file individually: bin/ mixes ELF binaries with shell scripts # (thisroot.sh, root-config, ...), and patchelf aborts on the first non-ELF # file, which would leave every ELF untouched if batched in one xargs call. while IFS= read -r -d '' f; do patchelf --set-rpath '$ORIGIN/../lib' "$f" 2>/dev/null || true done < <(find "$PREFIX/bin" -type f -perm -u+x -print0 2>/dev/null) while IFS= read -r -d '' f; do patchelf --set-rpath '$ORIGIN' "$f" 2>/dev/null || true done < <(find "$PREFIX/lib" -maxdepth 1 -name '*.so*' -type f -print0 2>/dev/null) else echo "WARNING: patchelf not found; the runtime may depend on absolute paths" >&2 fi log "Bundling non-system shared libraries into ${PREFIX}/lib" bundle_deps() { local dest="$PREFIX/lib" local libs base libs="$( { find "$PREFIX/bin" -type f -perm -u+x -exec ldd {} \; 2>/dev/null || true find "$PREFIX/lib" -maxdepth 1 -name '*.so*' -type f -exec ldd {} \; 2>/dev/null || true } | awk '/=>/ && $3 ~ /^\// {print $3}' | sort -u )" while IFS= read -r lib; do [ -n "$lib" ] || continue case "$lib" in "$PREFIX"/*) continue ;; esac base="$(basename "$lib")" if printf '%s\n' "$base" | grep -Eq "$SYSTEM_LIB_RE"; then continue fi if [ ! -e "$dest/$base" ]; then cp -Lf "$lib" "$dest/$base" if command -v patchelf >/dev/null 2>&1; then patchelf --set-rpath '$ORIGIN' "$dest/$base" 2>/dev/null || true fi fi done <<< "$libs" } bundle_deps log "Verifying required tools" build_info=(--source-repo "$MUSRFIT_REPO" --root-version "$ROOT_VERSION" --root-source "$ROOT_SOURCE" --musrfit-cache /tmp/musrfit-build/CMakeCache.txt) if [ "$ROOT_SOURCE" = "build" ]; then build_info+=(--root-cache /tmp/root-build/CMakeCache.txt) fi if [ -n "${BASE_IMAGE:-}" ]; then build_info+=(--base-image "$BASE_IMAGE") fi bash "$(dirname "${BASH_SOURCE[0]}")/validate-runtime.sh" --record-manifest "$PREFIX" "${build_info[@]}" log "Result" du -sh "$PREFIX" ls "$PREFIX/bin" | head -40 echo "musrfit_startup.xml present: $([ -f "$PREFIX/musrfit_startup.xml" ] && echo yes || echo no)"