Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/architecture/packaging.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ The release workflow runs the three scripts directly. Without the parser pack, D

## Native runtimes

- `scripts/fetch-llamacpp.mjs` stages a pinned llama.cpp build, checked against its pinned SHA-256, into `electron/llamacpp/`, keeping `llama-server` and the libraries it links. Its rules, the pin, the Vulkan-only GPU backend and the pruning, are in [local-models/runtime.md](local-models/runtime.md).
- `scripts/fetch-llamacpp.mjs` stages a pinned llama.cpp build, checked against its pinned SHA-256, into `electron/llamacpp/`, keeping `llama-server` and the libraries it links. Its rules, the pin, the Vulkan-only GPU backend and the pruning, are in [local-models/runtime.md](local-models/runtime.md). It also stages each licence file the archive carries under its own name, and llama.cpp's own `LICENSE` on every platform. The Windows archives carry only LLVM OpenMP's (`LICENSE-LLVM-OpenMP`, for their `libomp.dll`), so that `LICENSE` comes from the copy in `scripts/llamacpp/`, taken at the pinned build. An archive that carries its own must match that copy, or the fetch stops.
- `scripts/sdcpp/stage.mjs`, which `build:sdcpp` runs, stages stable-diffusion.cpp's `sd-server` into `electron/sdcpp/` with the libraries it loads from beside itself and its licences, leaving `sd-cli` behind. It runs `sd-server --help` from the staged folder before it swaps the folder into place.
- Windows downloads upstream's Vulkan archive, checked against its pinned SHA-256, and copies the MSVC and OpenMP runtimes beside it, which the archive needs and does not carry.
- Linux and macOS compile the pinned commit, because upstream's Linux archives need glibc 2.38 and its macOS archive needs macOS 26.0. The recipe, `scripts/sdcpp/recipe.mjs`, builds Vulkan on Linux, with one ggml library per micro-architecture, and Metal on macOS, for 13.3.
Expand Down
2 changes: 1 addition & 1 deletion surfsense_local/electron/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ node_modules/
out/
dist/
release/
llamacpp/
/llamacpp/
/sdcpp/
/audiocpp/
/opencode/
6 changes: 4 additions & 2 deletions surfsense_local/electron/scripts/fetch-llamacpp.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { tmpdir } from "node:os"
import { basename, dirname, join } from "node:path"
import { fileURLToPath } from "node:url"

import { stageLicences } from "./llamacpp/licences.mjs"
import { renameWhenUnlocked } from "./windows-locks/rename-when-unlocked.mjs"

export const BUILD = "b11050"
Expand Down Expand Up @@ -183,8 +184,9 @@ async function main() {
bytes += lstatSync(from).size
}

const licence = find(unpacked, (entry) => entry.toUpperCase().startsWith("LICENSE"))
if (licence) copyFileSync(licence, join(stage, "LICENSE"))
// Each licence under its own name, and llama.cpp's own on every platform:
// the Windows archives carry only LLVM OpenMP's, for their libomp.dll.
stageLicences(source, stage)

verifyStage(stage)

Expand Down
36 changes: 36 additions & 0 deletions surfsense_local/electron/scripts/llamacpp/licences.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// llama.cpp's licence files, staged beside the server under their own names.
//
// The macOS and Linux archives carry llama.cpp's LICENSE. The Windows ones do
// not: their one licence file is LLVM OpenMP's, LICENSE-LLVM-OpenMP, for the
// libomp.dll beside llama-server. So llama.cpp's own comes from the copy kept
// here, taken at the pinned build, and an archive that carries one must match
// it, which keeps the copy current when the build moves.
import { copyFileSync, readdirSync, readFileSync } from "node:fs"
import { join } from "node:path"
import { fileURLToPath } from "node:url"

export const PINNED_LICENSE = fileURLToPath(new URL("llama.cpp-LICENSE", import.meta.url))

const OWN = "LICENSE"
// A bundled library's licence, as LICENSE-LLVM-OpenMP is.
const BUNDLED = /^LICENSE-.+/i

const text = (path) => readFileSync(path, "utf8").replace(/\r\n/g, "\n")

/** Copy the licence files in `source` into `stage`, and return their names. */
export function stageLicences(source, stage, pinned = PINNED_LICENSE) {
const names = readdirSync(source, { withFileTypes: true })
.filter((entry) => entry.isFile())
.map((entry) => entry.name)
const bundled = names.filter((name) => BUNDLED.test(name))
for (const name of bundled) copyFileSync(join(source, name), join(stage, name))

if (!names.includes(OWN)) {
copyFileSync(pinned, join(stage, OWN))
} else if (text(join(source, OWN)) === text(pinned)) {
copyFileSync(join(source, OWN), join(stage, OWN))
} else {
throw new Error(`the archive's ${OWN} differs from ${pinned}: replace that copy with the archive's`)
}
return [OWN, ...bundled].sort()
}
56 changes: 56 additions & 0 deletions surfsense_local/electron/scripts/llamacpp/licences.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import assert from "node:assert/strict"
import { mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os"
import { join } from "node:path"
import test from "node:test"

import { PINNED_LICENSE, stageLicences } from "./licences.mjs"

const OPENMP = "LLVM OpenMP's licence\n"
const pinned = () => readFileSync(PINNED_LICENSE, "utf8")

/** An unpacked archive holding `files`, and an empty stage beside it. */
function unpacked(t, files) {
const root = mkdtempSync(join(tmpdir(), "llamacpp-licences-"))
t.after(() => rmSync(root, { recursive: true, force: true }))
const source = join(root, "unpacked")
const stage = join(root, "stage")
mkdirSync(source)
mkdirSync(stage)
for (const [name, text] of Object.entries(files)) writeFileSync(join(source, name), text)
return { source, stage }
}

test("the kept copy is llama.cpp's MIT licence", () => {
assert.match(pinned(), /^MIT License/)
assert.match(pinned(), /The ggml authors/)
})

test("a Windows archive gets llama.cpp's licence from the kept copy, and OpenMP's under its own name", (t) => {
const { source, stage } = unpacked(t, {
"llama-server.exe": "",
"libomp.dll": "",
"LICENSE-LLVM-OpenMP": OPENMP,
})

assert.deepEqual(stageLicences(source, stage), ["LICENSE", "LICENSE-LLVM-OpenMP"])
assert.deepEqual(readdirSync(stage).sort(), ["LICENSE", "LICENSE-LLVM-OpenMP"])
assert.equal(readFileSync(join(stage, "LICENSE"), "utf8"), pinned())
assert.equal(readFileSync(join(stage, "LICENSE-LLVM-OpenMP"), "utf8"), OPENMP)
})

test("a macOS or Linux archive's own licence is staged when it matches the kept copy", (t) => {
const { source, stage } = unpacked(t, { "llama-server": "", LICENSE: pinned().replace(/\n/g, "\r\n") })

assert.deepEqual(stageLicences(source, stage), ["LICENSE"])
assert.equal(readFileSync(join(stage, "LICENSE"), "utf8"), pinned().replace(/\n/g, "\r\n"))
})

test("an archive whose licence differs from the kept copy stops the fetch", (t) => {
const { source, stage } = unpacked(t, {
"llama-server": "",
LICENSE: "MIT License\n\nCopyright (c) 2023-2027 The ggml authors\n",
})

assert.throws(() => stageLicences(source, stage), /differs from/)
})
21 changes: 21 additions & 0 deletions surfsense_local/electron/scripts/llamacpp/llama.cpp-LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023-2026 The ggml authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading