-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuildtool.cpp
525 lines (433 loc) · 15.9 KB
/
buildtool.cpp
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
#include <iostream>
#include <string>
#include <vector>
#include <optional>
#include <fstream>
#include <filesystem>
#include <unordered_map>
#include <queue>
#include <cassert>
#include <cstdlib>
#include <spawn.h>
#include <unistd.h>
/* concatenate environment variables to the beginning of each command on Windows. There does not
* seem to be an easy way to modify the environment for commands launched with system() on MSYS2 */
static std::string unixEnv = "";
void setEnvitonmentVariable(const std::string &name, const std::string &value) {
#if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__)
unixEnv += name + "='" + value + "' ";
#else
int result = setenv(name.c_str(), value.c_str(), 1);
assert(result == 0);
#endif
}
int runCommand(std::string command) {
return system((unixEnv + command).c_str());
}
class Module {
private:
enum class Type {
SCRIPTS, MAKE, AUTOTOOLS
};
std::filesystem::path srcDir;
std::filesystem::path buildDir;
Type type;
void detectType();
void compile();
void install();
public:
std::string name;
uint32_t id;
std::filesystem::file_time_type lastModified;
std::filesystem::file_time_type lastBuilt;
std::vector<uint32_t> dependents;
size_t numDependencies;
bool dependenciesUpdated;
Module(std::string moduleName, uint32_t _id);
void loadDependencies();
void build();
};
static int ParallelJobs;
static std::string TargetArchitecture;
static std::string ModulesString;
static std::vector<Module> Modules;
static std::unordered_map<std::string, uint32_t> ModulesByName;
static std::vector<uint32_t> BuildOrder;
static std::filesystem::path BuildRoot;
/* for environment variables */
static std::string BuildRootString;
static std::string ParallelJobsString;
static std::string PathString;
static std::string SysrootString;
const char SPACES[] = " \t";
template <typename TP>
std::time_t to_time_t(TP tp)
{
using namespace std::chrono;
auto sctp = time_point_cast<system_clock::duration>(tp - TP::clock::now()
+ system_clock::now());
return system_clock::to_time_t(sctp);
}
std::filesystem::file_time_type lastModifiedDir(std::string path) {
std::filesystem::file_time_type result = std::filesystem::file_time_type::min();
for (const std::filesystem::directory_entry& entry:
std::filesystem::recursive_directory_iterator(path)) {
std::string filename = entry.path().string();
if (filename.find(".creator.") == std::string::npos
&& filename.find("/.qtc_clangd/") == std::string::npos
&& filename.substr(filename.length() - 4) != ".bak"
&& filename.substr(filename.length() - 7) != ".config"
&& filename.substr(filename.length() - 8) != ".creator"
&& filename.substr(filename.length() - 6) != ".files"
&& filename.substr(filename.length() - 9) != ".includes"
&& filename.substr(filename.length() - 7) != ".cflags"
&& filename.substr(filename.length() - 9) != ".cxxflags"
&& filename.substr(0, 1) != ".") {
result = max(result, entry.last_write_time());
}
}
return result;
}
Module::Module(std::string moduleName, uint32_t _id) :
name{moduleName},
id{_id},
numDependencies{0},
dependenciesUpdated{false} {
buildDir = BuildRoot;
srcDir = BuildRoot;
buildDir += "/build/" + TargetArchitecture + "/" + name;
srcDir += "/src/" + name;
detectType();
lastModified = lastModifiedDir("src/" + name);
/* if the module used sources from other directories, it specifies them is this file */
std::string sourceFileName = "src/" + name + "/source-dirs";
if (std::filesystem::exists(sourceFileName)) {
std::ifstream sourceDirsFile(sourceFileName);
if (!sourceDirsFile.is_open()) {
std::cerr << "Could not open source-dirs file for module " << name
<< " (" << sourceFileName << ")." << std::endl;
exit(1);
}
while(sourceDirsFile.good()) {
std::string sourceDir;
sourceDirsFile >> sourceDir;
if (sourceDir == "") {
continue;
}
lastModified = max(lastModified, lastModifiedDir("src/" + sourceDir));
}
}
try {
lastBuilt = std::filesystem::last_write_time(
"build/" + TargetArchitecture + "/" + name + "/timestamp");
} catch (std::filesystem::filesystem_error) {
lastBuilt = std::filesystem::file_time_type::min();
}
ModulesByName.insert({name, id});
}
void Module::loadDependencies() {
std::string fileName = "src/" + name + "/dependencies";
std::ifstream file(fileName);
if (!file.is_open()) {
std::cerr << "Could not open dependencies file for module " << name
<< " (" << fileName << ")." << std::endl;
exit(1);
}
while(file.good()) {
std::string entry;
file >> entry;
if (entry == "") {
return;
}
auto it = ModulesByName.find(entry);
if (it == ModulesByName.end()) {
std::cerr << "Module " << name << " has dependency '" << entry
<< "' which is not an enabled module." << std::endl;
exit(1);
}
numDependencies++;
Modules[it->second].dependents.push_back(id);
}
}
void Module::build() {
std::string buildDirString = buildDir.string();
std::filesystem::current_path(srcDir);
setEnvitonmentVariable("SHBUILD_BUILD_DIR", buildDirString);
std::filesystem::create_directories(buildDir);
if (!dependenciesUpdated && lastBuilt > lastModified) {
std::cout << "buildtool: skipping " << name
<< "(no changes)" << std::endl;
} else {
if (dependenciesUpdated) {
std::cout << "buildtool: completely rebuilding " << name
<< " (changed dependencies)" << std::endl;
try {
std::filesystem::remove_all(buildDir);
std::filesystem::create_directories(buildDir);
} catch (std::filesystem::filesystem_error) {
std::cerr << "unable to remove build/" << name << std::endl;
exit(1);
}
} else {
std::cout << "buildtool: building " << name << " (changes)" << std::endl;
}
compile();
std::filesystem::current_path(srcDir);
install();
std::ofstream timestampFile(buildDir.string() + "/timestamp",
std::ios_base::openmode::_S_out | std::ios_base::openmode::_S_trunc);
assert(timestampFile.is_open());
timestampFile << "!";
timestampFile.close();
for (uint32_t dep: dependents) {
Modules[dep].dependenciesUpdated = true;
}
}
std::filesystem::current_path(BuildRoot);
setEnvitonmentVariable("SHBUILD_BUILD_DIR", "");
}
void Module::detectType() {
std::filesystem::current_path(srcDir);
if (std::filesystem::exists("build-script")) {
type = Type::SCRIPTS;
} else if (std::filesystem::exists("configure")) {
type = Type::AUTOTOOLS;
} else if (std::filesystem::exists("Makefile")) {
type = Type::MAKE;
} else {
std::cerr << "Could not detect type of module " << name << "."
<< "It has neiter build-script nor Makefile nor configure." << std::endl;
exit(1);
}
std::filesystem::current_path(BuildRoot);
}
void Module::compile() {
switch (type) {
case Type::AUTOTOOLS:
std::filesystem::current_path(buildDir);
if (runCommand((srcDir.string() + "/configure --host=" + TargetArchitecture + "-zagto-zagtos").c_str())) {
std::cerr << "Could not run configure script of module " << name << "." << std::endl;
exit(1);
}
/* fall-through */
case Type::MAKE:
if (runCommand(("make -j" + ParallelJobsString + " > /dev/null").c_str())) {
std::cerr << "Could not run Makefile of module " << name << "." << std::endl;
exit(1);
}
break;
case Type::SCRIPTS:
if (runCommand("./build-script > /dev/null")) {
std::cerr << "Could not run build-script of module " << name << "." << std::endl;
exit(1);
}
}
}
void Module::install() {
std::cout << "buildtool: installing " << name << std::endl;
switch (type) {
case Type::AUTOTOOLS:
std::filesystem::current_path(buildDir);
if (runCommand(("make install -j" + ParallelJobsString + " DESTDIR=" + BuildRootString).c_str())) {
std::cerr << "Could not run Makefile Install target of module " << name << "." << std::endl;
exit(1);
}
break;
case Type::MAKE:
if (runCommand(("make install -j" + ParallelJobsString + " > /dev/null").c_str())) {
std::cerr << "Could not run Makefile Install target of module " << name << "." << std::endl;
exit(1);
}
break;
case Type::SCRIPTS:
if (runCommand("./install-script > /dev/null")) {
std::cerr << "Could not run install-script of module " << name << "." << std::endl;
exit(1);
}
}
}
void prepareEnvironment() {
BuildRoot = std::filesystem::current_path();
BuildRootString = BuildRoot.string();
setEnvitonmentVariable("SHBUILD_ROOT", BuildRootString);
ParallelJobsString = std::to_string(ParallelJobs);
setEnvitonmentVariable("PARALLEL_JOBS", ParallelJobsString);
PathString = BuildRootString + "/out/" + TargetArchitecture + "/toolchain/kernel/bin:"
+ BuildRootString + "/out/" + TargetArchitecture + "/toolchain/system/bin:"
+ getenv("PATH");
setEnvitonmentVariable("PATH", PathString);
SysrootString = BuildRootString + "/out/" + TargetArchitecture + "/toolchain/sysroot";
setEnvitonmentVariable("SYSROOT", SysrootString.c_str());
setEnvitonmentVariable("KERNEL_CFLAGS_x86_64", "-mcmodel=large -mno-red-zone -D _ZAGTOS_KERNEL=1");
setEnvitonmentVariable("KERNEL_CFLAGS_aarch64", "-mcmodel=large -D _ZAGTOS_KERNEL=1");
setEnvitonmentVariable("AS_x86_64", "nasm -f elf64");
setEnvitonmentVariable("AS_aarch64", "aarch64-elf-as");
setEnvitonmentVariable("ARCH", TargetArchitecture);
std::cout << "target arch: " << TargetArchitecture << std::endl;
try {
std::filesystem::remove_all("out/" + TargetArchitecture + "/esp");
} catch (std::filesystem::filesystem_error) {
std::cerr << "unable to clear out/" << std::endl;
exit(1);
}
}
void createBootImage() {
if (runCommand((std::string("./create-boot-image-")
+ TargetArchitecture
+ std::string(".sh")).c_str())) {
std::cerr << "buildtool: error during create-boot-image-" << TargetArchitecture << ".sh"
<< std::endl;
exit(1);
}
}
void loadModules() {
size_t position = 0;
while (true) {
size_t nameStart = ModulesString.find_first_not_of(SPACES, position);
if (nameStart == std::string::npos) {
return;
}
size_t nameEnd = ModulesString.find_first_of(SPACES, nameStart);
if (nameEnd == std::string::npos) {
nameEnd = ModulesString.size();
}
std::string name = ModulesString.substr(nameStart, nameEnd - nameStart);
try {
Modules.emplace_back(name, Modules.size());
} catch (std::filesystem::filesystem_error) {
std::cerr << "Could not find module '" << name << "'" << std::endl;
exit(1);
}
position = nameEnd + 1;
}
}
void solveBuildOrder() {
std::queue<uint32_t> q;
for (const Module &mod: Modules) {
if (mod.numDependencies == 0) {
q.push(mod.id);
}
}
while (!q.empty()) {
Module &mod = Modules[q.front()];
q.pop();
for (uint32_t id: mod.dependents) {
Module &other = Modules[id];
other.numDependencies--;
if (other.numDependencies == 0) {
q.push(id);
}
}
BuildOrder.push_back(mod.id);
}
bool foundProblem = false;
for (const Module &mod: Modules) {
if (mod.numDependencies != 0) {
if (foundProblem) {
std::cerr << ", ";
} else {
std::cerr << "The following modules have cyclic dependencies: ";
foundProblem = true;
}
std::cerr << mod.name;
}
}
if (foundProblem) {
std::cerr << "." << std::endl;
exit(1);
}
}
void buildModules() {
for (uint32_t id: BuildOrder) {
Modules[id].build();
}
}
void loadConfig() {
std::ifstream file("config.ini");
if (!file.is_open()) {
std::cerr << "Could not open config.ini file" << std::endl;
exit(1);
}
bool modules{false}, architecture{false}, parallelJobs{false};
size_t lineNumber = 0;
while (file.good()) {
std::string line;
getline(file, line);
lineNumber++;
size_t commentStart = line.find_first_of("#;");
line = line.substr(0, commentStart);
size_t assignmentPos = line.find('=');
size_t keyStart = line.find_first_not_of(SPACES, 0);
size_t keyEnd = line.find_last_not_of(SPACES, assignmentPos - 1);
if (assignmentPos == std::string::npos) {
if (keyStart == std::string::npos) {
/* empty line */
continue;
} else {
std::cerr << "config.ini: " << lineNumber << ": missing '='" << std::endl;
exit(1);
}
} else {
if (assignmentPos == 0 || keyEnd == std::string::npos) {
std::cerr << "config.ini: " << lineNumber << ": missing option name" << std::endl;
exit(1);
}
}
size_t valueStart = line.find_first_not_of(SPACES, assignmentPos + 1);
size_t valueEnd = line.find_last_not_of(SPACES);
std::string value;
if (valueStart == std::string::npos) {
value = "";
} else {
value = line.substr(valueStart, valueEnd + 1 - valueStart);
}
std::string key = line.substr(keyStart, keyEnd - keyStart + 1);
if (key == "PARALLEL_JOBS" && !parallelJobs) {
try {
ParallelJobs = std::stoi(value);
} catch (...) {
ParallelJobs = 0;
}
if (ParallelJobs <= 0) {
std::cerr << "config.ini: PARALLEL_JOBS must be a positive integer." << std::endl;
exit(1);
}
parallelJobs = true;
} else if (key == "ARCH" && !architecture) {
if (value != "x86_64" && value != "aarch64") {
std::cerr << "config.ini: ARCH must be one of the following: x86_64, aarch64."
<< std::endl;
exit(1);
}
TargetArchitecture = value;
architecture = true;
} else if (key == "MODULES" && !modules) {
ModulesString = value;
modules = true;
} else {
std::cerr << "config.ini: " << lineNumber << ": option '" << key
<< "' is either invalid or already defined." << std::endl;
exit(1);
}
}
if (!modules || !parallelJobs || !architecture) {
std::cerr << "config.ini: must set the following options: MODULES, ARCH, PARALLEL_JOBS."
<< std::endl;
exit(1);
}
}
int main(int argc, char **argv) {
if (argc > 1) {
std::cerr << "Usage: " << argv[0] << std::endl;
return 1;
}
loadConfig();
prepareEnvironment();
loadModules();
for (auto &mod: Modules) {
mod.loadDependencies();
}
solveBuildOrder();
buildModules();
createBootImage();
}