-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.cpp
More file actions
234 lines (188 loc) · 6.03 KB
/
compiler.cpp
File metadata and controls
234 lines (188 loc) · 6.03 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
#include "compiler.hpp"
#include <iostream>
#include <sstream>
#include <cstdlib>
#ifdef _WIN32
#include <direct.h>
#include <windows.h>
#define mkdir(dir, mode) _mkdir(dir)
#else
#include <sys/stat.h>
#include <sys/types.h>
#endif
Compiler::Compiler(const BuildConfig& config) : config(config) {
}
bool Compiler::createBuildDir() {
if (!depChecker.fileExists(config.build_dir)) {
std::cout << "Creating build directory: " << config.build_dir << std::endl;
#ifdef _WIN32
if (_mkdir(config.build_dir.c_str()) != 0) {
#else
if (mkdir(config.build_dir.c_str(), 0755) != 0) {
#endif
std::cerr << "Error: Failed to create build directory" << std::endl;
return false;
}
}
return true;
}
std::string Compiler::getObjectFilePath(const std::string& sourceFile) {
// 从源文件路径提取文件名
size_t lastSlash = sourceFile.find_last_of("/\\");
std::string filename = (lastSlash != std::string::npos) ?
sourceFile.substr(lastSlash + 1) : sourceFile;
// 替换扩展名为 .o
size_t lastDot = filename.find_last_of(".");
if (lastDot != std::string::npos) {
filename = filename.substr(0, lastDot);
}
return config.build_dir + "/" + filename + ".o";
}
std::string Compiler::getOutputFilePath() {
std::string outputName = config.output_name.empty() ?
config.project_name : config.output_name;
#ifdef _WIN32
if (config.output_type == "executable") {
outputName += ".exe";
} else {
outputName += ".dll";
}
#else
if (config.output_type == "library") {
outputName = "lib" + outputName + ".so";
}
#endif
return config.build_dir + "/" + outputName;
}
std::string Compiler::buildCompileCommand(const std::string& sourceFile,
const std::string& objectFile) {
std::stringstream cmd;
cmd << config.compiler << " ";
// C++ 标准
cmd << "-std=" << config.cpp_standard << " ";
// 优化级别
cmd << "-" << config.optimization << " ";
// 调试信息
if (config.debug) {
cmd << "-g ";
}
// 包含目录
for (const auto& includeDir : config.include_dirs) {
cmd << "-I" << includeDir << " ";
}
// 额外的编译标志
for (const auto& flag : config.compile_flags) {
cmd << flag << " ";
}
// 编译为目标文件
cmd << "-c " << sourceFile << " -o " << objectFile;
return cmd.str();
}
std::string Compiler::buildLinkCommand() {
std::stringstream cmd;
cmd << config.compiler << " ";
// 所有目标文件
for (const auto& objFile : objectFiles) {
cmd << objFile << " ";
}
// 库目录
for (const auto& libDir : config.library_dirs) {
cmd << "-L" << libDir << " ";
}
// 库文件
for (const auto& lib : config.libraries) {
cmd << "-l" << lib << " ";
}
// 额外的链接标志
for (const auto& flag : config.link_flags) {
cmd << flag << " ";
}
// 输出文件
cmd << "-o " << getOutputFilePath();
// 如果是共享库
if (config.output_type == "library") {
cmd << " -shared";
}
return cmd.str();
}
bool Compiler::executeCommand(const std::string& command) {
std::cout << "Executing: " << command << std::endl;
int result = system(command.c_str());
return result == 0;
}
bool Compiler::compileSource(const std::string& sourceFile,
const std::string& objectFile) {
// 检查是否需要重新编译
if (!depChecker.needsRecompile(sourceFile, objectFile)) {
std::cout << "Skipping " << sourceFile << " (up to date)" << std::endl;
return true;
}
std::cout << "Compiling " << sourceFile << "..." << std::endl;
std::string command = buildCompileCommand(sourceFile, objectFile);
if (!executeCommand(command)) {
std::cerr << "Error: Failed to compile " << sourceFile << std::endl;
return false;
}
return true;
}
bool Compiler::linkObjects() {
std::cout << "\nLinking..." << std::endl;
std::string command = buildLinkCommand();
if (!executeCommand(command)) {
std::cerr << "Error: Failed to link" << std::endl;
return false;
}
return true;
}
bool Compiler::build() {
std::cout << "\n=== Starting Build ===" << std::endl;
std::cout << "Project: " << config.project_name << std::endl;
std::cout << "Output: " << getOutputFilePath() << "\n" << std::endl;
// 创建构建目录
if (!createBuildDir()) {
return false;
}
// 编译所有源文件
objectFiles.clear();
bool allCompiled = true;
for (const auto& sourceFile : config.source_files) {
std::string objectFile = getObjectFilePath(sourceFile);
objectFiles.push_back(objectFile);
if (!compileSource(sourceFile, objectFile)) {
allCompiled = false;
break;
}
}
if (!allCompiled) {
std::cerr << "\nBuild failed during compilation" << std::endl;
return false;
}
// 链接
if (!linkObjects()) {
std::cerr << "\nBuild failed during linking" << std::endl;
return false;
}
std::cout << "\n=== Build Successful ===" << std::endl;
std::cout << "Output: " << getOutputFilePath() << std::endl;
return true;
}
bool Compiler::clean() {
std::cout << "Cleaning build directory..." << std::endl;
#ifdef _WIN32
std::string command = "rmdir /s /q " + config.build_dir;
#else
std::string command = "rm -rf " + config.build_dir;
#endif
if (depChecker.fileExists(config.build_dir)) {
system(command.c_str());
std::cout << "Clean complete" << std::endl;
} else {
std::cout << "Build directory does not exist" << std::endl;
}
return true;
}
bool Compiler::rebuild() {
std::cout << "=== Rebuilding ===" << std::endl;
clean();
return build();
}