Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: only build one binary for all node versions #50

Merged
merged 8 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
46 changes: 21 additions & 25 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,30 +126,30 @@ jobs:
const data = await res.json();
const versions = new Map();
let latestVersion = null;

for (const version of data) {
const majorVersion = Number(version.version.split(".")[0].slice("v".length));
const versionDate = new Date(version.date);

if (maxDate != null && versionDate.getTime() > maxDate)
continue;

if (!versions.has(majorVersion)) {
versions.set(majorVersion, version.version);
}

if (latestVersion === null || majorVersion > latestVersion) {
latestVersion = majorVersion;
}
}

return {versions, latestVersion};
}

function getArches() {
switch (process.env.ARTIFACT_NAME) {
case "win":
return ["x64"];
return ["x64", "arm64"];
case "linux":
return ["x64", "arm64", "armv7l", "ppc64le"];
case "mac":
Expand All @@ -159,32 +159,28 @@ jobs:
return ["x64"];
}

const {versions: latestNodeVersions, latestVersion: latestNodeVersion} = await getLatestNodeVersions(Date.now() - 1000 * 60 * 60 * 24 * 14);

const minNodeVersion = latestNodeVersion - 4;
const {versions: latestNodeVersions} = await getLatestNodeVersions(Date.now() - 1000 * 60 * 60 * 24 * 14);

const nodeVersions = [...latestNodeVersions].reduce((acc, [majorVersion, version]) => {
if (majorVersion >= minNodeVersion)
acc.push(version);

return acc;
}, []);
const nodeVersion = latestNodeVersions.get(18);
const windowsOnArmNodeVersion = latestNodeVersions.get(20);
const arches = getArches();

console.log("Building for node versions", nodeVersions, "and archs", arches);
console.log("Building for node version", nodeVersion, "and archs", arches);

await $`mkdir -p llamaBins`;

for (const nodeVersion of nodeVersions) {
for (const arch of arches) {
console.log(`Building ${arch} for node ${nodeVersion}`);

const majorNodeVersion = parseInt(nodeVersion.slice("v".length))

const binName = `${process.env.ARTIFACT_NAME}-${arch}-${majorNodeVersion}`;
await $`node ./dist/cli/cli.js build --arch ${arch} --nodeTarget ${nodeVersion}`;
await $`mv ./llama/build/Release ${"./llamaBins/" + binName}`;
for (const arch of arches) {
let buildNodeVersion = nodeVersion;

if (process.env.ARTIFACT_NAME === "win" && arch === "arm64") {
buildNodeVersion = windowsOnArmNodeVersion;
}

console.log(`Building ${arch} for node ${buildNodeVersion}`);

const binName = `${process.env.ARTIFACT_NAME}-${arch}`;
await $`node ./dist/cli/cli.js build --arch ${arch} --nodeTarget ${buildNodeVersion}`;
await $`mv ./llama/build/Release ${"./llamaBins/" + binName}`;
}

await $`echo "Built binaries:"`;
Expand Down
23 changes: 9 additions & 14 deletions src/utils/getBin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,15 @@ import {getCompiledLlamaCppBinaryPath} from "./compileLLamaCpp.js";
const require = createRequire(import.meta.url);

export async function getPrebuildBinPath(): Promise<string | null> {
const majorNodeVersion = parseInt(process.version.slice("v".length));
const supportedVersions = [majorNodeVersion, majorNodeVersion - 1];

function createPath(platform: string, arch: string, nodeVersion: number) {
return path.join(llamaBinsDirectory, `${platform}-${arch}-${nodeVersion}/llama-addon.node`);
function createPath(platform: string, arch: string) {
return path.join(llamaBinsDirectory, `${platform}-${arch}/llama-addon.node`);
}

async function resolvePath(platform: string, arch: string, nodeVersions: number[]) {
for (const nodeVersion of nodeVersions) {
const binPath = createPath(platform, arch, nodeVersion);
async function resolvePath(platform: string, arch: string) {
const binPath = createPath(platform, arch);

if (await fs.pathExists(binPath))
return binPath;
}
if (await fs.pathExists(binPath))
return binPath;

return null;
}
Expand All @@ -36,14 +31,14 @@ export async function getPrebuildBinPath(): Promise<string | null> {
switch (process.platform) {
case "win32":
case "cygwin":
return resolvePath("win", process.arch, supportedVersions);
return resolvePath("win", process.arch);

case "linux":
case "android":
return resolvePath("linux", process.arch, supportedVersions);
return resolvePath("linux", process.arch);

case "darwin":
return resolvePath("mac", process.arch, supportedVersions);
return resolvePath("mac", process.arch);
}

return null;
Expand Down
Loading