|
| 1 | +// @ts-check |
| 2 | + |
| 3 | +import util from 'node:util'; |
| 4 | +import process from 'node:process'; |
| 5 | +import fs, { readFile } from 'node:fs/promises'; |
| 6 | +import child_process from 'node:child_process'; |
| 7 | +import events from 'node:events'; |
| 8 | +import path from 'node:path'; |
| 9 | +import https from 'node:https'; |
| 10 | +import stream from 'node:stream/promises'; |
| 11 | +import { |
| 12 | + buildLibmongocryptDownloadUrl, |
| 13 | + getLibmongocryptPrebuildName, |
| 14 | + resolveRoot, |
| 15 | + run |
| 16 | +} from './utils.mjs'; |
| 17 | + |
| 18 | +async function parseArguments() { |
| 19 | + const pkg = JSON.parse(await fs.readFile(resolveRoot('package.json'), 'utf8')); |
| 20 | + |
| 21 | + const options = { |
| 22 | + gitURL: { short: 'u', type: 'string', default: 'https://github.com/mongodb/libmongocrypt.git' }, |
| 23 | + libVersion: { short: 'l', type: 'string', default: pkg['mongodb:libmongocrypt'] }, |
| 24 | + clean: { short: 'c', type: 'boolean', default: false }, |
| 25 | + build: { short: 'b', type: 'boolean', default: false }, |
| 26 | + dynamic: { type: 'boolean', default: false }, |
| 27 | + 'skip-bindings': { type: 'boolean', default: false }, |
| 28 | + help: { short: 'h', type: 'boolean', default: false } |
| 29 | + }; |
| 30 | + |
| 31 | + const args = util.parseArgs({ args: process.argv.slice(2), options, allowPositionals: false }); |
| 32 | + |
| 33 | + if (args.values.help) { |
| 34 | + console.log( |
| 35 | + `${path.basename(process.argv[1])} ${[...Object.keys(options)] |
| 36 | + .filter(k => k !== 'help') |
| 37 | + .map(k => `[--${k}=${options[k].type}]`) |
| 38 | + .join(' ')}` |
| 39 | + ); |
| 40 | + process.exit(0); |
| 41 | + } |
| 42 | + |
| 43 | + return { |
| 44 | + url: args.values.gitURL, |
| 45 | + ref: args.values.libVersion, |
| 46 | + clean: args.values.clean, |
| 47 | + build: args.values.build, |
| 48 | + dynamic: args.values.dynamic, |
| 49 | + skipBindings: args.values['skip-bindings'], |
| 50 | + pkg |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +export async function cloneLibMongoCrypt(libmongocryptRoot, { url, ref }) { |
| 55 | + console.error('fetching libmongocrypt...', { url, ref }); |
| 56 | + await fs.rm(libmongocryptRoot, { recursive: true, force: true }); |
| 57 | + await run('git', ['clone', url, libmongocryptRoot]); |
| 58 | + if (ref !== 'latest') { |
| 59 | + await run('git', ['fetch', '--tags'], { cwd: libmongocryptRoot }); |
| 60 | + await run('git', ['checkout', ref, '-b', `r-${ref}`], { cwd: libmongocryptRoot }); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +export async function buildLibMongoCrypt(libmongocryptRoot, nodeDepsRoot, options) { |
| 65 | + function toCLIFlags(object) { |
| 66 | + return Array.from(Object.entries(object)).map(([k, v]) => `-${k}=${v}`); |
| 67 | + } |
| 68 | + |
| 69 | + console.error('building libmongocrypt...'); |
| 70 | + |
| 71 | + const nodeBuildRoot = resolveRoot(nodeDepsRoot, 'tmp', 'libmongocrypt-build'); |
| 72 | + |
| 73 | + await fs.rm(nodeBuildRoot, { recursive: true, force: true }); |
| 74 | + await fs.mkdir(nodeBuildRoot, { recursive: true }); |
| 75 | + |
| 76 | + const CMAKE_FLAGS = toCLIFlags({ |
| 77 | + DDISABLE_NATIVE_CRYPTO: '1', |
| 78 | + DCMAKE_INSTALL_LIBDIR: 'lib', |
| 79 | + DENABLE_MORE_WARNINGS_AS_ERRORS: 'ON', |
| 80 | + DCMAKE_PREFIX_PATH: nodeDepsRoot, |
| 81 | + DCMAKE_INSTALL_PREFIX: nodeDepsRoot |
| 82 | + }); |
| 83 | + |
| 84 | + const WINDOWS_CMAKE_FLAGS = |
| 85 | + process.platform === 'win32' |
| 86 | + ? toCLIFlags({ |
| 87 | + DCMAKE_MSVC_RUNTIME_LIBRARY: 'MultiThreaded', |
| 88 | + T: 'host=x64', |
| 89 | + A: 'x64' |
| 90 | + }) |
| 91 | + : []; |
| 92 | + |
| 93 | + const DARWIN_CMAKE_FLAGS = |
| 94 | + process.platform === 'darwin' ? toCLIFlags({ DCMAKE_OSX_DEPLOYMENT_TARGET: '10.12' }) : []; |
| 95 | + |
| 96 | + const cmakeProgram = process.platform === 'win32' ? 'cmake.exe' : 'cmake'; |
| 97 | + |
| 98 | + await run( |
| 99 | + cmakeProgram, |
| 100 | + [...CMAKE_FLAGS, ...WINDOWS_CMAKE_FLAGS, ...DARWIN_CMAKE_FLAGS, libmongocryptRoot], |
| 101 | + { cwd: nodeBuildRoot, shell: process.platform === 'win32' } |
| 102 | + ); |
| 103 | + |
| 104 | + await run(cmakeProgram, ['--build', '.', '--target', 'install', '--config', 'RelWithDebInfo'], { |
| 105 | + cwd: nodeBuildRoot, |
| 106 | + shell: process.platform === 'win32' |
| 107 | + }); |
| 108 | +} |
| 109 | + |
| 110 | +export async function downloadLibMongoCrypt(nodeDepsRoot, { ref }) { |
| 111 | + const prebuild = getLibmongocryptPrebuildName(); |
| 112 | + const downloadURL = buildLibmongocryptDownloadUrl(ref, prebuild); |
| 113 | + |
| 114 | + console.error('downloading libmongocrypt...', downloadURL); |
| 115 | + const destination = resolveRoot(`_libmongocrypt-${ref}`); |
| 116 | + |
| 117 | + await fs.rm(destination, { recursive: true, force: true }); |
| 118 | + await fs.mkdir(destination); |
| 119 | + |
| 120 | + const downloadDestination = 'nocrypto'; |
| 121 | + const unzipArgs = ['-xzv', '-C', `_libmongocrypt-${ref}`, downloadDestination]; |
| 122 | + console.error(`+ tar ${unzipArgs.join(' ')}`); |
| 123 | + const unzip = child_process.spawn('tar', unzipArgs, { |
| 124 | + stdio: ['pipe', 'inherit', 'pipe'], |
| 125 | + cwd: resolveRoot('.') |
| 126 | + }); |
| 127 | + if (unzip.stdin == null) throw new Error('Tar process must have piped stdin'); |
| 128 | + |
| 129 | + const [response] = await events.once(https.get(downloadURL), 'response'); |
| 130 | + const start = performance.now(); |
| 131 | + |
| 132 | + try { |
| 133 | + await stream.pipeline(response, unzip.stdin); |
| 134 | + } catch { |
| 135 | + await fs.access(path.join(`_libmongocrypt-${ref}`, downloadDestination)); |
| 136 | + } |
| 137 | + |
| 138 | + const end = performance.now(); |
| 139 | + console.error(`downloaded libmongocrypt in ${(end - start) / 1000} secs...`); |
| 140 | + |
| 141 | + await fs.rm(nodeDepsRoot, { recursive: true, force: true }); |
| 142 | + await fs.cp(resolveRoot(destination, 'nocrypto'), nodeDepsRoot, { recursive: true }); |
| 143 | + const potentialLib64Path = path.join(nodeDepsRoot, 'lib64'); |
| 144 | + try { |
| 145 | + await fs.rename(potentialLib64Path, path.join(nodeDepsRoot, 'lib')); |
| 146 | + } catch { |
| 147 | + await fs.access(path.join(nodeDepsRoot, 'lib')); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +async function buildBindings(args, pkg) { |
| 152 | + await fs.rm(resolveRoot('build'), { force: true, recursive: true }); |
| 153 | + await fs.rm(resolveRoot('prebuilds'), { force: true, recursive: true }); |
| 154 | + |
| 155 | + await run('npm', ['install', '--ignore-scripts']); |
| 156 | + |
| 157 | + let gypDefines = process.env.GYP_DEFINES ?? ''; |
| 158 | + if (args.dynamic) { |
| 159 | + gypDefines += ' libmongocrypt_link_type=dynamic'; |
| 160 | + } |
| 161 | + |
| 162 | + gypDefines = gypDefines.trim(); |
| 163 | + const prebuildOptions = |
| 164 | + gypDefines.length > 0 ? { env: { ...process.env, GYP_DEFINES: gypDefines } } : undefined; |
| 165 | + |
| 166 | + await run('npm', ['run', 'prebuild'], prebuildOptions); |
| 167 | + await run('npm', ['run', 'prepare']); |
| 168 | + |
| 169 | + if (process.platform === 'darwin' && process.arch === 'arm64') { |
| 170 | + const { |
| 171 | + binary: { |
| 172 | + napi_versions: [napiVersion] |
| 173 | + } |
| 174 | + } = JSON.parse(await readFile(resolveRoot('package.json'), 'utf-8')); |
| 175 | + const armTar = `mongodb-client-encryption-v${pkg.version}-napi-v${napiVersion}-darwin-arm64.tar.gz`; |
| 176 | + const x64Tar = `mongodb-client-encryption-v${pkg.version}-napi-v${napiVersion}-darwin-x64.tar.gz`; |
| 177 | + await fs.copyFile(resolveRoot('prebuilds', armTar), resolveRoot('prebuilds', x64Tar)); |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +async function main() { |
| 182 | + const { pkg, ...args } = await parseArguments(); |
| 183 | + console.log(args); |
| 184 | + |
| 185 | + const nodeDepsDir = resolveRoot('deps'); |
| 186 | + |
| 187 | + if (args.build && !args.dynamic) { |
| 188 | + const libmongocryptCloneDir = resolveRoot('_libmongocrypt'); |
| 189 | + |
| 190 | + const currentLibMongoCryptBranch = await fs |
| 191 | + .readFile(path.join(libmongocryptCloneDir, '.git', 'HEAD'), 'utf8') |
| 192 | + .catch(() => ''); |
| 193 | + const isClonedAndCheckedOut = currentLibMongoCryptBranch.trim().endsWith(`r-${args.ref}`); |
| 194 | + |
| 195 | + if (args.clean || !isClonedAndCheckedOut) { |
| 196 | + await cloneLibMongoCrypt(libmongocryptCloneDir, args); |
| 197 | + } |
| 198 | + |
| 199 | + const libmongocryptBuiltVersion = await fs |
| 200 | + .readFile(path.join(libmongocryptCloneDir, 'VERSION_CURRENT'), 'utf8') |
| 201 | + .catch(() => ''); |
| 202 | + const isBuilt = libmongocryptBuiltVersion.trim() === args.ref; |
| 203 | + |
| 204 | + if (args.clean || !isBuilt) { |
| 205 | + await buildLibMongoCrypt(libmongocryptCloneDir, nodeDepsDir, args); |
| 206 | + } |
| 207 | + } else if (!args.dynamic) { |
| 208 | + await downloadLibMongoCrypt(nodeDepsDir, args); |
| 209 | + } |
| 210 | + |
| 211 | + if (!args.skipBindings) { |
| 212 | + await buildBindings(args, pkg); |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +await main(); |
0 commit comments