koffi 2.16.2 → 3.0.0-alpha.10

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.
Files changed (123) hide show
  1. package/CHANGELOG.md +42 -2
  2. package/LICENSE.txt +1 -1
  3. package/README.md +8 -8
  4. package/build/koffi/darwin_arm64/koffi.node +0 -0
  5. package/build/koffi/darwin_x64/koffi.node +0 -0
  6. package/build/koffi/freebsd_arm64/koffi.node +0 -0
  7. package/build/koffi/freebsd_ia32/koffi.node +0 -0
  8. package/build/koffi/freebsd_x64/koffi.node +0 -0
  9. package/build/koffi/linux_arm64/koffi.node +0 -0
  10. package/build/koffi/linux_ia32/koffi.node +0 -0
  11. package/build/koffi/linux_x64/koffi.node +0 -0
  12. package/build/koffi/musl_arm64/koffi.node +0 -0
  13. package/build/koffi/musl_x64/koffi.node +0 -0
  14. package/build/koffi/openbsd_ia32/koffi.node +0 -0
  15. package/build/koffi/openbsd_x64/koffi.node +0 -0
  16. package/build/koffi/win32_ia32/koffi.node +0 -0
  17. package/build/koffi/win32_x64/koffi.node +0 -0
  18. package/cnoke.cjs +816 -0
  19. package/doc/benchmarks.md +33 -33
  20. package/doc/callbacks.md +9 -9
  21. package/doc/contribute.md +41 -27
  22. package/doc/functions.md +6 -6
  23. package/doc/index.md +8 -8
  24. package/doc/input.md +70 -6
  25. package/doc/misc.md +14 -15
  26. package/doc/output.md +10 -10
  27. package/doc/packaging.md +7 -7
  28. package/doc/pointers.md +14 -16
  29. package/doc/start.md +4 -4
  30. package/doc/unions.md +2 -2
  31. package/doc/variables.md +5 -5
  32. package/index.cjs +1 -0
  33. package/index.d.ts +111 -104
  34. package/index.js +1 -634
  35. package/indirect.cjs +1 -0
  36. package/indirect.js +1 -533
  37. package/lib/native/base/base.cc +86 -92
  38. package/lib/native/base/base.hh +203 -159
  39. package/lib/native/base/unicode.inc +1 -1
  40. package/package.json +18 -7
  41. package/src/koffi/CMakeLists.txt +98 -35
  42. package/src/koffi/index.cjs +333 -0
  43. package/src/koffi/index.js +271 -0
  44. package/src/koffi/indirect.cjs +332 -0
  45. package/src/koffi/indirect.js +182 -0
  46. package/src/koffi/src/{abi_arm32.cc → abi/arm32.cc} +280 -276
  47. package/src/koffi/src/{abi_arm32_asm.S → abi/arm32_asm.S} +3 -6
  48. package/src/koffi/src/abi/arm64.cc +1516 -0
  49. package/src/koffi/src/{abi_arm64_asm.S → abi/arm64_asm.S} +45 -24
  50. package/src/koffi/src/{abi_arm64_asm.asm → abi/arm64_asm.asm} +33 -26
  51. package/src/koffi/src/{abi_loong64.cc → abi/loong64.cc} +1 -1
  52. package/src/koffi/src/{abi_loong64_asm.S → abi/loong64_asm.S} +2 -6
  53. package/src/koffi/src/{abi_riscv64.cc → abi/riscv64.cc} +280 -274
  54. package/src/koffi/src/{abi_riscv64_asm.S → abi/riscv64_asm.S} +2 -5
  55. package/src/koffi/src/abi/x64sysv.cc +1456 -0
  56. package/src/koffi/src/{abi_x64_sysv_asm.S → abi/x64sysv_asm.S} +31 -34
  57. package/src/koffi/src/abi/x64win.cc +1124 -0
  58. package/src/koffi/src/{abi_x64_win_asm.S → abi/x64win_asm.S} +9 -13
  59. package/src/koffi/src/{abi_x64_win_asm.asm → abi/x64win_asm.asm} +12 -15
  60. package/src/koffi/src/abi/x86.cc +1236 -0
  61. package/src/koffi/src/{abi_x86_asm.S → abi/x86_asm.S} +50 -26
  62. package/src/koffi/src/{abi_x86_asm.asm → abi/x86_asm.asm} +12 -15
  63. package/src/koffi/src/call.cc +534 -628
  64. package/src/koffi/src/call.hh +62 -83
  65. package/src/koffi/src/errno.inc +1 -1
  66. package/src/koffi/src/ffi.cc +787 -532
  67. package/src/koffi/src/ffi.hh +149 -99
  68. package/src/koffi/src/parser.cc +1 -5
  69. package/src/koffi/src/parser.hh +1 -1
  70. package/src/koffi/src/primitives.inc +39 -0
  71. package/src/koffi/{tools/write_trampolines.js → src/trampolines.cjs} +4 -4
  72. package/src/koffi/src/util.cc +745 -491
  73. package/src/koffi/src/util.hh +332 -60
  74. package/src/koffi/src/uv.cc +37 -22
  75. package/src/koffi/src/uv.def +1 -0
  76. package/src/koffi/src/uv.hh +74 -3
  77. package/src/koffi/src/win32.cc +1 -1
  78. package/src/koffi/src/win32.hh +31 -2
  79. package/vendor/node-addon-api/napi-inl.h +32 -32
  80. package/vendor/node-addon-api/napi.h +1 -0
  81. package/build/koffi/linux_armhf/koffi.node +0 -0
  82. package/build/koffi/linux_loong64/koffi.node +0 -0
  83. package/build/koffi/linux_riscv64d/koffi.node +0 -0
  84. package/build/koffi/win32_arm64/koffi.exp +0 -0
  85. package/build/koffi/win32_arm64/koffi.lib +0 -0
  86. package/build/koffi/win32_arm64/koffi.node +0 -0
  87. package/doc/platforms.md +0 -36
  88. package/lib/native/base/crc.inc +0 -2214
  89. package/lib/native/base/crc_gen.py +0 -72
  90. package/lib/native/base/mimetypes.inc +0 -1248
  91. package/lib/native/base/mimetypes_gen.py +0 -58
  92. package/lib/native/base/tower.cc +0 -821
  93. package/lib/native/base/tower.hh +0 -81
  94. package/lib/native/base/unicode_gen.py +0 -152
  95. package/src/cnoke/LICENSE.txt +0 -22
  96. package/src/cnoke/README.md +0 -99
  97. package/src/cnoke/assets/FindCNoke.cmake +0 -127
  98. package/src/cnoke/assets/toolchains.json +0 -126
  99. package/src/cnoke/assets/win_delay_hook.c +0 -36
  100. package/src/cnoke/cnoke.js +0 -170
  101. package/src/cnoke/package.json +0 -24
  102. package/src/cnoke/src/builder.js +0 -511
  103. package/src/cnoke/src/index.js +0 -10
  104. package/src/cnoke/src/tools.js +0 -407
  105. package/src/koffi/src/abi_arm64.cc +0 -1295
  106. package/src/koffi/src/abi_x64_sysv.cc +0 -939
  107. package/src/koffi/src/abi_x64_win.cc +0 -715
  108. package/src/koffi/src/abi_x86.cc +0 -860
  109. package/src/koffi/src/init.js +0 -105
  110. package/vendor/node-api-headers/include/uv/aix.h +0 -32
  111. package/vendor/node-api-headers/include/uv/bsd.h +0 -34
  112. package/vendor/node-api-headers/include/uv/darwin.h +0 -61
  113. package/vendor/node-api-headers/include/uv/errno.h +0 -483
  114. package/vendor/node-api-headers/include/uv/linux.h +0 -34
  115. package/vendor/node-api-headers/include/uv/os390.h +0 -33
  116. package/vendor/node-api-headers/include/uv/posix.h +0 -31
  117. package/vendor/node-api-headers/include/uv/sunos.h +0 -44
  118. package/vendor/node-api-headers/include/uv/threadpool.h +0 -37
  119. package/vendor/node-api-headers/include/uv/tree.h +0 -521
  120. package/vendor/node-api-headers/include/uv/unix.h +0 -512
  121. package/vendor/node-api-headers/include/uv/version.h +0 -43
  122. package/vendor/node-api-headers/include/uv/win.h +0 -698
  123. package/vendor/node-api-headers/include/uv.h +0 -1990
package/cnoke.cjs ADDED
@@ -0,0 +1,816 @@
1
+ #!/usr/bin/env -S node --no-warnings
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (let key of __getOwnPropNames(from))
11
+ if (!__hasOwnProp.call(to, key) && key !== except)
12
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
+ }
14
+ return to;
15
+ };
16
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
+ // If the importer is in node compatibility mode or this is not an ESM
18
+ // file that has been converted to a CommonJS file using a Babel-
19
+ // compatible transform (i.e. "__esModule" has not been set), then set
20
+ // "default" to the CommonJS "module.exports" for node compatibility.
21
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
22
+ mod
23
+ ));
24
+
25
+ // ../cnoke/cnoke.js
26
+ var import_node_fs4 = __toESM(require("node:fs"), 1);
27
+
28
+ // ../cnoke/src/builder.js
29
+ var import_node_fs3 = __toESM(require("node:fs"), 1);
30
+ var import_node_os = __toESM(require("node:os"), 1);
31
+ var import_node_path = __toESM(require("node:path"), 1);
32
+ var import_node_child_process = require("node:child_process");
33
+
34
+ // ../cnoke/src/abi.js
35
+ var import_node_fs = __toESM(require("node:fs"), 1);
36
+ function determineAbi() {
37
+ let abi = process.arch;
38
+ if (abi == "riscv32" || abi == "riscv64") {
39
+ let buf = readFileHeader(process.execPath, 512);
40
+ let header = decodeElfHeader(buf);
41
+ let float_abi = header.e_flags & 6;
42
+ switch (float_abi) {
43
+ case 0:
44
+ {
45
+ }
46
+ break;
47
+ case 2:
48
+ {
49
+ abi += "f";
50
+ }
51
+ break;
52
+ case 4:
53
+ {
54
+ abi += "d";
55
+ }
56
+ break;
57
+ case 6:
58
+ {
59
+ abi += "q";
60
+ }
61
+ break;
62
+ }
63
+ } else if (abi == "arm") {
64
+ let buf = readFileHeader(process.execPath, 512);
65
+ let header = decodeElfHeader(buf);
66
+ if (header.e_flags & 1024) {
67
+ abi += "hf";
68
+ } else if (header.e_flags & 512) {
69
+ abi += "sf";
70
+ } else {
71
+ throw new Error("Unknown ARM floating-point ABI");
72
+ }
73
+ }
74
+ return abi;
75
+ }
76
+ function readFileHeader(filename, read) {
77
+ let fd = null;
78
+ try {
79
+ let fd2 = import_node_fs.default.openSync(filename);
80
+ let buf = Buffer.allocUnsafe(read);
81
+ let len = import_node_fs.default.readSync(fd2, buf);
82
+ return buf.subarray(0, len);
83
+ } finally {
84
+ if (fd != null)
85
+ import_node_fs.default.closeSync(fd);
86
+ }
87
+ }
88
+ function decodeElfHeader(buf) {
89
+ let header = {};
90
+ if (buf.length < 16)
91
+ throw new Error("Truncated header");
92
+ if (buf[0] != 127 || buf[1] != 69 || buf[2] != 76 || buf[3] != 70)
93
+ throw new Error("Invalid magic number");
94
+ if (buf[6] != 1)
95
+ throw new Error("Invalid ELF version");
96
+ if (buf[5] != 1)
97
+ throw new Error("Big-endian architectures are not supported");
98
+ header.e_machine = buf.readUInt16LE(18);
99
+ switch (buf[4]) {
100
+ case 1:
101
+ {
102
+ buf = buf.subarray(0, 68);
103
+ if (buf.length < 68)
104
+ throw new Error("Truncated ELF header");
105
+ header.ei_class = 32;
106
+ header.e_flags = buf.readUInt32LE(36);
107
+ }
108
+ break;
109
+ case 2:
110
+ {
111
+ buf = buf.subarray(0, 120);
112
+ if (buf.length < 120)
113
+ throw new Error("Truncated ELF header");
114
+ header.ei_class = 64;
115
+ header.e_flags = buf.readUInt32LE(48);
116
+ }
117
+ break;
118
+ default:
119
+ throw new Error("Invalid ELF class");
120
+ }
121
+ return header;
122
+ }
123
+
124
+ // ../cnoke/src/util.js
125
+ var import_node_fs2 = __toESM(require("node:fs"), 1);
126
+ function pathIsAbsolute(path2) {
127
+ if (process.platform == "win32" && path2.match(/^[a-zA-Z]:/))
128
+ path2 = path2.substr(2);
129
+ return isPathSeparator(path2[0]);
130
+ }
131
+ function isPathSeparator(c) {
132
+ if (c == "/")
133
+ return true;
134
+ if (process.platform == "win32" && c == "\\")
135
+ return true;
136
+ return false;
137
+ }
138
+ function syncFiles(src_dir, dest_dir) {
139
+ let keep = /* @__PURE__ */ new Set();
140
+ {
141
+ let entries = import_node_fs2.default.readdirSync(src_dir, { withFileTypes: true });
142
+ for (let entry of entries) {
143
+ if (!entry.isFile())
144
+ continue;
145
+ keep.add(entry.name);
146
+ import_node_fs2.default.copyFileSync(src_dir + `/${entry.name}`, dest_dir + `/${entry.name}`);
147
+ }
148
+ }
149
+ {
150
+ let entries = import_node_fs2.default.readdirSync(dest_dir, { withFileTypes: true });
151
+ for (let entry of entries) {
152
+ if (!entry.isFile())
153
+ continue;
154
+ if (keep.has(entry.name))
155
+ continue;
156
+ import_node_fs2.default.unlinkSync(dest_dir + `/${entry.name}`);
157
+ }
158
+ }
159
+ }
160
+ function unlinkRecursive(path2) {
161
+ try {
162
+ import_node_fs2.default.rmSync(path2, { recursive: true, maxRetries: process.platform == "win32" ? 3 : 0 });
163
+ } catch (err) {
164
+ if (err.code !== "ENOENT")
165
+ throw err;
166
+ }
167
+ }
168
+ function getNapiVersion(napi, major) {
169
+ if (napi > 8)
170
+ return null;
171
+ const support = {
172
+ 6: ["6.14.2", "6.14.2", "6.14.2"],
173
+ 8: ["8.6.0", "8.10.0", "8.11.2"],
174
+ 9: ["9.0.0", "9.3.0", "9.11.0"],
175
+ 10: ["10.0.0", "10.0.0", "10.0.0", "10.16.0", "10.17.0", "10.20.0", "10.23.0"],
176
+ 11: ["11.0.0", "11.0.0", "11.0.0", "11.8.0"],
177
+ 12: ["12.0.0", "12.0.0", "12.0.0", "12.0.0", "12.11.0", "12.17.0", "12.19.0", "12.22.0"],
178
+ 13: ["13.0.0", "13.0.0", "13.0.0", "13.0.0", "13.0.0"],
179
+ 14: ["14.0.0", "14.0.0", "14.0.0", "14.0.0", "14.0.0", "14.0.0", "14.12.0", "14.17.0"],
180
+ 15: ["15.0.0", "15.0.0", "15.0.0", "15.0.0", "15.0.0", "15.0.0", "15.0.0", "15.12.0"]
181
+ };
182
+ const max = Math.max(...Object.keys(support).map((k) => parseInt(k, 10)));
183
+ if (major > max)
184
+ return major + ".0.0";
185
+ if (support[major] == null)
186
+ return null;
187
+ let required = support[major][napi - 1] || null;
188
+ return required;
189
+ }
190
+ function compareVersions(ver1, ver2) {
191
+ ver1 = String(ver1).replace(/-.*$/, "").split(".").reduce((acc, v, idx) => acc + parseInt(v, 10) * Math.pow(10, 2 * (5 - idx)), 0);
192
+ ver2 = String(ver2).replace(/-.*$/, "").split(".").reduce((acc, v, idx) => acc + parseInt(v, 10) * Math.pow(10, 2 * (5 - idx)), 0);
193
+ let cmp = Math.min(Math.max(ver1 - ver2, -1), 1);
194
+ return cmp;
195
+ }
196
+
197
+ // ../cnoke/src/assets.js
198
+ var FIND_CNOKE_CMAKE = `# SPDX-License-Identifier: MIT
199
+ # SPDX-FileCopyrightText: 2026 Niels Martign\xE8ne <niels.martignene@protonmail.com>
200
+
201
+ if(CMAKE_BUILD_TYPE STREQUAL "Release" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo" OR
202
+ CMAKE_BUILD_TYPE STREQUAL "MinSizeRel")
203
+ set(USE_UNITY_BUILDS ON CACHE BOOL "Use single-TU builds (aka. Unity builds)")
204
+ else()
205
+ set(USE_UNITY_BUILDS OFF CACHE BOOL "Use single-TU builds (aka. Unity builds)")
206
+ endif()
207
+
208
+ function(add_node_addon)
209
+ cmake_parse_arguments(ARG "" "NAME" "SOURCES" \${ARGN})
210
+ add_library(\${ARG_NAME} SHARED \${ARG_SOURCES} \${NODE_JS_SOURCES})
211
+ target_link_node(\${ARG_NAME})
212
+ set_target_properties(\${ARG_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
213
+ endfunction()
214
+
215
+ function(target_link_node TARGET)
216
+ target_include_directories(\${TARGET} PRIVATE \${NODE_JS_INCLUDE_DIRS})
217
+ if(NODE_JS_LINK_DEF)
218
+ target_sources(\${TARGET} PRIVATE node.lib)
219
+ endif()
220
+ if(NODE_JS_LINK_LIB)
221
+ target_link_libraries(\${TARGET} PRIVATE \${NODE_JS_LINK_LIB})
222
+ endif()
223
+ target_compile_options(\${TARGET} PRIVATE \${NODE_JS_COMPILE_FLAGS})
224
+ if(NODE_JS_LINK_FLAGS)
225
+ target_link_options(\${TARGET} PRIVATE \${NODE_JS_LINK_FLAGS})
226
+ endif()
227
+ endfunction()
228
+
229
+ if(WIN32)
230
+ function(create_import_lib OUTPUT SRC)
231
+ if (MSVC)
232
+ add_custom_command(OUTPUT \${OUTPUT}
233
+ COMMAND \${CMAKE_AR} \${CMAKE_STATIC_LINKER_FLAGS}
234
+ /def:\${SRC} /out:"\${CMAKE_CURRENT_BINARY_DIR}/\${OUTPUT}"
235
+ WORKING_DIRECTORY \${CMAKE_CURRENT_SOURCE_DIR}
236
+ MAIN_DEPENDENCY \${SRC})
237
+ elseif(DEFINED NODE_JS_DLLTOOL_MACHINE)
238
+ add_custom_command(OUTPUT \${OUTPUT}
239
+ COMMAND \${CMAKE_DLLTOOL} -d \${SRC} -l "\${CMAKE_CURRENT_BINARY_DIR}/\${OUTPUT}" -m \${NODE_JS_DLLTOOL_MACHINE}
240
+ WORKING_DIRECTORY \${CMAKE_CURRENT_SOURCE_DIR}
241
+ MAIN_DEPENDENCY \${SRC})
242
+ else()
243
+ add_custom_command(OUTPUT \${OUTPUT}
244
+ COMMAND \${CMAKE_DLLTOOL} -d \${SRC} -l "\${CMAKE_CURRENT_BINARY_DIR}/\${OUTPUT}"
245
+ WORKING_DIRECTORY \${CMAKE_CURRENT_SOURCE_DIR}
246
+ MAIN_DEPENDENCY \${SRC})
247
+ endif()
248
+ endfunction()
249
+ endif()
250
+
251
+ if(NODE_JS_LINK_DEF)
252
+ create_import_lib(node.lib \${NODE_JS_LINK_DEF})
253
+ set(NODE_JS_LINK_LIB "\${CMAKE_CURRENT_BINARY_DIR}/node.lib")
254
+ endif()
255
+
256
+ if(USE_UNITY_BUILDS)
257
+ function(enable_unity_build TARGET)
258
+ cmake_parse_arguments(ARG "" "" "EXCLUDE" \${ARGN})
259
+
260
+ get_target_property(sources \${TARGET} SOURCES)
261
+ string(GENEX_STRIP "\${sources}" sources)
262
+
263
+ set(unity_file_c "\${CMAKE_CURRENT_BINARY_DIR}/\${TARGET}_unity.c")
264
+ set(unity_file_cpp "\${CMAKE_CURRENT_BINARY_DIR}/\${TARGET}_unity.cpp")
265
+ file(REMOVE \${unity_file_c} \${unity_file_cpp})
266
+
267
+ set(c_definitions "")
268
+ set(cpp_definitions "")
269
+
270
+ foreach(src \${sources})
271
+ if (src IN_LIST ARG_EXCLUDE)
272
+ continue()
273
+ endif()
274
+
275
+ get_source_file_property(language \${src} LANGUAGE)
276
+ get_property(definitions SOURCE \${src} PROPERTY COMPILE_DEFINITIONS)
277
+ if(IS_ABSOLUTE \${src})
278
+ set(src_full \${src})
279
+ else()
280
+ set(src_full "\${CMAKE_CURRENT_SOURCE_DIR}/\${src}")
281
+ endif()
282
+ if(language STREQUAL "C")
283
+ set_source_files_properties(\${src} PROPERTIES HEADER_FILE_ONLY 1)
284
+ file(APPEND \${unity_file_c} "#include \\"\${src_full}\\"\\n")
285
+ if (definitions)
286
+ set(c_definitions "\${c_definitions} \${definitions}")
287
+ endif()
288
+ elseif(language STREQUAL "CXX")
289
+ set_source_files_properties(\${src} PROPERTIES HEADER_FILE_ONLY 1)
290
+ file(APPEND \${unity_file_cpp} "#include \\"\${src_full}\\"\\n")
291
+ if (definitions)
292
+ set(cpp_definitions "\${cpp_definitions} \${definitions}")
293
+ endif()
294
+ endif()
295
+ endforeach()
296
+
297
+ if(EXISTS \${unity_file_c})
298
+ target_sources(\${TARGET} PRIVATE \${unity_file_c})
299
+ if(c_definitions)
300
+ set_source_files_properties(\${unity_file_c} PROPERTIES COMPILE_DEFINITIONS \${c_definitions})
301
+ endif()
302
+ endif()
303
+ if(EXISTS \${unity_file_cpp})
304
+ target_sources(\${TARGET} PRIVATE \${unity_file_cpp})
305
+ if(cpp_definitions)
306
+ set_source_files_properties(\${unity_file_cpp} PROPERTIES COMPILE_DEFINITIONS \${cpp_definitions})
307
+ endif()
308
+ endif()
309
+
310
+ target_compile_definitions(\${TARGET} PRIVATE -DUNITY_BUILD=1)
311
+ endfunction()
312
+ else()
313
+ function(enable_unity_build TARGET)
314
+ endfunction()
315
+ endif()
316
+
317
+ if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
318
+ if(CMAKE_SYSTEM_PROCESSOR MATCHES "(amd64|x86_64)")
319
+ foreach(lang C CXX)
320
+ set(CMAKE_\${lang}_FLAGS_RELEASE "\${CMAKE_\${lang}_FLAGS_RELEASE} -mpopcnt -msse4.1 -msse4.2 -mssse3 -mcx16")
321
+ set(CMAKE_\${lang}_FLAGS_RELWITHDEBINFO "\${CMAKE_\${lang}_FLAGS_RELWITHDEBINFO} -mpopcnt -msse4.1 -msse4.2 -mssse3 -mcx16")
322
+ endforeach()
323
+ endif()
324
+ endif()
325
+ `;
326
+ var WIN_DELAY_HOOK_C = `// SPDX-License-Identifier: MIT
327
+ // SPDX-FileCopyrightText: 2026 Niels Martign\xE8ne <niels.martignene@protonmail.com>
328
+
329
+ #include <stdlib.h>
330
+ #if !defined(NOMINMAX)
331
+ #define NOMINMAX
332
+ #endif
333
+ #if !defined(WIN32_LEAN_AND_MEAN)
334
+ #define WIN32_LEAN_AND_MEAN
335
+ #endif
336
+ #include <windows.h>
337
+ #include <delayimp.h>
338
+
339
+ static HMODULE node_dll;
340
+
341
+ static FARPROC WINAPI self_exe_hook(unsigned int event, DelayLoadInfo *info)
342
+ {
343
+ if (event == dliStartProcessing) {
344
+ node_dll = GetModuleHandleA("node.dll");
345
+ if (!node_dll) {
346
+ node_dll = GetModuleHandle(NULL);
347
+ }
348
+ return NULL;
349
+ }
350
+
351
+ if (event == dliNotePreLoadLibrary && !stricmp(info->szDll, "node.exe"))
352
+ return (FARPROC)node_dll;
353
+
354
+ return NULL;
355
+ }
356
+
357
+ #if defined(__MINGW32__)
358
+ PfnDliHook __pfnDliNotifyHook2 = self_exe_hook;
359
+ #else
360
+ const PfnDliHook __pfnDliNotifyHook2 = self_exe_hook;
361
+ #endif
362
+ `;
363
+
364
+ // ../cnoke/src/builder.js
365
+ var DefaultOptions = {
366
+ mode: "RelWithDebInfo"
367
+ };
368
+ function Builder(config = {}) {
369
+ let self = this;
370
+ let host = `${process.platform}_${determineAbi()}`;
371
+ let project_dir = config.project_dir;
372
+ let package_dir = config.package_dir;
373
+ if (project_dir == null)
374
+ project_dir = process.cwd();
375
+ project_dir = project_dir.replace(/\\/g, "/");
376
+ if (package_dir == null)
377
+ package_dir = findParentDirectory(project_dir, "package.json");
378
+ if (package_dir != null)
379
+ package_dir = package_dir.replace(/\\/g, "/");
380
+ let runtime_version = config.runtime_version;
381
+ let toolchain = config.toolchain || null;
382
+ let prefer_clang = config.prefer_clang || false;
383
+ let mode = config.mode || DefaultOptions.mode;
384
+ let targets = config.targets || [];
385
+ let verbose = config.verbose || false;
386
+ let prebuild = config.prebuild || false;
387
+ let defines = config.defines || [];
388
+ if (runtime_version == null)
389
+ runtime_version = process.version;
390
+ if (runtime_version.startsWith("v"))
391
+ runtime_version = runtime_version.substr(1);
392
+ let options = null;
393
+ let cache_dir = getCacheDirectory();
394
+ let build_dir = config.build_dir;
395
+ let work_dir = null;
396
+ let output_dir = null;
397
+ if (build_dir == null) {
398
+ let options2 = readCNokeOptions();
399
+ if (options2.output != null) {
400
+ build_dir = expandPath(options2.output, options2.directory);
401
+ } else {
402
+ build_dir = project_dir + "/build";
403
+ }
404
+ }
405
+ build_dir = build_dir.replace(/\\/g, "/");
406
+ work_dir = build_dir + `/v${runtime_version}_${toolchain ?? "native"}/${mode}`;
407
+ output_dir = work_dir + "/Output";
408
+ let cmake_bin = null;
409
+ this.configure = async function(retry = true) {
410
+ let options2 = readCNokeOptions();
411
+ let args = [project_dir];
412
+ checkCMake();
413
+ checkCompatibility();
414
+ console.log(`>> Node: ${runtime_version}`);
415
+ console.log(`>> Toolchain: ${toolchain ?? "native"}`);
416
+ import_node_fs3.default.mkdirSync(build_dir, { recursive: true, mode: 493 });
417
+ import_node_fs3.default.mkdirSync(work_dir, { recursive: true, mode: 493 });
418
+ import_node_fs3.default.mkdirSync(output_dir, { recursive: true, mode: 493 });
419
+ retry &= import_node_fs3.default.existsSync(work_dir + "/CMakeCache.txt");
420
+ args.push(`-DNODE_JS_EXECPATH=${process.execPath}`);
421
+ if (options2.api == null) {
422
+ let downloaded = false;
423
+ if (!downloaded)
424
+ throw new Error("Cannot download API headers");
425
+ } else {
426
+ console.log(`>> Using local node-api headers`);
427
+ let api_dir = expandPath(options2.api, project_dir);
428
+ args.push(`-DNODE_JS_INCLUDE_DIRS=${api_dir}/include`);
429
+ }
430
+ import_node_fs3.default.writeFileSync(work_dir + "/FindCNoke.cmake", FIND_CNOKE_CMAKE);
431
+ args.push(`-DCMAKE_MODULE_PATH=${work_dir}`);
432
+ let win32 = (toolchain ?? host).startsWith("win32_");
433
+ let mingw = process.platform == "win32" && process.env.MSYSTEM != null;
434
+ if (win32) {
435
+ if (mingw) {
436
+ args.push(`-DNODE_JS_LINK_LIB=node.dll`);
437
+ } else {
438
+ if (options2.api == null) {
439
+ let downloaded = false;
440
+ if (!downloaded)
441
+ throw new Error("Cannot download Node import library");
442
+ } else {
443
+ let api_dir = expandPath(options2.api, project_dir);
444
+ args.push(`-DNODE_JS_LINK_DEF=${api_dir}/def/node_api.def`);
445
+ }
446
+ }
447
+ import_node_fs3.default.writeFileSync(work_dir + "/win_delay_hook.c", WIN_DELAY_HOOK_C);
448
+ args.push(`-DNODE_JS_SOURCES=${work_dir}/win_delay_hook.c`);
449
+ }
450
+ if (process.platform != "win32" || mingw) {
451
+ if ((0, import_node_child_process.spawnSync)("ninja", ["--version"]).status === 0) {
452
+ args.push("-G", "Ninja");
453
+ } else if (process.platform == "win32") {
454
+ args.push("-G", "MinGW Makefiles");
455
+ }
456
+ if (config.ccache && (0, import_node_child_process.spawnSync)("ccache", ["--version"]).status === 0) {
457
+ args.push("-DCMAKE_C_COMPILER_LAUNCHER=ccache");
458
+ args.push("-DCMAKE_CXX_COMPILER_LAUNCHER=ccache");
459
+ }
460
+ }
461
+ if (prefer_clang) {
462
+ if (process.platform == "win32" && !mingw) {
463
+ args.push("-T", "ClangCL");
464
+ } else {
465
+ args.push("-DCMAKE_C_COMPILER=clang");
466
+ args.push("-DCMAKE_CXX_COMPILER=clang++");
467
+ }
468
+ }
469
+ args.push(`-DCMAKE_BUILD_TYPE=${mode}`);
470
+ for (let type of ["ARCHIVE", "RUNTIME", "LIBRARY"]) {
471
+ for (let suffix of ["", "_DEBUG", "_RELEASE", "_RELWITHDEBINFO"])
472
+ args.push(`-DCMAKE_${type}_OUTPUT_DIRECTORY${suffix}=${output_dir}`);
473
+ }
474
+ for (let define of defines)
475
+ args.push(`-D${define}`);
476
+ args.push("--no-warn-unused-cli");
477
+ console.log(">> Running configuration");
478
+ let proc = (0, import_node_child_process.spawnSync)(cmake_bin, args, { cwd: work_dir, stdio: "inherit" });
479
+ if (proc.status !== 0) {
480
+ unlinkRecursive(work_dir);
481
+ if (retry)
482
+ return self.configure(false);
483
+ throw new Error("Failed to run configure step");
484
+ }
485
+ };
486
+ this.build = async function() {
487
+ checkCompatibility();
488
+ if (prebuild) {
489
+ let valid = await checkPrebuild();
490
+ if (valid) {
491
+ return;
492
+ } else {
493
+ console.error("Failed to load prebuilt binary, rebuilding from source");
494
+ }
495
+ }
496
+ checkCMake();
497
+ if (!import_node_fs3.default.existsSync(work_dir + "/CMakeCache.txt"))
498
+ await self.configure();
499
+ if (process.env.MAKEFLAGS == null)
500
+ process.env.MAKEFLAGS = "-j" + (import_node_os.default.cpus().length || 1);
501
+ let args = [
502
+ "--build",
503
+ work_dir,
504
+ "--config",
505
+ mode
506
+ ];
507
+ if (verbose)
508
+ args.push("--verbose");
509
+ for (let target of targets)
510
+ args.push("--target", target);
511
+ console.log(">> Running build");
512
+ let proc = (0, import_node_child_process.spawnSync)(cmake_bin, args, { stdio: "inherit" });
513
+ if (proc.status !== 0)
514
+ throw new Error("Failed to run build step");
515
+ console.log(">> Copy target files");
516
+ syncFiles(output_dir, build_dir);
517
+ };
518
+ async function checkPrebuild() {
519
+ let proc = (0, import_node_child_process.spawnSync)(process.execPath, ["-e", "require(process.argv[1])", package_dir]);
520
+ return proc.status === 0;
521
+ }
522
+ this.clean = function() {
523
+ unlinkRecursive(build_dir);
524
+ };
525
+ function findParentDirectory(dirname, basename) {
526
+ if (process.platform == "win32")
527
+ dirname = dirname.replace(/\\/g, "/");
528
+ do {
529
+ if (import_node_fs3.default.existsSync(dirname + "/" + basename))
530
+ return dirname;
531
+ dirname = import_node_path.default.dirname(dirname);
532
+ } while (!dirname.endsWith("/"));
533
+ return null;
534
+ }
535
+ function getCacheDirectory() {
536
+ if (process.platform == "win32") {
537
+ let cache_dir2 = process.env["LOCALAPPDATA"] || process.env["APPDATA"];
538
+ if (cache_dir2 == null)
539
+ throw new Error("Missing LOCALAPPDATA and APPDATA environment variable");
540
+ cache_dir2 = import_node_path.default.join(cache_dir2, "cnoke");
541
+ return cache_dir2;
542
+ } else {
543
+ let cache_dir2 = process.env["XDG_CACHE_HOME"];
544
+ if (cache_dir2 == null) {
545
+ let home = process.env["HOME"];
546
+ if (home == null)
547
+ throw new Error("Missing HOME environment variable");
548
+ cache_dir2 = import_node_path.default.join(home, ".cache");
549
+ }
550
+ cache_dir2 = import_node_path.default.join(cache_dir2, "cnoke");
551
+ return cache_dir2;
552
+ }
553
+ }
554
+ function checkCMake() {
555
+ if (cmake_bin != null)
556
+ return;
557
+ if (!import_node_fs3.default.existsSync(project_dir + "/CMakeLists.txt"))
558
+ throw new Error("This directory does not appear to have a CMakeLists.txt file");
559
+ {
560
+ let proc = (0, import_node_child_process.spawnSync)("cmake", ["--version"]);
561
+ if (proc.status === 0) {
562
+ cmake_bin = "cmake";
563
+ } else {
564
+ if (process.platform == "win32") {
565
+ let proc2 = (0, import_node_child_process.spawnSync)("reg", ["query", "HKEY_LOCAL_MACHINE\\SOFTWARE\\Kitware\\CMake", "/v", "InstallDir"]);
566
+ if (proc2.status === 0) {
567
+ let matches = proc2.stdout.toString("utf-8").match(/InstallDir[ \t]+REG_[A-Z_]+[ \t]+(.*)+/);
568
+ if (matches != null) {
569
+ let bin = import_node_path.default.join(matches[1].trim(), "bin\\cmake.exe");
570
+ if (import_node_fs3.default.existsSync(bin))
571
+ cmake_bin = bin;
572
+ }
573
+ }
574
+ }
575
+ if (cmake_bin == null)
576
+ throw new Error("CMake does not seem to be available");
577
+ }
578
+ }
579
+ console.log(`>> Using CMake binary: ${cmake_bin}`);
580
+ }
581
+ function checkCompatibility() {
582
+ let options2 = readCNokeOptions();
583
+ if (options2.node != null && compareVersions(runtime_version, options2.node) < 0)
584
+ throw new Error(`Project ${options2.name} requires Node.js >= ${options2.node}`);
585
+ if (options2.napi != null) {
586
+ let major = parseInt(runtime_version, 10);
587
+ let required = getNapiVersion(options2.napi, major);
588
+ if (required == null)
589
+ throw new Error(`Project ${options2.name} does not support the Node ${major}.x branch (old or missing N-API)`);
590
+ if (compareVersions(runtime_version, required) < 0)
591
+ throw new Error(`Project ${options2.name} requires Node >= ${required} in the Node ${major}.x branch (with N-API >= ${options2.napi})`);
592
+ }
593
+ }
594
+ function readCNokeOptions() {
595
+ if (options != null)
596
+ return options;
597
+ let directory = project_dir;
598
+ let pkg = null;
599
+ let cnoke = null;
600
+ if (package_dir != null) {
601
+ try {
602
+ let json = import_node_fs3.default.readFileSync(package_dir + "/package.json", { encoding: "utf-8" });
603
+ pkg = JSON.parse(json);
604
+ directory = package_dir;
605
+ } catch (err) {
606
+ if (err.code != "ENOENT")
607
+ throw err;
608
+ }
609
+ }
610
+ if (cnoke == null)
611
+ cnoke = pkg?.cnoke ?? {};
612
+ options = {
613
+ name: pkg?.name ?? import_node_path.default.basename(project_dir),
614
+ version: pkg?.version ?? null,
615
+ directory,
616
+ ...cnoke
617
+ };
618
+ return options;
619
+ }
620
+ function expandString(str, values = {}) {
621
+ let expanded = str.replace(/{{ *([a-zA-Z_][a-zA-Z_0-9]*) *}}/g, (match, p1) => {
622
+ switch (p1) {
623
+ case "version":
624
+ {
625
+ let options2 = readCNokeOptions();
626
+ return options2.version || "";
627
+ }
628
+ break;
629
+ case "toolchain":
630
+ return toolchain ?? host;
631
+ default:
632
+ {
633
+ if (Object.hasOwn(values, p1)) {
634
+ return values[p1];
635
+ } else {
636
+ return match;
637
+ }
638
+ }
639
+ break;
640
+ }
641
+ });
642
+ return expanded;
643
+ }
644
+ function expandPath(str, root) {
645
+ let expanded = expandString(str);
646
+ if (!pathIsAbsolute(expanded))
647
+ expanded = import_node_path.default.join(root, expanded);
648
+ expanded = import_node_path.default.normalize(expanded);
649
+ return expanded;
650
+ }
651
+ }
652
+
653
+ // ../cnoke/cnoke.js
654
+ var VALID_COMMANDS = ["build", "configure", "clean"];
655
+ main();
656
+ async function main() {
657
+ let config = {};
658
+ let command = "build";
659
+ {
660
+ let i = 2;
661
+ if (process.argv.length >= 3 && process.argv[2][0] != "-") {
662
+ let cmd = process.argv[2];
663
+ if (VALID_COMMANDS.includes(cmd)) {
664
+ command = cmd;
665
+ i++;
666
+ }
667
+ }
668
+ for (; i < process.argv.length; i++) {
669
+ let arg = process.argv[i];
670
+ let value = null;
671
+ if (arg[0] == "-") {
672
+ if (arg.length > 2 && arg[1] != "-") {
673
+ value = arg.substr(2);
674
+ arg = arg.substr(0, 2);
675
+ } else if (arg[1] == "-") {
676
+ let offset = arg.indexOf("=");
677
+ if (offset > 2 && arg.length > offset + 1) {
678
+ value = arg.substr(offset + 1);
679
+ arg = arg.substr(0, offset);
680
+ }
681
+ }
682
+ if (value == null && process.argv[i + 1] != null && process.argv[i + 1][0] != "-") {
683
+ value = process.argv[i + 1];
684
+ i++;
685
+ }
686
+ }
687
+ if (arg == "--help") {
688
+ printUsage();
689
+ return;
690
+ } else if (arg == "-D" || arg == "--directory") {
691
+ if (value == null)
692
+ throw new Error(`Missing value for ${arg}`);
693
+ config.project_dir = import_node_fs4.default.realpathSync(value);
694
+ } else if (arg == "-P" || arg == "--package") {
695
+ if (value == null)
696
+ throw new Error(`Missing value for ${arg}`);
697
+ config.package_dir = import_node_fs4.default.realpathSync(value);
698
+ } else if (arg == "-O" || arg == "--output") {
699
+ if (value == null)
700
+ throw new Error(`Missing value for ${arg}`);
701
+ config.output_directory = value;
702
+ } else if ((command == "build" || command == "configure") && arg == "--runtime") {
703
+ if (value == null)
704
+ throw new Error(`Missing value for ${arg}`);
705
+ if (!value.match(/^[0-9]+\.[0-9]+\.[0-9]+$/))
706
+ throw new Error(`Malformed runtime version '${value}'`);
707
+ config.runtime_version = value;
708
+ } else if ((command == "build" || command == "configure") && arg == "--clang") {
709
+ config.prefer_clang = true;
710
+ } else if ((command == "build" || command == "configure") && (arg == "-t" || arg == "--toolchain")) {
711
+ if (value == null)
712
+ throw new Error(`Missing value for ${arg}`);
713
+ config.toolchain = value;
714
+ } else if (arg == "-c" || arg == "--config") {
715
+ if (value == null)
716
+ throw new Error(`Missing value for ${arg}`);
717
+ switch (value.toLowerCase()) {
718
+ case "relwithdebinfo":
719
+ {
720
+ config.mode = "RelWithDebInfo";
721
+ }
722
+ break;
723
+ case "debug":
724
+ {
725
+ config.mode = "Debug";
726
+ }
727
+ break;
728
+ case "release":
729
+ {
730
+ config.mode = "Release";
731
+ }
732
+ break;
733
+ default:
734
+ {
735
+ throw new Error(`Unknown value '${value}' for ${arg}`);
736
+ }
737
+ break;
738
+ }
739
+ } else if (arg == "--debug") {
740
+ config.mode = "Debug";
741
+ } else if (arg == "--release") {
742
+ config.mode = "Release";
743
+ } else if ((command == "build" || command == "configure") && arg == "--ccache") {
744
+ config.ccache = true;
745
+ } else if ((command == "build" || command == "configure") && (arg == "-d" || arg == "--define")) {
746
+ if (value == null)
747
+ throw new Error(`Missing value for ${arg}`);
748
+ config.defines ??= [];
749
+ config.defines.push(value);
750
+ } else if (command == "build" && arg == "--prebuild") {
751
+ config.prebuild = true;
752
+ } else if (command == "build" && arg == "--target") {
753
+ if (value == null)
754
+ throw new Error(`Missing value for ${arg}`);
755
+ config.targets = [value];
756
+ } else if (command == "build" && (arg == "-v" || arg == "--verbose")) {
757
+ config.verbose = true;
758
+ } else {
759
+ if (arg[0] == "-") {
760
+ throw new Error(`Unexpected argument '${arg}'`);
761
+ } else {
762
+ throw new Error(`Unexpected value '${arg}'`);
763
+ }
764
+ }
765
+ }
766
+ }
767
+ try {
768
+ let builder = new Builder(config);
769
+ await builder[command]();
770
+ } catch (err) {
771
+ console.error(err);
772
+ process.exit(1);
773
+ }
774
+ }
775
+ function printUsage() {
776
+ let help = `Usage: cnoke [command] [options...]
777
+
778
+ Commands:
779
+
780
+ configure Configure CMake build
781
+ build Build project (configure if needed)
782
+ clean Clean build files
783
+
784
+ Options:
785
+
786
+ -D, --directory <DIR> Change source directory
787
+ (default: current working directory)
788
+ -P, --package <DIR> Change package directory
789
+ (default: current working directory)
790
+ -O, --output <DIR> Set explicit output directory
791
+ (default: ./build)
792
+
793
+ -c, --config <CONFIG> Change build type: RelWithDebInfo, Debug, Release
794
+ (default: ${DefaultOptions.mode})
795
+ --debug Shortcut for --config Debug
796
+ --release Shortcut for --config Release
797
+
798
+ --ccache Use ccache if available
799
+
800
+ -t, --toolchain <TOOLCHAIN> Cross-compile for specific platform
801
+ --runtime <VERSION> Change node version
802
+ (default: ${process.version})
803
+ --clang Use Clang instead of default CMake compiler
804
+
805
+ --prebuild Use prebuilt binary if available
806
+ --target <TARGET> Only build the specified target
807
+
808
+ -v, --verbose Show build commands while building
809
+
810
+ The ARCH value is similar to process.arch, with the following differences:
811
+
812
+ - arm is changed to arm32hf or arm32sf depending on the floating-point ABI used (hard-float, soft-float)
813
+ - riscv32 is changed to riscv32sf, riscv32hf32, riscv32hf64 or riscv32hf128 depending on the floating-point ABI
814
+ - riscv64 is changed to riscv64sf, riscv64hf32, riscv64hf64 or riscv64hf128 depending on the floating-point ABI`;
815
+ console.log(help);
816
+ }