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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
|
// -*- 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 <span>
#include <set>
#include "ctor.h"
#include "configure.h"
#include "rebuild.h"
#include "tasks.h"
#include "build.h"
#include "unittest.h"
#include "argparser.h"
#include "util.h"
extern "C" {
#include <archive.h>
#include <archive_entry.h>
#include <sys/stat.h>
#include <fcntl.h>
}
int main(int argc, char* argv[])
{
auto args = std::span(argv, static_cast<std::size_t>(argc));
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(args.size() > 1 && std::string(args[1]) == "configure")
{
return configure(settings, argc, argv);
}
if(args.size() > 1 && std::string(args[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};
bool install{false};
std::vector<std::string> arguments;
arg::Parser<int, std::string> opt(argc, argv);
opt.set_pos_cb(
[&](std::string_view arg)
{
arguments.emplace_back(std::string(arg));
return 0;
});
opt.add('j', "--jobs",
std::function([&](int jobs)
{
settings.parallel_processes = static_cast<std::size_t>(jobs);
return 0;
}),
"Number of parallel jobs. (default: cpucount * 2 - 1)");
opt.add('b', "--build-dir",
std::function([&](std::string builddir) {
settings.builddir = builddir;
return 0;
}),
"Overload output directory for build files (default: '" +
settings.builddir + "').");
opt.add('v', "--verbose",
std::function([&]()
{
settings.verbose++;
return 0;
}),
"Be verbose. Add multiple times for more verbosity.");
opt.add('q', "--quiet",
std::function([&]() {
settings.verbose = -1;
return 0;
}),
"Be completely silent.");
opt.add('a', "--add",
std::function([&](std::string filename) {
no_relaunch = true;
add_files.emplace_back(filename);
return 0;
}),
"Add specified file to the build configurations.");
opt.add('r', "--remove",
std::function([&](std::string filename)
{
no_relaunch = true;
remove_files.emplace_back(filename);
return 0;
}),
"Remove specified file from the build configurations.");
opt.add('L', "--list-files",
std::function([&]()
{
no_relaunch = true;
list_files = true;
return 0;
}),
"List files in the build configurations.");
opt.add('l', "--list-targets",
std::function([&]()
{
no_relaunch = true;
list_targets = true;
return 0;
}),
"List targets.");
opt.add('n', "--dry-run",
std::function([&]()
{
settings.dry_run = true;
return 0;
}),
"Print the commands to be executed, but do not execute them.");
opt.add({}, "--configure-cmd",
std::function([&]()
{
no_relaunch = true;
print_configure_cmd = true;
return 0;
}),
"Print commandline for last configure.");
opt.add({}, "--configure-db",
std::function([&]()
{
no_relaunch = true;
print_configure_db = true;
return 0;
}),
"Print entire configure parameter database.");
opt.add('d', "--database",
std::function([&](std::string database)
{
no_relaunch = true;
write_compilation_database = true;
compilation_database = database;
return 0;
}),
"Write compilation database json file.");
opt.add('h', "--help",
std::function([&]() -> int
{
std::cout << "Usage: " << args[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);
}),
"Print this help text.");
opt.set_err_cb(
[&](arg::error err, std::string_view arg)
{
switch(err)
{
case arg::error::invalid_arg:
std::cerr << opt.prog_name() <<
": invalid argument for option '" << arg << "'\n";
std::cerr << "Type '" << opt.prog_name() <<
" -h' for more information.\n";
break;
case arg::error::missing_arg:
std::cerr << opt.prog_name() << ": option requires and argument '" <<
arg << "'\n";
std::cerr << "Type '" << opt.prog_name() <<
" -h' for more information.\n";
break;
case arg::error::invalid_opt:
std::cerr << opt.prog_name() << ": invalid option '" << arg << "'\n";
std::cerr << "Type '" << opt.prog_name() <<
" -h' for more information.\n";
break;
}
});
auto res = opt.parse();
if(res != 0)
{
return res;
}
if(std::string value; get_env("V", value))
{
try
{
settings.verbose = std::stoi(value);
}
catch(...)
{
// not an integer
}
}
if(list_files)
{
no_default_build = true;
std::vector<std::string> files;
const auto& configFiles = getConfigFileList();
for(const auto& configFile : configFiles)
{
files.emplace_back(configFile.file);
}
const auto& externalConfigFiles = getExternalConfigFileList();
for(const auto& externalConfigFile : externalConfigFiles)
{
files.emplace_back(externalConfigFile.file);
}
std::sort(files.begin(), files.end());
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;
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 : 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 if(arg == "dist")
{
ctor::project project{};
std::set<std::string> dist_files;
auto& targets = getTargets(settings);
for(const auto& target : targets)
{
if(!target.config.project.name.empty())
{
project = target.config.project;
}
for(const auto& source : target.config.sources)
{
if(source.nodist)
{
continue;
}
std::filesystem::path p = target.path;
p /= source.file;
dist_files.insert(p.lexically_normal().string());
}
for(const auto& header : target.config.headers)
{
if(header.nodist)
{
continue;
}
std::filesystem::path p = target.path;
p /= header.file;
dist_files.insert(p.lexically_normal().string());
}
for(const auto& extra_dist_file : target.config.extra_dist)
{
if(extra_dist_file.nodist)
{
continue;
}
std::filesystem::path p = target.path;
p /= extra_dist_file.file;
dist_files.insert(p.lexically_normal().string());
}
}
const auto& configFiles = getConfigFileList();
for(const auto& configFile : configFiles)
{
std::filesystem::path p = configFile.file;
dist_files.insert(p.lexically_normal().string());
}
const auto& externalConfigFiles = getExternalConfigFileList();
for(const auto& externalConfigFile : externalConfigFiles)
{
std::filesystem::path p = externalConfigFile.file;
dist_files.insert(p.lexically_normal().string());
}
if(project.name.empty())
{
std::cerr << "Missing ctor::project description."
" Cannot create dist-file.\n";
return 1;
}
std::string outname = project.name + "-" +
std::to_string(project.version.major) + "." +
std::to_string(project.version.minor) + "." +
std::to_string(project.version.patch);
if(!project.version.tweak.empty())
{
outname += "-" + project.version.tweak;
}
outname += ".tar.gz";
{
struct archive *a;
struct archive_entry *entry;
struct stat st;
char buff[8192];
ssize_t len;
int fd;
a = archive_write_new();
// Filters: https://man.archlinux.org/man/archive_write_filter.3.en
archive_write_add_filter_gzip(a);
archive_write_set_format_pax_restricted(a); // Note 1
archive_write_open_filename(a, outname.data());
for(const auto& dist_file : dist_files)
{
const char*filename = dist_file.data();
stat(filename, &st);
auto mode = st.st_mode & 0xfff;//0644
std::cout << dist_file << std::oct << mode << '\n';
entry = archive_entry_new(); // Note 2
archive_entry_set_pathname(entry, filename);
archive_entry_set_size(entry, st.st_size); // Note 3
archive_entry_set_mtime(entry, st.st_mtime, 0);
archive_entry_set_filetype(entry, AE_IFREG);
archive_entry_set_perm(entry, mode);
archive_write_header(a, entry);
fd = open(filename, O_RDONLY);
len = read(fd, buff, sizeof(buff));
while ( len > 0 )
{
archive_write_data(a, buff, len);
len = read(fd, buff, sizeof(buff));
}
close(fd);
archive_entry_free(entry);
}
archive_write_close(a); // Note 4
archive_write_free(a); // Note 5
}
}
else if(arg == "install")
{
install = true;
build_all = true;
}
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)
{
auto ret = runUnitTests(all_tasks, settings);
if(ret != 0)
{
return ret;
}
}
if(install)
{
auto destdir_env = std::getenv("DESTDIR");
if(!destdir_env)
{
std::cerr << "No DESTDIR provided!\n";
return 1;
}
std::filesystem::path destdir = destdir_env;
if(!destdir.is_absolute())
{
std::cerr << "DESTDIR must be absolute!\n";
return 1;
}
std::cout << "Installing in DESTDIR: " << destdir.string() << "\n";
std::string prefix = "/usr";
for(const auto& task : all_tasks)
{
auto install_target = task->buildConfig().install;
if(!task->derived() && install_target.do_install)
{
std::string target_dir{};
if(!install_target.path.empty())
{
target_dir = install_target.path;
}
else
{
switch(task->targetType())
{
case ctor::target_type::unit_test:
case ctor::target_type::unit_test_library:
case ctor::target_type::object:
case ctor::target_type::function:
case ctor::target_type::automatic:
case ctor::target_type::unknown:
// Ignore
continue;
break;
case ctor::target_type::executable:
target_dir = "bin";
break;
case ctor::target_type::shared_library:
case ctor::target_type::static_library:
case ctor::target_type::module:
target_dir = "lib";
break;
}
}
std::filesystem::path target;
target = prefix;
target /= target_dir;
target /= task->target();
if(destdir_env)
{
if(target.is_absolute())
{
target = destdir / target.relative_path();
}
else
{
target = destdir / target;
}
}
std::cout << "Bin install " << task->target() <<
" -> " << target.string() << "\n";
}
}
auto& targets = getTargets(settings);
for(const auto& target : targets)
{
for(const auto& header : target.config.headers)
{
if(header.install.do_install)
{
std::filesystem::path target_file = prefix;
target_file /= "include";
if(!header.install.path.empty())
{
target_file /= header.install.path;
}
target_file /= std::filesystem::path(header.file).filename();
if(destdir_env)
{
if(target_file.is_absolute())
{
target_file = destdir / target_file.relative_path();
}
else
{
target_file = destdir / target_file;
}
}
std::cout << "Include/header install " << header.file <<
" -> " << target_file.string() << "\n";
}
}
}
}
return 0;
}
|