rcompiler 0.0.0-stage → 2.1.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.
package/src/targets.js ADDED
@@ -0,0 +1,129 @@
1
+ // rCompiler V2 — 目标三元组 / 后端能力描述
2
+ //
3
+ // 设计原则:
4
+ // - Cargo 的 target triple 只决定代码生成目标;项目能否运行还取决于
5
+ // std/alloc、时间、输入、音频、UI 等能力。编译器按 backend 选择运行时。
6
+ // - native:沿用现有 skia/winit/softbuffer/rodio 桌面运行时
7
+ // - web:wasm32-unknown-unknown + scratch-render JS 宿主
8
+ // - wasi:无桌面 std 目标,仅纯计算(不含舞台/声音)
9
+ // - headless:其他无桌面原生目标,仅纯计算
10
+ //
11
+ // 该模块不依赖编译器其它模块,CLI/analysis/emit 都可安全引用。
12
+
13
+ 'use strict';
14
+ const { execFileSync } = require('child_process');
15
+
16
+ const DESKTOP_OS = /(-linux-|-linux$|-windows-|-apple-darwin)/i;
17
+
18
+ let HOST_TARGET = null;
19
+ function hostTarget() {
20
+ if (HOST_TARGET) return HOST_TARGET;
21
+ try {
22
+ const out = execFileSync('rustc', ['-vV'], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] });
23
+ const m = /^host:\s*(\S+)$/m.exec(out);
24
+ if (m) return (HOST_TARGET = m[1]);
25
+ } catch (_) { /* 没装 rustc 时回退到常见宿主映射 */ }
26
+ const arch = process.arch === 'x64' ? 'x86_64'
27
+ : process.arch === 'arm64' ? 'aarch64'
28
+ : process.arch === 'ia32' ? 'i686' : process.arch;
29
+ if (process.platform === 'win32') return (HOST_TARGET = `${arch}-pc-windows-msvc`);
30
+ if (process.platform === 'darwin') return (HOST_TARGET = `${arch}-apple-darwin`);
31
+ return (HOST_TARGET = `${arch}-unknown-linux-gnu`);
32
+ }
33
+
34
+ function normalizeTriple(v) {
35
+ const s = String(v == null ? '' : v).trim();
36
+ if (!s) return hostTarget();
37
+ // 常用别名:wasm → wasm32-unknown-unknown;wasi → wasm32-wasip1
38
+ if (s === 'wasm' || s === 'web' || s === 'wasm32') return 'wasm32-unknown-unknown';
39
+ if (s === 'wasi') return 'wasm32-wasip1';
40
+ if (s === 'native' || s === 'host' || s === 'desktop') return hostTarget();
41
+ return s;
42
+ }
43
+
44
+ function isWasmTriple(t) {
45
+ return /^wasm32(?:-|$)/.test(t);
46
+ }
47
+
48
+ function resolveTarget(name) {
49
+ const triple = normalizeTriple(name);
50
+ const host = hostTarget();
51
+ const wasm = isWasmTriple(triple);
52
+ const wasi = wasm && /(?:-wasi|wasi-)/.test(triple);
53
+ const windows = /-windows-/.test(triple);
54
+ const explicit = String(name == null ? '' : name).trim() !== '';
55
+
56
+ let kind, backend, canStage, canSound, canRun, note;
57
+ if (wasi) {
58
+ kind = 'wasi';
59
+ backend = 'native'; // std 代码生成;仅禁用舞台/声音/输入
60
+ canStage = false;
61
+ canSound = false;
62
+ canRun = true; // wasmtime/wasmer 可运行
63
+ note = 'WASI 有 std 但无桌面 UI;仅支持无舞台/无声音项目';
64
+ } else if (wasm) {
65
+ kind = 'web';
66
+ backend = 'web';
67
+ canStage = true;
68
+ canSound = true;
69
+ canRun = false; // 需要浏览器/宿主
70
+ note = 'WASM 舞台由 scratch-render JS 宿主渲染;纯计算由 wasm-bindgen 启动';
71
+ } else if (DESKTOP_OS.test(triple) || triple === host) {
72
+ kind = 'desktop';
73
+ backend = 'native';
74
+ canStage = true;
75
+ canSound = true;
76
+ canRun = true;
77
+ note = '';
78
+ } else {
79
+ kind = 'headless';
80
+ backend = 'native';
81
+ canStage = false;
82
+ canSound = false;
83
+ canRun = true; // 若目标有 std,可运行;由 cargo 决定
84
+ note = '无桌面目标:仅支持纯计算(无舞台/声音)';
85
+ }
86
+
87
+ const target = {
88
+ triple,
89
+ host,
90
+ explicit,
91
+ isHost: triple === host,
92
+ kind,
93
+ backend,
94
+ isWasm: wasm,
95
+ isWasi: wasi,
96
+ isWindows: windows,
97
+ canStage,
98
+ canSound,
99
+ canRun,
100
+ artifactExt: wasm ? '.wasm' : (windows ? '.exe' : ''),
101
+ // cargo --target 后的产物子目录;未显式指定 target 时保持旧路径 target/{debug,release}
102
+ cargoTargetDir: explicit ? triple : null,
103
+ note
104
+ };
105
+ return target;
106
+ }
107
+
108
+ // 产物路径(相对项目目录)
109
+ function artifactPath(projectDir, target, release, name) {
110
+ const profile = release ? 'release' : 'debug';
111
+ const base = target.cargoTargetDir
112
+ ? `${target.triple}/${profile}`
113
+ : profile;
114
+ return require('path').join(projectDir, 'target', base, `${name}${target.artifactExt}`);
115
+ }
116
+
117
+ // 展示用说明
118
+ function describeTarget(target) {
119
+ return `${target.triple}(${target.kind}/${target.backend})`;
120
+ }
121
+
122
+ module.exports = {
123
+ hostTarget,
124
+ normalizeTriple,
125
+ resolveTarget,
126
+ artifactPath,
127
+ describeTarget,
128
+ isWasmTriple
129
+ };