-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuilder.cpp
More file actions
350 lines (318 loc) · 11 KB
/
Builder.cpp
File metadata and controls
350 lines (318 loc) · 11 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
#include "AST.hpp"
#include "BreakPointer.hpp"
#include "CharStream.hpp"
#include "Compiler.hpp"
#include "FilePosition.hpp"
#include "Tokenizer.hpp"
#include "globals.hpp"
#include "trie.hpp"
#include "utils.hpp"
#include <bits/getopt_core.h>
#include <cctype>
#include <chrono>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <exception>
#include <filesystem>
#include <fstream>
#include <functional>
#include <getopt.h>
#include <iostream>
#include <memory>
#include <ratio>
#include <stdexcept>
#include <string>
#include <thread>
const char *helpMessage =
"Ost program builder\n"
"usage: ostbuild <program>.ost [OPTIONS]\n"
"Description:\n"
"Used for building programs that have multiple libs\n"
"if compiled lib is older that actual lib source so lib is rebuilded"
"\n"
"Awailable options are shown below:\n"
"-h, --help : print this help message and quit\n"
"-v, --verbose : detailed output if --print-debug-info enabled\n"
"-s, --sources : comma separated paths to places where sourse files(.ost) "
"are stored\n"
"\t(requires argument, default: './')\n"
"-f, --force : force recompile all"
"\t(default: false)\n"
"-l, --libdir : directory where precompiled libraries(.tu4, not source "
".ost files!) are stored, rebuilded libraries will be stored here\n"
"\t(requires argument, default: ./)\n"
"-o, --outputdir : directory where compiled <program>.ost program will be "
"stored\n"
"\t(requires argument, default: ./)\n"
"-g, --enable-breakpoints : enabling breakpoints setting option. Program "
"compiled with this flag can be debugged with ostdb utility\n"
"\t(default: disabled)\n"
"-d, --print-debug-info : enabling debug info\n"
"\t(default: disabled)\n"
"-b, --use-binary-format : save compiled sequence of commands in binary "
"format. Significant speed up to loading and saving\n"
"\t(default: disabled)\n"
"\n";
using duration_t = std::chrono::milliseconds;
const char *durationSuffix = "ms";
std::set<std::string> sourcePaths;
std::string sources = "./";
void fillSources(const std::string &s);
void parseCommandArgs(int argc, char *argw[]);
void compileMainProgram(const std::string &filename);
int main(int argc, char *argw[]) {
if (argc < 2)
throw std::invalid_argument("Usage: ostbuild <program>.ost [OPTIONS]");
std::string fileName = argw[1];
parseCommandArgs(argc, argw);
if (globals::outDir[globals::outDir.size() - 1] != '/')
globals::outDir.push_back('/');
if (globals::libDir[globals::libDir.size() - 1] != '/')
globals::libDir.push_back('/');
if (globals::enableBreakpoints)
globals::breakpointer =
std::shared_ptr<IBreakpointer>{new FileBreakpointer()};
if (!sources.empty())
fillSources(sources);
auto start_ts = std::chrono::system_clock::now();
compileMainProgram(fileName);
auto end_ts = std::chrono::system_clock::now();
logger::debug() << "BUILDER: total building time: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(
end_ts - start_ts)
.count()
<< "ms";
return 0;
}
SIZE_T nWorkers = 1;
bool forceRecompile = false;
void parseCommandArgs(int argc, char *argw[]) {
SIZE_T nopts = 11;
option *options = new option[nopts]{
{.name = "libdir", .has_arg = 1, .flag = NULL, .val = 'l'},
{.name = "outputdir", .has_arg = 1, .flag = NULL, .val = 'o'},
{.name = "enable-breakpoints", .has_arg = 0, .flag = NULL, .val = 'g'},
{.name = "print-debug-info", .has_arg = 0, .flag = NULL, .val = 'd'},
{.name = "force", .has_arg = 0, .flag = NULL, .val = 'f'},
{.name = "use-binary-format", .has_arg = 0, .flag = NULL, .val = 'b'},
{.name = "sources", .has_arg = 1, .flag = NULL, .val = 's'},
{.name = "jobs", .has_arg = 1, .flag = NULL, .val = 'j'},
{.name = "verbose", .has_arg = 0, .flag = NULL, .val = 'v'},
{.name = "help", .has_arg = 0, .flag = NULL, .val = 'h'}};
memset(&options[nopts - 1], 0, sizeof(option));
int arg, longindex;
while ((arg = getopt_long(argc, argw, "l:o:gdbhs:j:fv", options,
&longindex)) != -1) {
switch (arg) {
case '?':
logger::warning() << "Unrecognized option: " << optarg << std::endl;
break;
case 'l':
globals::libDir = optarg;
logger::info() << "Used library directory: " << optarg;
break;
case 'o':
globals::outDir = optarg;
logger::info() << "Used output directory: " << optarg;
break;
case 'v':
globals::verboseOutput = true;
logger::info() << "Verbose output enabled";
break;
case 'g':
globals::enableBreakpoints = true;
logger::info() << "Breakpoints enabled";
break;
case 'd':
globals::printDebugInfo = true;
logger::info() << "Printing debug info enabled";
break;
case 'b':
globals::useBinaryFormat = true;
logger::info() << "Binary format to saving enabled";
break;
case 's':
logger::info() << "Additional sources are added";
sources = std::string(optarg);
break;
case 'f':
logger::info() << "Force recompile enabled";
forceRecompile = true;
break;
case 'j':
nWorkers = atoll(optarg);
logger::info() << "Using " << nWorkers << " jobs";
break;
case 'h':
std::cout << helpMessage << std::endl;
exit(0);
break;
default:
logger::warning() << "Given option: [" << char(arg)
<< "] can't be processed";
}
}
delete[] options;
}
/**
* @brief Creates ast tree from program in file
*
* @param fileName -- path to program
*
* @return ast Tree
*/
ast::Tree toAST(const std::string &fileName) {
ast::Tree::clearNamesTable();
static token::Tokenizer tokenizer;
std::ifstream file(fileName);
if (!file.is_open())
throw std::invalid_argument(strfast() << "Can't open file: " << fileName);
CharStream stream(file);
FileRoller roller(std::make_shared<std::string>(fileName));
auto tokens = tokenizer.parse(stream, roller);
ast::Tree ast{tokens, fileName};
if (globals::verboseOutput) {
logger::debug out;
out << "Detected libs for [" << fileName << "]:\n";
for (const auto &lib : ast.libs)
out << lib << '\n';
}
return ast;
}
struct ThrowSourceNotFoundError {
ThrowSourceNotFoundError(const std::string &mt) {
throw std::invalid_argument(
strfast()
<< "Source(.ost) file for mt [" << mt
<< "] not found in source directories\n"
<< "Please ensure that it exist in directories specified by -s flag");
}
};
using mt_name_t = std::string;
using is_resolved_policy_t = std::function<bool(const mt_name_t &)>;
bool isSrcNotEditedAfterCompilation(const mt_name_t &mt,
bool useOutputDirAsCompiledMTDir);
static is_resolved_policy_t defaultPolicy = [](const mt_name_t &mt) {
return isSrcNotEditedAfterCompilation(mt, false);
};
class DependencyCollector {
impl::Trie<std::set<mt_name_t>> deps_;
impl::Trie<bool> isresolved_;
impl::Trie<std::string> mtNameFileNameMap_;
impl::Trie<bool> isbuilded_;
public:
DependencyCollector() = default;
std::set<mt_name_t> getDepsFor(const mt_name_t &mt) {
return deps_.find(mt).value_or(std::set<mt_name_t>{});
}
std::string getFileNameFor(const mt_name_t &mt) {
const auto &value = mtNameFileNameMap_.find(mt);
if (!value.has_value())
ThrowSourceNotFoundError error(mt);
return value.value();
}
/**
* @brief collects depedencies from program
*
* @param path -- path to program
*
* @return program main mt name
*/
mt_name_t collect(const std::filesystem::path &path,
is_resolved_policy_t policy = defaultPolicy) {
if (globals::verboseOutput)
logger::info() << "Parsing: " << path << '\n';
auto ast = toAST(path);
const auto &mtName = ast.getTreeName();
mtNameFileNameMap_.add(mtName, path);
const auto &libs = ast.libs;
std::set<mt_name_t> deps;
for (const auto &lib : libs)
deps.insert(lib);
deps_.add(mtName, deps);
isresolved_.add(mtName, policy(mtName));
if (globals::verboseOutput)
logger::log() << "Parsing: OK\n";
return mtName;
}
bool resolve(const mt_name_t &mt, bool useLibDirAsOutputDir = true) {
if (isbuilded_.contains(mt))
return false;
bool childsChanged = false;
// resolving depedencies first
const auto &deps = getDepsFor(mt);
for (const auto &dep : deps) {
childsChanged = resolve(dep) || childsChanged;
}
if (isresolved_.find(mt).value_or(false) && !childsChanged &&
!forceRecompile) {
logger::info() << "MT [" << mt << "] is up to date. Skipping";
} else {
std::string fileName = getFileNameFor(mt);
{
logger::info out;
if (forceRecompile)
out << "MT [" << mt << "] is forced to recompile";
else
out << "MT [" << mt << "] was changed after compilation";
out << "\n\tFollowing file will be recompiled: " << fileName;
}
compileAndSaveProgram(
fileName, globals::libDir,
(useLibDirAsOutputDir ? globals::libDir : globals::outDir),
globals::useBinaryFormat, globals::enableBreakpoints,
globals::verboseOutput);
*isresolved_.find(mt) = true;
childsChanged = true;
}
isbuilded_.add(mt, true);
return childsChanged;
}
};
static DependencyCollector depsCollector;
namespace fs = std::filesystem;
void fillSources(const std::string &s) {
auto paths = split(s, ',');
for (const auto &path : paths) {
sourcePaths.insert(strip(path));
}
for (const auto &path : sourcePaths) {
logger::debug out;
fs::directory_iterator it(path);
for (auto &file : it) {
if (file.path().extension() == ".ost") {
try {
depsCollector.collect(file.path());
} catch (std::exception &e) {
out << "Can't create AST from [" << file << "]. Skipping\n";
continue;
}
}
}
}
}
void compileMainProgram(const std::string &fileName) {
mt_name_t mt = depsCollector.collect(fileName, [](const mt_name_t &mt) {
return isSrcNotEditedAfterCompilation(mt, true);
});
depsCollector.resolve(mt, false);
}
bool isSrcNotEditedAfterCompilation(const mt_name_t &mt,
bool useOutputDirAsCompiledMTDir = false) {
fs::path compiledMtPath;
if (useOutputDirAsCompiledMTDir)
compiledMtPath = globals::outDir;
else
compiledMtPath = globals::libDir;
compiledMtPath.append(mt + ".tu4");
if (!fs::exists(compiledMtPath))
return false;
std::string srcPath = depsCollector.getFileNameFor(mt);
fs::file_time_type srcModifyTime = fs::last_write_time(srcPath),
compiledModifyTime = fs::last_write_time(compiledMtPath);
if (globals::verboseOutput)
logger::debug() << "mt: " << mt << " src modify time " << srcModifyTime
<< " compiled modify time " << compiledModifyTime;
return srcModifyTime < compiledModifyTime;
};