From b769117beac147473ded01b37257a9cdae6b1e8a Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Tue, 18 Jun 2024 15:32:31 -0400 Subject: [PATCH 01/10] chore: fix building on intel macs --- .github/scripts/libmongocrypt.mjs | 40 ++++++++++++++++++++----------- binding.gyp | 21 +++++++++------- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/.github/scripts/libmongocrypt.mjs b/.github/scripts/libmongocrypt.mjs index df963cc..e5ccb78 100644 --- a/.github/scripts/libmongocrypt.mjs +++ b/.github/scripts/libmongocrypt.mjs @@ -1,4 +1,5 @@ -//@ts-check +// @ts-check + import util from 'node:util'; import process from 'node:process'; import fs from 'node:fs/promises'; @@ -22,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 }, fastDownload: { type: 'boolean', default: false }, // Potentially incorrect download, only for the brave and impatient @@ -46,6 +48,7 @@ async function parseArguments() { fastDownload: args.values.fastDownload, clean: args.values.clean, build: args.values.build, + allowOnlyX64Darwin: args.values['allow-only-x64-darwin'], pkg }; } @@ -125,14 +128,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'], { @@ -272,16 +275,25 @@ 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${pkg.version}-napi-v4-darwin-arm64.tar.gz` + ), + resolveRoot( + 'prebuilds', + `mongodb-client-encryption-v${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 @@ " Date: Thu, 20 Jun 2024 13:50:01 -0400 Subject: [PATCH 02/10] chore: allow disabling universal macos building --- .github/scripts/libmongocrypt.mjs | 34 ++++++++++--------------------- README.md | 1 + binding.gyp | 17 ++++------------ 3 files changed, 16 insertions(+), 36 deletions(-) diff --git a/.github/scripts/libmongocrypt.mjs b/.github/scripts/libmongocrypt.mjs index e5ccb78..e4688da 100644 --- a/.github/scripts/libmongocrypt.mjs +++ b/.github/scripts/libmongocrypt.mjs @@ -23,7 +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 }, + 'no-macos-universal': { type: 'boolean', default: false }, clean: { short: 'c', type: 'boolean', default: false }, build: { short: 'b', type: 'boolean', default: false }, fastDownload: { type: 'boolean', default: false }, // Potentially incorrect download, only for the brave and impatient @@ -48,7 +48,7 @@ async function parseArguments() { fastDownload: args.values.fastDownload, clean: args.values.clean, build: args.values.build, - allowOnlyX64Darwin: args.values['allow-only-x64-darwin'], + noMacosUniversal: args.values['no-macos-universal'], pkg }; } @@ -269,31 +269,19 @@ async function main() { await run('npm', ['install', '--ignore-scripts']); // The prebuild command will make both a .node file in `./build` (local and CI testing will run on current code) // it will also produce `./prebuilds/mongodb-client-encryption-vVERSION-napi-vNAPI_VERSION-OS-ARCH.tar.gz`. - await run('npm', ['run', 'prebuild']); + const prebuildOptions = + process.platform === 'darwin' && args.noMacosUniversal + ? { env: { ...process.env, GYP_DEFINES: 'no_macos_universal=true' } } + : undefined; + await run('npm', ['run', 'prebuild'], prebuildOptions); // Compile Typescript await run('npm', ['run', 'prepare']); - if (process.platform === 'darwin') { + if (process.platform === 'darwin' && !args.noMacosUniversal) { // The "arm64" build is actually a universal binary - try { - 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` - ) - ); - } 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; - } - } + const armTar = `mongodb-client-encryption-v${pkg.version}-napi-v4-darwin-arm64.tar.gz`; + const x64Tar = `mongodb-client-encryption-v${pkg.version}-napi-v4-darwin-x64.tar.gz`; + await fs.copyFile(resolveRoot('prebuilds', armTar), resolveRoot('prebuilds', x64Tar)); } } diff --git a/README.md b/README.md index b7cd896..246905b 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ Can be configured to clone and build without crypto. You may use "latest" to get current libmongocrypt `HEAD`. --clean Combined with --build, the script will not skip cloning and rebuilding libmongocrypt. --build Instead of downloading, clone and build libmongocrypt along with the bindings. +--no-macos-universal Disable creating a universal binary for MacOS builds. Only suitable for local development: diff --git a/binding.gyp b/binding.gyp index 502c205..af987cb 100644 --- a/binding.gyp +++ b/binding.gyp @@ -5,18 +5,9 @@ " Date: Thu, 20 Jun 2024 13:50:55 -0400 Subject: [PATCH 03/10] chore: update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 246905b..5bd25f8 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ npm run install:libmongocrypt #### `libmongocrypt.mjs` ``` -node libmongocrypt.mjs [--gitURL=string] [--libVersion=string] [--clean] [--build] [--no-crypto] [--fastDownload] +node libmongocrypt.mjs [optional flags] By default attempts to download and compile the bindings with the crypto prebuilds of libmongocrypt. Can be configured to clone and build without crypto. From 0f020b6bcf81619ee8ed15b5b0c897afaaca9e6e Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Thu, 20 Jun 2024 16:44:38 -0400 Subject: [PATCH 04/10] chore: fix build_type --- .github/scripts/libmongocrypt.mjs | 21 +++++++++++++++------ README.md | 2 ++ binding.gyp | 2 +- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/.github/scripts/libmongocrypt.mjs b/.github/scripts/libmongocrypt.mjs index e4688da..24e90c5 100644 --- a/.github/scripts/libmongocrypt.mjs +++ b/.github/scripts/libmongocrypt.mjs @@ -26,6 +26,7 @@ async function parseArguments() { 'no-macos-universal': { type: 'boolean', default: false }, clean: { short: 'c', type: 'boolean', default: false }, build: { short: 'b', type: 'boolean', default: false }, + dynamic: { type: 'boolean', default: false }, fastDownload: { type: 'boolean', default: false }, // Potentially incorrect download, only for the brave and impatient help: { short: 'h', type: 'boolean', default: false } }; @@ -48,6 +49,7 @@ async function parseArguments() { fastDownload: args.values.fastDownload, clean: args.values.clean, build: args.values.build, + dynamic: args.values.dynamic, noMacosUniversal: args.values['no-macos-universal'], pkg }; @@ -237,7 +239,7 @@ async function main() { const nodeDepsDir = resolveRoot('deps'); - if (args.build) { + if (args.build && !args.dynamic) { const libmongocryptCloneDir = resolveRoot('_libmongocrypt'); const currentLibMongoCryptBranch = await fs @@ -257,7 +259,7 @@ async function main() { if (args.clean || !isBuilt) { await buildLibMongoCrypt(libmongocryptCloneDir, nodeDepsDir); } - } else { + } else if (!args.dynamic) { // Download await downloadLibMongoCrypt(nodeDepsDir, args); } @@ -269,10 +271,17 @@ async function main() { await run('npm', ['install', '--ignore-scripts']); // The prebuild command will make both a .node file in `./build` (local and CI testing will run on current code) // it will also produce `./prebuilds/mongodb-client-encryption-vVERSION-napi-vNAPI_VERSION-OS-ARCH.tar.gz`. - const prebuildOptions = - process.platform === 'darwin' && args.noMacosUniversal - ? { env: { ...process.env, GYP_DEFINES: 'no_macos_universal=true' } } - : undefined; + let prebuildOptions; + if (process.platform === 'darwin' && args.noMacosUniversal) { + prebuildOptions ??= { env: { ...process.env } }; + prebuildOptions.env.GYP_DEFINES = (prebuildOptions.env.GYP_DEFINES ?? '') + 'no_macos_universal=true ' + } + + if (args.dynamic) { + prebuildOptions ??= { env: { ...process.env } } + prebuildOptions.env.GYP_DEFINES = (prebuildOptions.env.GYP_DEFINES ?? '') + 'build_type=dynamic ' + } + await run('npm', ['run', 'prebuild'], prebuildOptions); // Compile Typescript await run('npm', ['run', 'prepare']); diff --git a/README.md b/README.md index 5bd25f8..215b218 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,8 @@ Can be configured to clone and build without crypto. --clean Combined with --build, the script will not skip cloning and rebuilding libmongocrypt. --build Instead of downloading, clone and build libmongocrypt along with the bindings. --no-macos-universal Disable creating a universal binary for MacOS builds. +--dynamic Skips cloning or downloading libmongocrypt, runs prebuild with build_type set to "dynamic" to compile + a prebuild that links to a system copy of libmongocrypt. Only suitable for local development: diff --git a/binding.gyp b/binding.gyp index af987cb..df4d2bb 100644 --- a/binding.gyp +++ b/binding.gyp @@ -7,7 +7,7 @@ 'variables': { 'ARCH': '<(host_arch)', 'no_macos_universal%': 'false', - 'build_type%': 'dynamic', + 'build_type%': 'static', }, 'sources': [ 'addon/mongocrypt.cc' From 071fdc6ab5f38fccff02f7f8dcc48762cbdfbf3b Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Mon, 24 Jun 2024 16:36:01 -0400 Subject: [PATCH 05/10] chore: add cmake-path --- .github/scripts/libmongocrypt.mjs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/.github/scripts/libmongocrypt.mjs b/.github/scripts/libmongocrypt.mjs index 24e90c5..d68df3c 100644 --- a/.github/scripts/libmongocrypt.mjs +++ b/.github/scripts/libmongocrypt.mjs @@ -23,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'] }, + 'cmake-path': { type: 'string', default: 'cmake' }, 'no-macos-universal': { type: 'boolean', default: false }, clean: { short: 'c', type: 'boolean', default: false }, build: { short: 'b', type: 'boolean', default: false }, @@ -51,6 +52,7 @@ async function parseArguments() { build: args.values.build, dynamic: args.values.dynamic, noMacosUniversal: args.values['no-macos-universal'], + cMakePath: args.values['cmake-path'], pkg }; } @@ -86,7 +88,7 @@ export async function cloneLibMongoCrypt(libmongocryptRoot, { url, ref }) { } } -export async function buildLibMongoCrypt(libmongocryptRoot, nodeDepsRoot) { +export async function buildLibMongoCrypt(libmongocryptRoot, nodeDepsRoot, options) { console.error('building libmongocrypt...'); const nodeBuildRoot = resolveRoot(nodeDepsRoot, 'tmp', 'libmongocrypt-build'); @@ -136,11 +138,11 @@ export async function buildLibMongoCrypt(libmongocryptRoot, nodeDepsRoot) { : []; await run( - 'cmake', + options.cMakePath, [...CMAKE_FLAGS, ...WINDOWS_CMAKE_FLAGS, ...DARWIN_CMAKE_FLAGS, libmongocryptRoot], { cwd: nodeBuildRoot } ); - await run('cmake', ['--build', '.', '--target', 'install', '--config', 'RelWithDebInfo'], { + await run(options.cMakePath, ['--build', '.', '--target', 'install', '--config', 'RelWithDebInfo'], { cwd: nodeBuildRoot }); } @@ -257,7 +259,7 @@ async function main() { const isBuilt = libmongocryptBuiltVersion.trim() === args.ref; if (args.clean || !isBuilt) { - await buildLibMongoCrypt(libmongocryptCloneDir, nodeDepsDir); + await buildLibMongoCrypt(libmongocryptCloneDir, nodeDepsDir, args); } } else if (!args.dynamic) { // Download From 5d7c1eea1746372285e781d3d08411e1dd858c05 Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Mon, 24 Jun 2024 17:34:13 -0400 Subject: [PATCH 06/10] chore: shell flag for windows --- .github/scripts/libmongocrypt.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/scripts/libmongocrypt.mjs b/.github/scripts/libmongocrypt.mjs index d68df3c..7801878 100644 --- a/.github/scripts/libmongocrypt.mjs +++ b/.github/scripts/libmongocrypt.mjs @@ -140,10 +140,10 @@ export async function buildLibMongoCrypt(libmongocryptRoot, nodeDepsRoot, option await run( options.cMakePath, [...CMAKE_FLAGS, ...WINDOWS_CMAKE_FLAGS, ...DARWIN_CMAKE_FLAGS, libmongocryptRoot], - { cwd: nodeBuildRoot } + { cwd: nodeBuildRoot, shell: process.platform === 'win32' } ); await run(options.cMakePath, ['--build', '.', '--target', 'install', '--config', 'RelWithDebInfo'], { - cwd: nodeBuildRoot + cwd: nodeBuildRoot, shell: process.platform === 'win32' }); } From 0928166674caf0775503428d861993aad27623b8 Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Tue, 25 Jun 2024 14:06:30 -0400 Subject: [PATCH 07/10] chore: remove cmake path flag --- .github/scripts/libmongocrypt.mjs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/.github/scripts/libmongocrypt.mjs b/.github/scripts/libmongocrypt.mjs index 7801878..d6685a1 100644 --- a/.github/scripts/libmongocrypt.mjs +++ b/.github/scripts/libmongocrypt.mjs @@ -23,7 +23,6 @@ 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'] }, - 'cmake-path': { type: 'string', default: 'cmake' }, 'no-macos-universal': { type: 'boolean', default: false }, clean: { short: 'c', type: 'boolean', default: false }, build: { short: 'b', type: 'boolean', default: false }, @@ -52,7 +51,6 @@ async function parseArguments() { build: args.values.build, dynamic: args.values.dynamic, noMacosUniversal: args.values['no-macos-universal'], - cMakePath: args.values['cmake-path'], pkg }; } @@ -77,6 +75,11 @@ function toFlags(object) { return Array.from(Object.entries(object)).map(([k, v]) => `-${k}=${v}`); } +/** GYP define maker: `toFlags({a: 1, b: 2})` yields `['a=1', 'b=2']` */ +function toDefines(object) { + return Array.from(Object.entries(object)).map(([k, v]) => `${k}=${v}`); +} + export async function cloneLibMongoCrypt(libmongocryptRoot, { url, ref }) { console.error('fetching libmongocrypt...', { url, ref }); await fs.rm(libmongocryptRoot, { recursive: true, force: true }); @@ -137,13 +140,17 @@ export async function buildLibMongoCrypt(libmongocryptRoot, nodeDepsRoot, option ? toFlags({ DCMAKE_OSX_DEPLOYMENT_TARGET: '10.12' }) : []; + const cmakeProgram = process.platform === 'win32' ? 'cmake.exe' : 'cmake'; + await run( - options.cMakePath, + cmakeProgram, [...CMAKE_FLAGS, ...WINDOWS_CMAKE_FLAGS, ...DARWIN_CMAKE_FLAGS, libmongocryptRoot], { cwd: nodeBuildRoot, shell: process.platform === 'win32' } ); - await run(options.cMakePath, ['--build', '.', '--target', 'install', '--config', 'RelWithDebInfo'], { - cwd: nodeBuildRoot, shell: process.platform === 'win32' + + await run(cmakeProgram, ['--build', '.', '--target', 'install', '--config', 'RelWithDebInfo'], { + cwd: nodeBuildRoot, + shell: process.platform === 'win32' }); } @@ -273,17 +280,20 @@ async function main() { await run('npm', ['install', '--ignore-scripts']); // The prebuild command will make both a .node file in `./build` (local and CI testing will run on current code) // it will also produce `./prebuilds/mongodb-client-encryption-vVERSION-napi-vNAPI_VERSION-OS-ARCH.tar.gz`. - let prebuildOptions; + const gypDefines = []; if (process.platform === 'darwin' && args.noMacosUniversal) { - prebuildOptions ??= { env: { ...process.env } }; - prebuildOptions.env.GYP_DEFINES = (prebuildOptions.env.GYP_DEFINES ?? '') + 'no_macos_universal=true ' + gypDefines.push({ no_macos_universal: true }); } if (args.dynamic) { - prebuildOptions ??= { env: { ...process.env } } - prebuildOptions.env.GYP_DEFINES = (prebuildOptions.env.GYP_DEFINES ?? '') + 'build_type=dynamic ' + gypDefines.push({ build_type: 'dynamic' }); } + const prebuildOptions = + gypDefines.length > 0 + ? { env: { ...process.env, GYP_DEFINES: toDefines(gypDefines) } } + : undefined; + await run('npm', ['run', 'prebuild'], prebuildOptions); // Compile Typescript await run('npm', ['run', 'prepare']); From d840143dbe1d07bbef8b47b391fa6ee21099b9ff Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Wed, 26 Jun 2024 10:43:05 -0400 Subject: [PATCH 08/10] chore: set _type and use it's value to determine defining arch --- .github/scripts/libmongocrypt.mjs | 4 ++-- binding.gyp | 24 ++++++++---------------- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/.github/scripts/libmongocrypt.mjs b/.github/scripts/libmongocrypt.mjs index d6685a1..f9e463f 100644 --- a/.github/scripts/libmongocrypt.mjs +++ b/.github/scripts/libmongocrypt.mjs @@ -125,7 +125,7 @@ export async function buildLibMongoCrypt(libmongocryptRoot, nodeDepsRoot, option /** * Where to install libmongocrypt * Note that `binding.gyp` will set `./deps/include` - * as an include path if BUILD_TYPE=static + * as an include path if libmongocrypt_link_type=static */ DCMAKE_INSTALL_PREFIX: nodeDepsRoot }); @@ -286,7 +286,7 @@ async function main() { } if (args.dynamic) { - gypDefines.push({ build_type: 'dynamic' }); + gypDefines.push({ libmongocrypt_link_type: 'dynamic' }); } const prebuildOptions = diff --git a/binding.gyp b/binding.gyp index df4d2bb..70f5cc8 100644 --- a/binding.gyp +++ b/binding.gyp @@ -1,13 +1,13 @@ { 'targets': [{ 'target_name': 'mongocrypt', + 'type': 'loadable_module', 'include_dirs': [ " Date: Wed, 26 Jun 2024 11:39:16 -0400 Subject: [PATCH 09/10] chore: remove no-universal flag --- .github/scripts/libmongocrypt.mjs | 19 +++++-------------- README.md | 1 - binding.gyp | 4 ++-- 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/.github/scripts/libmongocrypt.mjs b/.github/scripts/libmongocrypt.mjs index f9e463f..3352fb3 100644 --- a/.github/scripts/libmongocrypt.mjs +++ b/.github/scripts/libmongocrypt.mjs @@ -23,7 +23,6 @@ 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'] }, - 'no-macos-universal': { type: 'boolean', default: false }, clean: { short: 'c', type: 'boolean', default: false }, build: { short: 'b', type: 'boolean', default: false }, dynamic: { type: 'boolean', default: false }, @@ -50,7 +49,6 @@ async function parseArguments() { clean: args.values.clean, build: args.values.build, dynamic: args.values.dynamic, - noMacosUniversal: args.values['no-macos-universal'], pkg }; } @@ -75,11 +73,6 @@ function toFlags(object) { return Array.from(Object.entries(object)).map(([k, v]) => `-${k}=${v}`); } -/** GYP define maker: `toFlags({a: 1, b: 2})` yields `['a=1', 'b=2']` */ -function toDefines(object) { - return Array.from(Object.entries(object)).map(([k, v]) => `${k}=${v}`); -} - export async function cloneLibMongoCrypt(libmongocryptRoot, { url, ref }) { console.error('fetching libmongocrypt...', { url, ref }); await fs.rm(libmongocryptRoot, { recursive: true, force: true }); @@ -280,25 +273,23 @@ async function main() { await run('npm', ['install', '--ignore-scripts']); // The prebuild command will make both a .node file in `./build` (local and CI testing will run on current code) // it will also produce `./prebuilds/mongodb-client-encryption-vVERSION-napi-vNAPI_VERSION-OS-ARCH.tar.gz`. - const gypDefines = []; - if (process.platform === 'darwin' && args.noMacosUniversal) { - gypDefines.push({ no_macos_universal: true }); - } + let gypDefines = process.env.GYP_DEFINES ?? ''; if (args.dynamic) { - gypDefines.push({ libmongocrypt_link_type: 'dynamic' }); + gypDefines += ' libmongocrypt_link_type=dynamic'; } + gypDefines = gypDefines.trim(); const prebuildOptions = gypDefines.length > 0 - ? { env: { ...process.env, GYP_DEFINES: toDefines(gypDefines) } } + ? { env: { ...process.env, GYP_DEFINES: gypDefines } } : undefined; await run('npm', ['run', 'prebuild'], prebuildOptions); // Compile Typescript await run('npm', ['run', 'prepare']); - if (process.platform === 'darwin' && !args.noMacosUniversal) { + if (process.platform === 'darwin' && process.arch === 'arm64') { // The "arm64" build is actually a universal binary const armTar = `mongodb-client-encryption-v${pkg.version}-napi-v4-darwin-arm64.tar.gz`; const x64Tar = `mongodb-client-encryption-v${pkg.version}-napi-v4-darwin-x64.tar.gz`; diff --git a/README.md b/README.md index 215b218..7f1ec06 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,6 @@ Can be configured to clone and build without crypto. You may use "latest" to get current libmongocrypt `HEAD`. --clean Combined with --build, the script will not skip cloning and rebuilding libmongocrypt. --build Instead of downloading, clone and build libmongocrypt along with the bindings. ---no-macos-universal Disable creating a universal binary for MacOS builds. --dynamic Skips cloning or downloading libmongocrypt, runs prebuild with build_type set to "dynamic" to compile a prebuild that links to a system copy of libmongocrypt. diff --git a/binding.gyp b/binding.gyp index 70f5cc8..9fc0370 100644 --- a/binding.gyp +++ b/binding.gyp @@ -25,7 +25,7 @@ }, 'conditions': [ ['OS=="mac"', { 'cflags+': ['-fvisibility=hidden'] }], - ['_type!="static_library"', { + ['_type!="static_library" and ARCH=="arm64"', { 'xcode_settings': { "OTHER_CFLAGS": [ "-arch x86_64", @@ -40,7 +40,7 @@ ['libmongocrypt_link_type=="dynamic"', { 'link_settings': { 'libraries': ['-lmongocrypt'] } }], - ['libmongocrypt_link_type!="dynamic"', { + ['libmongocrypt_link_type=="static"', { 'conditions': [ ['OS!="win"', { 'include_dirs': [ From 3a6ef47c736c2b590279b50d2aacde5cadc4071f Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Wed, 26 Jun 2024 11:58:20 -0400 Subject: [PATCH 10/10] chore: skip bindings flag --- .github/scripts/libmongocrypt.mjs | 64 +++++++++++++++++-------------- README.md | 1 + 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/.github/scripts/libmongocrypt.mjs b/.github/scripts/libmongocrypt.mjs index 3352fb3..0f83095 100644 --- a/.github/scripts/libmongocrypt.mjs +++ b/.github/scripts/libmongocrypt.mjs @@ -27,6 +27,7 @@ async function parseArguments() { build: { short: 'b', type: 'boolean', default: false }, dynamic: { type: 'boolean', default: false }, fastDownload: { type: 'boolean', default: false }, // Potentially incorrect download, only for the brave and impatient + 'skip-bindings': { type: 'boolean', default: false }, help: { short: 'h', type: 'boolean', default: false } }; @@ -49,6 +50,7 @@ async function parseArguments() { clean: args.values.clean, build: args.values.build, dynamic: args.values.dynamic, + skipBindings: args.values['skip-bindings'], pkg }; } @@ -235,6 +237,38 @@ export async function downloadLibMongoCrypt(nodeDepsRoot, { ref, fastDownload }) } } +async function buildBindings(args, pkg) { + await fs.rm(resolveRoot('build'), { force: true, recursive: true }); + await fs.rm(resolveRoot('prebuilds'), { force: true, recursive: true }); + + // install with "ignore-scripts" so that we don't attempt to download a prebuild + await run('npm', ['install', '--ignore-scripts']); + // The prebuild command will make both a .node file in `./build` (local and CI testing will run on current code) + // it will also produce `./prebuilds/mongodb-client-encryption-vVERSION-napi-vNAPI_VERSION-OS-ARCH.tar.gz`. + + let gypDefines = process.env.GYP_DEFINES ?? ''; + if (args.dynamic) { + gypDefines += ' libmongocrypt_link_type=dynamic'; + } + + gypDefines = gypDefines.trim(); + const prebuildOptions = + gypDefines.length > 0 + ? { env: { ...process.env, GYP_DEFINES: gypDefines } } + : undefined; + + await run('npm', ['run', 'prebuild'], prebuildOptions); + // Compile Typescript + await run('npm', ['run', 'prepare']); + + if (process.platform === 'darwin' && process.arch === 'arm64') { + // The "arm64" build is actually a universal binary + const armTar = `mongodb-client-encryption-v${pkg.version}-napi-v4-darwin-arm64.tar.gz`; + const x64Tar = `mongodb-client-encryption-v${pkg.version}-napi-v4-darwin-x64.tar.gz`; + await fs.copyFile(resolveRoot('prebuilds', armTar), resolveRoot('prebuilds', x64Tar)); + } +} + async function main() { const { pkg, ...args } = await parseArguments(); console.log(args); @@ -266,34 +300,8 @@ async function main() { await downloadLibMongoCrypt(nodeDepsDir, args); } - await fs.rm(resolveRoot('build'), { force: true, recursive: true }); - await fs.rm(resolveRoot('prebuilds'), { force: true, recursive: true }); - - // install with "ignore-scripts" so that we don't attempt to download a prebuild - await run('npm', ['install', '--ignore-scripts']); - // The prebuild command will make both a .node file in `./build` (local and CI testing will run on current code) - // it will also produce `./prebuilds/mongodb-client-encryption-vVERSION-napi-vNAPI_VERSION-OS-ARCH.tar.gz`. - - let gypDefines = process.env.GYP_DEFINES ?? ''; - if (args.dynamic) { - gypDefines += ' libmongocrypt_link_type=dynamic'; - } - - gypDefines = gypDefines.trim(); - const prebuildOptions = - gypDefines.length > 0 - ? { env: { ...process.env, GYP_DEFINES: gypDefines } } - : undefined; - - await run('npm', ['run', 'prebuild'], prebuildOptions); - // Compile Typescript - await run('npm', ['run', 'prepare']); - - if (process.platform === 'darwin' && process.arch === 'arm64') { - // The "arm64" build is actually a universal binary - const armTar = `mongodb-client-encryption-v${pkg.version}-napi-v4-darwin-arm64.tar.gz`; - const x64Tar = `mongodb-client-encryption-v${pkg.version}-napi-v4-darwin-x64.tar.gz`; - await fs.copyFile(resolveRoot('prebuilds', armTar), resolveRoot('prebuilds', x64Tar)); + if (!args.skipBindings) { + await buildBindings(args, pkg); } } diff --git a/README.md b/README.md index 7f1ec06..910c19f 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ Can be configured to clone and build without crypto. --build Instead of downloading, clone and build libmongocrypt along with the bindings. --dynamic Skips cloning or downloading libmongocrypt, runs prebuild with build_type set to "dynamic" to compile a prebuild that links to a system copy of libmongocrypt. +--skip-bindings Skips running prebuild. Useful if only the libmongocrypt dependency is desired. Only suitable for local development: