Skip to content

Commit d76ff5c

Browse files
committed
chore: align build steps with mongodb-client-encryption
1 parent 65573c7 commit d76ff5c

File tree

7 files changed

+290
-1830
lines changed

7 files changed

+290
-1830
lines changed

.github/docker/Dockerfile.glibc

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ COPY . .
1111

1212
RUN apt-get -qq update && apt-get -qq install -y python3 build-essential libkrb5-dev && ldd --version
1313

14-
RUN npm clean-install --ignore-scripts
15-
RUN npm run prebuild
14+
RUN node .github/scripts/build.mjs
1615

1716
FROM scratch
1817

19-
COPY --from=build /kerberos/prebuilds/ /
18+
COPY --from=build /kerberos/prebuilds/ /

.github/scripts/build.mjs

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// @ts-check
2+
3+
import util from 'node:util';
4+
import process from 'node:process';
5+
import fs 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 url from 'node:url';
10+
11+
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
12+
13+
/** Resolves to the root of this repository */
14+
function resolveRoot(...paths) {
15+
return path.resolve(__dirname, '..', '..', ...paths);
16+
}
17+
18+
async function parseArguments() {
19+
const pkg = JSON.parse(await fs.readFile(resolveRoot('package.json'), 'utf8'));
20+
21+
const options = {
22+
'kerberos_use_rtld': { type: 'boolean', default: false },
23+
help: { short: 'h', type: 'boolean', default: false }
24+
};
25+
26+
const args = util.parseArgs({ args: process.argv.slice(2), options, allowPositionals: false });
27+
28+
if (args.values.help) {
29+
console.log(
30+
`${path.basename(process.argv[1])} ${[...Object.keys(options)]
31+
.filter(k => k !== 'help')
32+
.map(k => `[--${k}=${options[k].type}]`)
33+
.join(' ')}`
34+
);
35+
process.exit(0);
36+
}
37+
38+
return {
39+
kerberos_use_rtld: !!args.values.kerberos_use_rtld,
40+
pkg
41+
};
42+
}
43+
44+
/** `xtrace` style command runner, uses spawn so that stdio is inherited */
45+
async function run(command, args = [], options = {}) {
46+
const commandDetails = `+ ${command} ${args.join(' ')}${options.cwd ? ` (in: ${options.cwd})` : ''}`;
47+
console.error(commandDetails);
48+
const proc = child_process.spawn(command, args, {
49+
shell: process.platform === 'win32',
50+
stdio: 'inherit',
51+
cwd: resolveRoot('.'),
52+
...options
53+
});
54+
await events.once(proc, 'exit');
55+
56+
if (proc.exitCode != 0) throw new Error(`CRASH(${proc.exitCode}): ${commandDetails}`);
57+
}
58+
59+
async function buildBindings(args, pkg) {
60+
await fs.rm(resolveRoot('build'), { force: true, recursive: true });
61+
await fs.rm(resolveRoot('prebuilds'), { force: true, recursive: true });
62+
63+
// install with "ignore-scripts" so that we don't attempt to download a prebuild
64+
await run('npm', ['install', '--ignore-scripts']);
65+
// The prebuild command will make both a .node file in `./build` (local and CI testing will run on current code)
66+
// it will also produce `./prebuilds/kerberos-vVERSION-napi-vNAPI_VERSION-OS-ARCH.tar.gz`.
67+
68+
let gypDefines = process.env.GYP_DEFINES ?? '';
69+
if (args.kerberos_use_rtld) {
70+
gypDefines += ' kerberos_use_rtld=true';
71+
}
72+
73+
gypDefines = gypDefines.trim();
74+
const prebuildOptions =
75+
gypDefines.length > 0
76+
? { env: { ...process.env, GYP_DEFINES: gypDefines } }
77+
: undefined;
78+
79+
await run('npm', ['run', 'prebuild'], prebuildOptions);
80+
81+
await run('npm', ['run', 'prepare']);
82+
83+
if (process.platform === 'darwin' && process.arch === 'arm64') {
84+
// The "arm64" build is actually a universal binary
85+
const armTar = `kerberos-v${pkg.version}-napi-v4-darwin-arm64.tar.gz`;
86+
const x64Tar = `kerberos-v${pkg.version}-napi-v4-darwin-x64.tar.gz`;
87+
await fs.copyFile(resolveRoot('prebuilds', armTar), resolveRoot('prebuilds', x64Tar));
88+
}
89+
}
90+
91+
async function main() {
92+
const { pkg, ...args } = await parseArguments();
93+
console.log(args);
94+
await buildBindings(args, pkg);
95+
}
96+
97+
await main();

.github/workflows/build.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ jobs:
2121
- uses: actions/checkout@v4
2222

2323
- name: Build ${{ matrix.os }} Prebuild
24-
run: |
25-
npm clean-install --ignore-scripts
26-
npm run prebuild
24+
run: node .github/scripts/build.mjs
2725

2826
- id: upload
2927
name: Upload prebuild
@@ -64,4 +62,4 @@ jobs:
6462
path: prebuilds/
6563
if-no-files-found: 'error'
6664
retention-days: 1
67-
compression-level: 0
65+
compression-level: 0

binding.gyp

+17-14
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,21 @@
33
{
44
'target_name': 'kerberos',
55
'type': 'loadable_module',
6-
'include_dirs': [ "<!(node -p \"require('node-addon-api').include_dir\")" ],
6+
'include_dirs': [
7+
"<!(node -p \"require('node-addon-api').include_dir\")"
8+
],
79
'sources': [
810
'src/kerberos.cc'
911
],
1012
'variables': {
13+
'ARCH': '<(host_arch)',
1114
'kerberos_use_rtld%': 'false'
1215
},
1316
'xcode_settings': {
1417
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
1518
'CLANG_CXX_LIBRARY': 'libc++',
1619
'MACOSX_DEPLOYMENT_TARGET': '10.12',
17-
"OTHER_CFLAGS": [
18-
"-arch x86_64",
19-
"-arch arm64"
20-
],
21-
"OTHER_LDFLAGS": [
22-
"-arch x86_64",
23-
"-arch arm64"
24-
]
20+
'GCC_SYMBOLS_PRIVATE_EXTERN': 'YES', # -fvisibility=hidden
2521
},
2622
'cflags!': [ '-fno-exceptions' ],
2723
'cflags_cc!': [ '-fno-exceptions' ],
@@ -36,11 +32,18 @@
3632
},
3733
},
3834
'conditions': [
39-
['OS=="mac"', {
40-
'cflags+': ['-fvisibility=hidden'],
41-
'xcode_settings': {
42-
'GCC_SYMBOLS_PRIVATE_EXTERN': 'YES', # -fvisibility=hidden
43-
}
35+
['OS=="mac"', { 'cflags+': ['-fvisibility=hidden'] }],
36+
['_type!="static_library" and ARCH=="arm64"', {
37+
'xcode_settings': {
38+
"OTHER_CFLAGS": [
39+
"-arch x86_64",
40+
"-arch arm64"
41+
],
42+
"OTHER_LDFLAGS": [
43+
"-arch x86_64",
44+
"-arch arm64"
45+
]
46+
}
4447
}],
4548
['OS=="mac" or OS=="linux"', {
4649
'sources': [

0 commit comments

Comments
 (0)