Skip to content

Commit

Permalink
chore: fix building on intel macs
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Jun 18, 2024
1 parent 95849dd commit 07722d8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
44 changes: 29 additions & 15 deletions .github/scripts/libmongocrypt.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check

import util from 'node:util';
import process from 'node:process';
import fs from 'node:fs/promises';
Expand All @@ -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 }
Expand All @@ -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
};
}
Expand Down Expand Up @@ -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'], {
Expand Down Expand Up @@ -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');

Expand All @@ -190,35 +195,35 @@ 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
.readFile(path.join(libmongocryptCloneDir, '.git', 'HEAD'), 'utf8')
.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 });
Expand All @@ -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;
}
}
}
}

Expand Down
21 changes: 13 additions & 8 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"<!(node -p \"require('node-addon-api').include_dir\")",
],
'variables': {
'ARCH': '<(host_arch)',
'variables': {
'build_type%': "dynamic",
},
Expand All @@ -24,14 +25,6 @@
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
'CLANG_CXX_LIBRARY': 'libc++',
'MACOSX_DEPLOYMENT_TARGET': '10.12',
"OTHER_CFLAGS": [
"-arch x86_64",
"-arch arm64"
],
"OTHER_LDFLAGS": [
"-arch x86_64",
"-arch arm64"
]
},
'cflags!': [ '-fno-exceptions' ],
'cflags_cc!': [ '-fno-exceptions' ],
Expand All @@ -45,6 +38,18 @@
'GCC_SYMBOLS_PRIVATE_EXTERN': 'YES', # -fvisibility=hidden
}
}],
['OS=="mac" and ARCH=="arm64"', {
'xcode_settings': {
"OTHER_CFLAGS": [
"-arch x86_64",
"-arch arm64"
],
"OTHER_LDFLAGS": [
"-arch x86_64",
"-arch arm64"
]
}
}],
['build_type=="dynamic"', {
'link_settings': {
'libraries': [
Expand Down

0 comments on commit 07722d8

Please sign in to comment.