-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnative-compiler-lib.ts
More file actions
811 lines (741 loc) · 27 KB
/
native-compiler-lib.ts
File metadata and controls
811 lines (741 loc) · 27 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
// Native compiler library — the self-hosted compilation path.
// This file is compiled by ChadScript itself, so it uses the native runtime
// declarations rather than Node.js imports.
import { parseSource } from "./parser-native/index.js";
import { transformTree, setCurrentFile } from "./parser-native/transformer.js";
import { LLVMGenerator, LLVMGeneratorOptions, SemaSymbolData } from "./codegen/llvm-generator.js";
import { SemanticAnalyzer } from "./analysis/semantic-analyzer.js";
import { AST, ImportDeclaration, FunctionNode, ClassNode, ClassMethod } from "./ast/types.js";
import { TargetInfo } from "./target-types.js";
const stdlibKeys: string[] = [];
const stdlibValues: string[] = [];
export function registerStdlib(key: string, content: string): void {
stdlibKeys.push(key);
stdlibValues.push(content);
}
declare const child_process: {
execSync(command: string): number;
};
declare const fs: {
readFileSync(filename: string): string;
writeFileSync(filename: string, data: string): number;
appendFileSync(filename: string, data: string): number;
existsSync(filename: string): boolean;
unlinkSync(filename: string): number;
readdirSync(dirname: string): string[];
};
declare const path: {
resolve(p: string): string;
dirname(p: string): string;
basename(p: string): string;
};
declare const process: {
exit(code: number): void;
argv: string[];
argv0: string;
platform: string;
env: { [key: string]: string };
};
declare function __gc_disable(): void;
function findLLVMTool(name: string): string {
const candidates = [
"/opt/homebrew/opt/llvm/bin/" + name,
"/usr/local/opt/llvm/bin/" + name,
"/opt/homebrew/opt/lld/bin/" + name,
"/usr/local/opt/lld/bin/" + name,
];
for (const candidate of candidates) {
if (fs.existsSync(candidate)) {
return candidate;
}
}
return name;
}
// Find lld for cross-linking ELF binaries. Homebrew LLVM on macOS may only
// install ld64.lld (Mach-O). Since lld is a multicall binary that uses argv[0]
// to pick its flavor, we can symlink ld64.lld as ld.lld to get ELF mode.
function findLLD(): string {
const names = ["ld.lld", "lld"];
for (const name of names) {
const resolved = findLLVMTool(name);
if (resolved !== name || fs.existsSync(name)) {
return resolved;
}
}
// ld64.lld is the same multicall binary — symlink it as ld.lld for ELF mode
const ld64Path = findLLVMTool("ld64.lld");
if (ld64Path !== "ld64.lld" || fs.existsSync("ld64.lld")) {
const lldLink = "/tmp/ld.lld";
child_process.execSync("ln -sf " + ld64Path + " " + lldLink);
return lldLink;
}
return "lld";
}
export let skipSemanticAnalysis = false;
export let emitLLVMOnly = false;
export let verbose = false;
export let debugInfo = false;
export let targetCpu = "native";
export let targetTriple = "";
export let diagnosticColorEnabled = false; // set via setDiagnosticColor() by caller
// Extra linker flags from --link-obj, --link-lib, --link-path
export let extraLinkObjs: string[] = [];
export let extraLinkLibs: string[] = [];
export let extraLinkPaths: string[] = [];
export function setDiagnosticColor(value: boolean): void {
diagnosticColorEnabled = value;
}
export function setSkipSemanticAnalysis(value: boolean): void {
skipSemanticAnalysis = value;
}
export function setEmitLLVMOnly(value: boolean): void {
emitLLVMOnly = value;
}
export function setVerbose(value: boolean): void {
verbose = value;
}
export function setDebugInfo(value: boolean): void {
debugInfo = value;
}
export function setTargetCpu(value: string): void {
targetCpu = value;
}
export function setTargetTriple(value: string): void {
targetTriple = value;
}
export function addLinkObj(objPath: string): void {
extraLinkObjs.push(objPath);
}
export function addLinkLib(lib: string): void {
extraLinkLibs.push(lib);
}
export function addLinkPath(libPath: string): void {
extraLinkPaths.push(libPath);
}
// Resolve the home directory for SDK lookups
function getHomeDir(): string {
// Use HOME env var directly — works in the native runtime
const home = process.env.HOME;
if (home.length > 0) return home;
// Fallback: construct from platform + username heuristic
if (process.platform === "darwin") {
return "/Users/" + getUsername();
}
return "/home/" + getUsername();
}
function getUsername(): string {
// process.env is available in the native runtime via dotenv bridge
// but we can parse /etc/passwd or use a simpler approach
// For now, use the execDir parent heuristic or fall back
const argv0 = process.argv0;
const dir = path.dirname(path.resolve(argv0));
// If installed at ~/.chadscript/chad, dirname is ~/.chadscript
if (dir.indexOf("/.chadscript") !== -1) {
const parts = dir.substr(0, dir.indexOf("/.chadscript"));
const lastSlash = parts.lastIndexOf("/");
if (lastSlash !== -1) {
return parts.substr(lastSlash + 1);
}
}
// Fall back: try whoami
return "";
}
function getSDKDir(targetName: string): string {
const home = getHomeDir();
if (home.length === 0) return "";
return home + "/.chadscript/targets/" + targetName;
}
// Determine the short target name from a triple (e.g., "x86_64-unknown-linux-gnu" -> "linux-x64")
function tripleToTargetName(triple: string): string {
const isLinux = triple.indexOf("linux") !== -1;
const isDarwin = triple.indexOf("darwin") !== -1 || triple.indexOf("apple") !== -1;
const isAarch64 = triple.indexOf("aarch64") !== -1 || triple.indexOf("arm64") !== -1;
const osName = isDarwin ? "macos" : "linux";
const archName = isAarch64 ? "arm64" : "x64";
if (!isLinux && !isDarwin) return "";
return osName + "-" + archName;
}
export function parseFileToAST(inputFile: string): string {
__gc_disable();
const absPath = path.resolve(inputFile);
const code = fs.readFileSync(absPath);
setCurrentFile(absPath);
const tree = parseSource(code);
const ast = transformTree(tree);
// JSON.stringify(ast) doesn't work for interface-typed objects in native ChadScript —
// it falls to the number path and treats pointers as doubles. Access fields explicitly.
// JSON.stringify(string[]) is also unimplemented; iterate manually instead.
let out = '{"imports":[';
let ii = 0;
while (ii < ast.imports.length) {
if (ii > 0) out = out + ",";
const imp = ast.imports[ii] as ImportDeclaration;
let specJson = "[";
let si = 0;
while (si < imp.specifiers.length) {
if (si > 0) specJson = specJson + ",";
specJson = specJson + JSON.stringify(imp.specifiers[si]);
si = si + 1;
}
specJson = specJson + "]";
out = out + '{"source":' + JSON.stringify(imp.source) + ',"specifiers":' + specJson + "}";
ii = ii + 1;
}
out = out + '],"functions":[';
let fi = 0;
while (fi < ast.functions.length) {
if (fi > 0) out = out + ",";
const fn = ast.functions[fi] as FunctionNode;
let paramsJson = "[";
let pi = 0;
while (pi < fn.params.length) {
if (pi > 0) paramsJson = paramsJson + ",";
paramsJson = paramsJson + JSON.stringify(fn.params[pi]);
pi = pi + 1;
}
paramsJson = paramsJson + "]";
out = out + '{"name":' + JSON.stringify(fn.name) + ',"params":' + paramsJson + "}";
fi = fi + 1;
}
out = out + '],"classes":[';
let ci = 0;
while (ci < ast.classes.length) {
if (ci > 0) out = out + ",";
const cls = ast.classes[ci] as ClassNode;
let methodsJson = "[";
let mi = 0;
while (mi < cls.methods.length) {
if (mi > 0) methodsJson = methodsJson + ",";
const m = cls.methods[mi] as ClassMethod;
methodsJson = methodsJson + JSON.stringify(m.name);
mi = mi + 1;
}
methodsJson = methodsJson + "]";
out = out + '{"name":' + JSON.stringify(cls.name) + ',"methods":' + methodsJson + "}";
ci = ci + 1;
}
out = out + "]}";
return out;
}
export function compileNative(inputFile: string, outputFile: string): void {
const execDir = path.dirname(path.resolve(process.argv0));
const installedLibDir = execDir + "/lib";
const isInstalled = fs.existsSync(installedLibDir + "/libgc.a");
const crossCompiling = targetTriple.length > 0;
const BDWGC_PATH = isInstalled ? installedLibDir : "./vendor/bdwgc";
const LWS_BRIDGE_PATH = isInstalled ? installedLibDir : "./c_bridges";
const PICOHTTPPARSER_PATH = isInstalled ? installedLibDir : "./vendor/picohttpparser";
const CHADSCRIPT_PATH = ".";
if (verbose) {
console.log("ChadScript native compiler v0.1.0");
console.log("Input file: " + inputFile);
}
__gc_disable();
const compiledFiles: string[] = [];
const mergedAST = compileMultiFile(inputFile, compiledFiles);
let semaSymbols: SemaSymbolData | undefined = undefined;
if (skipSemanticAnalysis) {
if (verbose) {
console.log("Skipping semantic analysis (--skip-semantic-analysis)");
}
} else {
if (verbose) {
console.log("Running semantic analysis...");
}
const analyzer = new SemanticAnalyzer(mergedAST);
analyzer.setDiagnosticColor(diagnosticColorEnabled);
const analysisSuccess = analyzer.analyze();
if (!analysisSuccess) {
const errorOutput = analyzer.formatErrors();
console.log(errorOutput);
process.exit(1);
}
const semaData: SemaSymbolData = {
names: [],
types: [],
llvmTypes: [],
schemaKeys: [],
schemaTypes: [],
};
for (let tsi = 0; tsi < mergedAST.topLevelStatements.length; tsi++) {
const stmt = mergedAST.topLevelStatements[tsi];
if (stmt.type === "variable_declaration") {
const declName = (stmt as { name?: string }).name;
if (declName) {
const symType = analyzer.getSymbolTypeByName(declName);
if (symType !== "unknown") {
semaData.names.push(declName);
semaData.types.push(symType);
semaData.llvmTypes.push(analyzer.getSymbolLlvmTypeByName(declName));
semaData.schemaKeys.push(analyzer.getSymbolSchemaKeysByName(declName));
semaData.schemaTypes.push(analyzer.getSymbolSchemaTypesByName(declName));
}
}
}
}
semaSymbols = semaData;
if (verbose) {
console.log("Semantic analysis passed");
}
}
if (verbose) {
console.log("Generating LLVM IR...");
if (crossCompiling) {
console.log("Cross-compiling for: " + targetTriple);
}
}
// Build a TargetInfo for the LLVM generator when cross-compiling.
// TargetInfo is imported from target-types.ts (dependency-free) so the
// native compiler sees the interface definition and resolves correct GEP indices.
let targetInfo: TargetInfo | undefined = undefined;
if (crossCompiling) {
const isDarwin = targetTriple.indexOf("darwin") !== -1 || targetTriple.indexOf("apple") !== -1;
const isAarch64 =
targetTriple.indexOf("aarch64") !== -1 || targetTriple.indexOf("arm64") !== -1;
const tOs = isDarwin ? "darwin" : "linux";
const tArch = isAarch64 ? "aarch64" : "x86_64";
const tArchStr = isAarch64 ? "arm64" : "x64";
// Data layout strings must match what LLVM expects
let dl = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128";
if (isDarwin && isAarch64) {
dl = "e-m:o-i64:64-i128:128-n32:64-S128-Fn32";
} else if (isDarwin) {
dl = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128";
} else if (isAarch64) {
dl = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32";
}
targetInfo = {
triple: targetTriple,
os: tOs,
arch: tArch,
cpu: "generic",
platformString: tOs,
archString: tArchStr,
dataLayout: dl,
libc: isDarwin ? "system" : "gnu",
};
}
const generatorOptions: LLVMGeneratorOptions = {
sourceCode: "",
filename: inputFile,
analyzedSymbols: semaSymbols,
target: targetInfo,
};
const generator = new LLVMGenerator(mergedAST, null, generatorOptions);
generator.diagnostics.setColor(diagnosticColorEnabled);
const irParts = generator.generateParts();
if (verbose) {
console.log("Generated IR parts: " + irParts.length);
}
const irFile = outputFile + ".ll";
fs.writeFileSync(irFile, "");
for (let pi = 0; pi < irParts.length; pi++) {
const part = irParts[pi];
if (verbose && part.indexOf("ts_parser_language") !== -1) {
const preview = part.substr(0, 80);
console.log(
"Part " + pi + " contains ts_parser_language, len=" + part.length + " preview=" + preview,
);
}
fs.appendFileSync(irFile, part);
}
if (emitLLVMOnly) {
if (verbose) {
console.log("LLVM IR written to " + irFile);
}
return;
}
const objFile = outputFile + ".o";
const optFile = irFile.replace(".ll", ".opt.bc");
const optTool = findLLVMTool("opt");
const llcTool = findLLVMTool("llc");
const clangTool = findLLVMTool("clang");
// Cross-compilation: add triple flags to opt/llc
const cpuFlag = crossCompiling ? "-mcpu=generic" : "-mcpu=" + targetCpu;
const tripleFlag = crossCompiling ? " -mtriple=" + targetTriple : "";
const optCmd = optTool + " -O2 " + cpuFlag + tripleFlag + " " + irFile + " -o " + optFile;
if (verbose) {
console.log("Running: " + optCmd);
}
child_process.execSync(optCmd);
const llcCmd =
llcTool + " -O2 " + cpuFlag + tripleFlag + " -filetype=obj " + optFile + " -o " + objFile;
if (verbose) {
console.log("Running: " + llcCmd);
}
child_process.execSync(llcCmd);
if (!fs.existsSync(objFile)) {
console.log("Error: llc failed to produce " + objFile);
process.exit(1);
}
// Resolve SDK paths for cross-compilation
const tgtName = crossCompiling ? tripleToTargetName(targetTriple) : "";
const sdkDir = crossCompiling ? getSDKDir(tgtName) : "";
const hasSDK = crossCompiling && sdkDir.length > 0 && fs.existsSync(sdkDir + "/sdk.json");
if (crossCompiling && !hasSDK) {
console.log("chad: error: target SDK '" + tgtName + "' not installed");
console.log("Run: chad target add " + tgtName);
process.exit(1);
}
// When cross-compiling with SDK, use SDK paths; otherwise use local/installed paths
const sdkVendor = hasSDK ? sdkDir + "/vendor" : "";
const sdkBridges = hasSDK ? sdkDir + "/bridges" : "";
const sdkSysroot = hasSDK && fs.existsSync(sdkDir + "/sysroot") ? sdkDir + "/sysroot" : "";
const effectiveGcPath = hasSDK ? sdkVendor : BDWGC_PATH;
const effectiveBridgePath = hasSDK ? sdkBridges : LWS_BRIDGE_PATH;
const effectivePicoPath = hasSDK ? sdkVendor : PICOHTTPPARSER_PATH;
const targetIsDarwin = crossCompiling
? targetTriple.indexOf("darwin") !== -1
: process.platform === "darwin";
const isMac = process.platform === "darwin";
const platformLibs = targetIsDarwin ? "" : " -lm -ldl -lrt -lpthread";
// -no-pie only for native Linux builds (not when cross-compiling from macOS)
const noPie = !targetIsDarwin && !crossCompiling ? " -no-pie" : "";
const tsObjDir = isInstalled ? installedLibDir : CHADSCRIPT_PATH + "/build";
let treeSitterObjs = "";
let tsLibPath = "";
if (generator.getUsesTreeSitter()) {
if (hasSDK) {
const sdkTsParser = sdkVendor + "/tree-sitter-typescript-parser.o";
const sdkTsScanner = sdkVendor + "/tree-sitter-typescript-scanner.o";
const sdkTsBridge = sdkBridges + "/treesitter-bridge.o";
if (fs.existsSync(sdkTsParser)) {
treeSitterObjs = sdkTsParser + " " + sdkTsScanner + " " + sdkTsBridge;
}
tsLibPath = sdkVendor + "/libtree-sitter.a";
} else {
treeSitterObjs =
tsObjDir +
"/tree-sitter-typescript-parser.o " +
tsObjDir +
"/tree-sitter-typescript-scanner.o " +
tsObjDir +
"/treesitter-bridge.o";
tsLibPath = isInstalled
? installedLibDir + "/libtree-sitter.a"
: "./vendor/tree-sitter/libtree-sitter.a";
}
}
let linkLibs = "-L" + effectiveGcPath + " -lgc" + platformLibs;
if (tsLibPath) {
linkLibs = linkLibs + " " + tsLibPath;
}
const yyjsonDir = hasSDK ? sdkVendor : isInstalled ? installedLibDir : "./vendor/yyjson";
if (generator.getUsesJson()) {
linkLibs = "-L" + yyjsonDir + " -lyyjson " + linkLibs;
}
const uvDir = hasSDK ? sdkVendor : isInstalled ? installedLibDir : "./vendor/libuv/build";
if (
generator.getUsesTimers() ||
generator.getUsesPromises() ||
generator.getUsesCurl() ||
generator.getUsesUvHrtime() ||
generator.getUsesHttpServer()
) {
linkLibs = "-L" + uvDir + " -luv " + linkLibs;
}
if (generator.getUsesCurl()) {
linkLibs = "-lcurl " + linkLibs;
}
if (generator.getUsesCrypto()) {
linkLibs = "-lcrypto " + linkLibs;
}
if (generator.getUsesSqlite()) {
linkLibs = "-lsqlite3 " + linkLibs;
}
if (generator.getUsesHttpServer()) {
linkLibs = "-lz -lzstd " + linkLibs;
}
const lwsBridgeObj = generator.getUsesHttpServer()
? effectiveBridgePath +
"/lws-bridge.o " +
effectivePicoPath +
"/picohttpparser.o " +
effectiveBridgePath +
"/multipart-bridge.o"
: "";
const regexBridgeObj = generator.getUsesRegex() ? effectiveBridgePath + "/regex-bridge.o" : "";
const cpBridgeObj = generator.getUsesChildProcess()
? effectiveBridgePath + "/child-process-bridge.o"
: "";
const osBridgeObj = effectiveBridgePath + "/os-bridge.o";
const timeBridgeObj = effectiveBridgePath + "/time-bridge.o";
const base64BridgeObj = effectiveBridgePath + "/base64-bridge.o";
const urlBridgeObj = effectiveBridgePath + "/url-bridge.o";
const uriBridgeObj = effectiveBridgePath + "/uri-bridge.o";
const dotenvBridgePath = effectiveBridgePath + "/dotenv-bridge.o";
const dotenvBridgeObj = fs.existsSync(dotenvBridgePath) ? dotenvBridgePath : "";
const watchBridgeObj = effectiveBridgePath + "/watch-bridge.o";
const cpSpawnObj = generator.getUsesSpawn() ? effectiveBridgePath + "/child-process-spawn.o" : "";
// Sysroot and target flags for cross-compilation
if (hasSDK && sdkSysroot.length > 0) {
linkLibs = "--sysroot=" + sdkSysroot + " " + linkLibs;
} else if (!crossCompiling && isMac) {
// Only add -L flags for paths that actually exist to avoid ld warnings.
// Both prefixes are checked because we can't detect arch at runtime in native code.
if (generator.getUsesCrypto()) {
if (fs.existsSync("/opt/homebrew/opt/openssl/lib"))
linkLibs = "-L/opt/homebrew/opt/openssl/lib " + linkLibs;
if (fs.existsSync("/usr/local/opt/openssl/lib"))
linkLibs = "-L/usr/local/opt/openssl/lib " + linkLibs;
}
if (generator.getUsesSqlite()) {
if (fs.existsSync("/opt/homebrew/opt/sqlite/lib"))
linkLibs = "-L/opt/homebrew/opt/sqlite/lib " + linkLibs;
if (fs.existsSync("/usr/local/opt/sqlite/lib"))
linkLibs = "-L/usr/local/opt/sqlite/lib " + linkLibs;
}
if (generator.getUsesHttpServer()) {
if (fs.existsSync("/opt/homebrew/opt/zstd/lib"))
linkLibs = "-L/opt/homebrew/opt/zstd/lib " + linkLibs;
if (fs.existsSync("/usr/local/opt/zstd/lib"))
linkLibs = "-L/usr/local/opt/zstd/lib " + linkLibs;
}
let macLinkPrefix = "-Wl,-syslibroot,$(xcrun --show-sdk-path)";
if (fs.existsSync("/usr/local/lib")) macLinkPrefix = macLinkPrefix + " -L/usr/local/lib";
linkLibs = macLinkPrefix + " " + linkLibs;
}
// Cross-compiled Linux binaries must link statically — the SDK sysroot only
// has .a archives (Ubuntu's .so files are linker scripts with hardcoded paths).
const shouldStatic = !targetIsDarwin && crossCompiling;
const staticFlag = shouldStatic ? " -static" : "";
const crossTargetFlag = crossCompiling ? " --target=" + targetTriple : "";
const debugFlag = debugInfo ? " -g" : "";
// Strip symbol table from release builds. Skipped on macOS (linker handles it
// differently) and when -g is set (stripping debug info defeats the purpose).
const stripFlag = !debugInfo && !isMac ? " -s" : "";
// Cross-compiling requires lld — the host linker can't produce foreign binaries.
// Use full path because Homebrew clang can't find lld by short name.
// Try ld.lld first (ELF-specific), fall back to lld (multicall binary, auto-detects format).
const crossLinker = crossCompiling ? " -fuse-ld=" + findLLD() : "";
const suppressLdWarnings = isMac ? " -Wl,-w" : "";
// User-provided linker flags (--link-obj, --link-lib, --link-path)
let userLinkObjs = "";
for (let _oi = 0; _oi < extraLinkObjs.length; _oi++) {
userLinkObjs = userLinkObjs + " " + extraLinkObjs[_oi];
}
let userLinkFlags = "";
for (let _pi = 0; _pi < extraLinkPaths.length; _pi++) {
userLinkFlags = userLinkFlags + " -L" + extraLinkPaths[_pi];
}
for (let _li = 0; _li < extraLinkLibs.length; _li++) {
userLinkFlags = userLinkFlags + " -l" + extraLinkLibs[_li];
}
const linkCmd =
clangTool +
" " +
objFile +
" " +
lwsBridgeObj +
" " +
regexBridgeObj +
" " +
cpBridgeObj +
" " +
osBridgeObj +
" " +
timeBridgeObj +
" " +
base64BridgeObj +
" " +
urlBridgeObj +
" " +
uriBridgeObj +
" " +
dotenvBridgeObj +
" " +
watchBridgeObj +
" " +
cpSpawnObj +
" " +
treeSitterObjs +
userLinkObjs +
" -o " +
outputFile +
noPie +
debugFlag +
stripFlag +
staticFlag +
crossTargetFlag +
crossLinker +
suppressLdWarnings +
" " +
linkLibs +
userLinkFlags;
if (verbose) {
console.log("Running: " + linkCmd);
}
child_process.execSync(linkCmd);
if (!fs.existsSync(outputFile)) {
console.log("Error: clang failed to produce " + outputFile);
process.exit(1);
}
fs.unlinkSync(objFile);
fs.unlinkSync(optFile);
if (verbose) {
console.log("Compiled: " + outputFile);
}
}
export function compileMultiFile(entryFile: string, compiledFiles: string[]): AST {
const absPath = path.resolve(entryFile);
for (let i = 0; i < compiledFiles.length; i++) {
if (compiledFiles[i] === absPath) {
return emptyAST();
}
}
compiledFiles.push(absPath);
if (verbose) {
console.log("Parsing: " + absPath);
}
const STDLIB_PREFIX = "/CHADSCRIPT_STDLIB/";
let code = "";
if (absPath.substr(0, STDLIB_PREFIX.length) === STDLIB_PREFIX) {
const key = absPath.substr(STDLIB_PREFIX.length);
let found = false;
for (let si = 0; si < stdlibKeys.length; si++) {
if (stdlibKeys[si] === key) {
code = stdlibValues[si];
found = true;
break;
}
}
if (!found) {
console.log("stdlib module not found: " + key);
process.exit(1);
}
} else {
code = fs.readFileSync(absPath);
}
setCurrentFile(absPath);
const tree = parseSource(code);
const ast = transformTree(tree);
const mergedAST: AST = {
imports: [],
functions: ast.functions.slice(0),
classes: ast.classes.slice(0),
exports: ast.exports.slice(0),
interfaces: ast.interfaces.slice(0),
typeAliases: ast.typeAliases ? ast.typeAliases.slice(0) : [],
enums: ast.enums ? ast.enums.slice(0) : [],
defaultExportName: ast.defaultExportName,
topLevelStatements: ast.topLevelStatements.slice(0),
topLevelExpressions: ast.topLevelExpressions.slice(0),
topLevelItems: ast.topLevelItems ? ast.topLevelItems.slice(0) : [],
topLevelItemTypes: ast.topLevelItemTypes ? ast.topLevelItemTypes.slice(0) : [],
importAliasNames: [],
importAliasOriginals: [],
};
let i = 0;
while (i < ast.imports.length) {
const imp = ast.imports[i] as ImportDeclaration;
const src = imp.source;
const isRelative =
src.substr(0, 2) === "./" || src.substr(0, 3) === "../" || src.substr(0, 1) === "/";
if (!isRelative) {
const builtins = ["fs", "path", "child_process"];
let isBuiltin = false;
for (let j = 0; j < builtins.length; j++) {
if (builtins[j] === src) {
isBuiltin = true;
}
}
if (isBuiltin) {
i = i + 1;
continue;
}
if (src.substr(0, 11) === "chadscript/") {
const stdlibName = src.substr(11);
const virtualPath = "/CHADSCRIPT_STDLIB/" + stdlibName + ".ts";
const importedAST = compileMultiFile(virtualPath, compiledFiles);
mergedAST.functions = mergedAST.functions.concat(importedAST.functions);
mergedAST.classes = mergedAST.classes.concat(importedAST.classes);
mergedAST.interfaces = mergedAST.interfaces.concat(importedAST.interfaces);
mergedAST.typeAliases = mergedAST.typeAliases.concat(importedAST.typeAliases);
mergedAST.enums = mergedAST.enums.concat(importedAST.enums);
mergedAST.topLevelStatements = importedAST.topLevelStatements.concat(
mergedAST.topLevelStatements,
);
if (importedAST.topLevelItems) {
mergedAST.topLevelItems = importedAST.topLevelItems.concat(mergedAST.topLevelItems || []);
}
if (importedAST.topLevelItemTypes) {
mergedAST.topLevelItemTypes = importedAST.topLevelItemTypes.concat(
mergedAST.topLevelItemTypes || [],
);
}
i = i + 1;
continue;
}
console.log("Cannot compile npm package: " + src);
process.exit(1);
}
const importPath = resolveImportPath(absPath, src);
const importedAST = compileMultiFile(importPath, compiledFiles);
mergedAST.functions = mergedAST.functions.concat(importedAST.functions);
mergedAST.classes = mergedAST.classes.concat(importedAST.classes);
mergedAST.interfaces = mergedAST.interfaces.concat(importedAST.interfaces);
mergedAST.typeAliases = mergedAST.typeAliases.concat(importedAST.typeAliases);
mergedAST.enums = mergedAST.enums.concat(importedAST.enums);
mergedAST.topLevelStatements = importedAST.topLevelStatements.concat(
mergedAST.topLevelStatements,
);
if (importedAST.topLevelItems) {
mergedAST.topLevelItems = importedAST.topLevelItems.concat(mergedAST.topLevelItems || []);
}
if (importedAST.topLevelItemTypes) {
mergedAST.topLevelItemTypes = importedAST.topLevelItemTypes.concat(
mergedAST.topLevelItemTypes || [],
);
}
i = i + 1;
}
return mergedAST;
}
export function resolveImportPath(fromFile: string, importSource: string): string {
const dir = path.dirname(fromFile);
const resolved = path.resolve(dir + "/" + importSource);
// Prefer .ts/.tsx source over compiled .js
if (importSource.substr(importSource.length - 3) === ".js") {
const tsPath = resolved.substr(0, resolved.length - 3) + ".ts";
if (fs.existsSync(tsPath)) {
return tsPath;
}
const tsxPath = resolved.substr(0, resolved.length - 3) + ".tsx";
if (fs.existsSync(tsxPath)) {
return tsxPath;
}
}
if (fs.existsSync(resolved)) {
return resolved;
}
if (fs.existsSync(resolved + ".ts")) {
return resolved + ".ts";
}
if (fs.existsSync(resolved + ".tsx")) {
return resolved + ".tsx";
}
if (fs.existsSync(resolved + ".js")) {
return resolved + ".js";
}
console.log("Cannot resolve import: " + importSource + " from " + fromFile);
process.exit(1);
return "";
}
export function emptyAST(): AST {
return {
imports: [],
functions: [],
classes: [],
exports: [],
interfaces: [],
typeAliases: [],
enums: [],
defaultExportName: undefined,
topLevelStatements: [],
topLevelExpressions: [],
topLevelItems: [],
topLevelItemTypes: [],
importAliasNames: [],
importAliasOriginals: [],
};
}