Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

build(engine): stage the CUDA runtime beside the worker

Akarshan Biswasde8daa9fbfb7
diff --git a/src-tauri/build-utils/stage-engine.sh b/src-tauri/build-utils/stage-engine.sh
index 1b07f053e..f41466123 100755
--- a/src-tauri/build-utils/stage-engine.sh
+++ b/src-tauri/build-utils/stage-engine.sh
@@ -9,6 +9,13 @@
# merely somewhere in the bundle. libggml/libggml-base are ordinary shared
# libraries found via the rpath ($ORIGIN / @loader_path) build.rs emits, which
# resolves to the same directory.
+#
+# A CUDA module additionally links the toolkit's cudart/cublas/cublasLt at
+# load time, and nothing on a user's machine provides those (the driver ships
+# libcuda only). They are copied out of the build host's toolkit beside the
+# module, so the bundle's tauri.<os>.conf.json globs are lib*.so* / *.dll rather
+# than libggml*: a Vulkan-only bundle has no cudart to match, and the bundler
+# treats a glob with zero matches as an error.
set -euo pipefail
# Positional first: cmd.exe has no `VAR=value command` prefix, so the Windows
@@ -41,6 +48,16 @@ if [ ! -f "$WORKER" ]; then
fi
mkdir -p "$DEST"
+
+# The bundle globs take whatever sits in DEST, so a module or runtime left by
+# an earlier variant's staging would ship with this one.
+purge_stale() {
+ find "$DEST" -maxdepth 1 \( -name 'libggml*' -o -name 'ggml*.dll' \
+ -o -name 'libcudart.so*' -o -name 'libcublas*.so*' \
+ -o -name 'cudart64_*.dll' -o -name 'cublas*.dll' \) -exec rm -f {} +
+}
+purge_stale
+
install -m755 "$WORKER" "$DEST/jan-llama-worker$EXE"
echo "stage-engine: staged jan-llama-worker$EXE"
@@ -92,6 +109,7 @@ done
# stops the bundle carrying the same bytes three times.
staged=0
modules=0
+cuda_module=""
for src in "${srcs[@]}"; do
[ -e "$src" ] || [ -L "$src" ] || continue
name="$(basename "$src")"
@@ -100,6 +118,7 @@ for src in "${srcs[@]}"; do
staged=$((staged + 1))
case "$name" in
libggml.*|libggml-base.*|ggml.dll|ggml-base.dll) ;;
+ libggml-cuda.*|ggml-cuda.dll) modules=$((modules + 1)); cuda_module="$DEST/$name" ;;
*) modules=$((modules + 1)) ;;
esac
done
@@ -119,3 +138,78 @@ if [ "$modules" -eq 0 ]; then
fi
echo "stage-engine: staged $staged ggml libraries ($modules backend modules) into $DEST"
+
+[ -n "$cuda_module" ] || exit 0
+
+# The toolkit that compiled the module is the one to ship from: its runtime
+# major matches by construction. CUDA_PATH is what the Windows installer sets,
+# CUDA_HOME the usual Linux spelling, nvcc's parent the fallback for both.
+cuda_root() {
+ local d
+ for d in "${CUDA_PATH:-}" "${CUDA_HOME:-}"; do
+ if [ -n "$d" ] && [ -d "$d" ]; then echo "$d"; return 0; fi
+ done
+ local nvcc
+ nvcc="$(command -v nvcc 2>/dev/null || true)"
+ [ -n "$nvcc" ] || return 1
+ (cd "$(dirname "$nvcc")/.." && pwd)
+}
+
+ROOT="$(cuda_root)" || {
+ echo "stage-engine: staged $(basename "$cuda_module") but found no CUDA toolkit" >&2
+ echo "stage-engine: set CUDA_PATH/CUDA_HOME or put nvcc on PATH to stage its runtime" >&2
+ exit 1
+}
+
+# CUDA 13 moved the Windows DLLs from bin/ to bin/x64/. On Linux lib64 is a
+# symlink to targets/<arch>/lib, so the first directory that holds cudart wins
+# rather than copying the same files twice.
+if [ "$LIBEXT" = "dll" ]; then
+ candidates=("$ROOT/bin/x64" "$ROOT/bin")
+ runtime_globs=('cudart64_*.dll' 'cublas64_*.dll' 'cublasLt64_*.dll')
+else
+ candidates=("$ROOT/lib64" "$ROOT"/targets/*/lib)
+ runtime_globs=('libcudart.so.[0-9]*' 'libcublas.so.[0-9]*' 'libcublasLt.so.[0-9]*')
+fi
+
+LIBDIR=""
+for d in "${candidates[@]}"; do
+ if compgen -G "$d/${runtime_globs[0]}" >/dev/null; then LIBDIR="$d"; break; fi
+done
+[ -n "$LIBDIR" ] || {
+ echo "stage-engine: no ${runtime_globs[0]} under $ROOT (looked in: ${candidates[*]})" >&2
+ exit 1
+}
+
+runtime=0
+for pat in "${runtime_globs[@]}"; do
+ matched=0
+ for src in "$LIBDIR"/$pat; do
+ [ -e "$src" ] || [ -L "$src" ] || continue
+ name="$(basename "$src")"
+ cp -Pf "$src" "$DEST/$name"
+ [ -L "$src" ] || chmod 644 "$DEST/$name"
+ matched=$((matched + 1))
+ done
+ if [ "$matched" -eq 0 ]; then
+ echo "stage-engine: $LIBDIR has no $pat; the CUDA module would not load without it" >&2
+ exit 1
+ fi
+ runtime=$((runtime + matched))
+done
+
+# The name-pattern copy above is a guess about what the module wants; the ELF
+# NEEDED list is the fact. A missing entry here is exactly the silent
+# fall-to-Vulkan this step exists to prevent.
+if [ "$LIBEXT" = "so" ] && command -v objdump >/dev/null 2>&1; then
+ missing=""
+ while read -r needed; do
+ [ -e "$DEST/$needed" ] || missing="$missing $needed"
+ done < <(objdump -p "$cuda_module" | awk '/NEEDED/ && $2 ~ /^libcu(dart|blas)/ {print $2}')
+ if [ -n "$missing" ]; then
+ echo "stage-engine: $(basename "$cuda_module") needs$missing, not staged from $LIBDIR" >&2
+ exit 1
+ fi
+fi
+
+echo "stage-engine: staged $runtime CUDA runtime libraries from $LIBDIR"
diff --git a/src-tauri/tauri.linux.conf.json b/src-tauri/tauri.linux.conf.json
index e7d676e09..201dfecf1 100644
--- a/src-tauri/tauri.linux.conf.json
+++ b/src-tauri/tauri.linux.conf.json
@@ -27,7 +27,7 @@
"resources/LICENSE",
"resources/bin/jan",
"resources/bin/jan-llama-worker",
- "resources/bin/libggml*.so*"
+ "resources/bin/lib*.so*"
],
"externalBin": [
"resources/bin/uv"
diff --git a/src-tauri/tauri.windows.conf.json b/src-tauri/tauri.windows.conf.json
index 4604c4585..22eda38fc 100644
--- a/src-tauri/tauri.windows.conf.json
+++ b/src-tauri/tauri.windows.conf.json
@@ -43,7 +43,7 @@
"resources/LICENSE",
"resources/bin/jan.exe",
"resources/bin/jan-llama-worker.exe",
- "resources/bin/ggml*.dll"
+ "resources/bin/*.dll"
],
"externalBin": [
"resources/bin/bun",