11import path from "path" ;
22import { fileURLToPath } from "url" ;
33import process from "process" ;
4+ import os from "os" ;
45import fs from "fs-extra" ;
56import chalk from "chalk" ;
67import which from "which" ;
@@ -17,7 +18,7 @@ import {getModuleVersion} from "../../utils/getModuleVersion.js";
1718import { ensureLlamaCppRepoIsCloned , isLlamaCppRepoCloned } from "./cloneLlamaCppRepo.js" ;
1819import { getBuildFolderNameForBuildOptions } from "./getBuildFolderNameForBuildOptions.js" ;
1920import { setLastBuildInfo } from "./lastBuildInfo.js" ;
20- import { getPlatform } from "./getPlatform.js" ;
21+ import { BinaryPlatform , getPlatform } from "./getPlatform.js" ;
2122import { logDistroInstallInstruction } from "./logDistroInstallInstruction.js" ;
2223import { testCmakeBinary } from "./testCmakeBinary.js" ;
2324import { getCudaNvccPaths } from "./detectAvailableComputeLayers.js" ;
@@ -45,6 +46,7 @@ export async function compileLlamaCpp(buildOptions: BuildOptions, compileOptions
4546 ciMode = false
4647 } = compileOptions ;
4748
49+ const platform = getPlatform ( ) ;
4850 const buildFolderName = await getBuildFolderNameForBuildOptions ( buildOptions ) ;
4951 const finalBuildFolderName = includeBuildOptionsInBinaryFolderName
5052 ? buildFolderName . withCustomCmakeOptions
@@ -123,6 +125,7 @@ export async function compileLlamaCpp(buildOptions: BuildOptions, compileOptions
123125 "--arch=" + buildOptions . arch ,
124126 "--out" , path . relative ( llamaDirectory , outDirectory ) ,
125127 "--runtime-version=" + runtimeVersion ,
128+ "--parallel=" + getParallelBuildThreadsToUse ( platform ) ,
126129 ...cmakePathArgs ,
127130 ...(
128131 [ ...cmakeCustomOptions ] . map ( ( [ key , value ] ) => "--CD" + key + "=" + value )
@@ -174,7 +177,6 @@ export async function compileLlamaCpp(buildOptions: BuildOptions, compileOptions
174177 }
175178 } ) ;
176179 } catch ( err ) {
177- const platform = getPlatform ( ) ;
178180 if ( platform === "linux" && await which ( "make" , { nothrow : true } ) == null ) {
179181 console . info ( "\n" +
180182 getConsoleLogPrefix ( true ) +
@@ -456,3 +458,15 @@ async function getToolchainFileForArch(targetArch: string) {
456458
457459 return null ;
458460}
461+
462+ function getParallelBuildThreadsToUse ( platform : BinaryPlatform ) {
463+ const cpuCount = os . cpus ( ) . length ;
464+
465+ if ( cpuCount <= 4 )
466+ return cpuCount ;
467+
468+ if ( platform === "mac" && process . arch === "arm64" )
469+ return cpuCount - 1 ;
470+
471+ return cpuCount - 2 ;
472+ }
0 commit comments