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
|
// -*- c++ -*-
// Distributed under the BSD 2-Clause License.
// See accompanying file LICENSE for details.
#pragma once
#include <source_location>
#include <string>
#include <vector>
#include <map>
#include <variant>
#include <cstddef>
#include <functional>
#include <string_view>
#include <cassert>
namespace ctor {
enum class target_type
{
automatic, // Default - deduce from target name and sources extensions
executable,
static_library,
shared_library,
dynamic_library = shared_library,
module,
object,
unit_test,
unit_test_library,
function,
unknown,
};
enum class language
{
automatic, // Default - deduce language from source extensions
c,
cpp,
assembler,
};
enum class output_system
{
host, // Output for the target system
build, // Internal tool during cross-compilation
};
enum class arch
{
unix, //!< Target platform architecture is unix-based (ie. linux, bsd, etc)
apple, //!< Target platform architecture is macos
windows, //!< Target platform architecture is windows
unknown, //!< Target platform architecture has not yet detected or was not possible to detect
};
enum class toolchain
{
any,
none,
gcc,
clang,
};
struct output_file
{
std::string file;
};
enum class source_type
{
regular,
generated,
};
struct source
{
template <class ... Args>
requires ((
std::is_convertible_v<Args, std::string_view> ||
std::is_same_v<Args, ctor::toolchain> ||
std::is_same_v<Args, ctor::language> ||
std::is_same_v<Args, ctor::source_type> ||
std::is_same_v<Args, ctor::output_file>
) && ...)
constexpr source(Args && ... arg)
{
([&]
{
if constexpr(std::is_convertible_v<Args, std::string_view>)
{
file = arg;
}
else if constexpr(std::is_same_v<Args, ctor::toolchain>)
{
toolchain = arg;
}
else if constexpr(std::is_same_v<Args, ctor::language>)
{
language = arg;
}
else if constexpr(std::is_same_v<Args, ctor::output_file>)
{
output = arg.file;
}
else if constexpr(std::is_same_v<Args, ctor::source_type>)
{
source_type = arg;
}
}(), ...);
}
std::string file;
ctor::toolchain toolchain{ctor::toolchain::any};
ctor::language language{ctor::language::automatic};
std::string output{};
ctor::source_type source_type{ctor::source_type::regular};
};
enum class cxx_opt
{
// gcc/clang
output, // -o
debug, // -g
warn_all, // -Wall
warn_conversion, // -Wconversion
warn_shadow, // -Wshadow
warn_extra, // -Wextra
warnings_as_errors, // -Werror
exceptions, // -fexceptions
generate_dep_tree, // -MMD
no_link, // -c
include_path, // -I<arg>
cpp_std, // -std=<arg>
optimization, // -O<arg>
position_independent_code, // -fPIC
position_independent_executable, // -fPIE
define, // -D<arg>[=<arg2>]
custom, // entire option taken verbatim from <arg>
};
enum class c_opt
{
// gcc/clang
output, // -o
debug, // -g
warn_all, // -Wall
warn_conversion, // -Wconversion
warn_shadow, // -Wshadow
warn_extra, // -Wextra
warnings_as_errors, // -Werror
generate_dep_tree, // -MMD
no_link, // -c
include_path, // -I<arg>
c_std, // -std=<arg>
optimization, // -O<arg>
position_independent_code, // -fPIC
position_independent_executable, // -fPIE
define, // -D<arg>[=<arg2>]
custom, // entire option taken verbatim from <arg>
};
enum class ld_opt
{
// gcc/clang
output, // -o
warn_all, // -Wall
warnings_as_errors, // -Werror
library_path, // -L<arg>
link, // -l<arg>
cpp_std, // -std=<arg>
build_shared, // -shared
threads, // -pthread
position_independent_code, // -fPIC
position_independent_executable, // -fPIE
custom, // entire option taken verbatim from <arg>
};
enum class ar_opt
{
// gcc/clang
replace, // -r
add_index, // -s
create, // -c
output, // <arg>
custom, // entire option taken verbatim from <arg>
};
enum class asm_opt
{
// gcc/clang
custom, // entire option taken verbatim from <arg>
};
template<typename T>
class flag
{
public:
template <class ... Args>
requires ((
std::is_convertible_v<Args, std::string_view> ||
std::is_same_v<Args, ctor::toolchain> ||
std::is_same_v<Args, T>
) && ...)
constexpr flag(Args && ... _arg)
{
constexpr std::size_t n = sizeof...(Args);
int state{}; // 0: opt, 1: arg1, 2: arg2, 3: error
([&]
{
if constexpr(std::is_convertible_v<Args, std::string_view>)
{
if constexpr(n == 1)
{
std::string str(_arg);
to_flag(str);
}
else
{
assert(state > 0); // opt must be before args
if(state == 1)
{
this->arg = _arg;
}
else
{
assert(state == 2); // up to 2 args supported
this->arg2 = _arg;
}
++state;
}
}
else if constexpr(std::is_same_v<Args, ctor::toolchain>)
{
toolchain = _arg;
}
else if constexpr(std::is_same_v<Args, T>)
{
assert(state == 0); // opt must be before args
opt = _arg;
++state;
}
}(), ...);
}
ctor::toolchain toolchain{ctor::toolchain::any};
T opt{};
std::string arg;
std::string arg2;
private:
void to_flag(std::string_view str);
};
using c_flag = ctor::flag<ctor::c_opt>;
using cxx_flag = ctor::flag<ctor::cxx_opt>;
using ld_flag = ctor::flag<ctor::ld_opt>;
using ar_flag = ctor::flag<ctor::ar_opt>;
using asm_flag = ctor::flag<ctor::asm_opt>;
using c_flags = std::vector<ctor::c_flag>;
using cxx_flags = std::vector<ctor::cxx_flag>;
using ld_flags = std::vector<ctor::ld_flag>;
using ar_flags = std::vector<ctor::ar_flag>;
using asm_flags = std::vector<ctor::asm_flag>;
struct flags
{
template <class ... Args>
requires ((
std::is_same_v<Args, ctor::c_flags> ||
std::is_same_v<Args, ctor::cxx_flags> ||
std::is_same_v<Args, ctor::ld_flags> ||
std::is_same_v<Args, ctor::ar_flags> ||
std::is_same_v<Args, ctor::asm_flags>
) && ...)
constexpr flags(Args && ... arg)
{
([&]
{
if constexpr(std::is_same_v<Args, ctor::c_flags>)
{
cflags = arg;
}
else if constexpr(std::is_same_v<Args, ctor::cxx_flags>)
{
cxxflags = arg;
}
else if constexpr(std::is_same_v<Args, ctor::ld_flags>)
{
ldflags = arg;
}
else if constexpr(std::is_same_v<Args, ctor::ar_flags>)
{
arflags = arg;
}
else if constexpr(std::is_same_v<Args, ctor::asm_flags>)
{
asmflags = arg;
}
}(), ...);
}
ctor::c_flags cflags; // flags for c compiler
ctor::cxx_flags cxxflags; // flags for c++ compiler
ctor::ld_flags ldflags; // flags for linker
ctor::ar_flags arflags; // flags for archiver
ctor::asm_flags asmflags; // flags for asm translator
};
struct settings
{
std::string builddir{"build"};
std::size_t parallel_processes{1};
int verbose{0}; // -1: completely silent, 0: normal, 1: verbose, ...
bool dry_run{false};
};
struct build_configuration;
using GeneratorOneToOne =
std::function<int(const std::string& input,
const std::string& output,
const build_configuration& config,
const ctor::settings& settings)>;
using GeneratorManyToOne =
std::function<int(const std::vector<std::string>& input,
const std::string& output,
const ctor::build_configuration& config,
const ctor::settings& settings)>;
struct name
{
std::string name;
};
struct target
{
std::string target;
};
using sources = std::vector<ctor::source>;
struct depends
{
std::vector<std::string> depends;
};
struct externals
{
std::vector<std::string> externals;
};
struct build_configuration
{
template <class ... Args>
requires ((
std::is_same_v<Args, ctor::name> ||
std::is_same_v<Args, ctor::target_type> ||
std::is_same_v<Args, ctor::output_system> ||
std::is_same_v<Args, ctor::target> ||
std::is_same_v<Args, ctor::sources> ||
std::is_same_v<Args, ctor::depends> ||
std::is_same_v<Args, ctor::flags> ||
std::is_same_v<Args, ctor::externals> ||
std::is_same_v<Args, ctor::GeneratorOneToOne> ||
std::is_same_v<Args, ctor::GeneratorManyToOne>
) && ...)
constexpr build_configuration(Args && ... arg)
{
([&]
{
if constexpr(std::is_same_v<Args, ctor::name>)
{
name = arg.name;
}
else if constexpr(std::is_same_v<Args, ctor::target_type>)
{
type = arg;
}
else if constexpr(std::is_same_v<Args, ctor::output_system>)
{
system = arg;
}
else if constexpr(std::is_same_v<Args, ctor::target>)
{
target = arg.target;
}
else if constexpr(std::is_same_v<Args, ctor::sources>)
{
sources = arg;
}
else if constexpr(std::is_same_v<Args, ctor::depends>)
{
depends = arg.depends;
}
else if constexpr(std::is_same_v<Args, ctor::flags>)
{
flags = arg;
}
else if constexpr(std::is_same_v<Args, ctor::externals>)
{
externals = arg.externals;
}
else if constexpr(std::is_same_v<Args, ctor::GeneratorOneToOne>)
{
function_one_to_one = arg;
}
else if constexpr(std::is_same_v<Args, ctor::GeneratorManyToOne>)
{
function_many_to_one = arg;
}
}(), ...);
}
std::string name; // Name - used for referring in other configurations.
ctor::target_type type{ctor::target_type::automatic};
ctor::output_system system{ctor::output_system::build};
std::string target; // Output target file for this configuration
std::vector<ctor::source> sources; // source list
std::vector<std::string> depends; // internal target dependencies
ctor::flags flags;
std::vector<std::string> externals; // externals used by this configuration
ctor::GeneratorOneToOne function_one_to_one;
ctor::GeneratorManyToOne function_many_to_one;
};
using build_configurations = std::vector<build_configuration>;
int reg(std::function<ctor::build_configurations (const ctor::settings&)> cb,
const std::source_location location = std::source_location::current());
// This type will use flags verbatim
struct external_manual
{
ctor::flags flags;
};
struct external_configuration
{
std::string name; // Name for configuration
ctor::output_system system{ctor::output_system::build};
std::variant<ctor::external_manual> external;
};
using external_configurations = std::vector<ctor::external_configuration>;
int reg(std::function<ctor::external_configurations (const ctor::settings&)> cb,
const std::source_location location = std::source_location::current());
// Convenience macro - ugly but keeps things simple(r)
#define CTOR_CONCAT(a, b) CTOR_CONCAT_INNER(a, b)
#define CTOR_CONCAT_INNER(a, b) a ## b
#define CTOR_UNIQUE_NAME(base) CTOR_CONCAT(base, __LINE__)
#define REG(cb) namespace { int CTOR_UNIQUE_NAME(unique) = reg(cb); }
// Predefined configuration keys
namespace cfg
{
constexpr auto builddir = "builddir";
constexpr auto host_cc = "host-cc";
constexpr auto host_cxx = "host-cxx";
constexpr auto host_ar = "host-ar";
constexpr auto host_ld = "host-ld";
constexpr auto build_cc = "build-cc";
constexpr auto build_cxx = "build-cxx";
constexpr auto build_ar = "build-ar";
constexpr auto build_ld = "build-ld";
constexpr auto ctor_includedir = "ctor-includedir";
constexpr auto ctor_libdir = "ctor-libdir";
}
struct configuration
{
bool has(const std::string& key) const;
std::string get(const std::string& key, const std::string& default_value = {}) const;
ctor::toolchain host_toolchain{ctor::toolchain::none};
ctor::arch host_arch{ctor::arch::unknown};
ctor::toolchain build_toolchain{ctor::toolchain::none};
ctor::arch build_arch{ctor::arch::unknown};
std::vector<std::string> args; // vector of arguments used when last calling configure
std::string getenv(const std::string& key) const;
std::map<std::string, std::string> env; // env used when last calling configure
std::map<std::string, std::string> tools; // tools
std::map<std::string, ctor::flags> externals;
};
const ctor::configuration& get_configuration();
} // ctor::
|