koffi 2.16.1 → 3.0.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -2
- package/README.md +8 -8
- package/build/koffi/darwin_arm64/koffi.node +0 -0
- package/build/koffi/darwin_x64/koffi.node +0 -0
- package/build/koffi/freebsd_arm64/koffi.node +0 -0
- package/build/koffi/freebsd_ia32/koffi.node +0 -0
- package/build/koffi/freebsd_x64/koffi.node +0 -0
- package/build/koffi/linux_arm64/koffi.node +0 -0
- package/build/koffi/linux_ia32/koffi.node +0 -0
- package/build/koffi/linux_x64/koffi.node +0 -0
- package/build/koffi/musl_arm64/koffi.node +0 -0
- package/build/koffi/musl_x64/koffi.node +0 -0
- package/build/koffi/openbsd_ia32/koffi.node +0 -0
- package/build/koffi/openbsd_x64/koffi.node +0 -0
- package/build/koffi/win32_ia32/koffi.node +0 -0
- package/build/koffi/win32_x64/koffi.node +0 -0
- package/doc/benchmarks.md +33 -33
- package/doc/benchmarks.xlsx +0 -0
- package/doc/contribute.md +37 -25
- package/doc/index.md +8 -8
- package/doc/variables.md +1 -1
- package/index.d.ts +11 -9
- package/index.js +1 -634
- package/indirect.js +1 -533
- package/lib/native/base/base.cc +63 -85
- package/lib/native/base/base.hh +178 -151
- package/package.json +3 -4
- package/src/build.js +1178 -0
- package/src/koffi/CMakeLists.txt +97 -34
- package/src/koffi/index.js +359 -0
- package/src/koffi/indirect.js +216 -0
- package/src/koffi/src/{abi_arm32.cc → abi/arm32.cc} +279 -275
- package/src/koffi/src/{abi_arm32_asm.S → abi/arm32_asm.S} +2 -5
- package/src/koffi/src/abi/arm64.cc +1511 -0
- package/src/koffi/src/{abi_arm64_asm.S → abi/arm64_asm.S} +44 -23
- package/src/koffi/src/{abi_arm64_asm.asm → abi/arm64_asm.asm} +32 -25
- package/src/koffi/src/{abi_loong64_asm.S → abi/loong64_asm.S} +1 -5
- package/src/koffi/src/{abi_riscv64.cc → abi/riscv64.cc} +279 -273
- package/src/koffi/src/{abi_riscv64_asm.S → abi/riscv64_asm.S} +1 -4
- package/src/koffi/src/abi/x64sysv.cc +1456 -0
- package/src/koffi/src/{abi_x64_sysv_asm.S → abi/x64sysv_asm.S} +30 -33
- package/src/koffi/src/abi/x64win.cc +1121 -0
- package/src/koffi/src/{abi_x64_win_asm.S → abi/x64win_asm.S} +8 -12
- package/src/koffi/src/{abi_x64_win_asm.asm → abi/x64win_asm.asm} +11 -14
- package/src/koffi/src/abi/x86.cc +1233 -0
- package/src/koffi/src/{abi_x86_asm.S → abi/x86_asm.S} +49 -25
- package/src/koffi/src/{abi_x86_asm.asm → abi/x86_asm.asm} +11 -14
- package/src/koffi/src/call.cc +458 -598
- package/src/koffi/src/call.hh +61 -82
- package/src/koffi/src/ffi.cc +595 -309
- package/src/koffi/src/ffi.hh +103 -89
- package/src/koffi/src/parser.cc +0 -4
- package/src/koffi/src/primitives.inc +39 -0
- package/src/koffi/{tools/write_trampolines.js → src/trampolines.js} +1 -1
- package/src/koffi/src/util.cc +503 -446
- package/src/koffi/src/util.hh +269 -57
- package/src/koffi/src/uv.cc +2 -2
- package/src/koffi/src/uv.def +3 -0
- package/src/koffi/src/win32.hh +30 -1
- package/vendor/node-addon-api/napi-inl.h +4 -0
- package/vendor/node-addon-api/napi.h +1 -0
- package/build/koffi/linux_armhf/koffi.node +0 -0
- package/build/koffi/linux_loong64/koffi.node +0 -0
- package/build/koffi/linux_riscv64d/koffi.node +0 -0
- package/build/koffi/win32_arm64/koffi.exp +0 -0
- package/build/koffi/win32_arm64/koffi.lib +0 -0
- package/build/koffi/win32_arm64/koffi.node +0 -0
- package/doc/platforms.md +0 -36
- package/src/cnoke/LICENSE.txt +0 -22
- package/src/cnoke/README.md +0 -99
- package/src/cnoke/assets/FindCNoke.cmake +0 -127
- package/src/cnoke/assets/toolchains.json +0 -126
- package/src/cnoke/assets/win_delay_hook.c +0 -36
- package/src/cnoke/cnoke.js +0 -170
- package/src/cnoke/package.json +0 -24
- package/src/cnoke/src/builder.js +0 -511
- package/src/cnoke/src/index.js +0 -10
- package/src/cnoke/src/tools.js +0 -407
- package/src/koffi/src/abi_arm64.cc +0 -1295
- package/src/koffi/src/abi_x64_sysv.cc +0 -939
- package/src/koffi/src/abi_x64_win.cc +0 -715
- package/src/koffi/src/abi_x86.cc +0 -860
- package/src/koffi/src/init.js +0 -105
- /package/src/koffi/src/{abi_loong64.cc → abi/loong64.cc} +0 -0
package/src/cnoke/src/builder.js
DELETED
|
@@ -1,511 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
// SPDX-FileCopyrightText: 2025 Niels Martignène <niels.martignene@protonmail.com>
|
|
3
|
-
|
|
4
|
-
'use strict';
|
|
5
|
-
|
|
6
|
-
const fs = require('fs');
|
|
7
|
-
const os = require('os');
|
|
8
|
-
const path = require('path');
|
|
9
|
-
const { spawnSync } = require('child_process');
|
|
10
|
-
const tools = require('./tools.js');
|
|
11
|
-
const TOOLCHAINS = require('../assets/toolchains.json');
|
|
12
|
-
|
|
13
|
-
const DefaultOptions = {
|
|
14
|
-
mode: 'RelWithDebInfo'
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
function Builder(config = {}) {
|
|
18
|
-
let self = this;
|
|
19
|
-
|
|
20
|
-
let host = `${process.platform}_${tools.determine_arch()}`;
|
|
21
|
-
|
|
22
|
-
let app_dir = config.app_dir;
|
|
23
|
-
let project_dir = config.project_dir;
|
|
24
|
-
let package_dir = config.package_dir;
|
|
25
|
-
|
|
26
|
-
if (app_dir == null)
|
|
27
|
-
app_dir = __dirname.replace(/\\/g, '/') + '/..';
|
|
28
|
-
if (project_dir == null)
|
|
29
|
-
project_dir = process.cwd();
|
|
30
|
-
app_dir = app_dir.replace(/\\/g, '/');
|
|
31
|
-
project_dir = project_dir.replace(/\\/g, '/');
|
|
32
|
-
if (package_dir == null)
|
|
33
|
-
package_dir = find_parent_directory(project_dir, 'package.json');
|
|
34
|
-
if (package_dir != null)
|
|
35
|
-
package_dir = package_dir.replace(/\\/g, '/');
|
|
36
|
-
|
|
37
|
-
let runtime_version = config.runtime_version;
|
|
38
|
-
let toolchain = config.toolchain || null;
|
|
39
|
-
let prefer_clang = config.prefer_clang || false;
|
|
40
|
-
let mode = config.mode || DefaultOptions.mode;
|
|
41
|
-
let targets = config.targets || [];
|
|
42
|
-
let verbose = config.verbose || false;
|
|
43
|
-
let prebuild = config.prebuild || false;
|
|
44
|
-
|
|
45
|
-
if (runtime_version == null)
|
|
46
|
-
runtime_version = process.version;
|
|
47
|
-
if (runtime_version.startsWith('v'))
|
|
48
|
-
runtime_version = runtime_version.substr(1);
|
|
49
|
-
|
|
50
|
-
if (toolchain != null && !Object.hasOwn(TOOLCHAINS, toolchain))
|
|
51
|
-
throw new Error(`Unknown cross-compilation toolchain '${toolchain}'`);
|
|
52
|
-
|
|
53
|
-
let options = null;
|
|
54
|
-
|
|
55
|
-
let cache_dir = get_cache_directory();
|
|
56
|
-
let build_dir = config.build_dir;
|
|
57
|
-
let work_dir = null;
|
|
58
|
-
let output_dir = null;
|
|
59
|
-
|
|
60
|
-
if (build_dir == null) {
|
|
61
|
-
let options = read_cnoke_options();
|
|
62
|
-
|
|
63
|
-
if (options.output != null) {
|
|
64
|
-
build_dir = expand_path(options.output, options.directory);
|
|
65
|
-
} else {
|
|
66
|
-
build_dir = project_dir + '/build';
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
build_dir = build_dir.replace(/\\/g, '/');
|
|
70
|
-
work_dir = build_dir + `/v${runtime_version}_${toolchain ?? 'native'}/${mode}`;
|
|
71
|
-
output_dir = work_dir + '/Output';
|
|
72
|
-
|
|
73
|
-
let cmake_bin = null;
|
|
74
|
-
|
|
75
|
-
this.configure = async function(retry = true) {
|
|
76
|
-
let options = read_cnoke_options();
|
|
77
|
-
|
|
78
|
-
let args = [project_dir];
|
|
79
|
-
|
|
80
|
-
check_cmake();
|
|
81
|
-
check_compatibility();
|
|
82
|
-
|
|
83
|
-
console.log(`>> Node: ${runtime_version}`);
|
|
84
|
-
console.log(`>> Toolchain: ${toolchain ?? 'native'}`);
|
|
85
|
-
|
|
86
|
-
// Prepare build directory
|
|
87
|
-
fs.mkdirSync(build_dir, { recursive: true, mode: 0o755 });
|
|
88
|
-
fs.mkdirSync(work_dir, { recursive: true, mode: 0o755 });
|
|
89
|
-
fs.mkdirSync(output_dir, { recursive: true, mode: 0o755 });
|
|
90
|
-
|
|
91
|
-
retry &= fs.existsSync(work_dir + '/CMakeCache.txt');
|
|
92
|
-
|
|
93
|
-
args.push(`-DNODE_JS_EXECPATH=${process.execPath}`);
|
|
94
|
-
|
|
95
|
-
// Download or use Node headers
|
|
96
|
-
if (options.api == null) {
|
|
97
|
-
let destname = `${cache_dir}/node-v${runtime_version}-headers.tar.gz`;
|
|
98
|
-
|
|
99
|
-
if (!fs.existsSync(destname)) {
|
|
100
|
-
fs.mkdirSync(cache_dir, { recursive: true, mode: 0o755 });
|
|
101
|
-
|
|
102
|
-
let url = `https://nodejs.org/dist/v${runtime_version}/node-v${runtime_version}-headers.tar.gz`;
|
|
103
|
-
await tools.download_http(url, destname);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
await tools.extract_targz(destname, work_dir + '/headers', 1);
|
|
107
|
-
|
|
108
|
-
args.push(`-DNODE_JS_INCLUDE_DIRS=${work_dir}/headers/include/node`);
|
|
109
|
-
} else {
|
|
110
|
-
console.log(`>> Using local node-api headers`);
|
|
111
|
-
|
|
112
|
-
let api_dir = expand_path(options.api, project_dir);
|
|
113
|
-
args.push(`-DNODE_JS_INCLUDE_DIRS=${api_dir}/include`);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
args.push(`-DCMAKE_MODULE_PATH=${app_dir}/assets`);
|
|
117
|
-
|
|
118
|
-
let win32 = (toolchain ?? host).startsWith('win32_');
|
|
119
|
-
let mingw = (process.platform == 'win32' && process.env.MSYSTEM != null);
|
|
120
|
-
|
|
121
|
-
// Handle Node import library on Windows
|
|
122
|
-
if (win32) {
|
|
123
|
-
if (mingw) {
|
|
124
|
-
args.push(`-DNODE_JS_LINK_LIB=node.dll`);
|
|
125
|
-
} else {
|
|
126
|
-
let info = TOOLCHAINS[toolchain ?? host];
|
|
127
|
-
|
|
128
|
-
if (options.api == null) {
|
|
129
|
-
let destname = `${cache_dir}/node_v${runtime_version}_${toolchain ?? host}.lib`;
|
|
130
|
-
|
|
131
|
-
if (!fs.existsSync(destname)) {
|
|
132
|
-
fs.mkdirSync(cache_dir, { recursive: true, mode: 0o755 });
|
|
133
|
-
|
|
134
|
-
let url = `https://nodejs.org/dist/v${runtime_version}/${info.lib}/node.lib`;
|
|
135
|
-
await tools.download_http(url, destname);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
fs.copyFileSync(destname, work_dir + '/node.lib');
|
|
139
|
-
args.push(`-DNODE_JS_LINK_LIB=${work_dir}/node.lib`);
|
|
140
|
-
} else {
|
|
141
|
-
let api_dir = expand_path(options.api, project_dir);
|
|
142
|
-
args.push(`-DNODE_JS_LINK_DEF=${api_dir}/def/node_api.def`);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
fs.copyFileSync(`${app_dir}/assets/win_delay_hook.c`, work_dir + '/win_delay_hook.c');
|
|
147
|
-
args.push(`-DNODE_JS_SOURCES=${work_dir}/win_delay_hook.c`);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
if (process.platform != 'win32' || mingw) {
|
|
151
|
-
if (spawnSync('ninja', ['--version']).status === 0) {
|
|
152
|
-
args.push('-G', 'Ninja');
|
|
153
|
-
} else if (process.platform == 'win32') {
|
|
154
|
-
args.push('-G', 'MinGW Makefiles');
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// Use CCache if available
|
|
158
|
-
if (spawnSync('ccache', ['--version']).status === 0) {
|
|
159
|
-
args.push('-DCMAKE_C_COMPILER_LAUNCHER=ccache');
|
|
160
|
-
args.push('-DCMAKE_CXX_COMPILER_LAUNCHER=ccache');
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// Handle toolchain flags and cross-compilation
|
|
165
|
-
{
|
|
166
|
-
let info = TOOLCHAINS[toolchain ?? host];
|
|
167
|
-
|
|
168
|
-
if (info != null) {
|
|
169
|
-
if (Object.hasOwn(info, process.platform))
|
|
170
|
-
info = Object.assign({}, info, info[process.platform]);
|
|
171
|
-
|
|
172
|
-
if (info.flags != null)
|
|
173
|
-
args.push(...info.flags);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if (toolchain == null && process.arch == 'ia32') {
|
|
177
|
-
let proc = spawnSync('uname', ['-m']);
|
|
178
|
-
let machine = (proc.stdout ?? '').toString('utf-8').trim();
|
|
179
|
-
|
|
180
|
-
if (machine == 'x86_64') {
|
|
181
|
-
// Compiler probably does not default to 32-bit... just force it
|
|
182
|
-
args.push('-DCMAKE_ASM_FLAGS=-m32', '-DCMAKE_C_FLAGS=-m32', '-DCMAKE_CXX_FLAGS=-m32');
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
if (toolchain != null && info.triplet != null) {
|
|
187
|
-
let values = [
|
|
188
|
-
['CMAKE_SYSTEM_NAME', info.system],
|
|
189
|
-
['CMAKE_SYSTEM_PROCESSOR', info.processor]
|
|
190
|
-
];
|
|
191
|
-
let sysroot = null;
|
|
192
|
-
|
|
193
|
-
// Switch to Clang automatically if the GCC cross-compiler does not exist
|
|
194
|
-
if (process.platform != 'win32' && !prefer_clang) {
|
|
195
|
-
let binary = info.triplet + '-gcc';
|
|
196
|
-
|
|
197
|
-
if (spawnSync(binary, ['-v']).status !== 0)
|
|
198
|
-
prefer_clang = true;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
if (prefer_clang) {
|
|
202
|
-
values.push(['CMAKE_ASM_COMPILER_TARGET', info.triplet]);
|
|
203
|
-
values.push(['CMAKE_C_COMPILER_TARGET', info.triplet]);
|
|
204
|
-
values.push(['CMAKE_CXX_COMPILER_TARGET', info.triplet]);
|
|
205
|
-
|
|
206
|
-
values.push(['CMAKE_EXE_LINKER_FLAGS', '-fuse-ld=lld']);
|
|
207
|
-
values.push(['CMAKE_SHARED_LINKER_FLAGS', '-fuse-ld=lld']);
|
|
208
|
-
values.push(['CMAKE_STATIC_LINKER_FLAGS', '-fuse-ld=lld']);
|
|
209
|
-
} else if (process.platform != 'win32') {
|
|
210
|
-
values.push(['CMAKE_ASM_COMPILER', info.triplet + '-gcc']);
|
|
211
|
-
values.push(['CMAKE_C_COMPILER', info.triplet + '-gcc']);
|
|
212
|
-
values.push(['CMAKE_CXX_COMPILER', info.triplet + '-g++']);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
if (info.sysroot != null) {
|
|
216
|
-
sysroot = expand_path(info.sysroot, __dirname + '/..');
|
|
217
|
-
if (!fs.existsSync(sysroot))
|
|
218
|
-
throw new Error(`Cross-compilation sysroot '${sysroot}' does not exist`);
|
|
219
|
-
|
|
220
|
-
values.push(['CMAKE_SYSROOT', sysroot]);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
if (info.variables != null) {
|
|
224
|
-
for (let key in info.variables) {
|
|
225
|
-
let value = expand_string(info.variables[key], { sysroot: sysroot });
|
|
226
|
-
values.push([key, value]);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
let filename = `${work_dir}/toolchain.cmake`;
|
|
231
|
-
let text = values.map(pair => `set(${pair[0]} "${pair[1]}")`).join('\n');
|
|
232
|
-
|
|
233
|
-
fs.writeFileSync(filename, text);
|
|
234
|
-
args.push(`-DCMAKE_TOOLCHAIN_FILE=${filename}`);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
if (prefer_clang) {
|
|
239
|
-
if (process.platform == 'win32' && !mingw) {
|
|
240
|
-
args.push('-T', 'ClangCL');
|
|
241
|
-
} else {
|
|
242
|
-
args.push('-DCMAKE_C_COMPILER=clang');
|
|
243
|
-
args.push('-DCMAKE_CXX_COMPILER=clang++');
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
args.push(`-DCMAKE_BUILD_TYPE=${mode}`);
|
|
248
|
-
for (let type of ['ARCHIVE', 'RUNTIME', 'LIBRARY']) {
|
|
249
|
-
for (let suffix of ['', '_DEBUG', '_RELEASE', '_RELWITHDEBINFO'])
|
|
250
|
-
args.push(`-DCMAKE_${type}_OUTPUT_DIRECTORY${suffix}=${output_dir}`);
|
|
251
|
-
}
|
|
252
|
-
args.push('--no-warn-unused-cli');
|
|
253
|
-
|
|
254
|
-
console.log('>> Running configuration');
|
|
255
|
-
|
|
256
|
-
let proc = spawnSync(cmake_bin, args, { cwd: work_dir, stdio: 'inherit' });
|
|
257
|
-
if (proc.status !== 0) {
|
|
258
|
-
tools.unlink_recursive(work_dir);
|
|
259
|
-
if (retry)
|
|
260
|
-
return self.configure(false);
|
|
261
|
-
|
|
262
|
-
throw new Error('Failed to run configure step');
|
|
263
|
-
}
|
|
264
|
-
};
|
|
265
|
-
|
|
266
|
-
this.build = async function() {
|
|
267
|
-
check_compatibility();
|
|
268
|
-
|
|
269
|
-
if (prebuild) {
|
|
270
|
-
let valid = await check_prebuild();
|
|
271
|
-
if (valid)
|
|
272
|
-
return;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
check_cmake();
|
|
276
|
-
|
|
277
|
-
if (!fs.existsSync(work_dir + '/CMakeCache.txt'))
|
|
278
|
-
await self.configure();
|
|
279
|
-
|
|
280
|
-
// In case Make gets used
|
|
281
|
-
// os.cpus() returns [] on Android (no /proc/cpuinfo access), so fall back to 1
|
|
282
|
-
if (process.env.MAKEFLAGS == null)
|
|
283
|
-
process.env.MAKEFLAGS = '-j' + (os.cpus().length || 1);
|
|
284
|
-
|
|
285
|
-
let args = [
|
|
286
|
-
'--build', work_dir,
|
|
287
|
-
'--config', mode
|
|
288
|
-
];
|
|
289
|
-
|
|
290
|
-
if (verbose)
|
|
291
|
-
args.push('--verbose');
|
|
292
|
-
for (let target of targets)
|
|
293
|
-
args.push('--target', target);
|
|
294
|
-
|
|
295
|
-
console.log('>> Running build');
|
|
296
|
-
|
|
297
|
-
let proc = spawnSync(cmake_bin, args, { stdio: 'inherit' });
|
|
298
|
-
if (proc.status !== 0)
|
|
299
|
-
throw new Error('Failed to run build step');
|
|
300
|
-
|
|
301
|
-
console.log('>> Copy target files');
|
|
302
|
-
|
|
303
|
-
tools.sync_files(output_dir, build_dir);
|
|
304
|
-
};
|
|
305
|
-
|
|
306
|
-
async function check_prebuild() {
|
|
307
|
-
let options = read_cnoke_options();
|
|
308
|
-
|
|
309
|
-
if (options.prebuild != null) {
|
|
310
|
-
fs.mkdirSync(build_dir, { recursive: true, mode: 0o755 });
|
|
311
|
-
|
|
312
|
-
let archive_filename = expand_path(options.prebuild, options.directory);
|
|
313
|
-
|
|
314
|
-
if (fs.existsSync(archive_filename)) {
|
|
315
|
-
try {
|
|
316
|
-
console.log('>> Extracting prebuilt binaries...');
|
|
317
|
-
await tools.extract_targz(archive_filename, build_dir, 1);
|
|
318
|
-
} catch (err) {
|
|
319
|
-
console.error('Failed to find prebuilt binary for your platform, building manually');
|
|
320
|
-
}
|
|
321
|
-
} else {
|
|
322
|
-
console.error('Cannot find local prebuilt archive');
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
if (options.require != null) {
|
|
327
|
-
let require_filename = expand_path(options.require, options.directory);
|
|
328
|
-
|
|
329
|
-
if (fs.existsSync(require_filename)) {
|
|
330
|
-
let proc = spawnSync(process.execPath, ['-e', 'require(process.argv[1])', require_filename]);
|
|
331
|
-
if (proc.status === 0)
|
|
332
|
-
return true;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
console.error('Failed to load prebuilt binary, rebuilding from source');
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
return false;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
this.clean = function() {
|
|
342
|
-
tools.unlink_recursive(build_dir);
|
|
343
|
-
};
|
|
344
|
-
|
|
345
|
-
function find_parent_directory(dirname, basename) {
|
|
346
|
-
if (process.platform == 'win32')
|
|
347
|
-
dirname = dirname.replace(/\\/g, '/');
|
|
348
|
-
|
|
349
|
-
do {
|
|
350
|
-
if (fs.existsSync(dirname + '/' + basename))
|
|
351
|
-
return dirname;
|
|
352
|
-
|
|
353
|
-
dirname = path.dirname(dirname);
|
|
354
|
-
} while (!dirname.endsWith('/'));
|
|
355
|
-
|
|
356
|
-
return null;
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
function get_cache_directory() {
|
|
360
|
-
if (process.platform == 'win32') {
|
|
361
|
-
let cache_dir = process.env['LOCALAPPDATA'] || process.env['APPDATA'];
|
|
362
|
-
if (cache_dir == null)
|
|
363
|
-
throw new Error('Missing LOCALAPPDATA and APPDATA environment variable');
|
|
364
|
-
|
|
365
|
-
cache_dir = path.join(cache_dir, 'cnoke');
|
|
366
|
-
return cache_dir;
|
|
367
|
-
} else {
|
|
368
|
-
let cache_dir = process.env['XDG_CACHE_HOME'];
|
|
369
|
-
|
|
370
|
-
if (cache_dir == null) {
|
|
371
|
-
let home = process.env['HOME'];
|
|
372
|
-
if (home == null)
|
|
373
|
-
throw new Error('Missing HOME environment variable');
|
|
374
|
-
|
|
375
|
-
cache_dir = path.join(home, '.cache');
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
cache_dir = path.join(cache_dir, 'cnoke');
|
|
379
|
-
return cache_dir;
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
function check_cmake() {
|
|
384
|
-
if (cmake_bin != null)
|
|
385
|
-
return;
|
|
386
|
-
|
|
387
|
-
// Check for CMakeLists.txt
|
|
388
|
-
if (!fs.existsSync(project_dir + '/CMakeLists.txt'))
|
|
389
|
-
throw new Error('This directory does not appear to have a CMakeLists.txt file');
|
|
390
|
-
|
|
391
|
-
// Check for CMake
|
|
392
|
-
{
|
|
393
|
-
let proc = spawnSync('cmake', ['--version']);
|
|
394
|
-
|
|
395
|
-
if (proc.status === 0) {
|
|
396
|
-
cmake_bin = 'cmake';
|
|
397
|
-
} else {
|
|
398
|
-
if (process.platform == 'win32') {
|
|
399
|
-
// I really don't want to depend on anything in CNoke, and Node.js does not provide
|
|
400
|
-
// anything to read from the registry. This is okay, REG.exe exists since Windows XP.
|
|
401
|
-
let proc = spawnSync('reg', ['query', 'HKEY_LOCAL_MACHINE\\SOFTWARE\\Kitware\\CMake', '/v', 'InstallDir']);
|
|
402
|
-
|
|
403
|
-
if (proc.status === 0) {
|
|
404
|
-
let matches = proc.stdout.toString('utf-8').match(/InstallDir[ \t]+REG_[A-Z_]+[ \t]+(.*)+/);
|
|
405
|
-
|
|
406
|
-
if (matches != null) {
|
|
407
|
-
let bin = path.join(matches[1].trim(), 'bin\\cmake.exe');
|
|
408
|
-
|
|
409
|
-
if (fs.existsSync(bin))
|
|
410
|
-
cmake_bin = bin;
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
if (cmake_bin == null)
|
|
416
|
-
throw new Error('CMake does not seem to be available');
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
console.log(`>> Using CMake binary: ${cmake_bin}`);
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
function check_compatibility() {
|
|
424
|
-
let options = read_cnoke_options();
|
|
425
|
-
|
|
426
|
-
if (options.node != null && tools.cmp_version(runtime_version, options.node) < 0)
|
|
427
|
-
throw new Error(`Project ${options.name} requires Node.js >= ${options.node}`);
|
|
428
|
-
|
|
429
|
-
if (options.napi != null) {
|
|
430
|
-
let major = parseInt(runtime_version, 10);
|
|
431
|
-
let required = tools.get_napi_version(options.napi, major);
|
|
432
|
-
|
|
433
|
-
if (required == null)
|
|
434
|
-
throw new Error(`Project ${options.name} does not support the Node ${major}.x branch (old or missing N-API)`);
|
|
435
|
-
if (tools.cmp_version(runtime_version, required) < 0)
|
|
436
|
-
throw new Error(`Project ${options.name} requires Node >= ${required} in the Node ${major}.x branch (with N-API >= ${options.napi})`);
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
function read_cnoke_options() {
|
|
441
|
-
if (options != null)
|
|
442
|
-
return options;
|
|
443
|
-
|
|
444
|
-
let directory = project_dir;
|
|
445
|
-
let pkg = null;
|
|
446
|
-
let cnoke = null;
|
|
447
|
-
|
|
448
|
-
if (package_dir != null) {
|
|
449
|
-
try {
|
|
450
|
-
let json = fs.readFileSync(package_dir + '/package.json', { encoding: 'utf-8' });
|
|
451
|
-
|
|
452
|
-
pkg = JSON.parse(json);
|
|
453
|
-
directory = package_dir;
|
|
454
|
-
} catch (err) {
|
|
455
|
-
if (err.code != 'ENOENT')
|
|
456
|
-
throw err;
|
|
457
|
-
}
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
if (cnoke == null)
|
|
461
|
-
cnoke = pkg?.cnoke ?? {};
|
|
462
|
-
|
|
463
|
-
options = {
|
|
464
|
-
name: pkg?.name ?? path.basename(project_dir),
|
|
465
|
-
version: pkg?.version ?? null,
|
|
466
|
-
|
|
467
|
-
directory: directory,
|
|
468
|
-
...cnoke
|
|
469
|
-
};
|
|
470
|
-
|
|
471
|
-
return options;
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
function expand_string(str, values = {}) {
|
|
475
|
-
let expanded = str.replace(/{{ *([a-zA-Z_][a-zA-Z_0-9]*) *}}/g, (match, p1) => {
|
|
476
|
-
switch (p1) {
|
|
477
|
-
case 'version': {
|
|
478
|
-
let options = read_cnoke_options();
|
|
479
|
-
return options.version || '';
|
|
480
|
-
} break;
|
|
481
|
-
|
|
482
|
-
case 'toolchain': return toolchain ?? host;
|
|
483
|
-
|
|
484
|
-
default: {
|
|
485
|
-
if (Object.hasOwn(values, p1)) {
|
|
486
|
-
return values[p1];
|
|
487
|
-
} else {
|
|
488
|
-
return match;
|
|
489
|
-
}
|
|
490
|
-
} break;
|
|
491
|
-
}
|
|
492
|
-
});
|
|
493
|
-
|
|
494
|
-
return expanded;
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
function expand_path(str, root) {
|
|
498
|
-
let expanded = expand_string(str);
|
|
499
|
-
|
|
500
|
-
if (!tools.path_is_absolute(expanded))
|
|
501
|
-
expanded = path.join(root, expanded);
|
|
502
|
-
expanded = path.normalize(expanded);
|
|
503
|
-
|
|
504
|
-
return expanded;
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
module.exports = {
|
|
509
|
-
Builder,
|
|
510
|
-
DefaultOptions
|
|
511
|
-
};
|
package/src/cnoke/src/index.js
DELETED