catchword 0.0.0-stage → 1.0.0

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 (157) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +314 -2
  3. package/bin/catchword.js +8 -0
  4. package/bin/falcon.js +5 -0
  5. package/dist/cli/adb.d.ts +6 -0
  6. package/dist/cli/adb.js +52 -0
  7. package/dist/cli/audit.d.ts +4 -0
  8. package/dist/cli/audit.js +91 -0
  9. package/dist/cli/build.d.ts +13 -0
  10. package/dist/cli/build.js +139 -0
  11. package/dist/cli/clean.d.ts +1 -0
  12. package/dist/cli/clean.js +15 -0
  13. package/dist/cli/create.d.ts +4 -0
  14. package/dist/cli/create.js +60 -0
  15. package/dist/cli/dev.d.ts +10 -0
  16. package/dist/cli/dev.js +23 -0
  17. package/dist/cli/index.d.ts +3 -0
  18. package/dist/cli/index.js +243 -0
  19. package/dist/cli/native.d.ts +18 -0
  20. package/dist/cli/native.js +232 -0
  21. package/dist/cli/simulator.d.ts +8 -0
  22. package/dist/cli/simulator.js +77 -0
  23. package/dist/cli/upload.d.ts +9 -0
  24. package/dist/cli/upload.js +135 -0
  25. package/dist/cli/util.d.ts +8 -0
  26. package/dist/cli/util.js +43 -0
  27. package/dist/config.d.ts +17 -0
  28. package/dist/config.js +90 -0
  29. package/dist/index.d.ts +16 -0
  30. package/dist/index.js +51 -0
  31. package/dist/native/api.d.ts +46 -0
  32. package/dist/native/api.js +185 -0
  33. package/dist/native/build.d.ts +47 -0
  34. package/dist/native/build.js +407 -0
  35. package/dist/native/elf.d.ts +15 -0
  36. package/dist/native/elf.js +200 -0
  37. package/dist/native/types.d.ts +9 -0
  38. package/dist/native/types.js +91 -0
  39. package/dist/native/wsl.d.ts +7 -0
  40. package/dist/native/wsl.js +29 -0
  41. package/dist/plugin/app-entry.d.ts +6 -0
  42. package/dist/plugin/app-entry.js +115 -0
  43. package/dist/plugin/assets.d.ts +4 -0
  44. package/dist/plugin/assets.js +84 -0
  45. package/dist/plugin/config.d.ts +14 -0
  46. package/dist/plugin/config.js +80 -0
  47. package/dist/plugin/context.d.ts +58 -0
  48. package/dist/plugin/context.js +255 -0
  49. package/dist/plugin/fallback.d.ts +4 -0
  50. package/dist/plugin/fallback.js +81 -0
  51. package/dist/plugin/image-service.d.ts +17 -0
  52. package/dist/plugin/image-service.js +138 -0
  53. package/dist/plugin/jsapi.d.ts +6 -0
  54. package/dist/plugin/jsapi.js +82 -0
  55. package/dist/plugin/output.d.ts +4 -0
  56. package/dist/plugin/output.js +75 -0
  57. package/dist/plugin/preview.d.ts +34 -0
  58. package/dist/plugin/preview.js +459 -0
  59. package/dist/plugin/sanitize.d.ts +5 -0
  60. package/dist/plugin/sanitize.js +48 -0
  61. package/dist/plugin/security.d.ts +4 -0
  62. package/dist/plugin/security.js +18 -0
  63. package/dist/plugin/sfc.d.ts +6 -0
  64. package/dist/plugin/sfc.js +181 -0
  65. package/dist/plugin/shim.d.ts +4 -0
  66. package/dist/plugin/shim.js +68 -0
  67. package/dist/plugin/state.d.ts +31 -0
  68. package/dist/plugin/state.js +61 -0
  69. package/dist/plugin/string-table.d.ts +7 -0
  70. package/dist/plugin/string-table.js +324 -0
  71. package/dist/plugin/style.d.ts +15 -0
  72. package/dist/plugin/style.js +114 -0
  73. package/dist/plugin/template.d.ts +7 -0
  74. package/dist/plugin/template.js +39 -0
  75. package/dist/steps/manifest.d.ts +9 -0
  76. package/dist/steps/manifest.js +60 -0
  77. package/dist/steps/pack.d.ts +3 -0
  78. package/dist/steps/pack.js +46 -0
  79. package/dist/steps/qjsc.d.ts +9 -0
  80. package/dist/steps/qjsc.js +186 -0
  81. package/dist/steps/report.d.ts +2 -0
  82. package/dist/steps/report.js +50 -0
  83. package/dist/steps/security.d.ts +17 -0
  84. package/dist/steps/security.js +310 -0
  85. package/dist/types.d.ts +158 -0
  86. package/dist/types.js +1 -0
  87. package/dist/util/class-tokenize.d.ts +2 -0
  88. package/dist/util/class-tokenize.js +56 -0
  89. package/dist/util/component-name.d.ts +6 -0
  90. package/dist/util/component-name.js +120 -0
  91. package/dist/util/fs.d.ts +14 -0
  92. package/dist/util/fs.js +70 -0
  93. package/dist/util/imports.d.ts +2 -0
  94. package/dist/util/imports.js +173 -0
  95. package/dist/util/log.d.ts +8 -0
  96. package/dist/util/log.js +24 -0
  97. package/package.json +61 -4
  98. package/template/README.md +38 -0
  99. package/template/catchword.config.ts +18 -0
  100. package/template/native/CMakeLists.txt +33 -0
  101. package/template/native/README.md +34 -0
  102. package/template/native/api.override.json +7 -0
  103. package/template/native/include/jsapi_demo.h +10 -0
  104. package/template/native/lib/README.md +10 -0
  105. package/template/native/sdk/README.md +6 -0
  106. package/template/native/src/bridge.cpp +9 -0
  107. package/template/native/src/jsapi_demo.cpp +11 -0
  108. package/template/package.json +38 -0
  109. package/template/src/app.js +45 -0
  110. package/template/src/app.json +11 -0
  111. package/template/src/base-page.js +150 -0
  112. package/template/src/components/HelloWorld.vue +20 -0
  113. package/template/src/mocks/$jsapi.test.js +5 -0
  114. package/template/src/mocks/test-module.js +5 -0
  115. package/template/src/pages/index/index.vue +41 -0
  116. package/template/src/pages/page/page.vue +63 -0
  117. package/template/src/shims.d.ts +21 -0
  118. package/template/src/styles/base.less +23 -0
  119. package/template/src/styles/mixin.less +32 -0
  120. package/template/src/styles/var.less +15 -0
  121. package/template/tsconfig.json +14 -0
  122. package/vendor/README.md +16 -0
  123. package/vendor/falcon-styler/.jscsrc +31 -0
  124. package/vendor/falcon-styler/LICENSE +21 -0
  125. package/vendor/falcon-styler/README.md +124 -0
  126. package/vendor/falcon-styler/index.js +273 -0
  127. package/vendor/falcon-styler/lib/shorthand-parser.js +78 -0
  128. package/vendor/falcon-styler/lib/util.js +30 -0
  129. package/vendor/falcon-styler/lib/validator.js +846 -0
  130. package/vendor/falcon-styler/package.json +32 -0
  131. package/vendor/licenses/falcon-styler.LICENSE +21 -0
  132. package/vendor/licenses/weex-template-compiler.README.md +3 -0
  133. package/vendor/package.json +5 -0
  134. package/vendor/qjsc/SHA256SUMS +11 -0
  135. package/vendor/qjsc/bin/darwin/arm64/qjsc20200705 +0 -0
  136. package/vendor/qjsc/bin/darwin/x64/qjsc20200412 +0 -0
  137. package/vendor/qjsc/bin/darwin/x64/qjsc20200705 +0 -0
  138. package/vendor/qjsc/bin/darwin/x64/qjsc20200906 +0 -0
  139. package/vendor/qjsc/bin/darwin/x64/qjscbn20200412 +0 -0
  140. package/vendor/qjsc/bin/darwin/x64/qjscbn20200705 +0 -0
  141. package/vendor/qjsc/bin/darwin/x64/qjscbn20200906 +0 -0
  142. package/vendor/qjsc/bin/linux/qjsc20200705 +0 -0
  143. package/vendor/qjsc/bin/win32/libgcc_s_dw2-1.dll +0 -0
  144. package/vendor/qjsc/bin/win32/pthreadGC-3.dll +0 -0
  145. package/vendor/qjsc/bin/win32/qjsc20200705.exe +0 -0
  146. package/vendor/web/falcon-js-web.js +863 -0
  147. package/vendor/web/falcon-jsapi-web.js +479 -0
  148. package/vendor/web/falcon-vue-render.js +11323 -0
  149. package/vendor/web/falcon-web-boot.js +4 -0
  150. package/vendor/web/falcon-web-hud.js +119 -0
  151. package/vendor/web/falcon-web-style.js +235 -0
  152. package/vendor/web/vue.runtime.common.dev.js +8467 -0
  153. package/vendor/web/vue.runtime.common.js +5 -0
  154. package/vendor/weex-template-compiler/README.md +3 -0
  155. package/vendor/weex-template-compiler/build.js +4824 -0
  156. package/vendor/weex-template-compiler/index.js +17 -0
  157. package/vendor/weex-template-compiler/package.json +25 -0
@@ -0,0 +1,47 @@
1
+ import type { FalconUserConfig } from '../types.js';
2
+ export interface NativeTarget {
3
+ name: string;
4
+ platform: string;
5
+ sourceDir: string;
6
+ buildDir: string;
7
+ outFile: string;
8
+ deps: string;
9
+ cross: string;
10
+ toolchainRoot: string | null;
11
+ maxGlibc: string;
12
+ elfClass: 32 | 64;
13
+ arch: string;
14
+ archFlags: string | null;
15
+ allowedNeeded: string[] | null;
16
+ cmakeArgs: string[];
17
+ strip: boolean;
18
+ }
19
+ export declare function loadBuildEnv(file: string): Record<string, string>;
20
+ export declare function resolveNativeTarget(root: string, config: FalconUserConfig, targetName: string, env?: Record<string, string>): NativeTarget;
21
+ export interface VerifyResult {
22
+ ok: boolean;
23
+ messages: string[];
24
+ warnings: string[];
25
+ size: number;
26
+ maxGlibc: string | null;
27
+ }
28
+ export interface VerifyOptions {
29
+ strict?: boolean;
30
+ }
31
+ export declare function verifyArtifact(file: string, target: NativeTarget, options?: VerifyOptions): VerifyResult;
32
+ export interface NativeBuildOptions {
33
+ root: string;
34
+ config: FalconUserConfig;
35
+ target: string;
36
+ skipVerify?: boolean;
37
+ strict?: boolean;
38
+ silent?: boolean;
39
+ }
40
+ export declare function runNativeBuild(opts: NativeBuildOptions): Promise<number>;
41
+ export declare function runNativeVerify(opts: {
42
+ root: string;
43
+ config: FalconUserConfig;
44
+ target: string;
45
+ file?: string;
46
+ strict?: boolean;
47
+ }): Promise<number>;
@@ -0,0 +1,407 @@
1
+ import fs from 'node:fs';
2
+ import path from 'node:path';
3
+ import { spawn } from 'node:child_process';
4
+ import { exceedsGlibc, readElfSummary } from './elf.js';
5
+ import { log } from '../util/log.js';
6
+ import { detectWsl, shellQuote, toWslPath } from './wsl.js';
7
+ const DEFAULTS = {
8
+ x7: {
9
+ platform: 'x7',
10
+ maxGlibc: '2.27',
11
+ elfClass: 64,
12
+ arch: 'aarch64',
13
+ cross: 'aarch64-linux-gnu-'
14
+ },
15
+ rk: {
16
+ platform: 'rk',
17
+ maxGlibc: '2.27',
18
+ elfClass: 64,
19
+ arch: 'aarch64',
20
+ cross: 'aarch64-linux-gnu-'
21
+ },
22
+ cvi: {
23
+ platform: 'cvi',
24
+ maxGlibc: '2.23',
25
+ elfClass: 32,
26
+ arch: 'arm',
27
+ cross: 'arm-linux-gnueabihf-'
28
+ }
29
+ };
30
+ export function loadBuildEnv(file) {
31
+ const out = {};
32
+ if (!fs.existsSync(file))
33
+ return out;
34
+ for (const raw of fs.readFileSync(file, 'utf8').split('\n')) {
35
+ let line = raw.trim();
36
+ if (!line || line.startsWith('#'))
37
+ continue;
38
+ if (line.startsWith('export '))
39
+ line = line.slice(7).trim();
40
+ const eq = line.indexOf('=');
41
+ if (eq <= 0)
42
+ continue;
43
+ const key = line.slice(0, eq).trim();
44
+ let value = line.slice(eq + 1).trim();
45
+ if ((value.startsWith('"') && value.endsWith('"')) ||
46
+ (value.startsWith("'") && value.endsWith("'"))) {
47
+ value = value.slice(1, -1);
48
+ }
49
+ if (key)
50
+ out[key] = value;
51
+ }
52
+ return out;
53
+ }
54
+ function envValue(env, keys, fallback) {
55
+ for (const key of keys) {
56
+ const fromEnv = process.env[key];
57
+ if (fromEnv !== undefined && fromEnv !== '')
58
+ return fromEnv;
59
+ const fromFile = env[key];
60
+ if (fromFile !== undefined && fromFile !== '')
61
+ return fromFile;
62
+ }
63
+ return fallback;
64
+ }
65
+ export function resolveNativeTarget(root, config, targetName, env = {}) {
66
+ const native = config.native ?? {};
67
+ const declared = native.targets?.[targetName] ?? {};
68
+ const base = DEFAULTS[targetName] ?? DEFAULTS.x7;
69
+ const nativeDir = path.resolve(root, native.dir ?? 'jsapi');
70
+ const upper = targetName.toUpperCase();
71
+ const toolchainRoot = envValue(env, ['LOLI_' + upper + '_TOOLCHAIN_ROOT', 'BOOTLIN_TOOLCHAIN'], declared.toolchainRoot) ?? null;
72
+ const cross = envValue(env, ['LOLI_' + upper + '_CROSS_PREFIX', 'CROSS_TOOLCHAIN_PREFIX'], declared.cross ?? base.cross) ?? 'aarch64-linux-gnu-';
73
+ const deps = path.resolve(root, envValue(env, ['LOLI_' + upper + '_DEPS_ROOT', 'LOLI_DEPS_ROOT', 'DEPS_ROOT'], declared.deps ?? 'lib') ?? 'lib');
74
+ const fullCross = toolchainRoot
75
+ ? path.join(toolchainRoot, 'bin', 'aarch64-buildroot-linux-gnu-')
76
+ : cross;
77
+ return {
78
+ name: targetName,
79
+ platform: declared.platform ?? base.platform ?? targetName,
80
+ sourceDir: nativeDir,
81
+ buildDir: path.resolve(root, declared.buildDir ?? path.join(nativeDir, 'build', targetName)),
82
+ outFile: path.resolve(root, declared.out ?? path.join(nativeDir, 'produce', 'libjsapi_bridge_' + targetName + '.so')),
83
+ deps: declared.deps
84
+ ? deps
85
+ : path.resolve(nativeDir, path.basename(deps) === 'lib' ? 'lib' : deps),
86
+ cross: fullCross,
87
+ toolchainRoot,
88
+ maxGlibc: declared.maxGlibc ?? base.maxGlibc ?? '2.27',
89
+ elfClass: (declared.elfClass ?? base.elfClass ?? 64),
90
+ arch: declared.arch ?? base.arch ?? 'aarch64',
91
+ archFlags: declared.archFlags ?? null,
92
+ allowedNeeded: declared.allowedNeeded ?? null,
93
+ cmakeArgs: declared.cmakeArgs ?? [],
94
+ strip: declared.strip !== false
95
+ };
96
+ }
97
+ function runStreaming(command, args, cwd, useShell = process.platform === 'win32') {
98
+ return new Promise((resolve) => {
99
+ const child = spawn(command, args, { cwd, stdio: 'inherit', shell: useShell });
100
+ child.on('close', (code) => resolve(code ?? 1));
101
+ child.on('error', () => resolve(1));
102
+ });
103
+ }
104
+ function commandAvailable(command) {
105
+ const extensions = process.platform === 'win32'
106
+ ? (process.env.PATHEXT ?? '.EXE;.CMD;.BAT').split(';').filter(Boolean)
107
+ : [''];
108
+ const dirs = (process.env.PATH ?? '').split(path.delimiter).filter(Boolean);
109
+ for (const dir of dirs) {
110
+ for (const ext of extensions) {
111
+ const file = path.join(dir, command + (dir.endsWith(path.sep) ? '' : '') + ext);
112
+ try {
113
+ if (fs.existsSync(file))
114
+ return true;
115
+ }
116
+ catch (error) { }
117
+ }
118
+ }
119
+ return false;
120
+ }
121
+ export function verifyArtifact(file, target, options = {}) {
122
+ const messages = [];
123
+ const warnings = [];
124
+ if (!fs.existsSync(file)) {
125
+ return { ok: false, messages: ['产物不存在: ' + file], warnings, size: 0, maxGlibc: null };
126
+ }
127
+ const buffer = fs.readFileSync(file);
128
+ let summary;
129
+ try {
130
+ summary = readElfSummary(buffer);
131
+ }
132
+ catch (error) {
133
+ return {
134
+ ok: false,
135
+ messages: [String(error.message)],
136
+ warnings,
137
+ size: buffer.length,
138
+ maxGlibc: null
139
+ };
140
+ }
141
+ let ok = true;
142
+ if (summary.elfClass !== target.elfClass) {
143
+ ok = false;
144
+ messages.push('ELF 位数不符: 期望 ' + target.elfClass + ',实际 ' + summary.elfClass);
145
+ }
146
+ if (summary.arch !== target.arch) {
147
+ ok = false;
148
+ messages.push('架构不符: 期望 ' + target.arch + ',实际 ' + summary.arch);
149
+ }
150
+ if (exceedsGlibc(summary.maxGlibc, target.maxGlibc)) {
151
+ ok = false;
152
+ messages.push('GLIBC 版本超限: ' + summary.maxGlibc + ' > ' + target.maxGlibc);
153
+ }
154
+ messages.push('架构 ' +
155
+ summary.arch +
156
+ ' / ELF' +
157
+ summary.elfClass +
158
+ ' / GLIBC ' +
159
+ (summary.maxGlibc ?? 'n/a') +
160
+ ' (上限 ' +
161
+ target.maxGlibc +
162
+ ')' +
163
+ ' / ' +
164
+ (summary.size / 1048576).toFixed(2) +
165
+ ' MB');
166
+ messages.push('依赖: ' + (summary.needed.length ? summary.needed.join(', ') : '无'));
167
+ if (summary.soname)
168
+ messages.push('SONAME: ' + summary.soname);
169
+ if (summary.undefinedSymbols.length > 0) {
170
+ const shown = summary.undefinedSymbols.slice(0, 8).join(', ');
171
+ const more = summary.undefinedSymbols.length > 8
172
+ ? ' 等 ' + summary.undefinedSymbols.length + ' 个'
173
+ : '';
174
+ messages.push('未解析符号: ' + shown + more);
175
+ }
176
+ messages.push('导出符号: ' + (summary.exports.length ? summary.exports.slice(0, 8).join(', ') : '无'));
177
+ const cxx = summary.needed.filter((name) => name.indexOf('libstdc++') === 0);
178
+ if (cxx.length > 0)
179
+ warnings.push('依赖 C++ 运行库: ' + cxx.join(', '));
180
+ if (target.allowedNeeded) {
181
+ const extra = summary.needed.filter((name) => !target.allowedNeeded.includes(name));
182
+ if (extra.length > 0)
183
+ warnings.push('不在允许列表的依赖: ' + extra.join(', '));
184
+ }
185
+ if (!summary.exports.includes('custom_init_jsapis')) {
186
+ warnings.push('未导出 custom_init_jsapis');
187
+ }
188
+ if (summary.exports.length > 20) {
189
+ warnings.push('导出符号过多 (' + summary.exports.length + '),检查 -fvisibility=hidden');
190
+ }
191
+ if (warnings.length > 0) {
192
+ const prefix = options.strict ? '[strict] ' : '提示: ';
193
+ for (const warning of warnings)
194
+ messages.push(prefix + warning);
195
+ if (options.strict)
196
+ ok = false;
197
+ }
198
+ return { ok, messages, warnings, size: summary.size, maxGlibc: summary.maxGlibc };
199
+ }
200
+ export async function runNativeBuild(opts) {
201
+ const native = opts.config.native ?? {};
202
+ const envFile = path.resolve(opts.root, native.buildEnv ?? 'scripts/build.env');
203
+ const env = loadBuildEnv(envFile);
204
+ const target = resolveNativeTarget(opts.root, opts.config, opts.target, env);
205
+ if (!fs.existsSync(target.sourceDir)) {
206
+ log.error('找不到 native 源码目录: ' + target.sourceDir);
207
+ return 2;
208
+ }
209
+ const wsl = detectWsl(target.toolchainRoot);
210
+ if (wsl && process.platform === 'win32') {
211
+ return runNativeBuildInWsl({
212
+ root: opts.root,
213
+ config: opts.config,
214
+ target,
215
+ wsl,
216
+ skipVerify: opts.skipVerify,
217
+ strict: opts.strict
218
+ });
219
+ }
220
+ const compiler = target.cross + 'gcc';
221
+ if (!commandAvailable(compiler)) {
222
+ log.error('找不到交叉编译器: ' + compiler);
223
+ log.error('在 catchword.config.ts 的 native.targets 里配置 toolchainRoot,或设置环境变量');
224
+ log.error(' LOLI_' + opts.target.toUpperCase() + '_TOOLCHAIN_ROOT / CROSS_TOOLCHAIN_PREFIX');
225
+ return 2;
226
+ }
227
+ const cmakeArgs = [
228
+ '-S',
229
+ target.sourceDir,
230
+ '-B',
231
+ target.buildDir,
232
+ '-DLOLI_PLATFORM=' + target.platform,
233
+ '-DLOLI_CROSS_TOOLCHAIN_PREFIX=' + target.cross,
234
+ '-DLOLI_DEPS_ROOT=' + target.deps,
235
+ '-DCMAKE_BUILD_TYPE=Release',
236
+ '-DLOLI_STATIC_LIBSTDCXX=ON',
237
+ ...target.cmakeArgs
238
+ ];
239
+ if (target.archFlags) {
240
+ cmakeArgs.push('-DCMAKE_C_FLAGS=' + target.archFlags, '-DCMAKE_CXX_FLAGS=' + target.archFlags);
241
+ }
242
+ const toolchainBin = target.toolchainRoot ? path.join(target.toolchainRoot, 'bin') : null;
243
+ if (toolchainBin) {
244
+ process.env.PATH = toolchainBin + path.delimiter + (process.env.PATH ?? '');
245
+ process.env.COMPILER_PATH = toolchainBin;
246
+ }
247
+ log.step('native 配置 ' + target.name);
248
+ if ((await runStreaming('cmake', cmakeArgs, opts.root)) !== 0) {
249
+ log.error('cmake 配置失败');
250
+ return 3;
251
+ }
252
+ log.step('native 编译 ' + target.name);
253
+ const jobs = String(Math.max(1, require('node:os').cpus().length));
254
+ if ((await runStreaming('cmake', ['--build', target.buildDir, '-j' + jobs], opts.root)) !== 0) {
255
+ log.error('cmake 编译失败');
256
+ return 4;
257
+ }
258
+ const built = path.join(target.buildDir, 'libjsapi_bridge.so');
259
+ if (!fs.existsSync(built)) {
260
+ log.error('编译产物不存在: ' + built);
261
+ return 4;
262
+ }
263
+ fs.mkdirSync(path.dirname(target.outFile), { recursive: true });
264
+ if (target.strip) {
265
+ const strip = target.cross + 'strip';
266
+ let stripped = false;
267
+ if (commandAvailable(strip)) {
268
+ stripped =
269
+ (await runStreaming(strip, ['--strip-unneeded', built, '-o', target.outFile], opts.root)) === 0;
270
+ }
271
+ if (!stripped) {
272
+ if (target.strip)
273
+ log.warn('strip 未成功,复制未裁剪产物');
274
+ fs.copyFileSync(built, target.outFile);
275
+ }
276
+ }
277
+ else {
278
+ fs.copyFileSync(built, target.outFile);
279
+ }
280
+ log.info('产物: ' + target.outFile);
281
+ if (!opts.skipVerify) {
282
+ const result = verifyArtifact(target.outFile, target, { strict: opts.strict });
283
+ for (const line of result.messages) {
284
+ if (result.ok)
285
+ log.info(' ' + line);
286
+ else
287
+ log.error(' ' + line);
288
+ }
289
+ if (!result.ok)
290
+ return 5;
291
+ }
292
+ if (native.copyTo) {
293
+ const dest = path.resolve(opts.root, native.copyTo);
294
+ fs.mkdirSync(path.dirname(dest), { recursive: true });
295
+ fs.copyFileSync(target.outFile, dest);
296
+ log.success('已同步到 ' + dest);
297
+ }
298
+ log.success('native 构建完成: ' + target.name);
299
+ return 0;
300
+ }
301
+ export async function runNativeVerify(opts) {
302
+ const native = opts.config.native ?? {};
303
+ const envFile = path.resolve(opts.root, native.buildEnv ?? 'scripts/build.env');
304
+ const target = resolveNativeTarget(opts.root, opts.config, opts.target, loadBuildEnv(envFile));
305
+ const file = opts.file ? path.resolve(opts.root, opts.file) : target.outFile;
306
+ const result = verifyArtifact(file, target, { strict: opts.strict });
307
+ for (const line of result.messages) {
308
+ if (result.ok)
309
+ log.info(' ' + line);
310
+ else
311
+ log.error(' ' + line);
312
+ }
313
+ return result.ok ? 0 : 5;
314
+ }
315
+ async function runNativeBuildInWsl(input) {
316
+ const { target, wsl } = input;
317
+ const projectWsl = toWslPath(input.root, wsl.distro);
318
+ const sourceWsl = toWslPath(target.sourceDir, wsl.distro);
319
+ const buildWsl = toWslPath(target.buildDir, wsl.distro);
320
+ const outWsl = toWslPath(target.outFile, wsl.distro);
321
+ const depsWsl = toWslPath(target.deps, wsl.distro);
322
+ const toolchainWsl = '/' + wsl.root.split(String.fromCharCode(92)).join('/');
323
+ const cross = target.cross || 'aarch64-linux-gnu-';
324
+ const toolchainBin = toolchainWsl + '/bin';
325
+ const toolchainCross = toolchainBin + '/aarch64-buildroot-linux-gnu-';
326
+ log.info('检测到 WSL 工具链,构建将在 WSL 内执行: ' + wsl.distro);
327
+ const configure = [
328
+ 'cmake',
329
+ '-S ' + shellQuote(sourceWsl),
330
+ '-B ' + shellQuote(buildWsl),
331
+ '-DLOLI_PLATFORM=' + target.platform,
332
+ '-DLOLI_CROSS_TOOLCHAIN_PREFIX=' + shellQuote(toolchainCross),
333
+ '-DLOLI_DEPS_ROOT=' + shellQuote(depsWsl),
334
+ '-DCMAKE_BUILD_TYPE=Release',
335
+ '-DLOLI_STATIC_LIBSTDCXX=ON',
336
+ ...target.cmakeArgs
337
+ ];
338
+ if (target.archFlags) {
339
+ configure.push('-DCMAKE_C_FLAGS=' + shellQuote(target.archFlags), '-DCMAKE_CXX_FLAGS=' + shellQuote(target.archFlags));
340
+ }
341
+ const logFile = path.join(input.root, '.falcon_tmp', 'native-build-' + target.name + '.log');
342
+ fs.mkdirSync(path.dirname(logFile), { recursive: true });
343
+ const logWsl = toWslPath(logFile, wsl.distro);
344
+ const script = [
345
+ 'set -eo pipefail',
346
+ 'exec > >(tee -a ' + shellQuote(logWsl) + ') 2>&1',
347
+ 'export LC_ALL=C',
348
+ 'STDBUF=""',
349
+ 'command -v stdbuf >/dev/null 2>&1 && STDBUF="stdbuf -oL -eL"',
350
+ 'step() { echo "[catchword] $1 $(date +%H:%M:%S)"; }',
351
+ 'echo "[catchword] start ' + target.name + ' $(date +%H:%M:%S)"',
352
+ 'mkdir -p ' + shellQuote(buildWsl) + ' ' + shellQuote(path.posix.dirname(outWsl)),
353
+ 'export PATH=' + shellQuote(toolchainBin) + ':"$PATH"',
354
+ 'export COMPILER_PATH=' + shellQuote(toolchainBin),
355
+ 'step configure',
356
+ '$STDBUF ' + configure.join(' ' + String.fromCharCode(92) + String.fromCharCode(10) + ' '),
357
+ 'step build',
358
+ '$STDBUF cmake --build ' + shellQuote(buildWsl) + ' -j"$(nproc)" --verbose',
359
+ 'step strip',
360
+ shellQuote(toolchainCross + 'strip') +
361
+ ' --strip-unneeded ' +
362
+ shellQuote(buildWsl + '/libjsapi_bridge.so') +
363
+ ' -o ' +
364
+ shellQuote(outWsl),
365
+ 'step done',
366
+ 'ls -l ' + shellQuote(outWsl)
367
+ ].join(String.fromCharCode(10));
368
+ log.step('native 构建 (WSL ' + wsl.distro + ')');
369
+ const scriptDir = path.join(input.root, '.falcon_tmp');
370
+ fs.mkdirSync(scriptDir, { recursive: true });
371
+ const scriptFile = path.join(scriptDir, 'native-build-' + target.name + '.sh');
372
+ const CRLF = new RegExp(String.fromCharCode(13) + String.fromCharCode(10), 'g');
373
+ fs.writeFileSync(scriptFile, script.replace(CRLF, String.fromCharCode(10)), { mode: 0o755 });
374
+ const scriptWsl = toWslPath(scriptFile, wsl.distro);
375
+ const startedAt = Date.now();
376
+ const code = await runStreaming('wsl', ['-d', wsl.distro, '-e', 'bash', '-lc', 'export LC_ALL=C; exec bash ' + scriptWsl], input.root, false);
377
+ log.info('耗时 ' + Math.round((Date.now() - startedAt) / 1000) + 's');
378
+ void cross;
379
+ if (code !== 0) {
380
+ log.error('WSL 内构建失败(exit ' + code + ')');
381
+ return 4;
382
+ }
383
+ if (!fs.existsSync(target.outFile)) {
384
+ log.error('构建完成但产物不存在: ' + target.outFile);
385
+ return 4;
386
+ }
387
+ if (!input.skipVerify) {
388
+ const result = verifyArtifact(target.outFile, target, { strict: input.strict });
389
+ for (const line of result.messages) {
390
+ if (result.ok)
391
+ log.info(' ' + line);
392
+ else
393
+ log.error(' ' + line);
394
+ }
395
+ if (!result.ok)
396
+ return 5;
397
+ }
398
+ const copyTo = input.config.native?.copyTo;
399
+ if (copyTo) {
400
+ const dest = path.resolve(input.root, copyTo);
401
+ fs.mkdirSync(path.dirname(dest), { recursive: true });
402
+ fs.copyFileSync(target.outFile, dest);
403
+ log.success('已同步到 ' + dest);
404
+ }
405
+ log.success('native 构建完成: ' + target.name);
406
+ return 0;
407
+ }
@@ -0,0 +1,15 @@
1
+ export interface ElfSummary {
2
+ elfClass: 32 | 64;
3
+ machine: number;
4
+ arch: string;
5
+ glibcVersions: string[];
6
+ maxGlibc: string | null;
7
+ needed: string[];
8
+ soname: string | null;
9
+ exports: string[];
10
+ undefinedSymbols: string[];
11
+ size: number;
12
+ }
13
+ export declare function compareVersion(a: string, b: string): number;
14
+ export declare function readElfSummary(buffer: Buffer): ElfSummary;
15
+ export declare function exceedsGlibc(maxGlibc: string | null, limit: string): boolean;
@@ -0,0 +1,200 @@
1
+ const MACHINES = {
2
+ 3: 'i386',
3
+ 8: 'mips',
4
+ 20: 'ppc',
5
+ 40: 'arm',
6
+ 62: 'x86_64',
7
+ 183: 'aarch64'
8
+ };
9
+ const SHT_GNU_VERDEF = 0x6ffffffd;
10
+ const SHT_GNU_VERNEED = 0x6ffffffe;
11
+ const SHT_DYNAMIC = 6;
12
+ const SHT_DYNSYM = 11;
13
+ function readCString(table, offset) {
14
+ if (offset < 0 || offset >= table.length)
15
+ return '';
16
+ let end = offset;
17
+ while (end < table.length && table[end] !== 0)
18
+ end++;
19
+ return table.toString('utf8', offset, end);
20
+ }
21
+ export function compareVersion(a, b) {
22
+ const left = String(a)
23
+ .split('.')
24
+ .map((part) => parseInt(part, 10) || 0);
25
+ const right = String(b)
26
+ .split('.')
27
+ .map((part) => parseInt(part, 10) || 0);
28
+ const size = Math.max(left.length, right.length);
29
+ for (let i = 0; i < size; i++) {
30
+ const x = left[i] ?? 0;
31
+ const y = right[i] ?? 0;
32
+ if (x !== y)
33
+ return x > y ? 1 : -1;
34
+ }
35
+ return 0;
36
+ }
37
+ function parseSections(buffer, is64) {
38
+ const shoff = is64 ? Number(buffer.readBigUInt64LE(40)) : buffer.readUInt32LE(32);
39
+ const shentsize = buffer.readUInt16LE(is64 ? 58 : 46);
40
+ const shnum = buffer.readUInt16LE(is64 ? 60 : 48);
41
+ const sections = [];
42
+ if (!shoff || !shentsize)
43
+ return sections;
44
+ for (let i = 0; i < shnum; i++) {
45
+ const base = shoff + i * shentsize;
46
+ if (base + shentsize > buffer.length)
47
+ break;
48
+ sections.push({
49
+ type: buffer.readUInt32LE(base + 4),
50
+ offset: is64
51
+ ? Number(buffer.readBigUInt64LE(base + 24))
52
+ : buffer.readUInt32LE(base + 16),
53
+ size: is64 ? Number(buffer.readBigUInt64LE(base + 32)) : buffer.readUInt32LE(base + 20),
54
+ link: buffer.readUInt32LE(base + (is64 ? 40 : 24)),
55
+ entsize: is64
56
+ ? Number(buffer.readBigUInt64LE(base + 56))
57
+ : buffer.readUInt32LE(base + 36)
58
+ });
59
+ }
60
+ return sections;
61
+ }
62
+ function parseDynamic(buffer, is64, sections) {
63
+ const section = sections.find((item) => item.type === SHT_DYNAMIC);
64
+ if (!section)
65
+ return { needed: [], soname: null };
66
+ const dynstr = sections[section.link];
67
+ if (!dynstr)
68
+ return { needed: [], soname: null };
69
+ const strtab = buffer.subarray(dynstr.offset, dynstr.offset + dynstr.size);
70
+ const entrySize = section.entsize || (is64 ? 16 : 8);
71
+ const needed = [];
72
+ let soname = null;
73
+ for (let cursor = 0; cursor + entrySize <= section.size; cursor += entrySize) {
74
+ const base = section.offset + cursor;
75
+ const tag = is64 ? Number(buffer.readBigUInt64LE(base)) : buffer.readUInt32LE(base);
76
+ if (tag === 0)
77
+ break;
78
+ const value = is64
79
+ ? Number(buffer.readBigUInt64LE(base + 8))
80
+ : buffer.readUInt32LE(base + 4);
81
+ if (tag === 1)
82
+ needed.push(readCString(strtab, value));
83
+ else if (tag === 14)
84
+ soname = readCString(strtab, value);
85
+ }
86
+ return { needed, soname };
87
+ }
88
+ function parseDynsym(buffer, is64, sections) {
89
+ const section = sections.find((item) => item.type === SHT_DYNSYM);
90
+ if (!section)
91
+ return { exports: [], undefinedSymbols: [] };
92
+ const dynstr = sections[section.link];
93
+ if (!dynstr)
94
+ return { exports: [], undefinedSymbols: [] };
95
+ const strtab = buffer.subarray(dynstr.offset, dynstr.offset + dynstr.size);
96
+ const entrySize = section.entsize || (is64 ? 24 : 16);
97
+ const exports = [];
98
+ const undefinedSymbols = [];
99
+ for (let cursor = 0; cursor + entrySize <= section.size; cursor += entrySize) {
100
+ const base = section.offset + cursor;
101
+ const name = readCString(strtab, buffer.readUInt32LE(base));
102
+ if (!name)
103
+ continue;
104
+ const info = buffer[base + (is64 ? 4 : 12)];
105
+ const bind = info >> 4;
106
+ if (bind !== 1 && bind !== 2)
107
+ continue;
108
+ const shndx = buffer.readUInt16LE(base + (is64 ? 6 : 14));
109
+ if (shndx === 0)
110
+ undefinedSymbols.push(name);
111
+ else
112
+ exports.push(name);
113
+ }
114
+ return {
115
+ exports: [...new Set(exports)].sort(),
116
+ undefinedSymbols: [...new Set(undefinedSymbols)].sort()
117
+ };
118
+ }
119
+ export function readElfSummary(buffer) {
120
+ if (buffer.length < 52)
121
+ throw new Error('文件太小,不是 ELF');
122
+ if (buffer[0] !== 0x7f || buffer[1] !== 0x45 || buffer[2] !== 0x4c || buffer[3] !== 0x46) {
123
+ throw new Error('不是 ELF 文件');
124
+ }
125
+ const is64 = buffer[4] === 2;
126
+ const elfClass = is64 ? 64 : 32;
127
+ const machine = buffer.readUInt16LE(18);
128
+ const sections = parseSections(buffer, is64);
129
+ const names = [];
130
+ for (const section of sections) {
131
+ if (section.type !== SHT_GNU_VERNEED && section.type !== SHT_GNU_VERDEF)
132
+ continue;
133
+ const dynstr = sections[section.link];
134
+ if (!dynstr)
135
+ continue;
136
+ const strtab = buffer.subarray(dynstr.offset, dynstr.offset + dynstr.size);
137
+ if (section.type === SHT_GNU_VERNEED) {
138
+ let cursor = 0;
139
+ while (cursor + 16 <= section.size) {
140
+ const base = section.offset + cursor;
141
+ const vnAux = buffer.readUInt32LE(base + 8);
142
+ const vnNext = buffer.readUInt32LE(base + 12);
143
+ let aux = base + vnAux;
144
+ if (vnAux) {
145
+ while (true) {
146
+ const nameOffset = buffer.readUInt32LE(aux + 8);
147
+ const name = readCString(strtab, nameOffset);
148
+ if (name)
149
+ names.push(name);
150
+ const next = buffer.readUInt32LE(aux + 12);
151
+ if (!next)
152
+ break;
153
+ aux += next;
154
+ }
155
+ }
156
+ if (!vnNext)
157
+ break;
158
+ cursor += vnNext;
159
+ }
160
+ continue;
161
+ }
162
+ let cursor = 0;
163
+ while (cursor + 20 <= section.size) {
164
+ const base = section.offset + cursor;
165
+ const vdAux = buffer.readUInt32LE(base + 12);
166
+ const vdNext = buffer.readUInt32LE(base + 16);
167
+ if (vdAux) {
168
+ const nameOffset = buffer.readUInt32LE(base + vdAux);
169
+ const name = readCString(strtab, nameOffset);
170
+ if (name)
171
+ names.push(name);
172
+ }
173
+ if (!vdNext)
174
+ break;
175
+ cursor += vdNext;
176
+ }
177
+ }
178
+ const glibcVersions = [
179
+ ...new Set(names.filter((name) => name.indexOf('GLIBC_') === 0).map((name) => name.slice(6)))
180
+ ].sort(compareVersion);
181
+ const dynamic = parseDynamic(buffer, is64, sections);
182
+ const dynsym = parseDynsym(buffer, is64, sections);
183
+ return {
184
+ elfClass,
185
+ machine,
186
+ arch: MACHINES[machine] ?? 'machine-' + machine,
187
+ glibcVersions,
188
+ maxGlibc: glibcVersions.length ? glibcVersions[glibcVersions.length - 1] : null,
189
+ needed: dynamic.needed,
190
+ soname: dynamic.soname,
191
+ exports: dynsym.exports,
192
+ undefinedSymbols: dynsym.undefinedSymbols,
193
+ size: buffer.length
194
+ };
195
+ }
196
+ export function exceedsGlibc(maxGlibc, limit) {
197
+ if (!maxGlibc)
198
+ return false;
199
+ return compareVersion(maxGlibc, limit) > 0;
200
+ }
@@ -0,0 +1,9 @@
1
+ import type { NativeApiFile } from './api.js';
2
+ export interface NativeApiOverrides {
3
+ types?: Record<string, string[]>;
4
+ methods?: Record<string, Record<string, string>>;
5
+ globals?: string[];
6
+ }
7
+ export declare function readOverrides(file: string): NativeApiOverrides;
8
+ export declare function generateTypes(api: NativeApiFile, overrides?: NativeApiOverrides): string;
9
+ export declare function writeTypesFile(file: string, content: string): void;