Skip to content

Commit 4978cbb

Browse files
committed
allow cross-compiling openssl for windows
1 parent cd1c5db commit 4978cbb

File tree

2 files changed

+57
-30
lines changed

2 files changed

+57
-30
lines changed

utils/acquireOpenSSL.mjs

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import crypto from "crypto";
2+
import { spawn } from "child_process";
23
import execPromise from "./execPromise.js";
34
import got from "got";
45
import path from "path";
@@ -19,6 +20,8 @@ const vendorPath = path.resolve(import.meta.dirname, "..", "vendor");
1920
const opensslPatchPath = path.join(vendorPath, "patches", "openssl");
2021
const extractPath = path.join(vendorPath, "openssl");
2122

23+
const exists = (filePath) => fs.stat(filePath).then(() => true).catch(() => false);
24+
2225
const convertArch = (archStr) => {
2326
const convertedArch = {
2427
'ia32': 'x86',
@@ -186,13 +189,7 @@ const buildLinux = async (buildCwd) => {
186189
}, { pipeOutput: true });
187190
};
188191

189-
const buildWin32 = async (buildCwd, vsBuildArch) => {
190-
if (!vsBuildArch) {
191-
throw new Error("Expected vsBuildArch to be specified");
192-
}
193-
194-
const exists = (filePath) => fs.stat(filePath).then(() => true).catch(() => false);
195-
192+
const buildWin32 = async (buildCwd) => {
196193
let vcvarsallPath = undefined;
197194

198195
if (process.env.npm_config_vcvarsall_path && await exists(process.env.npm_config_vcvarsall_path)) {
@@ -243,34 +240,57 @@ const buildWin32 = async (buildCwd, vsBuildArch) => {
243240
}
244241
}
245242

246-
console.log('using', vcvarsallPath);
247-
248243
let vcTarget;
249-
switch (vsBuildArch) {
250-
case "x64": {
244+
switch (targetArch) {
245+
case "x64":
251246
vcTarget = "VC-WIN64A";
252247
break;
253-
}
254248

255-
case "x86": {
249+
case "x86":
256250
vcTarget = "VC-WIN32";
257251
break;
258-
}
259252

260-
case "arm64": {
253+
case "arm64":
261254
vcTarget = "VC-WIN64-ARM";
262255
break;
263-
}
264-
265-
default: {
266-
throw new Error(`Unknown vsBuildArch: ${vsBuildArch}`);
267-
}
268256
}
269257

270-
await execPromise(`"${win32BatPath}" "${vcvarsallPath}" ${vsBuildArch} ${vcTarget}`, {
271-
cwd: buildCwd,
272-
maxBuffer: 10 * 1024 * 1024 // we should really just use spawn
273-
}, { 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+
274294
};
275295

276296
const removeOpenSSLIfOudated = async (openSSLVersion) => {
@@ -314,8 +334,7 @@ const makeOnStreamDownloadProgress = () => {
314334

315335
const buildOpenSSLIfNecessary = async ({
316336
macOsDeploymentTarget,
317-
openSSLVersion,
318-
vsBuildArch
337+
openSSLVersion
319338
}) => {
320339
if (process.platform !== "darwin" && process.platform !== "win32" && process.platform !== "linux") {
321340
console.log(`Skipping OpenSSL build, not required on ${process.platform}`);
@@ -359,7 +378,7 @@ const buildOpenSSLIfNecessary = async ({
359378
} else if (process.platform === "linux") {
360379
await buildLinux(buildCwd);
361380
} else if (process.platform === "win32") {
362-
await buildWin32(buildCwd, vsBuildArch);
381+
await buildWin32(buildCwd);
363382
} else {
364383
throw new Error(`Unknown platform: ${process.platform}`);
365384
}
@@ -474,8 +493,7 @@ const acquireOpenSSL = async () => {
474493

475494
await buildOpenSSLIfNecessary({
476495
openSSLVersion: OPENSSL_VERSION,
477-
macOsDeploymentTarget,
478-
vsBuildArch: process.platform === "win32" ? targetArch : undefined
496+
macOsDeploymentTarget
479497
});
480498
if (process.env.NODEGIT_OPENSSL_BUILD_PACKAGE) {
481499
await buildPackage();

utils/build-openssl.bat

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1+
rem Build OpenSSL for Windows
2+
rem %1 - path to vcvarsall.bat
3+
rem %2 - architecture argument for vcvarsall.bat
4+
rem %3 - OpenSSL Configure target
5+
16
@call %1 %2
27

38
perl .\Configure %3 no-shared no-ssl2 no-ssl3 no-comp --prefix="%cd%\.." --openssldir="%cd%\.." || goto :error
49

510
nmake || goto :error
6-
nmake test || goto :error
11+
12+
if "%NODEGIT_SKIP_TESTS%" NEQ "1" (
13+
nmake test || goto :error
14+
)
15+
716
nmake install || goto :error
817

918
goto :EOF

0 commit comments

Comments
 (0)