-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTzdExeCompiler.cpp
More file actions
1378 lines (1181 loc) · 53.7 KB
/
Copy pathTzdExeCompiler.cpp
File metadata and controls
1378 lines (1181 loc) · 53.7 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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef NOMINMAX
#define NOMINMAX
#endif
// ANTLR4 & Tzd Runtime Headers (must be included before windows.h)
#include "antlr4-runtime.h"
#include "Generated/TzdLangParser.h"
#include "Generated/TzdLangLexer.h"
#include "Generated/TzdInterpreter.h"
#include "Generated/TzdBytecode.h"
#include "Generated/TzdNativeModule.h"
#ifdef WITH_LIBTORCH
#include "Generated/TzdPyTorch.h"
#endif
#include "Generated/TzdTieringEngine.h"
#include "Generated/TzdOop.h"
#include "TzdExeCompiler.h"
#include "TzdNativeCodegen.h"
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <regex>
#include <chrono>
#include <set>
#include <iomanip>
namespace fs = std::filesystem;
namespace tzd {
// ============================================================================
// 0. Progress bar & smart dependency helpers
// ============================================================================
static void printProgressBar(int pct, const std::string& stage) {
const int barWidth = 30;
int filled = (pct * barWidth) / 100;
std::string bar;
bar.reserve(barWidth + 2);
for (int i = 0; i < filled; ++i) bar += "\xe2\x96\x88"; // UTF-8 block ████
for (int i = filled; i < barWidth; ++i) bar += "\xe2\x96\x91"; // ░░░░
// Pad / truncate stage string
std::string stageShort = stage;
if (stageShort.size() > 40) stageShort = stageShort.substr(0, 37) + "...";
std::cout << "\r [" << bar << "] " << std::setw(3) << pct << "% " << stageShort;
std::cout.flush();
}
// Keyword sets used for smart dependency detection
static const std::vector<std::string> k_torchKeywords = {
"import torch", "import \"torch", "import 'torch",
"tensor(", "Tensor(", "torch.", "loadModel(", "saveModel(",
"Linear(", "Conv2d(", "relu(", "sigmoid(", "softmax(",
"backward(", "optimizer(", "Adam(", "SGD(",
};
static const std::vector<std::string> k_gpuKeywords = {
"cuda(", ".gpu(", "toGpu(", "toCuda(", ".cuda", "cudaDevice",
"gpuAvailable(", "isCudaAvailable(", "tensorCuda(",
};
bool TzdExeCompiler::sourceUsesTorch(const std::string& sourceCode) {
for (const auto& kw : k_torchKeywords) {
if (sourceCode.find(kw) != std::string::npos)
return true;
}
return false;
}
bool TzdExeCompiler::sourceUsesGpu(const std::string& sourceCode) {
if (!sourceUsesTorch(sourceCode)) return false;
for (const auto& kw : k_gpuKeywords) {
if (sourceCode.find(kw) != std::string::npos)
return true;
}
return false;
}
// ============================================================================
// 1. Embedded Virtual File System (EmbeddedVFS)
// ============================================================================
EmbeddedVFS& EmbeddedVFS::instance() {
static EmbeddedVFS vfs;
return vfs;
}
void EmbeddedVFS::addFile(const std::string& virtualPath, std::vector<uint8_t> data) {
std::string norm = virtualPath;
std::replace(norm.begin(), norm.end(), '\\', '/');
m_files[norm] = std::move(data);
}
void EmbeddedVFS::addFile(const std::string& virtualPath, const std::string& text) {
std::vector<uint8_t> data(text.begin(), text.end());
addFile(virtualPath, std::move(data));
}
bool EmbeddedVFS::hasFile(const std::string& virtualPath) const {
std::string norm = virtualPath;
std::replace(norm.begin(), norm.end(), '\\', '/');
return m_files.find(norm) != m_files.end();
}
std::string EmbeddedVFS::getFileContent(const std::string& virtualPath) const {
std::string norm = virtualPath;
std::replace(norm.begin(), norm.end(), '\\', '/');
auto it = m_files.find(norm);
if (it == m_files.end()) return "";
return std::string(reinterpret_cast<const char*>(it->second.data()), it->second.size());
}
const std::vector<uint8_t>* EmbeddedVFS::getFileBytes(const std::string& virtualPath) const {
std::string norm = virtualPath;
std::replace(norm.begin(), norm.end(), '\\', '/');
auto it = m_files.find(norm);
if (it == m_files.end()) return nullptr;
return &it->second;
}
std::vector<std::string> EmbeddedVFS::listFiles() const {
std::vector<std::string> res;
for (const auto& [p, _] : m_files) {
res.push_back(p);
}
return res;
}
void EmbeddedVFS::extractToDirectory(const fs::path& targetDir) const {
for (const auto& [relPath, bytes] : m_files) {
fs::path fullPath = targetDir / relPath;
fs::create_directories(fullPath.parent_path());
std::ofstream out(fullPath, std::ios::binary);
if (out.is_open()) {
out.write(reinterpret_cast<const char*>(bytes.data()), bytes.size());
}
}
}
void EmbeddedVFS::clear() {
m_files.clear();
}
// ============================================================================
// 2. Helper Functions (Checksum, Bytecode Serialization, Stubs)
// ============================================================================
uint64_t TzdExeCompiler::computeChecksum(const uint8_t* data, size_t size) {
// 64-bit FNV-1a hash
uint64_t hash = 14695981039346656037ULL;
for (size_t i = 0; i < size; ++i) {
hash ^= static_cast<uint64_t>(data[i]);
hash *= 1099511628211ULL;
}
return hash;
}
std::string TzdExeCompiler::findRuntimeStub() {
// Get the directory of the currently running TzdTools.exe
fs::path exeDir;
wchar_t currentExe[MAX_PATH];
if (GetModuleFileNameW(NULL, currentExe, MAX_PATH)) {
exeDir = fs::path(currentExe).parent_path();
}
// ── Priority order ────────────────────────────────────────────────────────
// tzd_stub.exe is a ZERO-DLL lightweight runner (no torch/cuda imports).
// Always prefer it over TzdTools.exe which has torch/cuda in its IAT.
// ─────────────────────────────────────────────────────────────────────────
std::vector<fs::path> candidates;
// 1. tzd_stub.exe alongside TzdTools.exe (same dir — produced by build_stub.bat)
if (!exeDir.empty()) {
candidates.push_back(exeDir / "tzd_stub.exe");
}
// 2. Common relative build output locations
candidates.push_back(fs::path("x64") / "Release" / "tzd_stub.exe");
candidates.push_back(fs::path("build_vs18") / "Release" / "tzd_stub.exe");
candidates.push_back(fs::path("dist") / "TzdTools" / "tzd_stub.exe");
candidates.push_back(fs::path("tzd_stub.exe"));
// 3. Fallback: TzdTools.exe itself (will carry DLL dependencies — avoid if possible)
if (!exeDir.empty()) {
candidates.push_back(exeDir / "TzdTools.exe");
}
candidates.push_back(fs::path("x64") / "Release" / "TzdTools.exe");
candidates.push_back(fs::path("build_vs18") / "Release" / "TzdTools.exe");
candidates.push_back(fs::path("dist") / "TzdTools" / "TzdTools.exe");
candidates.push_back(fs::path("TzdTools.exe"));
std::error_code ec;
for (const auto& cand : candidates) {
if (fs::exists(cand, ec)) {
std::string resolved = fs::canonical(cand, ec).string();
if (!resolved.empty()) return resolved;
return cand.string();
}
}
return "";
}
bool TzdExeCompiler::copyRuntimeDependencies(const std::string& stubExeDir,
const std::string& targetExeDir,
const std::string& sourceCode,
const ExeCompileOptions& opts)
{
// If CPU-only mode: skip ALL torch / CUDA DLLs
// If smart mode: only copy torch/CUDA DLLs when source actually uses them
bool wantTorch = !opts.cpuOnly && (!opts.smartDeps || sourceUsesTorch(sourceCode));
bool wantGpu = wantTorch && !opts.cpuOnly && (!opts.smartDeps || sourceUsesGpu(sourceCode));
// Patterns that identify GPU/CUDA-specific DLLs (case-insensitive prefix match)
auto isGpuDll = [](const std::string& name) -> bool {
static const std::vector<std::string> gpuPrefixes = {
"cuda", "cubl", "cufft", "cusparse", "cusolver", "curand", "cupti",
"cudnn", "nvrtc", "nvjit", "nvtool", "caffe2_nvrtc",
};
std::string lower = name;
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
for (const auto& p : gpuPrefixes) {
if (lower.rfind(p, 0) == 0) return true;
}
return false;
};
// Patterns that identify torch/c10-specific DLLs
auto isTorchDll = [](const std::string& name) -> bool {
static const std::vector<std::string> torchPrefixes = {
"torch", "c10", "fbgemm", "cpuinfo", "dnnl", "xnnpack",
"pthreadpool", "pytorch", "libtorch",
};
std::string lower = name;
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
for (const auto& p : torchPrefixes) {
if (lower.rfind(p, 0) == 0) return true;
}
return false;
};
try {
fs::path stubP = stubExeDir.empty() ? fs::current_path() : fs::absolute(stubExeDir);
fs::path targetP = targetExeDir.empty() ? fs::current_path() : fs::absolute(targetExeDir);
std::error_code ec;
if (fs::equivalent(stubP, targetP, ec)) return true;
if (!fs::exists(targetP, ec)) {
fs::create_directories(targetP, ec);
}
int copied = 0, skipped = 0;
// Copy companion runtime DLLs from stub directory to target directory
for (const auto& entry : fs::directory_iterator(stubP, ec)) {
if (!entry.is_regular_file(ec)) continue;
if (entry.path().extension() != ".dll") continue;
std::string fname = entry.path().filename().string();
// Apply smart filtering
if (isGpuDll(fname) && !wantGpu) { ++skipped; continue; }
if (isTorchDll(fname) && !wantTorch) { ++skipped; continue; }
fs::path dst = targetP / entry.path().filename();
if (!fs::exists(dst, ec)) {
fs::copy_file(entry.path(), dst, fs::copy_options::overwrite_existing, ec);
++copied;
}
}
if (copied > 0 || skipped > 0) {
std::cout << "\n [依赖拷贝] 已复制 " << copied << " 个 DLL"
<< (skipped > 0 ? (",跳过 " + std::to_string(skipped) + " 个 GPU/Torch DLL(未使用)") : "")
<< std::endl;
}
return true;
}
catch (const std::exception& e) {
std::cerr << "[TzdExeCompiler] Warning: Failed to copy companion DLLs: " << e.what() << std::endl;
return false;
}
}
std::string TzdExeCompiler::findMsvcVcvars() {
static const std::vector<std::string> searchPaths = {
"C:\\Program Files\\Microsoft Visual Studio\\18\\Community\\VC\\Auxiliary\\Build\\vcvars64.bat",
"C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Auxiliary\\Build\\vcvars64.bat",
"C:\\Program Files\\Microsoft Visual Studio\\2022\\Enterprise\\VC\\Auxiliary\\Build\\vcvars64.bat",
"C:\\Program Files\\Microsoft Visual Studio\\2022\\Professional\\VC\\Auxiliary\\Build\\vcvars64.bat",
"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Auxiliary\\Build\\vcvars64.bat",
"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\VC\\Auxiliary\\Build\\vcvars64.bat",
"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Professional\\VC\\Auxiliary\\Build\\vcvars64.bat",
};
for (const auto& p : searchPaths) {
if (fs::exists(p)) return p;
}
return "";
}
std::string TzdExeCompiler::findLlvmCompiler(std::string& outKind) {
// 1. Check alongside current running executable
wchar_t currentExe[MAX_PATH];
if (GetModuleFileNameW(NULL, currentExe, MAX_PATH)) {
fs::path p(currentExe);
fs::path exeDir = p.parent_path();
std::vector<fs::path> relCandidates = {
exeDir / "llvm" / "bin" / "clang++.exe",
exeDir / "bin" / "clang++.exe",
exeDir / "clang++.exe",
exeDir.parent_path() / "llvm" / "bin" / "clang++.exe",
exeDir.parent_path() / "bin" / "clang++.exe",
};
for (const auto& c : relCandidates) {
if (fs::exists(c)) { outKind = "clang++"; return c.string(); }
}
}
// 2. Check known SDK install directories
std::vector<std::string> knownPaths = {
"E:\\LLVM_SDK\\bin\\clang++.exe",
"C:\\Program Files\\LLVM\\bin\\clang++.exe",
"C:\\LLVM\\bin\\clang++.exe",
"D:\\LLVM\\bin\\clang++.exe",
"E:\\LLVM\\bin\\clang++.exe",
};
for (const auto& p : knownPaths) {
if (fs::exists(p)) { outKind = "clang++"; return p; }
}
// 3. Search in system PATH via where.exe
FILE* pipe = _popen("where.exe clang++.exe 2>nul", "r");
if (pipe) {
char buf[512];
if (fgets(buf, sizeof(buf), pipe)) {
std::string s(buf);
while (!s.empty() && (s.back() == '\r' || s.back() == '\n' || s.back() == ' ')) s.pop_back();
if (fs::exists(s)) { _pclose(pipe); outKind = "clang++"; return s; }
}
_pclose(pipe);
}
// 4. Fallback: MinGW g++
std::string gcc = findGccCompiler();
if (!gcc.empty()) {
outKind = "g++";
return gcc;
}
return "";
}
std::string TzdExeCompiler::findGccCompiler() {
std::vector<std::string> knownGcc = {
"C:\\msys64\\mingw64\\bin\\g++.exe",
"C:\\mingw64\\bin\\g++.exe",
"C:\\MinGW\\bin\\g++.exe",
"D:\\msys64\\mingw64\\bin\\g++.exe",
"D:\\mingw64\\bin\\g++.exe",
};
for (const auto& p : knownGcc) {
if (fs::exists(p)) return p;
}
FILE* pipe = _popen("where.exe g++.exe 2>nul", "r");
if (pipe) {
char buf[512];
if (fgets(buf, sizeof(buf), pipe)) {
std::string s(buf);
while (!s.empty() && (s.back() == '\r' || s.back() == '\n' || s.back() == ' ')) s.pop_back();
if (fs::exists(s)) { _pclose(pipe); return s; }
}
_pclose(pipe);
}
return "";
}
bool TzdExeCompiler::copyTorchDependencies(const std::string& targetExeDir, bool isGpu) {
fs::path targetDir = fs::absolute(targetExeDir);
std::error_code ec;
if (!fs::exists(targetDir, ec)) {
fs::create_directories(targetDir, ec);
}
std::vector<fs::path> srcDirs;
wchar_t currentExe[MAX_PATH];
if (GetModuleFileNameW(NULL, currentExe, MAX_PATH)) {
fs::path p(currentExe);
fs::path exeDir = p.parent_path();
srcDirs.push_back(exeDir);
srcDirs.push_back(exeDir / "lib");
srcDirs.push_back(exeDir.parent_path() / "lib");
}
srcDirs.push_back(fs::path("dist") / (isGpu ? "TzdTools" : "TzdTools_CPU"));
srcDirs.push_back(fs::path("dist") / "TzdTools");
srcDirs.push_back(fs::path("External") / "libtorch" / "lib");
srcDirs.push_back(fs::path("x64") / "Release");
fs::path validSrcDir;
for (const auto& d : srcDirs) {
if (fs::exists(d / "torch_cpu.dll", ec) || fs::exists(d / "c10.dll", ec)) {
validSrcDir = d;
break;
}
}
if (validSrcDir.empty()) {
std::cerr << " [PyTorch] 警告: 未找到 PyTorch 运行库源目录,跳过 DLL 拷贝。" << std::endl;
return false;
}
std::vector<std::string> cpuDlls = {
"torch_cpu.dll", "torch.dll", "c10.dll", "fbgemm.dll", "libiomp5md.dll",
"asmjit.dll", "mkl_core.1.dll", "mkl_intel_thread.1.dll", "vcomp140.dll",
"pytorch_jni.dll", "torch_global_deps.dll", "uv.dll"
};
std::vector<std::string> gpuDlls = {
"torch_cuda.dll", "c10_cuda.dll", "caffe2_nvrtc.dll",
"cublas64_12.dll", "cublasLt64_12.dll", "cudart64_12.dll",
"cudnn64_8.dll", "cufft64_11.dll", "curand64_10.dll",
"cusolver64_11.dll", "cusparse64_12.dll", "nvJitLink_120_0.dll",
"nvrtc-builtins64_126.dll", "nvrtc-builtins64_121.dll", "nvrtc64_120_0.dll"
};
std::vector<std::string> toCopy = cpuDlls;
if (isGpu) {
for (const auto& g : gpuDlls) toCopy.push_back(g);
}
int copied = 0;
for (const auto& dll : toCopy) {
fs::path srcFile = validSrcDir / dll;
fs::path dstFile = targetDir / dll;
if (fs::exists(srcFile, ec) && !fs::equivalent(validSrcDir, targetDir, ec)) {
fs::copy_file(srcFile, dstFile, fs::copy_options::overwrite_existing, ec);
if (!ec) copied++;
}
}
std::cout << "\n [PyTorch 依赖配置] 成功将 " << copied << " 个核心运行库 DLL 部署至目标目录 ("
<< (isGpu ? "GPU CUDA 完整版" : "CPU 轻量版") << ")。" << std::endl;
return true;
}
bool TzdExeCompiler::compileCppToExe(
const std::string& cppPath,
const std::string& exePath,
const ExeCompileOptions& opts,
std::string& err)
{
fs::path targetExeP = fs::absolute(exePath);
fs::path cppP = fs::absolute(cppPath);
fs::path targetDir = targetExeP.parent_path();
if (!fs::exists(targetDir)) {
std::error_code ec;
fs::create_directories(targetDir, ec);
}
// Locate repository directory where TzdNativeRuntime.hpp is stored
wchar_t currentExe[MAX_PATH];
fs::path repoDir = fs::current_path();
if (GetModuleFileNameW(NULL, currentExe, MAX_PATH)) {
fs::path p(currentExe);
fs::path dir1 = p.parent_path();
fs::path dir2 = dir1.parent_path();
fs::path dir3 = dir2.parent_path();
if (fs::exists(dir1 / "TzdNativeRuntime.hpp")) repoDir = dir1;
else if (fs::exists(dir2 / "TzdNativeRuntime.hpp")) repoDir = dir2;
else if (fs::exists(dir3 / "TzdNativeRuntime.hpp")) repoDir = dir3;
}
if (!fs::exists(repoDir / "TzdNativeRuntime.hpp")) {
if (fs::exists(fs::current_path() / "TzdNativeRuntime.hpp")) {
repoDir = fs::current_path();
}
}
// Copy TzdNativeRuntime.hpp to target directory so include always succeeds
std::error_code ec;
bool copiedHeader = false;
if (fs::exists(repoDir / "TzdNativeRuntime.hpp", ec)) {
if (!fs::equivalent(repoDir, targetDir, ec)) {
fs::copy_file(repoDir / "TzdNativeRuntime.hpp", targetDir / "TzdNativeRuntime.hpp",
fs::copy_options::overwrite_existing, ec);
if (!ec) copiedHeader = true;
}
}
// Select Toolchain
std::string vcvars = findMsvcVcvars();
std::string gccCompiler = findGccCompiler();
std::string llvmKind;
std::string llvmCompiler = findLlvmCompiler(llvmKind);
bool useMsvc = false;
bool useLlvm = false;
std::string selectedCompiler;
if (opts.toolchain == CompilerToolchain::MSVC) {
useMsvc = true;
} else if (opts.toolchain == CompilerToolchain::GCC) {
useLlvm = true;
selectedCompiler = !gccCompiler.empty() ? gccCompiler : "g++";
} else if (opts.toolchain == CompilerToolchain::LLVM) {
useLlvm = true;
selectedCompiler = !llvmCompiler.empty() ? llvmCompiler : "clang++";
} else {
// AUTO mode
if (!vcvars.empty()) {
useMsvc = true;
} else if (!gccCompiler.empty()) {
useLlvm = true;
selectedCompiler = gccCompiler;
std::cout << "\n [编译工具链] 未检测到 Visual Studio 环境,已自动启用内置 GCC/MinGW 编译器:\n "
<< gccCompiler << std::endl;
} else if (!llvmCompiler.empty()) {
useLlvm = true;
selectedCompiler = llvmCompiler;
std::cout << "\n [编译工具链] 未检测到 Visual Studio 环境,已自动启用内置 LLVM / Clang 编译器:\n "
<< llvmCompiler << std::endl;
} else {
useMsvc = true; // Attempt cl.exe in PATH
}
}
fs::path batPath = targetDir / "_tzd_compile.bat";
fs::path logPath = targetDir / "_tzd_compile.log";
// LibTorch configurations
fs::path libtorchDir = repoDir / "External" / "libtorch";
if (!fs::exists(libtorchDir, ec)) {
if (fs::exists(fs::path("External") / "libtorch", ec)) libtorchDir = fs::path("External") / "libtorch";
}
{
std::ofstream bat(batPath);
bat << "@echo off\n";
if (useMsvc) {
if (!vcvars.empty()) {
bat << "call \"" << vcvars << "\" >nul 2>&1\n";
}
std::string optFlag = "/O2";
if (opts.optLevel == 0) optFlag = "/Od";
else if (opts.optLevel == 1) optFlag = "/O1";
else if (opts.optLevel == 2) optFlag = "/O2";
else if (opts.optLevel >= 3) optFlag = "/Ox /fp:fast";
std::string incFlag = "/I\"" + repoDir.string() + "\" /I\"" + targetDir.string() + "\"";
std::string extraDefs = "";
std::string linkLibs = "";
if (opts.forceTorch && fs::exists(libtorchDir, ec)) {
incFlag += " /I\"" + (libtorchDir / "include").string() + "\"";
incFlag += " /I\"" + (libtorchDir / "include" / "torch" / "csrc" / "api" / "include").string() + "\"";
extraDefs += " /DWITH_LIBTORCH /D_GLIBCXX_USE_CXX11_ABI=0";
if (opts.torchGpu) {
extraDefs += " /DWITH_CUDA /I\"C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v12.6\\include\"";
linkLibs += " /LIBPATH:\"" + (libtorchDir / "lib").string() + "\" torch.lib torch_cpu.lib torch_cuda.lib c10.lib c10_cuda.lib";
} else {
linkLibs += " /LIBPATH:\"" + (libtorchDir / "lib").string() + "\" torch.lib torch_cpu.lib c10.lib";
}
}
fs::path exactObjP = targetDir / (cppP.stem().string() + ".obj");
bat << "cl.exe /nologo /MT " << optFlag << " /EHsc /std:c++20 /utf-8 "
<< extraDefs << " " << incFlag << " \"" << cppP.string() << "\" /Fe:\""
<< targetExeP.string() << "\" /Fo:\"" << exactObjP.string() << "\" "
<< (linkLibs.empty() ? "" : ("/link " + linkLibs)) << " >\""
<< logPath.string() << "\" 2>&1\n";
} else {
// LLVM / Clang++ or MinGW g++
std::string optFlag = "-O2 -s";
if (opts.optLevel == 0) optFlag = "-O0 -g";
else if (opts.optLevel == 1) optFlag = "-O1";
else if (opts.optLevel == 2) optFlag = "-O2 -s";
else if (opts.optLevel >= 3) optFlag = "-O3 -s";
std::string incFlag = "-I\"" + repoDir.string() + "\" -I\"" + targetDir.string() + "\"";
std::string extraDefs = "";
std::string linkLibs = "";
if (opts.forceTorch && fs::exists(libtorchDir, ec)) {
incFlag += " -I\"" + (libtorchDir / "include").string() + "\"";
incFlag += " -I\"" + (libtorchDir / "include" / "torch" / "csrc" / "api" / "include").string() + "\"";
extraDefs += " -DWITH_LIBTORCH -D_GLIBCXX_USE_CXX11_ABI=0";
linkLibs += " -L\"" + (libtorchDir / "lib").string() + "\" -ltorch -ltorch_cpu -lc10";
if (opts.torchGpu) {
extraDefs += " -DWITH_CUDA -I\"C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v12.6\\include\"";
linkLibs += " -ltorch_cuda -lc10_cuda";
}
}
std::string compExe = !selectedCompiler.empty() ? selectedCompiler : (!llvmCompiler.empty() ? llvmCompiler : "clang++");
bat << "\"" << compExe << "\" -std=c++20 " << optFlag << " -static -finput-charset=UTF-8 -fexec-charset=UTF-8 "
<< extraDefs << " " << incFlag << " \"" << cppP.string() << "\" -o \""
<< targetExeP.string() << "\" " << linkLibs << " >\""
<< logPath.string() << "\" 2>&1\n";
}
bat << "exit /b %ERRORLEVEL%\n";
}
// Delete any pre-existing target exe so stale binaries are never reported as success
if (fs::exists(targetExeP, ec)) {
fs::remove(targetExeP, ec);
}
std::string cmd = "cmd /c \"" + batPath.string() + "\"";
int ret = std::system(cmd.c_str());
// Clean up temporary obj, bat, and copied header
fs::path objPath = targetDir / (cppP.stem().string() + ".obj");
fs::remove(objPath, ec);
fs::remove(batPath, ec);
if (copiedHeader) {
fs::remove(targetDir / "TzdNativeRuntime.hpp", ec);
}
if (ret == 0 && fs::exists(targetExeP, ec) && fs::file_size(targetExeP, ec) > 0) {
fs::remove(logPath, ec);
// If PyTorch was forced, deploy companion runtime DLLs to target directory
if (opts.forceTorch) {
copyTorchDependencies(targetDir.string(), opts.torchGpu);
}
return true;
}
std::string logContent;
if (fs::exists(logPath, ec)) {
std::ifstream lf(logPath);
std::string line;
while (std::getline(lf, line)) {
if (!line.empty()) logContent += " " + line + "\n";
}
fs::remove(logPath, ec);
}
err = (useMsvc ? "MSVC cl.exe" : ("LLVM / " + (!llvmCompiler.empty() ? llvmCompiler : "clang++"))) +
" compiler returned code " + std::to_string(ret);
if (!logContent.empty()) {
err += ":\n" + logContent;
}
return false;
}
// ============================================================================
// 3. Recursive Import Scanning
// ============================================================================
std::vector<std::string> TzdExeCompiler::scanAndResolveImports(
const std::string& sourceCode,
const std::string& baseDir,
const std::vector<std::string>& extraIncludePaths)
{
std::vector<std::string> resolvedPaths;
std::set<std::string> visited;
std::function<void(const std::string&, const std::string&)> scanFile = [&](const std::string& code, const std::string& curDir) {
std::regex importRegex(R"(import\s+["']([^"']+)["'];)");
auto words_begin = std::sregex_iterator(code.begin(), code.end(), importRegex);
auto words_end = std::sregex_iterator();
for (std::sregex_iterator i = words_begin; i != words_end; ++i) {
std::smatch match = *i;
std::string importPath = match[1].str();
// Normalize path separator
std::string normImport = importPath;
if (normImport.find('/') == std::string::npos && normImport.find('\\') == std::string::npos && normImport.find('.') != std::string::npos) {
std::replace(normImport.begin(), normImport.end(), '.', '/');
normImport += ".tzd";
}
// Candidates to probe:
std::vector<fs::path> candidates;
candidates.push_back(fs::path(curDir) / normImport);
candidates.push_back(fs::path("stdlib") / normImport);
candidates.push_back(fs::path("dist/TzdTools/stdlib") / normImport);
for (const auto& inc : extraIncludePaths) {
candidates.push_back(fs::path(inc) / normImport);
}
// Also search parent directory stdlib
candidates.push_back(fs::path(baseDir) / normImport);
candidates.push_back(fs::path(baseDir) / "stdlib" / normImport);
bool found = false;
for (const auto& cand : candidates) {
if (fs::exists(cand) && !fs::is_directory(cand)) {
std::string absPath = fs::weakly_canonical(cand).string();
if (visited.insert(absPath).second) {
resolvedPaths.push_back(absPath);
// Read and scan sub-imports transitively
std::ifstream subF(absPath);
if (subF.is_open()) {
std::stringstream ss;
ss << subF.rdbuf();
scanFile(ss.str(), fs::path(absPath).parent_path().string());
}
}
found = true;
break;
}
}
}
};
scanFile(sourceCode, baseDir);
return resolvedPaths;
}
// ============================================================================
// 4. Binary Payload Builder
// ============================================================================
std::vector<uint8_t> TzdExeCompiler::buildPayload(
const std::string& entryName,
const std::vector<uint8_t>& bytecodeBytes,
const std::string& sourceCode,
const std::vector<std::pair<std::string, std::string>>& embeddedFiles,
const ExeCompileOptions& options)
{
std::ostringstream ss(std::ios::binary);
ExePayloadHeader header;
header.magic = ExePayloadHeader::MAGIC;
header.version = ExePayloadHeader::VERSION;
header.format = (uint32_t)options.payloadFormat;
header.flags = 0;
if (options.silent) header.flags |= 1;
if (!options.enableJit) header.flags |= 2;
header.entryNameLength = static_cast<uint32_t>(entryName.size());
header.mainPayloadSize = static_cast<uint64_t>(
options.payloadFormat == PayloadFormat::BYTECODE ? bytecodeBytes.size() : sourceCode.size()
);
header.embeddedFileCount = static_cast<uint32_t>(embeddedFiles.size());
// Reserve placeholder for header
std::streampos headerPos = ss.tellp();
ss.write(reinterpret_cast<const char*>(&header), sizeof(header));
// 1. Write entry script name
if (header.entryNameLength > 0) {
ss.write(entryName.data(), header.entryNameLength);
}
// 2. Write primary payload according to selected format
if (options.payloadFormat == PayloadFormat::BYTECODE) {
if (!bytecodeBytes.empty()) {
ss.write(reinterpret_cast<const char*>(bytecodeBytes.data()), bytecodeBytes.size());
}
} else if (options.payloadFormat == PayloadFormat::SCRIPT_SOURCE) {
if (!sourceCode.empty()) {
ss.write(sourceCode.data(), sourceCode.size());
}
} else if (options.payloadFormat == PayloadFormat::HYBRID) {
uint32_t srcLen = static_cast<uint32_t>(sourceCode.size());
ss.write(reinterpret_cast<const char*>(&srcLen), sizeof(srcLen));
if (srcLen > 0) {
ss.write(sourceCode.data(), srcLen);
}
uint32_t bcLen = static_cast<uint32_t>(bytecodeBytes.size());
ss.write(reinterpret_cast<const char*>(&bcLen), sizeof(bcLen));
if (bcLen > 0) {
ss.write(reinterpret_cast<const char*>(bytecodeBytes.data()), bcLen);
}
}
// 4. Write embedded auxiliary files (stdlib / local modules)
for (const auto& [relPath, content] : embeddedFiles) {
uint32_t pathLen = static_cast<uint32_t>(relPath.size());
ss.write(reinterpret_cast<const char*>(&pathLen), sizeof(pathLen));
ss.write(relPath.data(), pathLen);
uint64_t contentSize = static_cast<uint64_t>(content.size());
ss.write(reinterpret_cast<const char*>(&contentSize), sizeof(contentSize));
if (contentSize > 0) {
ss.write(content.data(), contentSize);
}
}
std::string payloadStr = ss.str();
std::vector<uint8_t> payloadData(payloadStr.begin(), payloadStr.end());
// Compute checksum over payload data (skipping header checksum field itself)
uint64_t cksum = computeChecksum(payloadData.data() + sizeof(ExePayloadHeader), payloadData.size() - sizeof(ExePayloadHeader));
reinterpret_cast<ExePayloadHeader*>(payloadData.data())->checksum = cksum;
return payloadData;
}
// ============================================================================
// 5. PE Overlay Injection
// ============================================================================
bool TzdExeCompiler::injectPayloadIntoExe(
const std::string& stubExePath,
const std::string& targetExePath,
const std::vector<uint8_t>& payloadData,
std::string& err)
{
std::ifstream stubFile(stubExePath, std::ios::binary);
if (!stubFile.is_open()) {
err = "Failed to open runtime stub executable: " + stubExePath;
return false;
}
// Read stub bytes
stubFile.seekg(0, std::ios::end);
size_t stubSize = stubFile.tellg();
stubFile.seekg(0, std::ios::beg);
std::vector<uint8_t> cleanStub(stubSize);
stubFile.read(reinterpret_cast<char*>(cleanStub.data()), stubSize);
stubFile.close();
// If stub already has an embedded payload trailer, strip it to avoid recursive nesting
if (stubSize >= sizeof(ExePayloadTrailer)) {
const ExePayloadTrailer* existingTrailer = reinterpret_cast<const ExePayloadTrailer*>(
cleanStub.data() + stubSize - sizeof(ExePayloadTrailer)
);
if (existingTrailer->trailerMagic == ExePayloadHeader::TRAILER_MAGIC &&
existingTrailer->payloadOffset < stubSize)
{
cleanStub.resize(static_cast<size_t>(existingTrailer->payloadOffset));
stubSize = cleanStub.size();
}
}
// Create target directory
std::error_code ec;
fs::path parent = fs::path(targetExePath).parent_path();
if (!parent.empty() && !fs::exists(parent, ec)) {
fs::create_directories(parent, ec);
}
// Write clean stub + payload + trailer
std::ofstream outExe(targetExePath, std::ios::binary | std::ios::trunc);
if (!outExe.is_open()) {
err = "Cannot create output executable: " + targetExePath;
return false;
}
// 1. Write PE Base stub
outExe.write(reinterpret_cast<const char*>(cleanStub.data()), cleanStub.size());
// 2. Write Payload Data
uint64_t payloadOffset = static_cast<uint64_t>(cleanStub.size());
outExe.write(reinterpret_cast<const char*>(payloadData.data()), payloadData.size());
// 3. Write Trailer Footer
ExePayloadTrailer trailer;
trailer.payloadOffset = payloadOffset;
trailer.payloadSize = static_cast<uint64_t>(payloadData.size());
trailer.trailerMagic = ExePayloadHeader::TRAILER_MAGIC;
outExe.write(reinterpret_cast<const char*>(&trailer), sizeof(trailer));
outExe.flush();
outExe.close();
return true;
}
// ============================================================================
// 6. Compiler Core Implementation
// ============================================================================
TzdExeCompiler::TzdExeCompiler() = default;
TzdExeCompiler::~TzdExeCompiler() = default;
ExeCompileResult TzdExeCompiler::compileTzd(
const std::string& tzdFilePath,
const std::string& outExePath,
const ExeCompileOptions& options)
{
auto startTime = std::chrono::high_resolution_clock::now();
ExeCompileResult res;
if (!fs::exists(tzdFilePath)) {
res.errorMessage = "Source file does not exist: " + tzdFilePath;
return res;
}
// Derive output executable name if not specified
std::string targetExe = outExePath;
if (targetExe.empty()) {
fs::path p(tzdFilePath);
targetExe = (p.parent_path() / (p.stem().string() + ".exe")).string();
}
if (options.onProgress) options.onProgress("Reading source file...", 10);
std::ifstream srcFile(tzdFilePath);
if (!srcFile.is_open()) {
res.errorMessage = "Failed to open input script: " + tzdFilePath;
return res;
}
std::stringstream ss;
ss << srcFile.rdbuf();
std::string sourceCode = ss.str();
srcFile.close();
std::string entryName = fs::path(tzdFilePath).filename().string();
return compileSource(sourceCode, entryName, targetExe, options);
}
ExeCompileResult TzdExeCompiler::compileSource(
const std::string& sourceCode,
const std::string& entryName,
const std::string& outExePath,
const ExeCompileOptions& options)
{
auto startTime = std::chrono::high_resolution_clock::now();
ExeCompileResult res;
std::string targetExe = fs::absolute(outExePath).string();
res.outputExePath = targetExe;
printProgressBar(10, "正在读取并解析源代码 AST...");
if (options.onProgress) options.onProgress("Parsing AST & compiling bytecode...", 25);
// 1. Compile source to bytecode via ANTLR4 & TzdBytecodeCompiler
antlr4::ANTLRInputStream input(sourceCode);
TzdLangLexer lexer(&input);
antlr4::CommonTokenStream tokens(&lexer);
tokens.fill();
TzdLangParser parser(&tokens);
auto* tree = parser.program();
if (parser.getNumberOfSyntaxErrors() > 0) {
res.errorMessage = "Tzd compilation failed: " + std::to_string(parser.getNumberOfSyntaxErrors()) + " syntax error(s).";
return res;
}
// ========================================================================
// AOT Native Machine Code Compilation Mode (Default)
// Translates AST directly into native C++20 and compiles to standalone .exe
// ========================================================================
if (options.targetMode == ExeTargetMode::NATIVE_MACHINE_CODE || options.targetMode == ExeTargetMode::NATIVE_CODEGEN) {
printProgressBar(35, "正在分析语法树结构、类定义与符号表...");
if (options.onProgress) options.onProgress("Analyzing AST & symbols...", 35);
printProgressBar(60, "正在生成原生机器码 IR/C++ 高性能源码...");
if (options.onProgress) options.onProgress("Generating native C++...", 60);
// Resolve imported .tzd libraries so their classes and functions are compiled natively
std::vector<std::string> resolvedImports;
if (options.bundleStdlib) {
fs::path baseDir = fs::current_path();
resolvedImports = scanAndResolveImports(sourceCode, baseDir.string(), options.extraIncludePaths);
}
TzdNativeCodegen codegen;
std::string cppCode = codegen.generate(tree, entryName, options.cpuOnly, resolvedImports);
fs::path outPath(targetExe);
fs::path outDir = outPath.parent_path();
if (!outDir.empty() && !fs::exists(outDir)) {
std::error_code ec;
fs::create_directories(outDir, ec);
}
// If user requested only C++ source generation (--codegen):
if (options.targetMode == ExeTargetMode::NATIVE_CODEGEN) {
std::string cppOut = (outDir / (outPath.stem().string() + ".cpp")).string();
std::ofstream f(cppOut);
f << cppCode;
f.close();
res.success = true;
res.outputExePath = cppOut;
printProgressBar(100, "原生 C++ 代码生成完成!");
std::cout << std::endl;
auto endTime = std::chrono::high_resolution_clock::now();
res.durationSeconds = std::chrono::duration<double>(endTime - startTime).count();
return res;
}
// Invoke native MSVC/Clang compiler to produce machine code
printProgressBar(80, "正在调用原生编译器编译为 x86_64 机器码...");
if (options.onProgress) options.onProgress("Compiling to native machine code...", 80);
std::string cppPath = (outDir / ("tmp_" + outPath.stem().string() + ".cpp")).string();
std::ofstream cppFile(cppPath);
cppFile << cppCode;
cppFile.close();
std::string compileErr;
bool ok = compileCppToExe(cppPath, targetExe, options, compileErr);