2023-04-20 17:14:30 +02:00
import path from "node:path"
import fs from "node:fs"
2023-04-19 13:58:49 +02:00
import { fileExists , LogWriter } from "./buildUtils.js"
2023-04-20 17:14:30 +02:00
import { createRequire } from "node:module"
import { spawn } from "node:child_process"
2023-04-19 13:58:49 +02:00
import { getElectronVersion , getInstalledModuleVersion } from "./getInstalledModuleVersion.js"
2022-01-12 14:43:01 +01:00
2024-07-05 19:02:03 +02:00
/ * *
* @ typedef { ( ... args : string [ ] ) => void } Logger
* /
2022-01-12 14:43:01 +01:00
/ * *
2022-02-21 17:48:12 +01:00
* Rebuild native lib for either current node version or for electron version and
2022-01-12 14:43:01 +01:00
* cache in the "native-cache" directory .
* ABI for nodejs and for electron differs and we need to build it differently for each . We do caching
* to avoid rebuilding between different invocations ( e . g . running desktop and running tests ) .
2024-07-05 19:02:03 +02:00
* @ param params { object }
* @ param params . environment { "electron" | "node" }
* @ param params . platform { "win32" | "linux" | "darwin" } platform to compile for in case of cross compilation
* @ param params . architecture : { "arm64" | "x64" | "universal" } the instruction set used in the built desktop binary
* @ param params . rootDir { string } path to the root of the project
* @ param params . nodeModule { string } name of the npm module to rebuild
* @ param params . log { Logger }
* @ param params . copyTarget { string | undefined } Which node - gyp target ( specified in binding . gyp ) to copy the output of . Defaults to the same name as the module
2022-03-02 18:21:01 +01:00
* @ returns { Promise < string > } path to cached native module
2022-01-12 14:43:01 +01:00
* /
2024-07-05 19:02:03 +02:00
export async function getNativeLibModulePath ( { environment , platform , architecture , rootDir , nodeModule , log , copyTarget } ) {
2023-11-20 17:47:22 +01:00
const libPath = await getCachedLibPath ( { rootDir , nodeModule , environment , platform , architecture } , log )
2022-01-12 14:43:01 +01:00
2022-03-02 18:21:01 +01:00
if ( await fileExists ( libPath ) ) {
2022-02-21 17:48:12 +01:00
log ( ` Using cached ${ nodeModule } at ` , libPath )
2022-03-02 18:21:01 +01:00
} else {
let isCrossCompilation = false
if ( platform === "win32" && process . platform !== "win32" ) {
isCrossCompilation = true
} else if ( platform !== process . platform ) {
// We only care about cross compiling the app when building for windows from linux
// since it's only possible to build for mac from mac,
// and there's no reason to build for linux from anything but linux
// Consider it an here error since if you're doing it it's probably a mistake
// And it's more effort than it's worth to allow arbitrary configurations
throw new Error ( ` Invalid cross compilation ${ process . platform } => ${ platform } . only * => win32 is allowed ` )
}
if ( isCrossCompilation ) {
log ( ` Getting prebuilt ${ nodeModule } using prebuild-install... ` )
2022-12-27 15:37:40 +01:00
await getPrebuiltNativeModuleForWindows ( {
nodeModule ,
rootDir ,
2024-07-05 19:02:03 +02:00
platform ,
2022-12-27 15:37:40 +01:00
log ,
} )
2022-03-02 18:21:01 +01:00
} else {
log ( ` Compiling ${ nodeModule } for ${ platform } ... ` )
2022-12-27 15:37:40 +01:00
await buildNativeModule ( {
environment ,
rootDir ,
log ,
nodeModule ,
2023-11-20 17:47:22 +01:00
copyTarget ,
architecture ,
2022-12-27 15:37:40 +01:00
} )
2022-03-02 18:21:01 +01:00
}
2022-03-02 13:49:49 +01:00
const moduleDir = await getModuleDir ( rootDir , nodeModule )
2022-12-27 15:37:40 +01:00
await fs . promises . copyFile ( path . join ( moduleDir , "build/Release" , ` ${ copyTarget ? ? nodeModule } .node ` ) , libPath )
2022-01-12 14:43:01 +01:00
}
2022-03-02 18:21:01 +01:00
2022-01-12 14:43:01 +01:00
return libPath
}
2022-03-02 18:21:01 +01:00
/ * *
* Build a native module using node - gyp
* Runs ` node-gyp rebuild ... ` from within ` node_modules/<nodeModule>/ `
2024-07-05 19:02:03 +02:00
* @ param params { object }
* @ param params . nodeModule { string } the node module being built . Must be installed , and must be a native module project with a ` binding.gyp ` at the root
* @ param params . copyTarget { string }
* @ param params . environment { "node" | "electron" } Used to determine which node version to use
* @ param params . rootDir { string } the root dir of the project
* @ param params . log { Logger }
* @ param params . architecture the architecture to build for : "x64" | "arm64" | "universal"
2022-03-02 18:21:01 +01:00
* @ returns { Promise < void > }
* /
2023-11-20 17:47:22 +01:00
export async function buildNativeModule ( { nodeModule , copyTarget , environment , rootDir , log , architecture } ) {
const moduleDir = await getModuleDir ( rootDir , nodeModule )
const electronVersion = await getElectronVersion ( log )
const doBuild = ( arch ) =>
callProgram ( {
command : "npm exec" ,
args : [
"--" ,
"node-gyp" ,
"rebuild" ,
"--release" ,
"--build-from-source" ,
` --arch= ${ arch } ` ,
... ( environment === "electron" ? [ "--runtime=electron" , "--dist-url=https://www.electronjs.org/headers" , ` --target= ${ electronVersion } ` ] : [ ] ) ,
] ,
cwd : moduleDir ,
log ,
} )
if ( architecture === "universal" ) {
2024-01-10 16:11:26 +01:00
const artifactPath = path . join ( moduleDir , "build/Release" , ` ${ copyTarget } .node ` )
const armArtifactPath = path . join ( moduleDir , ` ${ copyTarget } -arm64.node ` )
const intelArtifactPath = path . join ( moduleDir , ` ${ copyTarget } -x64.node ` )
await doBuild ( "x64" )
await fs . promises . copyFile ( artifactPath , intelArtifactPath )
await doBuild ( "arm64" )
await fs . promises . copyFile ( artifactPath , armArtifactPath )
await callProgram ( {
command : "lipo" ,
args : [ "-create" , "-output" , artifactPath , intelArtifactPath , armArtifactPath ] ,
cwd : path . join ( rootDir , "native-cache" ) ,
log ,
} )
2023-11-20 17:47:22 +01:00
} else {
await doBuild ( architecture )
}
2022-03-02 18:21:01 +01:00
}
/ * *
* Get a prebuilt version of a node native module
*
* we can ' t cross - compile with node - gyp , so we need to use the prebuilt version when building a desktop client for windows on linux
*
2024-07-05 19:02:03 +02:00
* @ param params { object }
* @ param params . nodeModule { string }
* @ param params . rootDir { string }
* @ param params . platform : { "win32" | "darwin" | "linux" }
* @ param params . log { Logger }
2022-03-02 18:21:01 +01:00
* @ returns { Promise < void > }
* /
2024-07-05 19:02:03 +02:00
export async function getPrebuiltNativeModuleForWindows ( { nodeModule , rootDir , platform , log } ) {
2022-03-02 18:21:01 +01:00
// We never want to use prebuilt native modules when building on jenkins, so it is considered an error as a safeguard
2022-07-29 10:19:32 +02:00
if ( process . env . JENKINS _HOME ) {
2022-03-02 18:21:01 +01:00
throw new Error ( "Should not be getting prebuilt native modules in CI" )
}
2024-07-05 19:02:03 +02:00
const target = await getPrebuildConfiguration ( nodeModule , platform , log )
2022-03-02 18:21:01 +01:00
await callProgram ( {
2022-02-10 16:32:47 +01:00
command : "npm exec" ,
2022-03-02 18:21:01 +01:00
args : [
2022-01-12 14:43:01 +01:00
"--" ,
2022-03-02 18:21:01 +01:00
"prebuild-install" ,
` --platform=win32 ` ,
"--tag-prefix=v" ,
2022-12-27 15:37:40 +01:00
... ( target != null ? [ ` --runtime= ${ target . runtime } ` , ` --target= ${ target . version } ` ] : [ ] ) ,
"--verbose" ,
2022-01-12 14:43:01 +01:00
] ,
2022-03-02 13:49:49 +01:00
cwd : await getModuleDir ( rootDir , nodeModule ) ,
2022-12-27 15:37:40 +01:00
log ,
2022-03-02 18:21:01 +01:00
} )
}
/ * *
* prebuild - install { runtime , target } configurations are a pain to maintain because they are specific for whichever native module you want to get a prebuild for ,
* So we just define them here and throw an error if we try to obtain a configuration for an unknown module
2024-07-05 19:02:03 +02:00
* @ param nodeModule { string }
* @ param platform { "electron" | "node" }
* @ param log { Logger }
* @ return { Promise < { runtime : string , version : number } | null > }
2022-03-02 18:21:01 +01:00
* /
2024-07-05 19:02:03 +02:00
async function getPrebuildConfiguration ( nodeModule , platform , log ) {
2024-01-10 16:11:26 +01:00
if ( nodeModule === "better-sqlite3" ) {
2024-07-05 19:02:03 +02:00
return platform === "electron"
2024-01-10 16:11:26 +01:00
? {
runtime : "electron" ,
version : await getElectronVersion ( log ) ,
}
: null
} else {
throw new Error ( ` Unknown prebuild-configuration for node module ${ nodeModule } , requires a definition ` )
2022-03-02 18:21:01 +01:00
}
}
/ * *
* Call a program , piping stdout and stderr to log , and resolves when the process exits
* @ returns { Promise < void > }
* /
2022-12-27 15:37:40 +01:00
function callProgram ( { command , args , cwd , log } ) {
const process = spawn ( command , args , {
stdio : [ null , "pipe" , "pipe" ] ,
shell : true ,
cwd ,
} )
2022-03-02 18:21:01 +01:00
const logStream = new LogWriter ( log )
process . stdout . pipe ( logStream )
process . stderr . pipe ( logStream )
2022-01-12 14:43:01 +01:00
return new Promise ( ( resolve , reject ) => {
2022-12-27 15:37:40 +01:00
process . on ( "exit" , ( code ) => {
2022-01-12 14:43:01 +01:00
if ( code === 0 ) {
resolve ( )
} else {
2022-03-02 18:21:01 +01:00
reject ( new Error ( ` command " ${ command } " failed with error code: ${ code } ` ) )
2022-01-12 14:43:01 +01:00
}
} )
} )
}
2022-03-02 18:21:01 +01:00
/ * *
* Get the target name for the built native library when cached
* @ param rootDir
* @ param nodeModule
* @ param environment
* @ param platform
2023-11-20 17:47:22 +01:00
* @ param architecture : { "arm64" | "x64" | "universal" } the instruction set used in the built desktop binary
2022-03-02 18:21:01 +01:00
* @ returns { Promise < string > }
* /
2023-04-19 13:58:49 +02:00
export async function getCachedLibPath ( { rootDir , nodeModule , environment , platform , architecture } , log ) {
2022-05-02 17:17:33 +02:00
const libraryVersion = await getInstalledModuleVersion ( nodeModule , log )
2022-03-02 18:21:01 +01:00
2023-04-19 13:58:49 +02:00
let versionedEnvironment
2022-03-02 18:21:01 +01:00
if ( environment === "electron" ) {
2023-04-19 13:58:49 +02:00
versionedEnvironment = ` electron- ${ await getInstalledModuleVersion ( "electron" , log ) } `
2022-03-02 18:21:01 +01:00
} else {
2024-02-23 14:03:19 +01:00
// process.versions.modules is an ABI version. It is not significant for modules that use new ABI but still matters for those we use
versionedEnvironment = ` node- ${ process . versions . modules } `
2022-03-02 18:21:01 +01:00
}
2023-04-19 13:58:49 +02:00
return await buildCachedLibPath ( { rootDir , nodeModule , environment , versionedEnvironment , platform , libraryVersion , architecture } )
}
export async function buildCachedLibPath ( { rootDir , nodeModule , environment , versionedEnvironment , platform , libraryVersion , architecture } ) {
const dir = path . join ( rootDir , "native-cache" , environment )
await fs . promises . mkdir ( dir , { recursive : true } )
return path . resolve ( dir , ` ${ nodeModule } - ${ libraryVersion } - ${ versionedEnvironment } - ${ platform } - ${ architecture } .node ` )
2022-03-02 18:21:01 +01:00
}
2022-02-24 16:01:20 +01:00
async function getModuleDir ( rootDir , nodeModule ) {
// We resolve relative to the rootDir passed to us
// however, if we just use rootDir as the base for require() it doesn't work: node_modules must be at the directory up from yours (for whatever reason).
// so we provide a directory one level deeper. Practically it doesn't matter if "src" subdirectory exists or not, this is just to give node some
// subdirectory to work against.
2022-02-10 16:32:47 +01:00
const someChild = path . resolve ( path . join ( rootDir , "src" ) ) . toString ( )
const filePath = createRequire ( someChild ) . resolve ( nodeModule )
2022-02-24 16:01:20 +01:00
const pathEnd = path . join ( "node_modules" , nodeModule )
const endIndex = filePath . lastIndexOf ( pathEnd )
return path . join ( filePath . substring ( 0 , endIndex ) , pathEnd )
2022-12-27 15:37:40 +01:00
}