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
|
#include <vector>
#include <string>
#include <filesystem>
#include <iostream>
#include <utility>
#include <list>
#include <chrono>
#include <thread>
#include <memory>
#include <algorithm>
#include <list>
#include <array>
#include <deque>
#include <getoptpp/getoptpp.hpp>
#include "libcppbuild.h"
#include "task_cc.h"
#include "task_ld.h"
#include "task_ar.h"
#include "task_so.h"
#include "settings.h"
#include "execute.h"
#include <unistd.h>
using namespace std::chrono_literals;
std::list<std::shared_ptr<Task>> taskFactory(const BuildConfiguration& config,
const Settings& settings,
const std::string& sourceDir)
{
std::filesystem::path targetFile(config.target);
std::vector<std::string> objects;
std::list<std::shared_ptr<Task>> tasks;
for(const auto& file : config.sources)
{
tasks.emplace_back(std::make_shared<TaskCC>(config, settings,
sourceDir, file));
objects.push_back(tasks.back()->target());
}
if(targetFile.extension() == ".a")
{
// static lib
tasks.emplace_back(std::make_shared<TaskAR>(config, settings, config.target,
objects));
}
else if(targetFile.extension() == ".so")
{
if(targetFile.stem().string().substr(0, 3) != "lib")
{
std::cerr << "Dynamic library target must have 'lib' prefix\n";
exit(1);
}
// dynamic lib
tasks.emplace_back(std::make_shared<TaskSO>(config, settings, config.target,
objects));
}
else
{
// executable
tasks.emplace_back(std::make_shared<TaskLD>(config, settings, config.target,
objects));
}
return tasks;
}
std::shared_ptr<Task> getNextTask(const std::list<std::shared_ptr<Task>>& allTasks,
std::list<std::shared_ptr<Task>>& dirtyTasks)
{
for(auto dirtyTask = dirtyTasks.begin();
dirtyTask != dirtyTasks.end();
++dirtyTask)
{
//std::cout << "Examining target " << (*dirtyTask)->target() << "\n";
if((*dirtyTask)->ready())
{
dirtyTasks.erase(dirtyTask);
return *dirtyTask;
}
}
//std::cout << "No task ready ... \n";
return nullptr;
}
struct Config
{
const char* file;
std::vector<BuildConfiguration> (*cb)();
};
std::array<Config, 100> configFiles;
int numConfigFiles{0};
// TODO: Use c++20 when ready, somehing like this:
//int reg(const std::source_location location = std::source_location::current())
int reg(const char* location,
std::vector<BuildConfiguration> (*cb)())
{
// NOTE: std::cout cannot be used here
configFiles[numConfigFiles].file = location;
configFiles[numConfigFiles].cb = cb;
++numConfigFiles;
return 0;
}
void recompileCheck(const Settings& settings, int argc, char* argv[])
{
bool dirty{false};
std::vector<std::string> args;
args.push_back("-s");
args.push_back("-O3");
args.push_back("-std=c++17");
args.push_back("-pthread");
std::filesystem::path binFile("cppbuild");
if(settings.verbose > 1)
{
std::cout << "Recompile check (" << numConfigFiles << "):\n";
}
for(int i = 0; i < numConfigFiles; ++i)
{
std::string location = configFiles[i].file;
if(settings.verbose > 1)
{
std::cout << " - " << location << "\n";
}
std::filesystem::path configFile(location);
if(std::filesystem::last_write_time(binFile) <=
std::filesystem::last_write_time(configFile))
{
dirty = true;
}
// Support adding multiple config functions from the same file
if(std::find(args.begin(), args.end(), location) == std::end(args))
{
args.push_back(location);
}
}
args.push_back("libcppbuild.a");
args.push_back("-o");
args.push_back(binFile.string());
if(dirty)
{
std::cout << "Rebuilding config\n";
auto ret = execute("/usr/bin/g++", args);
if(ret != 0)
{
std::cerr << "Failed.\n";
exit(1);
}
else
{
std::cout << "Re-launch\n";
std::vector<std::string> args;
for(int i = 1; i < argc; ++i)
{
args.push_back(argv[i]);
}
exit(execute(argv[0], args, settings.verbose));
}
}
}
int main(int argc, char* argv[])
{
Settings settings{};
settings.builddir = "build/";
settings.parallel_processes =
std::max(1u, std::thread::hardware_concurrency() * 2 - 1);
settings.verbose = 0;
dg::Options opt;
opt.add("jobs", required_argument, 'j',
"Number of parallel jobs. (default: cpucount * 2 - 1 )",
[&]() {
try
{
settings.parallel_processes = std::stoi(optarg);
}
catch(...)
{
std::cerr << "Not a number\n";
return 1;
}
return 0;
});
opt.add("build-dir", required_argument, 'b',
"Set output directory for build files (default: build).",
[&]() {
settings.builddir = optarg;
return 0;
});
opt.add("verbose", no_argument, 'v',
"Be verbose. Add multiple times for more verbosity.",
[&]() {
settings.verbose++;
return 0;
});
opt.add("help", no_argument, 'h',
"Print this help text.",
[&]() {
std::cout << "usage stuff\n";
opt.help();
return 0;
});
opt.process(argc, argv);
recompileCheck(settings, argc, argv);
std::filesystem::path builddir(settings.builddir);
std::filesystem::create_directories(builddir);
std::deque<BuildConfiguration> build_configs;
std::list<std::shared_ptr<Task>> tasks;
for(int i = 0; i < numConfigFiles; ++i)
{
std::string path =
std::filesystem::path(configFiles[i].file).parent_path();
if(settings.verbose > 1)
{
std::cout << configFiles[i].file << " in path " << path << "\n";
}
auto configs = configFiles[i].cb();
for(const auto& config : configs)
{
build_configs.push_back(config);
const auto& build_config = build_configs.back();
std::vector<std::string> objects;
auto t = taskFactory(build_config, settings, path);
tasks.insert(tasks.end(), t.begin(), t.end());
}
}
for(auto task : tasks)
{
task->registerDepTasks(tasks);
}
std::list<std::shared_ptr<Task>> dirtyTasks;
for(auto task : tasks)
{
if(task->dirty())
{
dirtyTasks.push_back(task);
}
}
for(auto const &arg : opt.arguments())
{
if(arg == "clean")
{
std::cout << "Cleaning\n";
//std::filesystem::remove_all(builddir);
for(auto& task : tasks)
{
if(task->clean() != 0)
{
return 1;
}
}
return 0;
}
}
if(dirtyTasks.empty())
{
return 0;
}
std::cout << "Building\n";
std::list<std::future<int>> processes;
// Start all tasks
bool done{false};
while(!done)
{
bool started_one{false};
while(processes.size() < settings.parallel_processes)
{
if(dirtyTasks.empty())
{
done = true;
break;
}
auto task = getNextTask(tasks, dirtyTasks);
if(task == nullptr)
{
break;
//return 1;
}
processes.emplace_back(
std::async(std::launch::async,
[task]()
{
return task->run();
}));
started_one = true;
std::this_thread::sleep_for(2ms);
}
for(auto process = processes.begin();
process != processes.end();
++process)
{
if(process->valid())
{
if(process->get() != 0)
{
return 1;
}
processes.erase(process);
break;
}
}
if(started_one)
{
std::this_thread::sleep_for(2ms);
}
else
{
std::this_thread::sleep_for(200ms);
}
}
for(auto process = processes.begin();
process != processes.end();
++process)
{
process->wait();
auto ret = process->get();
if(ret != 0)
{
return 1;
}
}
return 0;
}
|