#!/bin/bash # Spellcast3D's image engine for Linux x64: the stable-diffusion.cpp fork, built like the Windows release # (CUDA 12.8, one build for GTX 10xx .. RTX 50xx, a CPU backend per instruction set, no WebP / WebM). # # Source: https://github.com/IgorAherne/stable-diffusion.cpp tag spellcast-perf-25d4127 # commit 25d412700bd616e4a9118abe6c5b42498de038f4 # its ggml submodule (https://github.com/IgorAherne/ggml) at a7c45a7573f85e7032c1cf5da02dcc2ff2b0230b # # build_engine_linux.sh [work folder, default ./engine-build] # # Writes /sd--bin-linux-cuda12-x64.zip: a flat folder of real files (no symlinks) - # libstable-diffusion.so, libggml.so, libggml-base.so, libggml-cuda.so, every libggml-cpu-*.so, sd-cli, sd-server, # the licence texts and SPELLCAST-BUILD.txt. Every binary looks for its libraries in its own folder first # (DT_RPATH $ORIGIN). NVIDIA's CUDA runtime is not in the zip: libcudart.so.12, libcublas.so.12 and # libcublasLt.so.12 are put next to the engine by the installer. # # Needs: gcc / g++, the CUDA 12.8 toolkit (CUDA_HOME, default /usr/local/cuda-12.8), cmake >= 3.18, ninja, git, # zip, readelf. Build on the oldest Linux the release should run on: the binaries need that system's glibc and # libstdc++ or newer. JOBS sets the parallel compile jobs (default: every core). set -euo pipefail TAG=spellcast-perf-25d4127 COMMIT=25d412700bd616e4a9118abe6c5b42498de038f4 GGML_COMMIT=a7c45a7573f85e7032c1cf5da02dcc2ff2b0230b REPO=https://github.com/IgorAherne/stable-diffusion.cpp.git # the Windows release's list; a plain number makes nvcc emit both the machine code (sm_NN) and the PTX CUDA_ARCHS="61;70;75;80;86;89;90;100;120" OUT="${1:?usage: build_engine_linux.sh [work folder]}" WORK="${2:-$PWD/engine-build}" JOBS="${JOBS:-$(nproc)}" CUDA_HOME="${CUDA_HOME:-/usr/local/cuda-12.8}" export PATH="$CUDA_HOME/bin:$PATH" mkdir -p "$OUT" "$WORK" OUT="$(cd "$OUT" && pwd)" WORK="$(cd "$WORK" && pwd)" SRC="$WORK/sd" BUILD="$WORK/build" PKG="$WORK/pkg" START=$(date +%s) # ---- the sources, at the pinned commits -------------------------------------------------------------------- [ -d "$SRC/.git" ] || git clone --branch "$TAG" "$REPO" "$SRC" git -C "$SRC" fetch --tags origin git -C "$SRC" checkout -f "$TAG" git -C "$SRC" submodule update --init ggml [ "$(git -C "$SRC" rev-parse HEAD)" = "$COMMIT" ] || { echo "the tag $TAG is not at $COMMIT" >&2; exit 1; } [ "$(git -C "$SRC/ggml" rev-parse HEAD)" = "$GGML_COMMIT" ] || { echo "ggml is not at $GGML_COMMIT" >&2; exit 1; } [ -z "$(git -C "$SRC" status --porcelain --untracked-files=no)" ] || { echo "the sources are modified" >&2; exit 1; } # ---- the build --------------------------------------------------------------------------------------------- # CMAKE_PLATFORM_NO_VERSIONED_SONAME: libggml.so and libggml-base.so are built under these names (no .so.0 # links), which are the names the launcher loads and the names the other binaries ask the loader for. # --disable-new-dtags: $ORIGIN as DT_RPATH, which is searched before LD_LIBRARY_PATH (a DT_RUNPATH is searched # after it, and another ggml on a user's library path would then be taken instead of this folder's). cmake -S "$SRC" -B "$BUILD" -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \ -DSD_CUDA=ON -DSD_BUILD_SHARED_LIBS=ON -DCMAKE_CUDA_ARCHITECTURES="$CUDA_ARCHS" \ -DCMAKE_CUDA_FLAGS="-Xcudafe --diag_suppress=177 -Xcudafe --diag_suppress=550" \ -DGGML_NATIVE=OFF -DSD_BUILD_SHARED_GGML_LIB=ON -DGGML_BACKEND_DL=ON -DGGML_CPU_ALL_VARIANTS=ON \ -DGGML_CUDA_NCCL=OFF -DSD_WEBP=OFF -DSD_WEBM=OFF -DSD_BUILD_EXAMPLES=ON -DSD_SERVER_BUILD_FRONTEND=OFF \ -DCMAKE_PLATFORM_NO_VERSIONED_SONAME=ON -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON '-DCMAKE_INSTALL_RPATH=$ORIGIN' \ -DCMAKE_SHARED_LINKER_FLAGS=-Wl,--disable-new-dtags -DCMAKE_MODULE_LINKER_FLAGS=-Wl,--disable-new-dtags \ -DCMAKE_EXE_LINKER_FLAGS=-Wl,--disable-new-dtags cmake --build "$BUILD" -j "$JOBS" # ---- the folder -------------------------------------------------------------------------------------------- rm -rf "$PKG" mkdir -p "$PKG" for name in sd-cli sd-server; do install -m 755 "$BUILD/bin/$name" "$PKG/$name" done find "$BUILD/bin" -maxdepth 1 -type f -name '*.so' -exec install -m 755 {} "$PKG/" \; for name in libstable-diffusion.so libggml.so libggml-base.so libggml-cuda.so libggml-cpu-x64.so; do [ -f "$PKG/$name" ] || { echo "the build did not make $name" >&2; exit 1; } done cp "$SRC/LICENSE" "$PKG/stable-diffusion.cpp.txt" cp "$SRC/ggml/LICENSE" "$PKG/ggml.txt" cp "$SRC/ggml/src/ggml-cuda/sageattention/LICENSE" "$PKG/sageattention.txt" cat > "$PKG/SPELLCAST-BUILD.txt" <&2; bad=1; } soname="$(echo "$dyn" | sed -n 's/.*(SONAME).*\[\(.*\)\]/\1/p')" [ -z "$soname" ] || [ "$soname" = "$name" ] || { echo "$name: its SONAME is $soname" >&2; bad=1; } for need in $(echo "$dyn" | sed -n 's/.*(NEEDED).*\[\(.*\)\]/\1/p'); do [ -f "$PKG/$need" ] && continue case " $ALLOWED " in *" $need "*) ;; *) echo "$name needs $need, which nothing provides" >&2; bad=1 ;; esac done done [ "$bad" = 0 ] || exit 1 # ---- the zip (flat; Unix permissions kept) ------------------------------------------------------------------- ZIP="$OUT/sd-$TAG-bin-linux-cuda12-x64.zip" rm -f "$ZIP" (cd "$PKG" && zip -9 -X -q "$ZIP" $(ls | LC_ALL=C sort)) echo "built in $(( $(date +%s) - START )) s" ls -l "$ZIP" sha256sum "$ZIP" (cd "$PKG" && for f in $(ls | LC_ALL=C sort); do printf '%s %s %s\n' "$(sha256sum "$f" | cut -d' ' -f1)" "$(stat -c %s "$f")" "$f"; done)