Skip to content

Commit 07722d8

Browse files
committed
chore: fix building on intel macs
1 parent 95849dd commit 07722d8

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

.github/scripts/libmongocrypt.mjs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// @ts-check
2+
13
import util from 'node:util';
24
import process from 'node:process';
35
import fs from 'node:fs/promises';
@@ -21,6 +23,7 @@ async function parseArguments() {
2123
const options = {
2224
gitURL: { short: 'u', type: 'string', default: 'https://github.com/mongodb/libmongocrypt.git' },
2325
libVersion: { short: 'l', type: 'string', default: pkg['mongodb:libmongocrypt'] },
26+
'allow-only-x64-darwin': { type: 'boolean', default: false },
2427
clean: { short: 'c', type: 'boolean', default: false },
2528
build: { short: 'b', type: 'boolean', default: false },
2629
help: { short: 'h', type: 'boolean', default: false }
@@ -42,6 +45,7 @@ async function parseArguments() {
4245
libmongocrypt: { url: args.values.gitURL, ref: args.values.libVersion },
4346
clean: args.values.clean,
4447
build: args.values.build,
48+
allowOnlyX64Darwin: args.values['allow-only-x64-darwin'],
4549
pkg
4650
};
4751
}
@@ -121,14 +125,14 @@ export async function buildLibMongoCrypt(libmongocryptRoot, nodeDepsRoot) {
121125
? toFlags({ Thost: 'x64', A: 'x64', DENABLE_WINDOWS_STATIC_RUNTIME: 'ON' })
122126
: [];
123127

124-
const MACOS_CMAKE_FLAGS =
125-
process.platform === 'darwin' // The minimum macos target version we want for
128+
const DARWIN_CMAKE_FLAGS =
129+
process.platform === 'darwin' // The minimum darwin target version we want for
126130
? toFlags({ DCMAKE_OSX_DEPLOYMENT_TARGET: '10.12' })
127131
: [];
128132

129133
await run(
130134
'cmake',
131-
[...CMAKE_FLAGS, ...WINDOWS_CMAKE_FLAGS, ...MACOS_CMAKE_FLAGS, libmongocryptRoot],
135+
[...CMAKE_FLAGS, ...WINDOWS_CMAKE_FLAGS, ...DARWIN_CMAKE_FLAGS, libmongocryptRoot],
132136
{ cwd: nodeBuildRoot }
133137
);
134138
await run('cmake', ['--build', '.', '--target', 'install', '--config', 'RelWithDebInfo'], {
@@ -170,6 +174,7 @@ export async function downloadLibMongoCrypt(nodeDepsRoot, { ref }) {
170174
stdio: ['pipe', 'inherit'],
171175
cwd: resolveRoot('.')
172176
});
177+
if (unzip.stdin == null) throw new Error('Tar process stdin must be stream-able');
173178

174179
const [response] = await events.once(https.get(downloadURL), 'response');
175180

@@ -190,35 +195,35 @@ export async function downloadLibMongoCrypt(nodeDepsRoot, { ref }) {
190195
}
191196

192197
async function main() {
193-
const { libmongocrypt, build, clean, pkg } = await parseArguments();
198+
const args = await parseArguments();
194199

195200
const nodeDepsDir = resolveRoot('deps');
196201

197-
if (build) {
202+
if (args.build) {
198203
const libmongocryptCloneDir = resolveRoot('_libmongocrypt');
199204

200205
const currentLibMongoCryptBranch = await fs
201206
.readFile(path.join(libmongocryptCloneDir, '.git', 'HEAD'), 'utf8')
202207
.catch(() => '');
203208
const isClonedAndCheckedOut = currentLibMongoCryptBranch
204209
.trim()
205-
.endsWith(`r-${libmongocrypt.ref}`);
210+
.endsWith(`r-${args.libmongocrypt.ref}`);
206211

207-
if (clean || !isClonedAndCheckedOut) {
208-
await cloneLibMongoCrypt(libmongocryptCloneDir, libmongocrypt);
212+
if (args.clean || !isClonedAndCheckedOut) {
213+
await cloneLibMongoCrypt(libmongocryptCloneDir, args.libmongocrypt);
209214
}
210215

211216
const libmongocryptBuiltVersion = await fs
212217
.readFile(path.join(libmongocryptCloneDir, 'VERSION_CURRENT'), 'utf8')
213218
.catch(() => '');
214-
const isBuilt = libmongocryptBuiltVersion.trim() === libmongocrypt.ref;
219+
const isBuilt = libmongocryptBuiltVersion.trim() === args.libmongocrypt.ref;
215220

216-
if (clean || !isBuilt) {
221+
if (args.clean || !isBuilt) {
217222
await buildLibMongoCrypt(libmongocryptCloneDir, nodeDepsDir);
218223
}
219224
} else {
220225
// Download
221-
await downloadLibMongoCrypt(nodeDepsDir, libmongocrypt);
226+
await downloadLibMongoCrypt(nodeDepsDir, args.libmongocrypt);
222227
}
223228

224229
await fs.rm(resolveRoot('build'), { force: true, recursive: true });
@@ -232,10 +237,19 @@ async function main() {
232237

233238
if (process.platform === 'darwin') {
234239
// The "arm64" build is actually a universal binary
235-
await fs.copyFile(
236-
resolveRoot('prebuilds', `mongodb-client-encryption-v${pkg.version}-napi-v4-darwin-arm64.tar.gz`),
237-
resolveRoot('prebuilds', `mongodb-client-encryption-v${pkg.version}-napi-v4-darwin-x64.tar.gz`)
238-
);
240+
try {
241+
await fs.copyFile(
242+
resolveRoot('prebuilds', `mongodb-client-encryption-v${args.pkg.version}-napi-v4-darwin-arm64.tar.gz`),
243+
resolveRoot('prebuilds', `mongodb-client-encryption-v${args.pkg.version}-napi-v4-darwin-x64.tar.gz`)
244+
);
245+
} catch {
246+
if (process.arch === 'x64') {
247+
// The user of this script is building on an x64/intel/amd64 darwin which cannot build a universal bundle
248+
// By default we exit with failure because we do not want to release an intel only build
249+
console.error('Intel Darwin cannot build a universal bundle');
250+
process.exitCode = args.allowOnlyX64Darwin ? 0 : 1;
251+
}
252+
}
239253
}
240254
}
241255

binding.gyp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"<!(node -p \"require('node-addon-api').include_dir\")",
66
],
77
'variables': {
8+
'ARCH': '<(host_arch)',
89
'variables': {
910
'build_type%': "dynamic",
1011
},
@@ -24,14 +25,6 @@
2425
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
2526
'CLANG_CXX_LIBRARY': 'libc++',
2627
'MACOSX_DEPLOYMENT_TARGET': '10.12',
27-
"OTHER_CFLAGS": [
28-
"-arch x86_64",
29-
"-arch arm64"
30-
],
31-
"OTHER_LDFLAGS": [
32-
"-arch x86_64",
33-
"-arch arm64"
34-
]
3528
},
3629
'cflags!': [ '-fno-exceptions' ],
3730
'cflags_cc!': [ '-fno-exceptions' ],
@@ -45,6 +38,18 @@
4538
'GCC_SYMBOLS_PRIVATE_EXTERN': 'YES', # -fvisibility=hidden
4639
}
4740
}],
41+
['OS=="mac" and ARCH=="arm64"', {
42+
'xcode_settings': {
43+
"OTHER_CFLAGS": [
44+
"-arch x86_64",
45+
"-arch arm64"
46+
],
47+
"OTHER_LDFLAGS": [
48+
"-arch x86_64",
49+
"-arch arm64"
50+
]
51+
}
52+
}],
4853
['build_type=="dynamic"', {
4954
'link_settings': {
5055
'libraries': [

0 commit comments

Comments
 (0)