1
+ // @ts -check
2
+
1
3
import util from 'node:util' ;
2
4
import process from 'node:process' ;
3
5
import fs from 'node:fs/promises' ;
@@ -21,6 +23,7 @@ async function parseArguments() {
21
23
const options = {
22
24
gitURL : { short : 'u' , type : 'string' , default : 'https://github.com/mongodb/libmongocrypt.git' } ,
23
25
libVersion : { short : 'l' , type : 'string' , default : pkg [ 'mongodb:libmongocrypt' ] } ,
26
+ 'allow-only-x64-darwin' : { type : 'boolean' , default : false } ,
24
27
clean : { short : 'c' , type : 'boolean' , default : false } ,
25
28
build : { short : 'b' , type : 'boolean' , default : false } ,
26
29
help : { short : 'h' , type : 'boolean' , default : false }
@@ -42,6 +45,7 @@ async function parseArguments() {
42
45
libmongocrypt : { url : args . values . gitURL , ref : args . values . libVersion } ,
43
46
clean : args . values . clean ,
44
47
build : args . values . build ,
48
+ allowOnlyX64Darwin : args . values [ 'allow-only-x64-darwin' ] ,
45
49
pkg
46
50
} ;
47
51
}
@@ -121,14 +125,14 @@ export async function buildLibMongoCrypt(libmongocryptRoot, nodeDepsRoot) {
121
125
? toFlags ( { Thost : 'x64' , A : 'x64' , DENABLE_WINDOWS_STATIC_RUNTIME : 'ON' } )
122
126
: [ ] ;
123
127
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
126
130
? toFlags ( { DCMAKE_OSX_DEPLOYMENT_TARGET : '10.12' } )
127
131
: [ ] ;
128
132
129
133
await run (
130
134
'cmake' ,
131
- [ ...CMAKE_FLAGS , ...WINDOWS_CMAKE_FLAGS , ...MACOS_CMAKE_FLAGS , libmongocryptRoot ] ,
135
+ [ ...CMAKE_FLAGS , ...WINDOWS_CMAKE_FLAGS , ...DARWIN_CMAKE_FLAGS , libmongocryptRoot ] ,
132
136
{ cwd : nodeBuildRoot }
133
137
) ;
134
138
await run ( 'cmake' , [ '--build' , '.' , '--target' , 'install' , '--config' , 'RelWithDebInfo' ] , {
@@ -170,6 +174,7 @@ export async function downloadLibMongoCrypt(nodeDepsRoot, { ref }) {
170
174
stdio : [ 'pipe' , 'inherit' ] ,
171
175
cwd : resolveRoot ( '.' )
172
176
} ) ;
177
+ if ( unzip . stdin == null ) throw new Error ( 'Tar process stdin must be stream-able' ) ;
173
178
174
179
const [ response ] = await events . once ( https . get ( downloadURL ) , 'response' ) ;
175
180
@@ -190,35 +195,35 @@ export async function downloadLibMongoCrypt(nodeDepsRoot, { ref }) {
190
195
}
191
196
192
197
async function main ( ) {
193
- const { libmongocrypt , build , clean , pkg } = await parseArguments ( ) ;
198
+ const args = await parseArguments ( ) ;
194
199
195
200
const nodeDepsDir = resolveRoot ( 'deps' ) ;
196
201
197
- if ( build ) {
202
+ if ( args . build ) {
198
203
const libmongocryptCloneDir = resolveRoot ( '_libmongocrypt' ) ;
199
204
200
205
const currentLibMongoCryptBranch = await fs
201
206
. readFile ( path . join ( libmongocryptCloneDir , '.git' , 'HEAD' ) , 'utf8' )
202
207
. catch ( ( ) => '' ) ;
203
208
const isClonedAndCheckedOut = currentLibMongoCryptBranch
204
209
. trim ( )
205
- . endsWith ( `r-${ libmongocrypt . ref } ` ) ;
210
+ . endsWith ( `r-${ args . libmongocrypt . ref } ` ) ;
206
211
207
- if ( clean || ! isClonedAndCheckedOut ) {
208
- await cloneLibMongoCrypt ( libmongocryptCloneDir , libmongocrypt ) ;
212
+ if ( args . clean || ! isClonedAndCheckedOut ) {
213
+ await cloneLibMongoCrypt ( libmongocryptCloneDir , args . libmongocrypt ) ;
209
214
}
210
215
211
216
const libmongocryptBuiltVersion = await fs
212
217
. readFile ( path . join ( libmongocryptCloneDir , 'VERSION_CURRENT' ) , 'utf8' )
213
218
. catch ( ( ) => '' ) ;
214
- const isBuilt = libmongocryptBuiltVersion . trim ( ) === libmongocrypt . ref ;
219
+ const isBuilt = libmongocryptBuiltVersion . trim ( ) === args . libmongocrypt . ref ;
215
220
216
- if ( clean || ! isBuilt ) {
221
+ if ( args . clean || ! isBuilt ) {
217
222
await buildLibMongoCrypt ( libmongocryptCloneDir , nodeDepsDir ) ;
218
223
}
219
224
} else {
220
225
// Download
221
- await downloadLibMongoCrypt ( nodeDepsDir , libmongocrypt ) ;
226
+ await downloadLibMongoCrypt ( nodeDepsDir , args . libmongocrypt ) ;
222
227
}
223
228
224
229
await fs . rm ( resolveRoot ( 'build' ) , { force : true , recursive : true } ) ;
@@ -232,10 +237,19 @@ async function main() {
232
237
233
238
if ( process . platform === 'darwin' ) {
234
239
// 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
+ }
239
253
}
240
254
}
241
255
0 commit comments