-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathconf.cpp
More file actions
792 lines (647 loc) · 26 KB
/
conf.cpp
File metadata and controls
792 lines (647 loc) · 26 KB
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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
#include "pch.h"
#include "conf.h"
#include "../tasks/task.h"
#include "../tasks/task_manager.h"
#include "../tools/tools.h"
#include "../utility.h"
#include "context.h"
#include "env.h"
#include "ini.h"
#include "paths.h"
namespace mob::details {
using key_value_map = std::map<std::string, std::string, std::less<>>;
using section_map = std::map<std::string, key_value_map, std::less<>>;
static std::unordered_map<mob::config, std::string_view> s_configuration_values{
{mob::config::release, "Release"},
{mob::config::debug, "Debug"},
{mob::config::relwithdebinfo, "RelWithDebInfo"}};
// holds all the options not related to tasks (global, tools, paths, etc.)
static section_map g_conf;
// holds all the task options; has a special map element with an empty string
// for options that apply to all tasks, and elements with specific task names
// for overrides
static section_map g_tasks;
// special cases to avoid string manipulations
static int g_output_log_level = 3;
static int g_file_log_level = 5;
static bool g_dry = false;
// check if the two given string are equals case-insensitive
//
bool case_insensitive_equals(std::string_view lhs, std::string_view rhs)
{
// _strcmpi does not have a n-overload, and since string_view is not
// necessarily null-terminated, _strmcpi cannot be safely used
return std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs),
std::end(rhs), [](auto&& c1, auto&& c2) {
return ::tolower(c1) == ::tolower(c2);
});
}
bool bool_from_string(std::string_view s)
{
return (s == "true" || s == "yes" || s == "1");
}
// returns a string from conf, bails out if it doesn't exist
//
std::string get_string(std::string_view section, std::string_view key,
std::optional<std::string> default_)
{
auto sitor = g_conf.find(section);
if (sitor == g_conf.end())
gcx().bail_out(context::conf, "[{}] doesn't exist", section);
auto kitor = sitor->second.find(key);
if (kitor == sitor->second.end()) {
if (!default_.has_value()) {
gcx().bail_out(context::conf, "no key '{}' in [{}]", key, section);
}
return *default_;
}
return kitor->second;
}
// calls get_string(), converts to int
//
int get_int(std::string_view section, std::string_view key,
std::optional<int> default_)
{
const auto s = get_string(section, key, default_.transform([](auto v) {
return std::to_string(v);
}));
try {
return std::stoi(s);
}
catch (std::exception&) {
gcx().bail_out(context::conf, "bad int for {}/{}", section, key);
}
}
// calls get_string(), converts to bool
//
bool get_bool(std::string_view section, std::string_view key,
std::optional<bool> default_)
{
const auto s = get_string(section, key, default_.transform([](auto v) {
return v ? "true" : "false";
}));
return bool_from_string(s);
}
// sets the given option, bails out if the option doesn't exist
//
void set_string(std::string_view section, std::string_view key,
std::string_view value)
{
auto sitor = g_conf.find(section);
if (sitor == g_conf.end())
gcx().bail_out(context::conf, "[{}] doesn't exist", section);
auto kitor = sitor->second.find(key);
if (kitor == sitor->second.end())
gcx().bail_out(context::conf, "no key '{}' [{}]", key, section);
kitor->second = value;
}
config string_to_config(std::string_view value)
{
for (const auto& [c, v] : s_configuration_values) {
if (case_insensitive_equals(value, v)) {
return c;
}
}
gcx().bail_out(context::conf, "invalid configuration '{}'", value);
}
// sets the given option, adds it if it doesn't exist; used when setting options
// from the master ini
//
void add_string(const std::string& section, const std::string& key,
std::string value)
{
g_conf[section][key] = value;
}
// finds an option for the given task, returns empty if not found
//
std::optional<std::string> find_string_for_task(std::string_view task_name,
std::string_view key)
{
// find task
auto titor = g_tasks.find(task_name);
if (titor == g_tasks.end())
return {};
const auto& task = titor->second;
// find key
auto itor = task.find(key);
if (itor == task.end())
return {};
return itor->second;
}
// gets an option for any of the given task names, typically what task::names()
// returns, which contains the main task name plus some alternate names
//
// there's a hierarchy for task options:
//
// 1) there's a special "_override" entry in g_tasks, for options set from
// the command line that should override everything, like --no-pull
// should override all pull settings for all tasks
//
// 2) if the key is not found in "_override", then there can be an entry
// in g_tasks with any of given task names
//
// 3) if the key doesn't exist, then use the generic task option for it, stored
// in an element with an empty string in g_tasks
//
std::string get_string_for_task(const std::vector<std::string>& task_names,
std::string_view key)
{
// some command line options will override any user settings, like
// --no-pull, those are stored in a special _override task name
auto v = find_string_for_task("_override", key);
if (v)
return *v;
// look for an option for this task by name
for (auto&& tn : task_names) {
v = find_string_for_task(tn, key);
if (v)
return *v;
}
// default task options are in a special empty string entry in g_tasks
v = find_string_for_task("", key);
if (v)
return *v;
// doesn't exist anywhere
gcx().bail_out(context::conf, "no task option '{}' found for any of {}", key,
join(task_names, ","));
}
// calls get_string_for_task(), converts to bool
//
bool get_bool_for_task(const std::vector<std::string>& task_names,
std::string_view key)
{
const std::string s = get_string_for_task(task_names, key);
return bool_from_string(s);
}
// sets the given task option, bails out if the option doesn't exist
//
void set_string_for_task(const std::string& task_name, const std::string& key,
std::string value)
{
// make sure the key exists, will throw if it doesn't
get_string_for_task({task_name}, key);
g_tasks[task_name][key] = std::move(value);
}
// sets the given task option, adds it if it doesn't exist; used when setting
// options from the master ini
//
void add_string_for_task(const std::string& task_name, const std::string& key,
std::string value)
{
g_tasks[task_name][key] = std::move(value);
}
// read a CMake constant from the configuration
//
template <typename T>
T parse_cmake_value(std::string_view section, std::string_view key,
std::string_view value,
std::unordered_map<T, std::string_view> const& values)
{
for (const auto& [value_c, value_s] : values) {
if (case_insensitive_equals(value_s, value)) {
return value_c;
}
}
// build a string containing allowed value for logging
std::vector<std::string_view> values_s;
for (const auto& [value_c, value_s] : values) {
values_s.push_back(value_s);
}
gcx().bail_out(context::conf, "bad value '{}' for {}/{} (expected one of {})",
value, section, key, join(values_s, ", ", std::string{}));
}
} // namespace mob::details
namespace mob {
std::vector<std::string> format_options()
{
// don't log private stuff
auto hide = [](std::string_view section, std::string_view key) {
if (key == "github_key")
return true;
if (section == "transifex" && key == "key")
return true;
if (key == "git_email")
return true;
return false;
};
auto make_value = [&](auto&& s, auto&& k, auto&& v) -> std::string {
if (hide(s, k))
return v.empty() ? "" : "(hidden)";
else
return v;
};
auto& tm = task_manager::instance();
std::size_t longest_what = 0;
std::size_t longest_key = 0;
for (auto&& [section, kvs] : details::g_conf) {
longest_what = std::max(longest_what, section.size());
for (auto&& [k, v] : kvs)
longest_key = std::max(longest_key, k.size());
}
for (auto&& [k, v] : details::g_tasks[""])
longest_key = std::max(longest_key, k.size());
for (const auto* task : tm.all())
longest_what = std::max(longest_what, task->name().size());
std::vector<std::string> lines;
lines.push_back(pad_right("what", longest_what) + " " +
pad_right("key", longest_key) + " " + "value");
lines.push_back(pad_right("-", longest_what, '-') + " " +
pad_right("-", longest_key, '-') + " " + "-----");
for (auto&& [section, kvs] : details::g_conf) {
for (auto&& [k, v] : kvs) {
lines.push_back(pad_right(section, longest_what) + " " +
pad_right(k, longest_key) + " = " +
make_value(section, k, v));
}
}
for (auto&& [k, v] : details::g_tasks[""]) {
lines.push_back(pad_right("task", longest_what) + " " +
pad_right(k, longest_key) + " = " + make_value("", k, v));
}
for (const auto* t : tm.all()) {
for (auto&& [k, unused] : details::g_tasks[""]) {
lines.push_back(
pad_right(t->name(), longest_what) + " " +
pad_right(k, longest_key) + " = " +
make_value("", k, details::get_string_for_task({t->name()}, k)));
}
}
return lines;
}
// sets commonly used options that need to be converted to int/bool, for
// performance
//
void set_special_options()
{
details::g_output_log_level = details::get_int("global", "output_log_level");
details::g_file_log_level = details::get_int("global", "file_log_level");
details::g_dry = details::get_bool("global", "dry");
}
// sets an option `key` in the `paths` section; if the path is currently empty,
// sets it using `f` (which is either a callable or a string)
//
// in any case, makes it absolute and canonical, bails out if the path does not
// exist
//
// this is used for paths that should already exist (qt, vs, etc.)
//
template <class F>
void set_path_if_empty(std::string_view key, F&& f)
{
// current value
fs::path p = conf().path().get(key);
if (p.empty()) {
// empty, set it from `f`
if constexpr (std::is_same_v<fs::path, std::decay_t<decltype(f)>>)
p = f;
else
p = f();
}
p = fs::absolute(p);
if (!conf().global().dry()) {
if (!fs::exists(p))
gcx().bail_out(context::conf, "path {} not found", p);
p = fs::canonical(p);
}
// new value
details::set_string("paths", key, path_to_utf8(p));
}
// sets an option `key` in the `paths` section:
// - if the path is empty, sets it as default_parent/default_dir,
// - if the path is not empty but is relative, resolves it against
// default_parent
//
// in any case, makes it absolute but weakly canonical since it might not exist
// at that point (this is used for build, install, etc.)
//
void resolve_path(std::string_view key, const fs::path& default_parent,
std::string_view default_dir)
{
// current value
fs::path p = conf().path().get(key);
if (p.empty()) {
p = default_parent / default_dir;
}
else {
if (p.is_relative())
p = default_parent / p;
}
if (!conf().global().dry())
p = fs::weakly_canonical(fs::absolute(p));
details::set_string("paths", key, path_to_utf8(p));
}
// `section_string` can be something like "global" or "paths", but also "task"
// or a task-specific name like "uibase:task"
//
// `master` is true if the ini being processed is the master ini so options are
// added to the maps instead of set, which throws if they're not found
//
void process_option(const std::string& section_string, const std::string& key,
const std::string& value, bool master)
{
// split section string on ":"
const auto col = section_string.find(":");
std::string task, section;
if (col == std::string::npos) {
// not a "task_name:task" section
section = section_string;
}
else {
// that's a "task_name:task" section
task = section_string.substr(0, col);
section = section_string.substr(col + 1);
}
if (section == "task") {
// task options go in g_tasks
if (task == "_override") {
// special case, comes from options on the command line
details::set_string_for_task("_override", key, value);
}
else if (task != "") {
// task specific
// task must exist
const auto& tasks = task_manager::instance().find(task);
if (tasks.empty()) {
gcx().bail_out(context::conf, "bad option {}, task '{}' not found",
section_string, task);
}
MOB_ASSERT(!tasks.empty());
for (auto& t : tasks)
details::set_string_for_task(t->name(), key, value);
}
else {
// global task option
if (master)
details::add_string_for_task("", key, value);
else
details::set_string_for_task("", key, value);
}
}
else {
// not a task option, goes into g_conf
if (master)
details::add_string(section, key, value);
else
details::set_string(section, key, value);
}
}
// reads the given ini and adds all of its content to the options
//
void process_ini(const fs::path& ini, bool master)
{
const auto data = parse_ini(ini);
for (auto&& a : data.aliases)
task_manager::instance().add_alias(a.first, a.second);
for (auto&& [section_string, kvs] : data.sections) {
for (auto&& [k, v] : kvs)
process_option(section_string, k, v, master);
}
}
// parses the given option strings and adds them as options
//
void process_cmd_options(const std::vector<std::string>& opts)
{
// parses "section/key=value"
static std::regex re(R"((.+)/(.+)=(.*))");
gcx().debug(context::conf, "overriding from command line:");
for (auto&& o : opts) {
std::smatch m;
if (!std::regex_match(o, m, re)) {
gcx().bail_out(context::conf,
"bad option {}, must be [task:]section/key=value", o);
}
process_option(m[1], m[2], m[3], false);
}
}
// goes through all the options that have to do with paths, checks them and
// resolves them if necessary
//
void resolve_paths()
{
// first, if any of these paths are empty, they are set using the second
// argument, which can be callable or a path
//
// the resulting path is made absolute and canonical and will bail out if it
// doesn't exist
// make sure third-party is in PATH before the other paths are checked
// because some of these paths will need to look in there to find stuff
set_path_if_empty("third_party", find_third_party_directory);
this_env::prepend_to_path(conf().path().third_party() / "bin");
set_path_if_empty("pf_x86", find_program_files_x86);
set_path_if_empty("pf_x64", find_program_files_x64);
set_path_if_empty("vs", find_vs);
set_path_if_empty("vcpkg", find_vcpkg); // set after vs as it will use the VS
set_path_if_empty("qt_install", find_qt);
set_path_if_empty("temp_dir", find_temp_dir);
set_path_if_empty("icons", find_in_root("icons"));
set_path_if_empty("licenses", find_in_root("licenses"));
set_path_if_empty("qt_bin", qt::installation_path() / "bin");
set_path_if_empty("qt_translations", qt::installation_path() / "translations");
// second, if any of these paths are relative, they use the second argument
// as the root; if they're empty, they combine the second and third
// arguments
//
// these paths might not exist yet, so they're only made weakly canonical,
// they'll be created as needed during the build process
const auto p = conf().path();
resolve_path("cache", p.prefix(), "downloads");
resolve_path("build", p.prefix(), "build");
resolve_path("install", p.prefix(), "install");
resolve_path("install_installer", p.install(), "installer");
resolve_path("install_bin", p.install(), "bin");
resolve_path("install_libs", p.install(), "lib");
resolve_path("install_pdbs", p.install(), "pdb");
resolve_path("install_dlls", p.install_bin(), "dlls");
resolve_path("install_plugins", p.install_bin(), "plugins");
resolve_path("install_licenses", p.install_bin(), "licenses");
resolve_path("install_stylesheets", p.install_bin(), "stylesheets");
resolve_path("install_translations", p.install_bin(), "translations");
resolve_path("install_extensions", p.install_bin(), "extensions");
// finally, resolve the tools that are unlikely to be in PATH; all the
// other tools (7z, jom, patch, etc.) are assumed to be in PATH (which
// now contains third-party) or have valid absolute paths in the ini
details::set_string("tools", "vcvars", path_to_utf8(find_vcvars()));
details::set_string("tools", "iscc", path_to_utf8(find_iscc()));
}
void conf::set_log_file()
{
// set up the log file, resolve against prefix if relative
fs::path log_file = conf().global().get("log_file");
if (log_file.is_relative())
log_file = conf().path().prefix() / log_file;
context::set_log_file(log_file);
}
void init_options(const std::vector<fs::path>& inis,
const std::vector<std::string>& opts)
{
MOB_ASSERT(!inis.empty());
// some logging
gcx().debug(context::conf, "cl: {}", std::wstring(GetCommandLineW()));
gcx().debug(context::conf, "using inis in order:");
for (auto&& ini : inis)
gcx().debug(context::conf, " . {}", ini);
// used to resolve a relative prefix; by default, it's resolved against cwd,
// but if an ini other than the master contains a prefix, use the ini's
// parent directory instead
fs::path prefix_root = fs::current_path();
// true for the first ini, will add values to the configuration maps instead
// of setting them, which throws if the option doesn't exist
//
// the goal is that the first, master ini contains all existing options and
// if an option set in another ini or on the command line doesn't exist in
// the master, it's an error
bool master = true;
for (auto&& ini : inis) {
fs::path prefix_before;
// if this is the master ini, the prefix doesn't exist in the config
// yet because no inis have been loaded
if (!master)
prefix_before = conf().path().prefix();
process_ini(ini, master);
// check if the prefix was changed by this ini
if (!master && conf().path().prefix() != prefix_before) {
// remember its path
prefix_root = ini.parent_path();
}
// further inis should only contain options that already exist
master = false;
}
if (!opts.empty()) {
const fs::path prefix_before = conf().path().prefix();
process_cmd_options(opts);
// check if the prefix was changed on the command line
if (conf().path().prefix() != prefix_before) {
// use cwd as the parent of a relative prefix
prefix_root = fs::current_path();
}
}
// converts some options to ints or bools, these are used everywhere, like
// the log levels
set_special_options();
// an empty prefix is an error and will fail in validate_options(), but
// don't check it here to allow some commands to run, like `mob options`,
// and make sure it's not set to something that's not empty to make sure it
// _does_ fail later on
if (!conf().path().prefix().empty())
resolve_path("prefix", prefix_root, "");
// set up the log file, resolve against prefix if relative
conf().set_log_file();
// goes through all paths and tools, finds missing or relative stuff, bails
// out of stuff can't be found
resolve_paths();
// make sure qt's bin directory is in the path
this_env::append_to_path(conf().path().get("qt_bin"));
}
bool verify_options()
{
// can't have an empty prefix
if (conf().path().prefix().empty()) {
u8cerr << "missing prefix; either specify it the [paths] section of "
<< "the ini or pass '-d path'\n";
return false;
}
// don't build mo inside mob
if (fs::exists(conf().path().prefix())) {
if (fs::equivalent(conf().path().prefix(), mob_exe_path().parent_path())) {
u8cerr << "the prefix cannot be where mob.exe is, there's already a "
<< "build directory in there\n";
return false;
}
}
return true;
}
conf_global conf::global()
{
return {};
}
conf_task conf::task(const std::vector<std::string>& names)
{
return {names};
}
conf_cmake conf::cmake()
{
return {};
}
conf_tools conf::tool()
{
return {};
}
conf_transifex conf::transifex()
{
return {};
}
conf_translations conf::translation()
{
return {};
}
conf_versions conf::version()
{
return {};
}
conf_build_types conf::build_types()
{
return {};
}
conf_paths conf::path()
{
return {};
}
conf_global::conf_global() : conf_section("global") {}
int conf_global::output_log_level() const
{
return details::g_output_log_level;
}
int conf_global::file_log_level() const
{
return details::g_file_log_level;
}
bool conf_global::dry() const
{
return details::g_dry;
}
// use appropriate case for the below constants since we will be using them in
// to_string, although most of cmake and msbuild is case-insensitive so it will
// not matter much in the end
static std::unordered_map<conf_cmake::constant, std::string_view> constant_values{
{conf_cmake::always, "ALWAYS"},
{conf_cmake::lazy, "LAZY"},
{conf_cmake::never, "NEVER"}};
std::string conf_cmake::to_string(constant c)
{
return std::string{constant_values.at(c)};
}
conf_cmake::conf_cmake() : conf_section("cmake") {}
conf_cmake::constant conf_cmake::install_message() const
{
return details::parse_cmake_value(
name(), "install_message", details::get_string(name(), "install_message"),
constant_values);
}
std::string conf_cmake::host() const
{
return details::get_string(name(), "host");
}
conf_task::conf_task(std::vector<std::string> names) : names_(std::move(names)) {}
std::string conf_task::get(std::string_view key) const
{
return details::get_string_for_task(names_, key);
}
bool conf_task::get_bool(std::string_view key) const
{
return details::get_bool_for_task(names_, key);
}
mob::config conf_task::configuration() const
{
return details::parse_cmake_value(
names_[0], "configuration",
details::get_string_for_task(names_, "configuration"),
details::s_configuration_values);
}
conf_tools::conf_tools() : conf_section("tools") {}
conf_transifex::conf_transifex() : conf_section("transifex") {}
conf_versions::conf_versions() : conf_section("versions") {}
conf_build_types::conf_build_types() : conf_section("build-types") {}
conf_translations::conf_translations() : conf_section("translations") {}
conf_prebuilt::conf_prebuilt() : conf_section("prebuilt") {}
conf_paths::conf_paths() : conf_section("paths") {}
} // namespace mob