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.

ci: cache llama.cpp and MLX builds in S3 (#8945)

Hienf9d1d006328b
diff --git a/.github/workflows/template-tauri-build-linux-x64.yml b/.github/workflows/template-tauri-build-linux-x64.yml
index f0487e4fb..c9ee3e47a 100644
--- a/.github/workflows/template-tauri-build-linux-x64.yml
+++ b/.github/workflows/template-tauri-build-linux-x64.yml
@@ -56,6 +56,8 @@ on:
required: false
DELTA_AWS_SECRET_ACCESS_KEY:
required: false
+ DELTA_AWS_REGION:
+ required: false
TAURI_SIGNING_PRIVATE_KEY:
required: false
TAURI_SIGNING_PRIVATE_KEY_PASSWORD:
@@ -208,6 +210,22 @@ jobs:
yarn --version
yarn config set -H enableImmutableInstalls false
+ # Links a cached build of the pinned llama.cpp instead of compiling it
+ # (~80 of the ~100 minutes below). No credentials, or a missing,
+ # truncated or incomplete object, means the full cmake build. Key and
+ # layout: src-tauri/build-utils/engine-prebuilt.sh
+ - name: Restore the prebuilt llama.cpp engine
+ id: engine_prebuilt
+ continue-on-error: true
+ shell: bash
+ run: bash src-tauri/build-utils/engine-prebuilt.sh restore
+ env:
+ BUCKET: ${{ secrets.DELTA_AWS_S3_BUCKET_NAME }}
+ AWS_ACCESS_KEY_ID: ${{ secrets.DELTA_AWS_ACCESS_KEY_ID }}
+ AWS_SECRET_ACCESS_KEY: ${{ secrets.DELTA_AWS_SECRET_ACCESS_KEY }}
+ AWS_DEFAULT_REGION: ${{ secrets.DELTA_AWS_REGION }}
+ AWS_EC2_METADATA_DISABLED: 'true'
+
- name: Build app
run: |
make build
@@ -229,6 +247,25 @@ jobs:
JAN_SIGNING_KEY: ${{ secrets.JAN_SIGNING_KEY }}
TAURI_TRAY: libappindicator3
+ # Only after a miss, and only when the job builds main or a release tag
+ # itself: PR runs (github.ref refs/pull/N/merge) read the cache but never
+ # write it, and neither does a caller on main that builds another ref.
+ # `!cancelled()` rather than success() so the engine is kept when
+ # bundling fails later; the script itself refuses unless the worker was
+ # built and every output is present.
+ - name: Save the prebuilt llama.cpp engine
+ if: ${{ !cancelled() && steps.engine_prebuilt.outputs.key != '' && steps.engine_prebuilt.outputs.hit != 'true' && inputs.ref == github.ref && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')) }}
+ continue-on-error: true
+ shell: bash
+ run: bash src-tauri/build-utils/engine-prebuilt.sh save
+ env:
+ KEY: ${{ steps.engine_prebuilt.outputs.key }}
+ BUCKET: ${{ secrets.DELTA_AWS_S3_BUCKET_NAME }}
+ AWS_ACCESS_KEY_ID: ${{ secrets.DELTA_AWS_ACCESS_KEY_ID }}
+ AWS_SECRET_ACCESS_KEY: ${{ secrets.DELTA_AWS_SECRET_ACCESS_KEY }}
+ AWS_DEFAULT_REGION: ${{ secrets.DELTA_AWS_REGION }}
+ AWS_EC2_METADATA_DISABLED: 'true'
+
- name: Report ccache statistics
if: success() || failure()
shell: bash
diff --git a/.github/workflows/template-tauri-build-macos.yml b/.github/workflows/template-tauri-build-macos.yml
index 3080ffe45..10abb2454 100644
--- a/.github/workflows/template-tauri-build-macos.yml
+++ b/.github/workflows/template-tauri-build-macos.yml
@@ -50,6 +50,8 @@ on:
required: false
DELTA_AWS_SECRET_ACCESS_KEY:
required: false
+ DELTA_AWS_REGION:
+ required: false
CODE_SIGN_P12_BASE64:
required: false
CODE_SIGN_P12_PASSWORD:
@@ -203,6 +205,23 @@ jobs:
with:
variant: ${{ inputs.engine_variant }}
+ # Stages a cached mlx-server build instead of compiling it (~8 of the ~10
+ # minutes xcodebuild takes; resolving its Swift packages still runs, since
+ # the exact versions resolved are part of the key). No credentials, or a
+ # missing or incomplete object, means the full xcodebuild build. Key and
+ # layout: src-tauri/build-utils/mlx-prebuilt.sh
+ - name: Restore the prebuilt MLX server
+ id: mlx_prebuilt
+ continue-on-error: true
+ shell: bash
+ run: bash src-tauri/build-utils/mlx-prebuilt.sh restore
+ env:
+ BUCKET: ${{ secrets.DELTA_AWS_S3_BUCKET_NAME }}
+ AWS_ACCESS_KEY_ID: ${{ secrets.DELTA_AWS_ACCESS_KEY_ID }}
+ AWS_SECRET_ACCESS_KEY: ${{ secrets.DELTA_AWS_SECRET_ACCESS_KEY }}
+ AWS_DEFAULT_REGION: ${{ secrets.DELTA_AWS_REGION }}
+ AWS_EC2_METADATA_DISABLED: 'true'
+
- name: Build app
run: |
make build
@@ -222,6 +241,25 @@ jobs:
TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}
JAN_SIGNING_KEY: ${{ secrets.JAN_SIGNING_KEY }}
+ # Only after a miss, and only when the job builds main or a release tag
+ # itself: PR runs (github.ref refs/pull/N/merge) read the cache but never
+ # write it, and neither does a caller on main that builds another ref.
+ # `!cancelled()` rather than success() so the build is kept when signing
+ # or notarization fails later; the script itself refuses unless mlx-server
+ # was built and staged in this run.
+ - name: Save the prebuilt MLX server
+ if: ${{ !cancelled() && steps.mlx_prebuilt.outputs.key != '' && steps.mlx_prebuilt.outputs.hit != 'true' && inputs.ref == github.ref && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')) }}
+ continue-on-error: true
+ shell: bash
+ run: bash src-tauri/build-utils/mlx-prebuilt.sh save
+ env:
+ KEY: ${{ steps.mlx_prebuilt.outputs.key }}
+ BUCKET: ${{ secrets.DELTA_AWS_S3_BUCKET_NAME }}
+ AWS_ACCESS_KEY_ID: ${{ secrets.DELTA_AWS_ACCESS_KEY_ID }}
+ AWS_SECRET_ACCESS_KEY: ${{ secrets.DELTA_AWS_SECRET_ACCESS_KEY }}
+ AWS_DEFAULT_REGION: ${{ secrets.DELTA_AWS_REGION }}
+ AWS_EC2_METADATA_DISABLED: 'true'
+
- name: Report ccache statistics
if: success() || failure()
shell: bash
diff --git a/.github/workflows/template-tauri-build-windows-arm64.yml b/.github/workflows/template-tauri-build-windows-arm64.yml
index 4840c4167..1c14496d6 100644
--- a/.github/workflows/template-tauri-build-windows-arm64.yml
+++ b/.github/workflows/template-tauri-build-windows-arm64.yml
@@ -470,6 +470,22 @@ jobs:
corepack prepare [email protected] --activate
yarn config set -H enableImmutableInstalls false
+ # Links a cached build of the pinned llama.cpp instead of compiling it
+ # (~88 of the ~115 minutes below). No credentials, or a missing,
+ # truncated or incomplete object, means the full cmake build. Key and
+ # layout: src-tauri/build-utils/engine-prebuilt.sh
+ - name: Restore the prebuilt llama.cpp engine
+ id: engine_prebuilt
+ continue-on-error: true
+ shell: bash
+ run: bash src-tauri/build-utils/engine-prebuilt.sh restore
+ env:
+ BUCKET: ${{ secrets.DELTA_AWS_S3_BUCKET_NAME }}
+ AWS_ACCESS_KEY_ID: ${{ secrets.DELTA_AWS_ACCESS_KEY_ID }}
+ AWS_SECRET_ACCESS_KEY: ${{ secrets.DELTA_AWS_SECRET_ACCESS_KEY }}
+ AWS_DEFAULT_REGION: ${{ secrets.DELTA_AWS_REGION }}
+ AWS_EC2_METADATA_DISABLED: 'true'
+
- name: Build app
shell: bash
run: make build
@@ -486,6 +502,26 @@ jobs:
TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}
JAN_SIGNING_KEY: ${{ secrets.JAN_SIGNING_KEY }}
+ # Only after a miss, and only when the job builds main or a release tag
+ # itself: PR runs (github.ref refs/pull/N/merge) and this file's own
+ # branch push read the cache but never write it, and neither does a
+ # caller on main that builds another ref. `!cancelled()` rather than
+ # success() so the engine is kept when signing or bundling fails later;
+ # the script itself refuses unless the worker was built and every output
+ # is present.
+ - name: Save the prebuilt llama.cpp engine
+ if: ${{ !cancelled() && steps.engine_prebuilt.outputs.key != '' && steps.engine_prebuilt.outputs.hit != 'true' && (inputs.ref || github.ref) == github.ref && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')) }}
+ continue-on-error: true
+ shell: bash
+ run: bash src-tauri/build-utils/engine-prebuilt.sh save
+ env:
+ KEY: ${{ steps.engine_prebuilt.outputs.key }}
+ BUCKET: ${{ secrets.DELTA_AWS_S3_BUCKET_NAME }}
+ AWS_ACCESS_KEY_ID: ${{ secrets.DELTA_AWS_ACCESS_KEY_ID }}
+ AWS_SECRET_ACCESS_KEY: ${{ secrets.DELTA_AWS_SECRET_ACCESS_KEY }}
+ AWS_DEFAULT_REGION: ${{ secrets.DELTA_AWS_REGION }}
+ AWS_EC2_METADATA_DISABLED: 'true'
+
- name: Upload the engine build log
if: failure()
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
diff --git a/.github/workflows/template-tauri-build-windows-x64.yml b/.github/workflows/template-tauri-build-windows-x64.yml
index be38db2b6..75ec9dd4e 100644
--- a/.github/workflows/template-tauri-build-windows-x64.yml
+++ b/.github/workflows/template-tauri-build-windows-x64.yml
@@ -55,6 +55,8 @@ on:
required: false
DELTA_AWS_SECRET_ACCESS_KEY:
required: false
+ DELTA_AWS_REGION:
+ required: false
AZURE_KEY_VAULT_URI:
required: false
AZURE_CLIENT_ID:
@@ -289,6 +291,24 @@ jobs:
with:
variant: ${{ inputs.engine_variant }}
+ # Links a cached build of the pinned llama.cpp instead of compiling it
+ # (~80 of the ~115 minutes below). No credentials, or a missing,
+ # truncated or incomplete object, means the full cmake build. After the
+ # toolchain step, because the key reads the VCToolsVersion its
+ # msvc-dev-cmd exports for cuda variants; a vulkan-only build has no key
+ # and always compiles. Key and layout: src-tauri/build-utils/engine-prebuilt.sh
+ - name: Restore the prebuilt llama.cpp engine
+ id: engine_prebuilt
+ continue-on-error: true
+ shell: bash
+ run: bash src-tauri/build-utils/engine-prebuilt.sh restore
+ env:
+ BUCKET: ${{ secrets.DELTA_AWS_S3_BUCKET_NAME }}
+ AWS_ACCESS_KEY_ID: ${{ secrets.DELTA_AWS_ACCESS_KEY_ID }}
+ AWS_SECRET_ACCESS_KEY: ${{ secrets.DELTA_AWS_SECRET_ACCESS_KEY }}
+ AWS_DEFAULT_REGION: ${{ secrets.DELTA_AWS_REGION }}
+ AWS_EC2_METADATA_DISABLED: 'true'
+
- name: Build app
shell: bash
run: |
@@ -311,6 +331,25 @@ jobs:
TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}
JAN_SIGNING_KEY: ${{ secrets.JAN_SIGNING_KEY }}
+ # Only after a miss, and only when the job builds main or a release tag
+ # itself: PR runs (github.ref refs/pull/N/merge) read the cache but never
+ # write it, and neither does a caller on main that builds another ref.
+ # `!cancelled()` rather than success() so the engine is kept when signing
+ # or bundling fails later; the script itself refuses unless the worker
+ # was built and every output is present.
+ - name: Save the prebuilt llama.cpp engine
+ if: ${{ !cancelled() && steps.engine_prebuilt.outputs.key != '' && steps.engine_prebuilt.outputs.hit != 'true' && inputs.ref == github.ref && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')) }}
+ continue-on-error: true
+ shell: bash
+ run: bash src-tauri/build-utils/engine-prebuilt.sh save
+ env:
+ KEY: ${{ steps.engine_prebuilt.outputs.key }}
+ BUCKET: ${{ secrets.DELTA_AWS_S3_BUCKET_NAME }}
+ AWS_ACCESS_KEY_ID: ${{ secrets.DELTA_AWS_ACCESS_KEY_ID }}
+ AWS_SECRET_ACCESS_KEY: ${{ secrets.DELTA_AWS_SECRET_ACCESS_KEY }}
+ AWS_DEFAULT_REGION: ${{ secrets.DELTA_AWS_REGION }}
+ AWS_EC2_METADATA_DISABLED: 'true'
+
- name: Report ccache statistics
if: success() || failure()
shell: bash
diff --git a/Makefile b/Makefile
index cbe1bb0d7..1fe84341b 100644
--- a/Makefile
+++ b/Makefile
@@ -186,9 +186,12 @@ test-ci: test-prepare
check-cli:
cd src-tauri/jan-cli && cargo check --locked --no-default-features --features cli
-# Build MLX server (macOS Apple Silicon only) - always builds
+# Build MLX server (macOS Apple Silicon only) - always builds, unless
+# JAN_MLX_PREBUILT_DIR holds a cached build of the same inputs
+# (build-utils/mlx-prebuilt.sh), which is then staged and signed the same way
build-mlx-server:
ifeq ($(DETECTED_OS),Darwin)
+ifeq ($(JAN_MLX_PREBUILT_DIR),)
@echo "Building MLX server for Apple Silicon..."
# mlx-swift's Metal shaders are compiled by the PrepareMetalShaders
# plugin, which only runs under Xcode -- `swift build` produces a
@@ -196,8 +199,9 @@ ifeq ($(DETECTED_OS),Darwin)
# https://github.com/ml-explore/mlx-swift README ("SwiftPM (command
# line) cannot build the Metal shaders").
cd mlx-server && xcodebuild build -scheme mlx-server -destination 'platform=OS X' -configuration Release OTHER_LDFLAGS="-dead_strip"
+endif
@echo "Finding build products..."
- @DERIVED_DATA=$$(find ~/Library/Developer/Xcode/DerivedData/mlx-server-*/Build/Products/Release -maxdepth 0 2>/dev/null | head -1); \
+ @DERIVED_DATA=$${JAN_MLX_PREBUILT_DIR:-$$(find ~/Library/Developer/Xcode/DerivedData/mlx-server-*/Build/Products/Release -maxdepth 0 2>/dev/null | head -1)}; \
if [ -z "$$DERIVED_DATA" ] || [ ! -f "$$DERIVED_DATA/mlx-server" ]; then \
echo "Error: Could not find xcodebuild products under DerivedData"; \
exit 1; \
@@ -484,6 +488,11 @@ endif
check-engine-toolchain:
bash src-tauri/build-utils/check-engine-toolchain.sh $(JAN_ENGINE_VARIANT) $(ENGINE_FEATURES)
+# The S3 cache key for what build-engine would compile. Run through make so the
+# script sees the same resolved features and exported environment cargo gets.
+engine-prebuilt-key:
+ @bash src-tauri/build-utils/engine-prebuilt.sh key $(ENGINE_FEATURES)
+
# Release worker plus the ggml runtime it loads.
build-engine: engine-source check-engine-toolchain
@echo "Building llama.cpp engine worker (variant: $(JAN_ENGINE_VARIANT), features: $(ENGINE_FEATURES), jobs: $(if $(JAN_ENGINE_JOBS),$(JAN_ENGINE_JOBS),all cores))"
diff --git a/package.json b/package.json
index 9b5b4890e..8b62f7c86 100644
--- a/package.json
+++ b/package.json
@@ -14,7 +14,7 @@
"dev": "yarn dev:tauri",
"ios": "yarn tauri ios dev",
"android": "yarn tauri android dev",
- "build": "yarn build:web && yarn build:tauri",
+ "build": "yarn build:tauri",
"test": "yarn test:core && yarn test:web && yarn test:ext",
"test:core": "cross-env NODE_OPTIONS=--max-old-space-size=4096 vitest run --project @janhq/core",
"test:web": "cross-env NODE_OPTIONS=--max-old-space-size=4096 vitest run --project @janhq/web-app",
diff --git a/src-tauri/build-utils/engine-prebuilt.sh b/src-tauri/build-utils/engine-prebuilt.sh
new file mode 100755
index 000000000..82a00acc7
--- /dev/null
+++ b/src-tauri/build-utils/engine-prebuilt.sh
@@ -0,0 +1,146 @@
+#!/usr/bin/env bash
+# S3 cache of llama.cpp's cmake outputs, in the JAN_LLAMA_PREBUILT_DIR layout
+# build.rs links against: lib/ (stage 2 archives plus ggml-prefix/lib), an
+# empty include/ and bin/ (ggml-prefix/bin). Any failure here means no
+# prebuilt, which is exactly the full cmake build.
+#
+# key <features> print the object key for this job's native-build inputs,
+# exit 1 if not cacheable. Run it as `make engine-prebuilt-key`.
+# restore fetch + verify it, then export JAN_LLAMA_PREBUILT_DIR
+# (needs BUCKET + AWS env)
+# save pack this run's cmake outputs and upload them as $KEY
+# (needs BUCKET, KEY + AWS env)
+#
+# Run from the repo root, in the job env of `make build`.
+set -euo pipefail
+
+EPOCH=1 # hashed into the key with the rest of this file's code; bump to force every leg to rebuild
+P=src-tauri/plugins/tauri-plugin-llamacpp
+
+nocr() { tr -d '\r'; }
+unixpath() { if command -v cygpath >/dev/null 2>&1; then cygpath -u "$1"; else printf '%s\n' "$1"; fi; }
+# A shell script minus its comment and blank lines, so rewording one is not a rebuild
+script_code() { nocr < "$1" | sed '/^[[:space:]]*\(#.*\)\{0,1\}$/d'; }
+
+# build.rs's ARCHIVES, read rather than copied so the two cannot drift, minus
+# jan_llama_shim, which cc rebuilds on every build
+archives() {
+ nocr < "$P/build.rs" | sed -n '/const ARCHIVES: /,/];/s/^ *"\([^"]*\)",*$/\1/p' | grep -vx jan_llama_shim
+}
+
+# Everything the prebuilt link and stage-engine.sh need. build.rs itself only
+# checks that lib/ and include/ exist, so a gap would surface as a link error.
+is_complete() {
+ local a list
+ list=$(archives) || return 1
+ for a in $list ggml ggml-base; do # plus build.rs SHARED_LIBS; -e follows the .so symlink chain
+ [ -e "$1/lib/lib$a.a" ] || [ -e "$1/lib/$a.lib" ] || [ -e "$1/lib/lib$a.so" ] || return 1
+ done
+ [ -d "$1/include" ] && ls "$1"/bin/*ggml-cpu* >/dev/null 2>&1
+}
+
+case "${1:-}" in
+key)
+ features=${2:?usage: make engine-prebuilt-key}
+ [ -z "${JAN_LLAMA_CPP_DIR:-}" ] || exit 1 # a local checkout is not the pinned source
+ # ROCm's version is not keyed, and no desktop build ships the hip backend
+ [[ ",$features," != *,engine-hip,* ]] || exit 1
+ rs=$(nocr < "$P/build.rs")
+ tag=$(sed -n 's/^pub const LLAMA_CPP_TAG: &str = "\(.*\)";$/\1/p' <<<"$rs")
+ triple=$(rustc -vV | nocr | sed -n 's/^host: //p') # build-engine passes no --target
+ [ -n "$tag" ] && [ -n "$triple" ] || exit 1
+
+ t="$triple $features"
+ for v in JAN_ENGINE_CUDA_ARCHS CC CXX CFLAGS CXXFLAGS LDFLAGS CUDAFLAGS CUDAHOSTCXX \
+ CMAKE_GENERATOR CMAKE_TOOLCHAIN_FILE; do
+ t+=" $v=${!v:-}"
+ done
+ if [[ ",$features," == *,engine-cuda,* ]]; then
+ # Full version: it picks the default CUDA arch list, and the runtime
+ # stage-engine.sh copies from this toolkit must be at least as new
+ v=$(nvcc --version | nocr | sed -n 's/.*release [0-9.]*, \(V[0-9.]*\).*/\1/p')
+ [ -n "$v" ] || exit 1
+ t+=" nvcc=$v"
+ fi
+ if [[ ",$features," == *,engine-vulkan,* ]]; then
+ # glslc decides which GGML_VULKAN_*_GLSLC_SUPPORT shader sets get compiled
+ v=$(glslc --version | nocr | tr '\n' ' ')
+ [ -n "$v" ] || exit 1
+ t+=" glslc=$v"
+ fi
+ case "$triple" in
+ *-linux-gnu)
+ # Archives from a newer gcc need its newer libstdc++/glibc at link time
+ v=$(${CXX:-c++} -dumpfullversion)
+ [ -n "$v" ] || exit 1
+ t+=" gcc=$v $(getconf GNU_LIBC_VERSION)" ;;
+ *-windows-msvc)
+ # ponytail: needs a VS dev env (msvc-dev-cmd on x64 cuda, exported by the arm64 template);
+ # a vulkan-only Windows build has none and won't cache. Add a vswhere fallback if that matters.
+ v=${VCToolsVersion:-$(printf %s "${VCToolsInstallDir:-}" | nocr | tr '\\' / | sed 's#/*$##; s#.*/##')}
+ c=$(clang --version | nocr | sed -n '1s/.*version \([0-9]*\).*/\1/p')
+ [ -n "$v" ] && [ -n "$c" ] || exit 1
+ # major.minor: an older link.exe must not meet archives from a newer toolset
+ t+=" msvc=${v%.*} clang=$c" ;;
+ *)
+ exit 1 ;; # macOS: ~3 min engine with ccache, not worth caching
+ esac
+ echo "engine-prebuilt: $t" >&2
+
+ # One checked read per input: a slice that comes back empty, e.g. after
+ # `mod engine` is renamed, must make this uncacheable, not drop out of the key
+ pin=$(grep '^pub const LLAMA_CPP_' <<<"$rs" | grep -v '_VERSION:') # the pin, not its label
+ eng=$(awk '/^mod engine [{]/{m=1} m && NF && !/^[[:space:]]*\/\//' <<<"$rs") # cmake logic, minus comments
+ feat=$(nocr < "$P/Cargo.toml" | sed -n '/^\[features\]/,/^\[/p') # not CI's package.version stamp
+ fetch=$(script_code src-tauri/build-utils/fetch-engine-source.sh) # how that pin becomes a tree
+ self=$(script_code "$0") # this script, incl. EPOCH
+ [ -n "$pin" ] && [ -n "$eng" ] && [ -n "$feat" ] && [ -n "$fetch" ] && [ -n "$self" ] || exit 1
+ h=$(printf '%s\n' "$pin" "$eng" "$feat" "$fetch" "$self" "$t" | sha256sum | cut -c1-32)
+ echo "ci-cache/llama-prebuilt/$triple/$tag-${features//,/+}-$h.tar.gz" ;;
+
+restore)
+ [ -n "${BUCKET:-}" ] || exit 0 # no credentials in this context
+ k=$(make -s --no-print-directory engine-prebuilt-key) || {
+ echo "::notice::engine-prebuilt: not cacheable here"; exit 0; }
+ echo "key=$k" >> "$GITHUB_OUTPUT"
+ d="$RUNNER_TEMP/llama-prebuilt"
+ # D:/a/_temp/... is understood by bash, tar -C and build.rs's PathBuf alike
+ if command -v cygpath >/dev/null 2>&1; then d=$(cygpath -m "$d"); fi
+ rm -rf "$d" && mkdir -p "$d"
+ # Streamed: no second copy on disk, and GNU tar never parses "D:" in -f as a remote host
+ if aws s3 cp "s3://$BUCKET/$k" - | tar -xzf - -C "$d" && is_complete "$d"; then
+ echo "JAN_LLAMA_PREBUILT_DIR=$d" >> "$GITHUB_ENV"
+ echo "hit=true" >> "$GITHUB_OUTPUT"
+ echo "::notice::engine-prebuilt: hit $k"
+ else
+ rm -rf "$d"
+ echo "::notice::engine-prebuilt: miss $k"
+ fi ;;
+
+save)
+ [ -n "${BUCKET:-}" ] && [ -n "${KEY:-}" ] || exit 0
+ exe=
+ case "$(uname -s)" in MINGW*|MSYS*|CYGWIN*) exe=.exe ;; esac
+ # A non-empty staged worker proves both cmake stages built and linked in this run
+ [ -s "src-tauri/resources/bin/jan-llama-worker$exe" ] || {
+ echo "::warning::engine-prebuilt: engine not built this run; not saving"; exit 0; }
+ marker=$(ls -t "${CARGO_TARGET_DIR:-$P/target}"/release/build/tauri-plugin-llamacpp-*/out/engine-build-root.txt | head -1)
+ root=$(unixpath "$(nocr < "$marker" | tr -d '\n')") # relocated out of OUT_DIR on Windows
+ o=$(unixpath "$RUNNER_TEMP")/llama-prebuilt-pack
+ rm -rf "$o" && mkdir -p "$o/lib" "$o/include" "$o/bin"
+ # `-exec ... +` rather than `\;`, so a failed copy fails the save instead of being ignored
+ for a in $(archives); do # Ninja Multi-Config puts the Windows ones under Release/
+ find "$root/llama-build" -maxdepth 4 \( -name "lib$a.a" -o -name "$a.lib" \) -exec cp -t "$o/lib/" {} +
+ done
+ # soname symlink chains (Linux) or the ggml.lib/ggml-base.lib import libs (Windows); not lib/cmake
+ find "$root/ggml-prefix/lib" -maxdepth 1 ! -type d -exec cp -P -t "$o/lib/" {} +
+ # Verbatim: the backend modules, plus ggml.dll and ggml-base.dll on Windows
+ cp -P "$root/ggml-prefix/bin/"* "$o/bin/"
+ is_complete "$o" || { echo "::warning::engine-prebuilt: incomplete cmake outputs; not saving"; exit 0; }
+ tar -czf - -C "$o" lib include bin | aws s3 cp --no-progress - "s3://$BUCKET/$KEY"
+ echo "::notice::engine-prebuilt: saved $KEY" ;;
+
+*)
+ echo "usage: $0 key <features>|restore|save" >&2
+ exit 2 ;;
+esac
diff --git a/src-tauri/build-utils/mlx-prebuilt.sh b/src-tauri/build-utils/mlx-prebuilt.sh
new file mode 100755
index 000000000..e1135af0f
--- /dev/null
+++ b/src-tauri/build-utils/mlx-prebuilt.sh
@@ -0,0 +1,75 @@
+#!/usr/bin/env bash
+# S3 cache of the mlx-server build: the executable plus mlx-swift's Metal bundle,
+# as xcodebuild leaves them (unsigned), which `make build-mlx-server` then stages
+# and signs from JAN_MLX_PREBUILT_DIR instead of building. Any failure here means
+# no prebuilt, which is exactly the xcodebuild build.
+#
+# restore resolve the Swift packages, fetch + verify the object for exactly
+# that resolution, then export JAN_MLX_PREBUILT_DIR (needs BUCKET + AWS env)
+# save pack this run's xcodebuild products and upload them as $KEY
+# (needs BUCKET, KEY + AWS env)
+#
+# macOS only. Run from the repo root.
+set -euo pipefail
+
+EPOCH=1 # hashed into the key with the rest of this file's code; bump to force a rebuild
+
+# The two things the Makefile copies out of the build products
+is_complete() {
+ [ -x "$1/mlx-server" ] && [ -n "$(find "$1/mlx-swift_Cmlx.bundle" -name default.metallib -print -quit 2>/dev/null)" ]
+}
+
+case "${1:-}" in
+restore)
+ [ -n "${BUCKET:-}" ] || exit 0 # no credentials in this context
+ # Package.swift takes version ranges and no Package.resolved is committed, so
+ # what gets built is whatever resolves today: key on that, not the manifest.
+ # xcodebuild keeps the checkouts it fetches here, so a miss fetches them once.
+ if ! out=$(cd mlx-server && xcodebuild -resolvePackageDependencies -scheme mlx-server 2>&1); then
+ printf '%s\n' "$out" | tail -20
+ echo "::notice::mlx-prebuilt: package resolution failed; not cacheable here"
+ exit 0
+ fi
+ resolved=$(printf '%s\n' "$out" | sed -n '/^Resolved source packages:/,/^[[:space:]]*$/p' | grep ' @ ' | LC_ALL=C sort || true)
+ [ -n "$resolved" ] || resolved=$(cat mlx-server/Package.resolved \
+ mlx-server/.swiftpm/xcode/package.xcworkspace/xcshareddata/swiftpm/Package.resolved 2>/dev/null || true)
+ xcode=$(xcodebuild -version | tr '\n' ' ')
+ metal=$(xcrun metal --version 2>/dev/null | head -1 || true) # when readable; the toolchain ships apart from Xcode
+ flags=$(grep -F 'xcodebuild build' Makefile || true)
+ src=$(find mlx-server/Package.swift mlx-server/Sources -type f | LC_ALL=C sort | xargs shasum -a 256)
+ self=$(sed '/^[[:space:]]*\(#.*\)\{0,1\}$/d' "$0")
+ if [ -z "$resolved" ] || [ -z "$flags" ] || [ -z "$src" ]; then
+ echo "::notice::mlx-prebuilt: a key input came back empty; not cacheable here"
+ exit 0
+ fi
+ echo "mlx-prebuilt: $(uname -m) $xcode metal=$metal"
+ printf '%s\n' "$resolved"
+ h=$(printf '%s\n' "$(uname -m)" "$xcode" "$metal" "$resolved" "$flags" "$src" "$self" | shasum -a 256 | cut -c1-32)
+ k="ci-cache/mlx-server/$(uname -m)-$h.tar.gz"
+ echo "key=$k" >> "$GITHUB_OUTPUT"
+ d="$RUNNER_TEMP/mlx-prebuilt"
+ rm -rf "$d" && mkdir -p "$d"
+ if aws s3 cp "s3://$BUCKET/$k" - | tar -xzf - -C "$d" && is_complete "$d"; then
+ echo "JAN_MLX_PREBUILT_DIR=$d" >> "$GITHUB_ENV"
+ echo "hit=true" >> "$GITHUB_OUTPUT"
+ echo "::notice::mlx-prebuilt: hit $k"
+ else
+ rm -rf "$d"
+ echo "::notice::mlx-prebuilt: miss $k"
+ fi ;;
+
+save)
+ [ -n "${BUCKET:-}" ] && [ -n "${KEY:-}" ] || exit 0
+ # A staged binary proves xcodebuild built it in this run
+ [ -x src-tauri/resources/bin/mlx-server ] || {
+ echo "::warning::mlx-prebuilt: mlx-server not built this run; not saving"; exit 0; }
+ p=$(find ~/Library/Developer/Xcode/DerivedData/mlx-server-*/Build/Products/Release -maxdepth 0 2>/dev/null | head -1 || true)
+ [ -n "$p" ] && is_complete "$p" || {
+ echo "::warning::mlx-prebuilt: incomplete xcodebuild products; not saving"; exit 0; }
+ tar -czf - -C "$p" mlx-server mlx-swift_Cmlx.bundle | aws s3 cp --no-progress - "s3://$BUCKET/$KEY"
+ echo "::notice::mlx-prebuilt: saved $KEY" ;;
+
+*)
+ echo "usage: $0 restore|save" >&2
+ exit 2 ;;
+esac
diff --git a/src-tauri/build-utils/stage-engine.sh b/src-tauri/build-utils/stage-engine.sh
index 7f3e84a9c..fef97c99f 100755
--- a/src-tauri/build-utils/stage-engine.sh
+++ b/src-tauri/build-utils/stage-engine.sh
@@ -39,9 +39,12 @@ newest() {
| xargs -0 -r ls -dt 2>/dev/null | head -1 || true
}
-# On Windows build.rs may relocate the cmake trees out of OUT_DIR (MAX_PATH)
-# and leave the chosen root in a marker file.
-PREFIX="$(newest -type d -name ggml-prefix)"
+# A prebuilt engine (JAN_LLAMA_PREBUILT_DIR, see engine-prebuilt.sh) has no
+# cmake tree, but its lib/ and bin/ mirror ggml-prefix's, so it stands in for
+# one -- first, so a stale local ggml-prefix cannot win. On Windows build.rs may
+# relocate the cmake trees out of OUT_DIR (MAX_PATH) and leave the chosen root
+# in a marker file.
+PREFIX="${JAN_LLAMA_PREBUILT_DIR:-$(newest -type d -name ggml-prefix)}"
if [ -z "$PREFIX" ]; then
MARKER="$(newest -type f -name engine-build-root.txt)"
if [ -n "$MARKER" ]; then
diff --git a/src-tauri/plugins/tauri-plugin-llamacpp/build.rs b/src-tauri/plugins/tauri-plugin-llamacpp/build.rs
index 5a9bc1993..68bc6edf6 100644
--- a/src-tauri/plugins/tauri-plugin-llamacpp/build.rs
+++ b/src-tauri/plugins/tauri-plugin-llamacpp/build.rs
@@ -256,9 +256,11 @@ mod engine {
// Prebuilt path: a matrix job compiled both stages once and published
// them, so the app build only links. No network, no cmake, no nvcc.
- // Layout is `lib/` (stage 2 .a plus stage 1 ggml shared libs),
- // `include/` (llama.cpp's *generated* headers, e.g. build-info.h) and
- // `backends/` (the runtime-loaded ggml modules).
+ // Layout (produced by build-utils/engine-prebuilt.sh): `lib/` (stage 2
+ // static archives plus ggml-prefix/lib, the ggml link inputs),
+ // `include/` (may be empty; the shim's headers all come from the
+ // source tree) and `bin/` (ggml-prefix/bin: the runtime-loaded backend
+ // modules, plus ggml.dll and ggml-base.dll on Windows).
let (mut search_dirs, generated_include, backend_dir) =
if let Ok(dir) = env::var("JAN_LLAMA_PREBUILT_DIR") {
let dir = PathBuf::from(dir);
@@ -269,7 +271,7 @@ mod engine {
"JAN_LLAMA_PREBUILT_DIR={} must contain lib/ and include/",
dir.display()
);
- (vec![lib], inc, dir.join("backends"))
+ (vec![lib], inc, dir.join("bin"))
} else {
let root = build_root();
let ggml = build_ggml(&src, &root);