11import crypto from "crypto" ;
2+ import { spawn } from "child_process" ;
23import execPromise from "./execPromise.js" ;
34import got from "got" ;
45import path from "path" ;
@@ -19,6 +20,28 @@ const vendorPath = path.resolve(import.meta.dirname, "..", "vendor");
1920const opensslPatchPath = path . join ( vendorPath , "patches" , "openssl" ) ;
2021const extractPath = path . join ( vendorPath , "openssl" ) ;
2122
23+ const exists = ( filePath ) => fs . stat ( filePath ) . then ( ( ) => true ) . catch ( ( ) => false ) ;
24+
25+ const convertArch = ( archStr ) => {
26+ const convertedArch = {
27+ 'ia32' : 'x86' ,
28+ 'x86' : 'x86' ,
29+ 'x64' : 'x64' ,
30+ 'arm64' : 'arm64'
31+ } [ archStr ] ;
32+
33+ if ( ! convertedArch ) {
34+ throw new Error ( 'unsupported architecture' ) ;
35+ }
36+
37+ return convertedArch ;
38+ }
39+
40+ const hostArch = convertArch ( process . arch ) ;
41+ const targetArch = process . env . npm_config_arch
42+ ? convertArch ( process . env . npm_config_arch )
43+ : hostArch ;
44+
2245const pathsToIncludeForPackage = [
2346 "include" , "lib"
2447] ;
@@ -82,8 +105,10 @@ const buildDarwin = async (buildCwd, macOsDeploymentTarget) => {
82105 throw new Error ( "Expected macOsDeploymentTarget to be specified" ) ;
83106 }
84107
108+ const buildConfig = targetArch === "x64" ? "darwin64-x86_64-cc" : "darwin64-arm64-cc" ;
109+
85110 const configureArgs = [
86- process . arch === "x64" ? "darwin64-x86_64-cc" : "darwin64-arm64-cc" ,
111+ buildConfig ,
87112 // speed up ecdh on little-endian platforms with 128bit int support
88113 "enable-ec_nistp_64_gcc_128" ,
89114 // compile static libraries
@@ -107,7 +132,7 @@ const buildDarwin = async (buildCwd, macOsDeploymentTarget) => {
107132
108133 await applyOpenSSLPatches ( buildCwd , "darwin" ) ;
109134
110- // only build the libraries, not the tests/ fuzzer or apps
135+ // only build the libraries, not the fuzzer or apps
111136 await execPromise ( "make build_libs" , {
112137 cwd : buildCwd
113138 } , { pipeOutput : true } ) ;
@@ -123,8 +148,10 @@ const buildDarwin = async (buildCwd, macOsDeploymentTarget) => {
123148} ;
124149
125150const buildLinux = async ( buildCwd ) => {
151+ const buildConfig = targetArch === "x64" ? "linux-x86_64" : "linux-aarch64" ;
152+
126153 const configureArgs = [
127- "linux-x86_64" ,
154+ buildConfig ,
128155 // Electron(at least on centos7) imports the libcups library at runtime, which has a
129156 // dependency on the system libssl/libcrypto which causes symbol conflicts and segfaults.
130157 // To fix this we need to hide all the openssl symbols to prevent them from being overridden
@@ -146,7 +173,7 @@ const buildLinux = async (buildCwd) => {
146173
147174 await applyOpenSSLPatches ( buildCwd , "linux" ) ;
148175
149- // only build the libraries, not the tests/ fuzzer or apps
176+ // only build the libraries, not the fuzzer or apps
150177 await execPromise ( "make build_libs" , {
151178 cwd : buildCwd
152179 } , { pipeOutput : true } ) ;
@@ -162,13 +189,7 @@ const buildLinux = async (buildCwd) => {
162189 } , { pipeOutput : true } ) ;
163190} ;
164191
165- const buildWin32 = async ( buildCwd , vsBuildArch ) => {
166- if ( ! vsBuildArch ) {
167- throw new Error ( "Expected vsBuildArch to be specified" ) ;
168- }
169-
170- const exists = ( filePath ) => fs . stat ( filePath ) . then ( ( ) => true ) . catch ( ( ) => false ) ;
171-
192+ const buildWin32 = async ( buildCwd ) => {
172193 let vcvarsallPath = undefined ;
173194
174195 if ( process . env . npm_config_vcvarsall_path && await exists ( process . env . npm_config_vcvarsall_path ) ) {
@@ -219,29 +240,57 @@ const buildWin32 = async (buildCwd, vsBuildArch) => {
219240 }
220241 }
221242
222- console . log ( 'using' , vcvarsallPath ) ;
223-
224243 let vcTarget ;
225- switch ( vsBuildArch ) {
226- case "x64" : {
244+ switch ( targetArch ) {
245+ case "x64" :
227246 vcTarget = "VC-WIN64A" ;
228247 break ;
229- }
230248
231- case "x86" : {
249+ case "x86" :
232250 vcTarget = "VC-WIN32" ;
233251 break ;
234- }
235-
236- default : {
237- throw new Error ( `Unknown vsBuildArch: ${ vsBuildArch } ` ) ;
238- }
252+
253+ case "arm64" :
254+ vcTarget = "VC-WIN64-ARM" ;
255+ break ;
239256 }
240257
241- await execPromise ( `"${ win32BatPath } " "${ vcvarsallPath } " ${ vsBuildArch } ${ vcTarget } ` , {
242- cwd : buildCwd ,
243- maxBuffer : 10 * 1024 * 1024 // we should really just use spawn
244- } , { pipeOutput : true } ) ;
258+ let vsBuildArch = hostArch === targetArch
259+ ? hostArch
260+ : `${ hostArch } _${ targetArch } ` ;
261+
262+ console . log ( "Using vcvarsall.bat at: " , vcvarsallPath ) ;
263+ console . log ( "Using vsBuildArch: " , vsBuildArch ) ;
264+ console . log ( "Using vcTarget: " , vcTarget ) ;
265+
266+ await new Promise ( ( resolve , reject ) => {
267+ const buildProcess = spawn ( `"${ win32BatPath } " "${ vcvarsallPath } " ${ vsBuildArch } ${ vcTarget } ` , {
268+ cwd : buildCwd ,
269+ shell : process . platform === "win32" ,
270+ env : {
271+ ...process . env ,
272+ NODEGIT_SKIP_TESTS : targetArch !== hostArch ? "1" : undefined
273+ }
274+ } ) ;
275+
276+ buildProcess . stdout . on ( "data" , function ( data ) {
277+ console . info ( data . toString ( ) . trim ( ) ) ;
278+ } ) ;
279+
280+ buildProcess . stderr . on ( "data" , function ( data ) {
281+ console . error ( data . toString ( ) . trim ( ) ) ;
282+ } ) ;
283+
284+ buildProcess . on ( "close" , function ( code ) {
285+ if ( ! code ) {
286+ resolve ( ) ;
287+ } else {
288+ reject ( code ) ;
289+ }
290+ } ) ;
291+ } ) ;
292+
293+
245294} ;
246295
247296const removeOpenSSLIfOudated = async ( openSSLVersion ) => {
@@ -285,8 +334,7 @@ const makeOnStreamDownloadProgress = () => {
285334
286335const buildOpenSSLIfNecessary = async ( {
287336 macOsDeploymentTarget,
288- openSSLVersion,
289- vsBuildArch
337+ openSSLVersion
290338} ) => {
291339 if ( process . platform !== "darwin" && process . platform !== "win32" && process . platform !== "linux" ) {
292340 console . log ( `Skipping OpenSSL build, not required on ${ process . platform } ` ) ;
@@ -330,7 +378,7 @@ const buildOpenSSLIfNecessary = async ({
330378 } else if ( process . platform === "linux" ) {
331379 await buildLinux ( buildCwd ) ;
332380 } else if ( process . platform === "win32" ) {
333- await buildWin32 ( buildCwd , vsBuildArch ) ;
381+ await buildWin32 ( buildCwd ) ;
334382 } else {
335383 throw new Error ( `Unknown platform: ${ process . platform } ` ) ;
336384 }
@@ -383,14 +431,7 @@ const downloadOpenSSLIfNecessary = async ({
383431}
384432
385433export const getOpenSSLPackageName = ( ) => {
386- let arch = process . arch ;
387- if ( process . platform === "win32" && (
388- process . arch === "ia32" || process . env . NODEGIT_VS_BUILD_ARCH === "x86"
389- ) ) {
390- arch = "x86" ;
391- }
392-
393- return `openssl-${ OPENSSL_VERSION } -${ process . platform } -${ arch } .tar.gz` ;
434+ return `openssl-${ OPENSSL_VERSION } -${ process . platform } -${ targetArch } .tar.gz` ;
394435}
395436
396437export const getOpenSSLPackagePath = ( ) => path . join ( import . meta. dirname , getOpenSSLPackageName ( ) ) ;
@@ -450,18 +491,9 @@ const acquireOpenSSL = async () => {
450491 }
451492 }
452493
453- let vsBuildArch ;
454- if ( process . platform === "win32" ) {
455- vsBuildArch = process . env . NODEGIT_VS_BUILD_ARCH || ( process . arch === "x64" ? "x64" : "x86" ) ;
456- if ( ! [ "x64" , "x86" ] . includes ( vsBuildArch ) ) {
457- throw new Error ( `Invalid vsBuildArch: ${ vsBuildArch } ` ) ;
458- }
459- }
460-
461494 await buildOpenSSLIfNecessary ( {
462495 openSSLVersion : OPENSSL_VERSION ,
463- macOsDeploymentTarget,
464- vsBuildArch
496+ macOsDeploymentTarget
465497 } ) ;
466498 if ( process . env . NODEGIT_OPENSSL_BUILD_PACKAGE ) {
467499 await buildPackage ( ) ;
0 commit comments