diff --git a/.github/scripts/libmongocrypt.mjs b/.github/scripts/libmongocrypt.mjs index 7299ac1..03f7212 100644 --- a/.github/scripts/libmongocrypt.mjs +++ b/.github/scripts/libmongocrypt.mjs @@ -1,3 +1,5 @@ +// @ts-check + import util from 'node:util'; import process from 'node:process'; import fs from 'node:fs/promises'; @@ -21,6 +23,7 @@ async function parseArguments() { const options = { gitURL: { short: 'u', type: 'string', default: 'https://github.com/mongodb/libmongocrypt.git' }, libVersion: { short: 'l', type: 'string', default: pkg['mongodb:libmongocrypt'] }, + 'allow-only-x64-darwin': { type: 'boolean', default: false }, clean: { short: 'c', type: 'boolean', default: false }, build: { short: 'b', type: 'boolean', default: false }, help: { short: 'h', type: 'boolean', default: false } @@ -42,6 +45,7 @@ async function parseArguments() { libmongocrypt: { url: args.values.gitURL, ref: args.values.libVersion }, clean: args.values.clean, build: args.values.build, + allowOnlyX64Darwin: args.values['allow-only-x64-darwin'], pkg }; } @@ -121,14 +125,14 @@ export async function buildLibMongoCrypt(libmongocryptRoot, nodeDepsRoot) { ? toFlags({ Thost: 'x64', A: 'x64', DENABLE_WINDOWS_STATIC_RUNTIME: 'ON' }) : []; - const MACOS_CMAKE_FLAGS = - process.platform === 'darwin' // The minimum macos target version we want for + const DARWIN_CMAKE_FLAGS = + process.platform === 'darwin' // The minimum darwin target version we want for ? toFlags({ DCMAKE_OSX_DEPLOYMENT_TARGET: '10.12' }) : []; await run( 'cmake', - [...CMAKE_FLAGS, ...WINDOWS_CMAKE_FLAGS, ...MACOS_CMAKE_FLAGS, libmongocryptRoot], + [...CMAKE_FLAGS, ...WINDOWS_CMAKE_FLAGS, ...DARWIN_CMAKE_FLAGS, libmongocryptRoot], { cwd: nodeBuildRoot } ); await run('cmake', ['--build', '.', '--target', 'install', '--config', 'RelWithDebInfo'], { @@ -170,6 +174,7 @@ export async function downloadLibMongoCrypt(nodeDepsRoot, { ref }) { stdio: ['pipe', 'inherit'], cwd: resolveRoot('.') }); + if (unzip.stdin == null) throw new Error('Tar process stdin must be stream-able'); const [response] = await events.once(https.get(downloadURL), 'response'); @@ -190,11 +195,11 @@ export async function downloadLibMongoCrypt(nodeDepsRoot, { ref }) { } async function main() { - const { libmongocrypt, build, clean, pkg } = await parseArguments(); + const args = await parseArguments(); const nodeDepsDir = resolveRoot('deps'); - if (build) { + if (args.build) { const libmongocryptCloneDir = resolveRoot('_libmongocrypt'); const currentLibMongoCryptBranch = await fs @@ -202,23 +207,23 @@ async function main() { .catch(() => ''); const isClonedAndCheckedOut = currentLibMongoCryptBranch .trim() - .endsWith(`r-${libmongocrypt.ref}`); + .endsWith(`r-${args.libmongocrypt.ref}`); - if (clean || !isClonedAndCheckedOut) { - await cloneLibMongoCrypt(libmongocryptCloneDir, libmongocrypt); + if (args.clean || !isClonedAndCheckedOut) { + await cloneLibMongoCrypt(libmongocryptCloneDir, args.libmongocrypt); } const libmongocryptBuiltVersion = await fs .readFile(path.join(libmongocryptCloneDir, 'VERSION_CURRENT'), 'utf8') .catch(() => ''); - const isBuilt = libmongocryptBuiltVersion.trim() === libmongocrypt.ref; + const isBuilt = libmongocryptBuiltVersion.trim() === args.libmongocrypt.ref; - if (clean || !isBuilt) { + if (args.clean || !isBuilt) { await buildLibMongoCrypt(libmongocryptCloneDir, nodeDepsDir); } } else { // Download - await downloadLibMongoCrypt(nodeDepsDir, libmongocrypt); + await downloadLibMongoCrypt(nodeDepsDir, args.libmongocrypt); } await fs.rm(resolveRoot('build'), { force: true, recursive: true }); @@ -232,10 +237,19 @@ async function main() { if (process.platform === 'darwin') { // The "arm64" build is actually a universal binary - await fs.copyFile( - resolveRoot('prebuilds', `mongodb-client-encryption-v${pkg.version}-napi-v4-darwin-arm64.tar.gz`), - resolveRoot('prebuilds', `mongodb-client-encryption-v${pkg.version}-napi-v4-darwin-x64.tar.gz`) - ); + try { + await fs.copyFile( + resolveRoot('prebuilds', `mongodb-client-encryption-v${args.pkg.version}-napi-v4-darwin-arm64.tar.gz`), + resolveRoot('prebuilds', `mongodb-client-encryption-v${args.pkg.version}-napi-v4-darwin-x64.tar.gz`) + ); + } catch { + if (process.arch === 'x64') { + // The user of this script is building on an x64/intel/amd64 darwin which cannot build a universal bundle + // By default we exit with failure because we do not want to release an intel only build + console.error('Intel Darwin cannot build a universal bundle'); + process.exitCode = args.allowOnlyX64Darwin ? 0 : 1; + } + } } } diff --git a/binding.gyp b/binding.gyp index aeb631f..502c205 100644 --- a/binding.gyp +++ b/binding.gyp @@ -5,6 +5,7 @@ "