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
|
// -*- c++ -*-
// Distributed under the BSD 2-Clause License.
// See accompanying file LICENSE for details.
#include <vector>
#include <string>
#include <filesystem>
#include <iostream>
#include <utility>
#include <list>
#include <thread>
#include <memory>
#include <algorithm>
#include <list>
#include <array>
#include <deque>
#include <fstream>
#include <cstdlib>
#include <set>
#include <getoptpp/getoptpp.hpp>
#include "ctor.h"
#include "configure.h"
#include "rebuild.h"
#include "tasks.h"
#include "build.h"
#include "unittest.h"
int main(int argc, char* argv[])
{
ctor::settings settings{};
const auto& c = ctor::get_configuration();
settings.builddir = c.get(ctor::cfg::builddir, settings.builddir);
settings.parallel_processes =
std::max(1u, std::thread::hardware_concurrency()) * 2 - 1;
if(argc > 1 && std::string(argv[1]) == "configure")
{
return configure(settings, argc, argv);
}
if(argc > 1 && std::string(argv[1]) == "reconfigure")
{
return reconfigure(settings, argc, argv);
}
bool write_compilation_database{false};
std::string compilation_database;
bool print_configure_cmd{false};
bool print_configure_db{false};
std::vector<std::string> add_files;
std::vector<std::string> remove_files;
bool no_default_build{false}; // set to true to prevent empty arg list building 'all' target.
bool list_files{false};
bool list_targets{false};
bool no_relaunch{false}; // true means no re-launch after rebuild.
bool run_unittests{false};
dg::Options opt;
int key{128};
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',
"Overload output directory for build files (default: '" +
settings.builddir + "').",
[&]() {
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("quiet", no_argument, 'q',
"Be completely silent.",
[&]() {
settings.verbose = -1;
return 0;
});
opt.add("add", required_argument, 'a',
"Add specified file to the build configurations.",
[&]() {
no_relaunch = true;
add_files.push_back(optarg);
return 0;
});
opt.add("remove", required_argument, 'r',
"Remove specified file from the build configurations.",
[&]() {
no_relaunch = true;
remove_files.push_back(optarg);
return 0;
});
opt.add("list-files", no_argument, 'L',
"List files in the build configurations.",
[&]() {
no_relaunch = true;
list_files = true;
return 0;
});
opt.add("list-targets", no_argument, 'l',
"List targets.",
[&]() {
no_relaunch = true;
list_targets = true;
return 0;
});
opt.add("configure-cmd", no_argument, key++,
"Print commandline for last configure.",
[&]() {
no_relaunch = true;
print_configure_cmd = true;
return 0;
});
opt.add("configure-db", no_argument, key++,
"Print entire configure parameter database.",
[&]() {
no_relaunch = true;
print_configure_db = true;
return 0;
});
opt.add("database", required_argument, 'd',
"Write compilation database json file.",
[&]() {
no_relaunch = true;
write_compilation_database = true;
compilation_database = optarg;
return 0;
});
opt.add("help", no_argument, 'h',
"Print this help text.",
[&]() {
std::cout << "Usage: " << argv[0] << " [options] [target] ...\n";
std::cout <<
R"_( where target can be either:
configure - run configuration step (cannot be used with other targets).
reconfigure - rerun configuration step with the same arguments as last (cannot be used with other targets).
clean - clean all generated files.
all - build all targets (default)
or the name of a target which will be built along with its dependencies.
Use '-l' to see a list of possible target names.
Options:
)_";
opt.help();
exit(0);
return 0;
});
opt.process(argc, argv);
auto verbose_env = std::getenv("V");
if(verbose_env)
{
settings.verbose = std::atoi(verbose_env);
}
if(list_files)
{
no_default_build = true;
std::set<std::string> files;
for(std::size_t i = 0; i < numConfigFiles; ++i)
{
files.insert(configFiles[i].file);
}
for(std::size_t i = 0; i < numExternalConfigFiles; ++i)
{
files.insert(externalConfigFiles[i].file);
}
for(const auto& file : files)
{
std::cout << file << "\n";
}
}
if(!add_files.empty() || !remove_files.empty())
{
no_default_build = true;
for(const auto& add_file : add_files)
{
reg(add_file.data());
}
for(const auto& remove_file : remove_files)
{
unreg(remove_file.data());
}
// Force rebuild if files were added
recompileCheck(settings, 1, argv, no_relaunch == false);
}
recompileCheck(settings, argc, argv);
std::filesystem::path builddir(settings.builddir);
std::filesystem::create_directories(builddir);
auto all_tasks = getTasks(settings);
if(list_targets)
{
no_default_build = true;
auto& targets = getTargets(settings);
for(const auto& target : targets)
{
std::cout << target.config.target << "\n";
}
}
if(write_compilation_database)
{
std::ofstream istr(compilation_database);
istr << "[";
bool first{true};
for(auto task : all_tasks)
{
auto s = task->toJSON();
if(!s.empty())
{
if(!first)
{
istr << ",\n";
}
else
{
istr << "\n";
}
first = false;
istr << s;
}
}
istr << "\n]\n";
}
if(print_configure_cmd)
{
no_default_build = true;
std::cout << c.get("cmd") << "\n";
}
if(print_configure_db)
{
no_default_build = true;
const auto& c = ctor::get_configuration();
for(const auto& config : c.tools)
{
std::cout << config.first << ": " << config.second << "\n";
}
}
for(auto task : all_tasks)
{
if(task->registerDepTasks(all_tasks))
{
return 1;
}
}
bool build_all{!no_default_build};
for(const auto& arg : opt.arguments())
{
if(arg == "configure")
{
std::cerr << "The 'configure' target must be the first argument.\n";
return 1;
}
if(arg == "clean")
{
build_all = false;
std::cout << "Cleaning\n";
for(auto& task : all_tasks)
{
if(task->clean() != 0)
{
return 1;
}
}
}
else if(arg == "check")
{
build_all = false;
run_unittests = true;
std::vector<Target> unittest_targets;
auto& targets = getTargets(settings);
for(const auto& target : targets)
{
if(target.config.type == ctor::target_type::unit_test ||
target.config.type == ctor::target_type::unit_test_library)
{
unittest_targets.push_back(target);
}
}
auto ret = build(settings, "check", unittest_targets, all_tasks);
if(ret != 0)
{
return ret;
}
}
else
{
build_all = false;
if(arg == "all")
{
std::vector<Target> non_unittest_targets;
auto& targets = getTargets(settings);
for(const auto& target : targets)
{
if(target.config.type != ctor::target_type::unit_test &&
target.config.type != ctor::target_type::unit_test_library)
{
non_unittest_targets.push_back(target);
}
}
auto ret = build(settings, "all", non_unittest_targets, all_tasks);
if(ret != 0)
{
return ret;
}
}
else
{
auto ret = build(settings, arg, all_tasks);
if(ret != 0)
{
return ret;
}
}
}
}
if(build_all)
{
std::vector<Target> non_unittest_targets;
auto& targets = getTargets(settings);
for(const auto& target : targets)
{
if(target.config.type != ctor::target_type::unit_test &&
target.config.type != ctor::target_type::unit_test_library)
{
non_unittest_targets.push_back(target);
}
}
auto ret = build(settings, "all", non_unittest_targets, all_tasks);
if(ret != 0)
{
return ret;
}
}
if(run_unittests)
{
return runUnitTests(all_tasks, settings);
}
return 0;
}
|