This repository was archived by the owner on Jul 7, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathcli.js
More file actions
248 lines (214 loc) · 7.56 KB
/
Copy pathcli.js
File metadata and controls
248 lines (214 loc) · 7.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env node
// JSBin 命令行编译工具
import { existsSync, readFileSync, writeFileSync, statSync, mkdirSync } from "fs";
import { dirname, basename, join, resolve } from "path";
import { fileURLToPath } from "url";
import { Compiler } from "./compiler/index.js";
import { detectPlatform, resolveTarget, listTargets } from "./compiler/core/platform.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
console.log(`DEBUG: cli.js starting, Compiler path: ${resolve(__dirname, "./compiler/index.js")}`);
function printUsage() {
console.log(`
JSBin Compiler - JavaScript to Native
Usage:
jsbin <input.js> [options]
Options:
-o, --output <file> Output file path (default: input basename + target)
--os <os> Target OS: linux, macos, windows
--arch <arch> Target architecture: arm64, x64
--target <target> Target platform (e.g., macos-arm64, linux-x64)
--shared Build shared library (.dylib/.so/.dll)
--static Build static library (.a/.lib)
--no-jslib Don't generate .jslib declaration file
--export <name> Export symbol (can be used multiple times)
--lib <name> Link with library
--lib-path <path> Add library search path
--list-targets List all supported targets
--debug Enable debug output
-h, --help Show this help
Examples:
jsbin hello.js # Compile for current platform
jsbin hello.js -o hello # Custom output name
jsbin hello.js --os linux --arch x64 # Cross-compile to Linux x64
jsbin hello.js --target macos-arm64 # Cross-compile to macOS ARM64
jsbin mylib.js --shared -o libmy.dylib # Build shared library
jsbin mylib.js --static -o libmy.a # Build static library
jsbin app.js --lib mylib --lib-path ./libs # Link with library
`);
}
function parseArgs(args) {
const result = {
input: null,
output: null,
os: null,
arch: null,
target: null,
help: false,
listTargets: false,
debug: false,
shared: false,
static: false,
noJslib: false,
exports: [],
libs: [],
libPaths: [],
};
let i = 0;
while (i < args.length) {
const arg = args[i];
if (arg === "-h" || arg === "--help") {
result.help = true;
} else if (arg === "--list-targets") {
result.listTargets = true;
} else if (arg === "--debug") {
result.debug = true;
} else if (arg === "--shared") {
result.shared = true;
} else if (arg === "--static") {
result.static = true;
} else if (arg === "--no-jslib") {
result.noJslib = true;
} else if (arg === "-o" || arg === "--output") {
i++;
result.output = args[i];
} else if (arg === "--os") {
i++;
result.os = args[i];
} else if (arg === "--arch") {
i++;
result.arch = args[i];
} else if (arg === "-t" || arg === "--target") {
i++;
result.target = args[i];
} else if (arg === "--export") {
i++;
result.exports.push(args[i]);
} else if (arg === "--lib" || arg === "-l") {
i++;
result.libs.push(args[i]);
} else if (arg === "--lib-path" || arg === "-L") {
i++;
result.libPaths.push(args[i]);
} else if (!arg.startsWith("-")) {
result.input = arg;
} else {
console.error(`Unknown option: ${arg}`);
process.exit(1);
}
i++;
}
return result;
}
function main() {
const args = process.argv.slice(2);
const opts = parseArgs(args);
if (opts.help) {
printUsage();
process.exit(0);
}
if (opts.listTargets) {
console.log("Supported targets:");
for (const target of listTargets()) {
console.log(` ${target}`);
}
process.exit(0);
}
if (!opts.input) {
console.error("Error: No input file specified");
printUsage();
process.exit(1);
}
// 解析输入文件路径(支持相对路径)
const inputFile = resolve(process.cwd(), opts.input);
if (!existsSync(inputFile)) {
console.error(`Error: Input file not found: ${inputFile}`);
process.exit(1);
}
// 确定目标平台
let target;
if (opts.target) {
target = opts.target;
} else if (opts.os && opts.arch) {
target = `${opts.os}-${opts.arch}`;
} else if (opts.os || opts.arch) {
const detected = detectPlatform();
const [detectedOs, detectedArch] = detected.split("-");
target = `${opts.os || detectedOs}-${opts.arch || detectedArch}`;
} else {
target = detectPlatform();
}
// 验证目标
try {
resolveTarget(target);
} catch (e) {
console.error(`Error: ${e.message}`);
console.log("Use --list-targets to see supported targets");
process.exit(1);
}
// 确定输出文件名
let output = opts.output;
if (!output) {
// 默认输出文件名:去掉 .js 后缀,使用输入文件的目录
const inputDir = dirname(inputFile);
const inputBase = basename(inputFile, ".js");
// 根据输出类型添加适当的后缀
if (opts.shared) {
const ext = target.startsWith("macos") ? ".dylib" : target.startsWith("windows") ? ".dll" : ".so";
output = join(inputDir, `lib${inputBase}${ext}`);
} else if (opts.static) {
const ext = target.startsWith("windows") ? ".lib" : ".a";
output = join(inputDir, `lib${inputBase}${ext}`);
} else {
output = join(inputDir, `${inputBase}-${target}`);
}
} else {
// 解析输出路径
output = resolve(process.cwd(), output);
}
// 添加 Windows 的 .exe 后缀(仅可执行文件)
if (!opts.shared && !opts.static && target.includes("windows") && !output.endsWith(".exe")) {
output += ".exe";
}
// 确定输出类型描述
let outputTypeDesc = "executable";
if (opts.shared) outputTypeDesc = "shared library";
if (opts.static) outputTypeDesc = "static library";
console.log(`Compiling ${inputFile} -> ${output} (${target}, ${outputTypeDesc})`);
try {
const compiler = new Compiler(target);
// 设置源文件路径(用于解析相对路径的 jslib)
compiler.setSourcePath(inputFile);
// 设置编译选项
if (opts.shared) {
compiler.setOutputType("shared");
} else if (opts.static) {
compiler.setOutputType("static");
}
// 设置 jslib 生成选项
if (opts.noJslib) {
compiler.setOption("noJslib", true);
}
// 添加导出符号
for (const exp of opts.exports) {
compiler.addExport(exp);
}
// 添加库链接
for (const lib of opts.libs) {
compiler.addLibrary(lib);
}
// 添加库搜索路径
for (const libPath of opts.libPaths) {
compiler.addLibraryPath(libPath);
}
compiler.compileFile(inputFile, output);
console.log(`Successfully compiled: ${output}`);
} catch (e) {
console.error(`Compilation error: ${e.message}`);
if (opts.debug || process.env.DEBUG) {
console.error(e.stack);
}
process.exit(1);
}
}
main();