1
- //@ts -check
1
+ // @ts -check
2
+
2
3
import util from 'node:util' ;
3
4
import process from 'node:process' ;
4
5
import fs from 'node:fs/promises' ;
@@ -24,7 +25,9 @@ async function parseArguments() {
24
25
libVersion : { short : 'l' , type : 'string' , default : pkg [ 'mongodb:libmongocrypt' ] } ,
25
26
clean : { short : 'c' , type : 'boolean' , default : false } ,
26
27
build : { short : 'b' , type : 'boolean' , default : false } ,
28
+ dynamic : { type : 'boolean' , default : false } ,
27
29
fastDownload : { type : 'boolean' , default : false } , // Potentially incorrect download, only for the brave and impatient
30
+ 'skip-bindings' : { type : 'boolean' , default : false } ,
28
31
help : { short : 'h' , type : 'boolean' , default : false }
29
32
} ;
30
33
@@ -46,6 +49,8 @@ async function parseArguments() {
46
49
fastDownload : args . values . fastDownload ,
47
50
clean : args . values . clean ,
48
51
build : args . values . build ,
52
+ dynamic : args . values . dynamic ,
53
+ skipBindings : args . values [ 'skip-bindings' ] ,
49
54
pkg
50
55
} ;
51
56
}
@@ -81,7 +86,7 @@ export async function cloneLibMongoCrypt(libmongocryptRoot, { url, ref }) {
81
86
}
82
87
}
83
88
84
- export async function buildLibMongoCrypt ( libmongocryptRoot , nodeDepsRoot ) {
89
+ export async function buildLibMongoCrypt ( libmongocryptRoot , nodeDepsRoot , options ) {
85
90
console . error ( 'building libmongocrypt...' ) ;
86
91
87
92
const nodeBuildRoot = resolveRoot ( nodeDepsRoot , 'tmp' , 'libmongocrypt-build' ) ;
@@ -115,7 +120,7 @@ export async function buildLibMongoCrypt(libmongocryptRoot, nodeDepsRoot) {
115
120
/**
116
121
* Where to install libmongocrypt
117
122
* Note that `binding.gyp` will set `./deps/include`
118
- * as an include path if BUILD_TYPE =static
123
+ * as an include path if libmongocrypt_link_type =static
119
124
*/
120
125
DCMAKE_INSTALL_PREFIX : nodeDepsRoot
121
126
} ) ;
@@ -125,18 +130,22 @@ export async function buildLibMongoCrypt(libmongocryptRoot, nodeDepsRoot) {
125
130
? toFlags ( { Thost : 'x64' , A : 'x64' , DENABLE_WINDOWS_STATIC_RUNTIME : 'ON' } )
126
131
: [ ] ;
127
132
128
- const MACOS_CMAKE_FLAGS =
129
- process . platform === 'darwin' // The minimum macos target version we want for
133
+ const DARWIN_CMAKE_FLAGS =
134
+ process . platform === 'darwin' // The minimum darwin target version we want for
130
135
? toFlags ( { DCMAKE_OSX_DEPLOYMENT_TARGET : '10.12' } )
131
136
: [ ] ;
132
137
138
+ const cmakeProgram = process . platform === 'win32' ? 'cmake.exe' : 'cmake' ;
139
+
133
140
await run (
134
- 'cmake' ,
135
- [ ...CMAKE_FLAGS , ...WINDOWS_CMAKE_FLAGS , ...MACOS_CMAKE_FLAGS , libmongocryptRoot ] ,
136
- { cwd : nodeBuildRoot }
141
+ cmakeProgram ,
142
+ [ ...CMAKE_FLAGS , ...WINDOWS_CMAKE_FLAGS , ...DARWIN_CMAKE_FLAGS , libmongocryptRoot ] ,
143
+ { cwd : nodeBuildRoot , shell : process . platform === 'win32' }
137
144
) ;
138
- await run ( 'cmake' , [ '--build' , '.' , '--target' , 'install' , '--config' , 'RelWithDebInfo' ] , {
139
- cwd : nodeBuildRoot
145
+
146
+ await run ( cmakeProgram , [ '--build' , '.' , '--target' , 'install' , '--config' , 'RelWithDebInfo' ] , {
147
+ cwd : nodeBuildRoot ,
148
+ shell : process . platform === 'win32'
140
149
} ) ;
141
150
}
142
151
@@ -228,13 +237,45 @@ export async function downloadLibMongoCrypt(nodeDepsRoot, { ref, fastDownload })
228
237
}
229
238
}
230
239
240
+ async function buildBindings ( args , pkg ) {
241
+ await fs . rm ( resolveRoot ( 'build' ) , { force : true , recursive : true } ) ;
242
+ await fs . rm ( resolveRoot ( 'prebuilds' ) , { force : true , recursive : true } ) ;
243
+
244
+ // install with "ignore-scripts" so that we don't attempt to download a prebuild
245
+ await run ( 'npm' , [ 'install' , '--ignore-scripts' ] ) ;
246
+ // The prebuild command will make both a .node file in `./build` (local and CI testing will run on current code)
247
+ // it will also produce `./prebuilds/mongodb-client-encryption-vVERSION-napi-vNAPI_VERSION-OS-ARCH.tar.gz`.
248
+
249
+ let gypDefines = process . env . GYP_DEFINES ?? '' ;
250
+ if ( args . dynamic ) {
251
+ gypDefines += ' libmongocrypt_link_type=dynamic' ;
252
+ }
253
+
254
+ gypDefines = gypDefines . trim ( ) ;
255
+ const prebuildOptions =
256
+ gypDefines . length > 0
257
+ ? { env : { ...process . env , GYP_DEFINES : gypDefines } }
258
+ : undefined ;
259
+
260
+ await run ( 'npm' , [ 'run' , 'prebuild' ] , prebuildOptions ) ;
261
+ // Compile Typescript
262
+ await run ( 'npm' , [ 'run' , 'prepare' ] ) ;
263
+
264
+ if ( process . platform === 'darwin' && process . arch === 'arm64' ) {
265
+ // The "arm64" build is actually a universal binary
266
+ const armTar = `mongodb-client-encryption-v${ pkg . version } -napi-v4-darwin-arm64.tar.gz` ;
267
+ const x64Tar = `mongodb-client-encryption-v${ pkg . version } -napi-v4-darwin-x64.tar.gz` ;
268
+ await fs . copyFile ( resolveRoot ( 'prebuilds' , armTar ) , resolveRoot ( 'prebuilds' , x64Tar ) ) ;
269
+ }
270
+ }
271
+
231
272
async function main ( ) {
232
273
const { pkg, ...args } = await parseArguments ( ) ;
233
274
console . log ( args ) ;
234
275
235
276
const nodeDepsDir = resolveRoot ( 'deps' ) ;
236
277
237
- if ( args . build ) {
278
+ if ( args . build && ! args . dynamic ) {
238
279
const libmongocryptCloneDir = resolveRoot ( '_libmongocrypt' ) ;
239
280
240
281
const currentLibMongoCryptBranch = await fs
@@ -252,36 +293,15 @@ async function main() {
252
293
const isBuilt = libmongocryptBuiltVersion . trim ( ) === args . ref ;
253
294
254
295
if ( args . clean || ! isBuilt ) {
255
- await buildLibMongoCrypt ( libmongocryptCloneDir , nodeDepsDir ) ;
296
+ await buildLibMongoCrypt ( libmongocryptCloneDir , nodeDepsDir , args ) ;
256
297
}
257
- } else {
298
+ } else if ( ! args . dynamic ) {
258
299
// Download
259
300
await downloadLibMongoCrypt ( nodeDepsDir , args ) ;
260
301
}
261
302
262
- await fs . rm ( resolveRoot ( 'build' ) , { force : true , recursive : true } ) ;
263
- await fs . rm ( resolveRoot ( 'prebuilds' ) , { force : true , recursive : true } ) ;
264
-
265
- // install with "ignore-scripts" so that we don't attempt to download a prebuild
266
- await run ( 'npm' , [ 'install' , '--ignore-scripts' ] ) ;
267
- // The prebuild command will make both a .node file in `./build` (local and CI testing will run on current code)
268
- // it will also produce `./prebuilds/mongodb-client-encryption-vVERSION-napi-vNAPI_VERSION-OS-ARCH.tar.gz`.
269
- await run ( 'npm' , [ 'run' , 'prebuild' ] ) ;
270
- // Compile Typescript
271
- await run ( 'npm' , [ 'run' , 'prepare' ] ) ;
272
-
273
- if ( process . platform === 'darwin' ) {
274
- // The "arm64" build is actually a universal binary
275
- await fs . copyFile (
276
- resolveRoot (
277
- 'prebuilds' ,
278
- `mongodb-client-encryption-v${ pkg . version } -napi-v4-darwin-arm64.tar.gz`
279
- ) ,
280
- resolveRoot (
281
- 'prebuilds' ,
282
- `mongodb-client-encryption-v${ pkg . version } -napi-v4-darwin-x64.tar.gz`
283
- )
284
- ) ;
303
+ if ( ! args . skipBindings ) {
304
+ await buildBindings ( args , pkg ) ;
285
305
}
286
306
}
287
307
0 commit comments