Skip to content

Commit 7681940

Browse files
authored
chore(NODE-6177): set up tooling for new repo (#1)
1 parent 9f47b12 commit 7681940

File tree

11 files changed

+553
-182
lines changed

11 files changed

+553
-182
lines changed

.github/pull_request_template.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
### Description
2+
3+
#### What is changing?
4+
5+
##### Is there new documentation needed for these changes?
6+
7+
#### What is the motivation for this change?
8+
9+
<!-- If this is a bug, it helps to describe the current behavior and a clear outline of the expected behavior -->
10+
<!-- If this is a feature, it helps to describe the new use case enabled by this change -->
11+
12+
<!--
13+
Contributors!
14+
First of all, thank you so much!!
15+
If you haven't already, it would greatly help the team review this work in a timely manner if you create a JIRA ticket to track this PR.
16+
You can do that here: https://jira.mongodb.org/projects/NODE
17+
-->
18+
19+
### Release Highlight
20+
21+
<!-- RELEASE_HIGHLIGHT_START -->
22+
23+
### Fill in title or leave empty for no highlight
24+
25+
<!-- RELEASE_HIGHLIGHT_END -->
26+
27+
### Double check the following
28+
29+
- [ ] Ran `npm run check:lint` script
30+
- [ ] Self-review completed using the [steps outlined here](https://github.com/mongodb/node-mongodb-native/blob/HEAD/CONTRIBUTING.md#reviewer-guidelines)
31+
- [ ] PR title follows the [correct format](https://www.conventionalcommits.org/en/v1.0.0/): `type(NODE-xxxx)[!]: description`
32+
- Example: `feat(NODE-1234)!: rewriting everything in coffeescript`
33+
- [ ] Changes are covered by tests
34+
- [ ] New TODOs have a related JIRA ticket

.github/scripts/libmongocrypt.mjs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import util from 'node:util';
2+
import process from 'node:process';
3+
import fs from 'node:fs/promises';
4+
import child_process from 'node:child_process';
5+
import events from 'node:events';
6+
import path from 'node:path';
7+
8+
async function parseArguments() {
9+
const jsonImport = { [process.version.split('.').at(0) === 'v16' ? 'assert' : 'with']: { type: 'json' } };
10+
const pkg = (await import('../../package.json', jsonImport)).default;
11+
const libmongocryptVersion = pkg['mongodb:libmongocrypt'];
12+
13+
const options = {
14+
url: { short: 'u', type: 'string', default: 'https://github.com/mongodb/libmongocrypt.git' },
15+
libversion: { short: 'l', type: 'string', default: libmongocryptVersion },
16+
clean: { short: 'c', type: 'boolean' },
17+
help: { short: 'h', type: 'boolean' }
18+
};
19+
20+
const args = util.parseArgs({ args: process.argv.slice(2), options, allowPositionals: false });
21+
22+
if (args.values.help) {
23+
console.log(
24+
`${process.argv[1]} ${[...Object.keys(options)]
25+
.filter(k => k !== 'help')
26+
.map(k => `[--${k}=${options[k].type}]`)
27+
.join(' ')}`
28+
);
29+
process.exit(0);
30+
}
31+
32+
return {
33+
libmongocrypt: { url: args.values.url, ref: args.values.libversion },
34+
clean: args.values.clean
35+
};
36+
}
37+
38+
/** `xtrace` style command runner, uses spawn so that stdio is inherited */
39+
async function run(command, args = [], options = {}) {
40+
console.error(`+ ${command} ${args.join(' ')}`, options.cwd ? `(in: ${options.cwd})` : '');
41+
await events.once(child_process.spawn(command, args, { stdio: 'inherit', ...options }), 'exit');
42+
}
43+
44+
/** CLI flag maker: `toFlags({a: 1, b: 2})` yields `['-a=1', '-b=2']` */
45+
function toFlags(object) {
46+
return Array.from(Object.entries(object)).map(([k, v]) => `-${k}=${v}`);
47+
}
48+
49+
const args = await parseArguments();
50+
const libmongocryptRoot = path.resolve('_libmongocrypt');
51+
52+
const currentLibMongoCryptBranch = await fs.readFile(path.join(libmongocryptRoot, '.git', 'HEAD'), 'utf8').catch(() => '')
53+
const libmongocryptAlreadyClonedAndCheckedOut = currentLibMongoCryptBranch.trim().endsWith(`r-${args.libmongocrypt.ref}`);
54+
55+
if (args.clean || !libmongocryptAlreadyClonedAndCheckedOut) {
56+
console.error('fetching libmongocrypt...', args.libmongocrypt);
57+
await fs.rm(libmongocryptRoot, { recursive: true, force: true });
58+
await run('git', ['clone', args.libmongocrypt.url, libmongocryptRoot]);
59+
await run('git', ['fetch', '--tags'], { cwd: libmongocryptRoot });
60+
await run('git', ['checkout', args.libmongocrypt.ref, '-b', `r-${args.libmongocrypt.ref}`], { cwd: libmongocryptRoot });
61+
} else {
62+
console.error('libmongocrypt already up to date...', args.libmongocrypt);
63+
}
64+
65+
const libmongocryptBuiltVersion = await fs.readFile(path.join(libmongocryptRoot, 'VERSION_CURRENT'), 'utf8').catch(() => '');
66+
const libmongocryptAlreadyBuilt = libmongocryptBuiltVersion.trim() === args.libmongocrypt.ref;
67+
68+
if (args.clean || !libmongocryptAlreadyBuilt) {
69+
console.error('building libmongocrypt...\n', args);
70+
71+
const nodeDepsRoot = path.resolve('deps');
72+
const nodeBuildRoot = path.resolve(nodeDepsRoot, 'tmp', 'libmongocrypt-build');
73+
74+
await fs.rm(nodeBuildRoot, { recursive: true, force: true });
75+
await fs.mkdir(nodeBuildRoot, { recursive: true });
76+
77+
const CMAKE_FLAGS = toFlags({
78+
/**
79+
* We provide crypto hooks from Node.js binding to openssl (so disable system crypto)
80+
* TODO: NODE-5455
81+
*
82+
* One thing that is not obvious from the build instructions for libmongocrypt
83+
* and the Node.js bindings is that the Node.js driver uses libmongocrypt in
84+
* DISABLE_NATIVE_CRYPTO aka nocrypto mode, that is, instead of using native
85+
* system libraries for crypto operations, it provides callbacks to libmongocrypt
86+
* which, in the Node.js addon case, call JS functions that in turn call built-in
87+
* Node.js crypto methods.
88+
*
89+
* That’s way more convoluted than it needs to be, considering that we always
90+
* have a copy of OpenSSL available directly, but for now it seems to make sense
91+
* to stick with what the Node.js addon does here.
92+
*/
93+
DDISABLE_NATIVE_CRYPTO: '1',
94+
/** A consistent name for the output "library" directory */
95+
DCMAKE_INSTALL_LIBDIR: 'lib',
96+
/** No warnings allowed */
97+
DENABLE_MORE_WARNINGS_AS_ERRORS: 'ON',
98+
/** Where to build libmongocrypt */
99+
DCMAKE_PREFIX_PATH: nodeDepsRoot,
100+
/**
101+
* Where to install libmongocrypt
102+
* Note that `binding.gyp` will set `./deps/include`
103+
* as an include path if BUILD_TYPE=static
104+
*/
105+
DCMAKE_INSTALL_PREFIX: nodeDepsRoot
106+
});
107+
108+
const WINDOWS_CMAKE_FLAGS =
109+
process.platform === 'win32' // Windows is still called "win32" when it is 64-bit
110+
? toFlags({ Thost: 'x64', A: 'x64', DENABLE_WINDOWS_STATIC_RUNTIME: 'ON' })
111+
: [];
112+
113+
const MACOS_CMAKE_FLAGS =
114+
process.platform === 'darwin' // The minimum macos target version we want for
115+
? toFlags({ DCMAKE_OSX_DEPLOYMENT_TARGET: '10.12' })
116+
: [];
117+
118+
await run('cmake', [...CMAKE_FLAGS, ...WINDOWS_CMAKE_FLAGS, ...MACOS_CMAKE_FLAGS, libmongocryptRoot], { cwd: nodeBuildRoot });
119+
await run('cmake', ['--build', '.', '--target', 'install', '--config', 'RelWithDebInfo'], { cwd: nodeBuildRoot });
120+
} else {
121+
console.error('libmongocrypt already built...');
122+
}
123+
124+
await run('npm', ['install', '--ignore-scripts']);
125+
await run('npm', ['run', 'rebuild'], { env: { ...process.env, BUILD_TYPE: 'static' } });

.github/workflows/build.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
on:
2+
push:
3+
branches: [main]
4+
workflow_dispatch: {}
5+
6+
name: build
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
strategy:
12+
matrix:
13+
node: ['20.x'] # '16.x', '18.x',
14+
name: Node.js ${{ matrix.node }} build
15+
steps:
16+
- uses: actions/setup-node@v4
17+
with:
18+
node-version: ${{ matrix.node }}
19+
cache: 'npm'
20+
registry-url: 'https://registry.npmjs.org'
21+
- run: npm install -g npm@latest
22+
shell: bash
23+
- run: node .github/scripts/libmongocrypt.mjs
24+
shell: bash

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ xunit.xml
2424
# built typescript
2525
lib
2626
prebuilds
27+
28+
_libmongocrypt/

CHANGELOG.md

Lines changed: 0 additions & 119 deletions
This file was deleted.

0 commit comments

Comments
 (0)