summaryrefslogtreecommitdiff
path: root/src/Config.hpp
blob: 43c6a7fcf56fbbf2c673f71c0f367ca9ab7df027 (plain)
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
// Copyright (C) 2019-2023 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc., 51
// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

#pragma once

#include "NonCopyable.hpp"

#include <core/Sloppiness.hpp>

#include <sys/types.h>

#include <cstdint>
#include <functional>
#include <limits>
#include <optional>
#include <string>
#include <unordered_map>
#include <vector>

enum class CompilerType {
  auto_guess,
  clang,
  clang_cl,
  gcc,
  icl,
  msvc,
  nvcc,
  other
};

std::string compiler_type_to_string(CompilerType compiler_type);

class Config : NonCopyable
{
public:
  Config() = default;

  void read(const std::vector<std::string>& cmdline_config_settings = {});

  bool absolute_paths_in_stderr() const;
  const std::string& base_dir() const;
  const std::string& cache_dir() const;
  const std::string& compiler() const;
  const std::string& compiler_check() const;
  CompilerType compiler_type() const;
  bool compression() const;
  int8_t compression_level() const;
  const std::string& cpp_extension() const;
  bool debug() const;
  const std::string& debug_dir() const;
  bool depend_mode() const;
  bool direct_mode() const;
  bool disable() const;
  const std::string& extra_files_to_hash() const;
  bool file_clone() const;
  bool hard_link() const;
  bool hash_dir() const;
  const std::string& ignore_headers_in_manifest() const;
  const std::string& ignore_options() const;
  bool inode_cache() const;
  bool keep_comments_cpp() const;
  const std::string& log_file() const;
  uint64_t max_files() const;
  uint64_t max_size() const;
  const std::string& msvc_dep_prefix() const;
  const std::string& path() const;
  bool pch_external_checksum() const;
  const std::string& prefix_command() const;
  const std::string& prefix_command_cpp() const;
  bool read_only() const;
  bool read_only_direct() const;
  bool recache() const;
  bool remote_only() const;
  const std::string& remote_storage() const;
  bool reshare() const;
  bool run_second_cpp() const;
  core::Sloppiness sloppiness() const;
  bool stats() const;
  const std::string& stats_log() const;
  const std::string& namespace_() const;
  const std::string& temporary_dir() const;
  std::optional<mode_t> umask() const;

  // Return true for Clang and clang-cl.
  bool is_compiler_group_clang() const;

  // Return true for MSVC (cl.exe), clang-cl, and icl.
  bool is_compiler_group_msvc() const;

  std::string default_temporary_dir() const;

  void set_base_dir(const std::string& value);
  void set_cache_dir(const std::string& value);
  void set_compiler(const std::string& value);
  void set_compiler_type(CompilerType value);
  void set_cpp_extension(const std::string& value);
  void set_debug(bool value);
  void set_depend_mode(bool value);
  void set_direct_mode(bool value);
  void set_file_clone(bool value);
  void set_hard_link(bool value);
  void set_ignore_options(const std::string& value);
  void set_inode_cache(bool value);
  void set_max_files(uint64_t value);
  void set_max_size(uint64_t value);
  void set_msvc_dep_prefix(const std::string& value);
  void set_run_second_cpp(bool value);
  void set_temporary_dir(const std::string& value);

  // Where to write configuration changes.
  const std::string& config_path() const;
  // System (read-only) configuration file (if any).
  const std::string& system_config_path() const;

  void set_config_path(std::string path);
  void set_system_config_path(std::string path);

  using ItemVisitor = std::function<void(const std::string& key,
                                         const std::string& value,
                                         const std::string& origin)>;

  // Set config values from a configuration file.
  //
  // Returns false if the file can't be opened, otherwise true. Throws Error on
  // invalid configuration values.
  bool update_from_file(const std::string& path);

  // Set config values from a map with key-value pairs.
  //
  // Throws Error on invalid configuration values.
  void update_from_map(const std::unordered_map<std::string, std::string>& map);

  // Set config values from environment variables.
  //
  // Throws Error on invalid configuration values.
  void update_from_environment();

  // Get a config value in string form given a key.
  std::string get_string_value(const std::string& key) const;

  void visit_items(const ItemVisitor& item_visitor) const;

  void set_value_in_file(const std::string& path,
                         const std::string& key,
                         const std::string& value) const;

  // Called from unit tests.
  static void check_key_tables_consistency();

private:
  std::string m_config_path;
  std::string m_system_config_path;

  bool m_absolute_paths_in_stderr = false;
  std::string m_base_dir;
  std::string m_cache_dir;
  std::string m_compiler;
  std::string m_compiler_check = "mtime";
  CompilerType m_compiler_type = CompilerType::auto_guess;
  bool m_compression = true;
  int8_t m_compression_level = 0; // Use default level
  std::string m_cpp_extension;
  bool m_debug = false;
  std::string m_debug_dir;
  bool m_depend_mode = false;
  bool m_direct_mode = true;
  bool m_disable = false;
  std::string m_extra_files_to_hash;
  bool m_file_clone = false;
  bool m_hard_link = false;
  bool m_hash_dir = true;
  std::string m_ignore_headers_in_manifest;
  std::string m_ignore_options;
  bool m_inode_cache = true;
  bool m_keep_comments_cpp = false;
  std::string m_log_file;
  uint64_t m_max_files = 0;
  uint64_t m_max_size = 5ULL * 1000 * 1000 * 1000;
  std::string m_msvc_dep_prefix = "Note: including file:";
  std::string m_path;
  bool m_pch_external_checksum = false;
  std::string m_prefix_command;
  std::string m_prefix_command_cpp;
  bool m_read_only = false;
  bool m_read_only_direct = false;
  bool m_recache = false;
  bool m_reshare = false;
  bool m_run_second_cpp = true;
  bool m_remote_only = false;
  std::string m_remote_storage;
  core::Sloppiness m_sloppiness;
  bool m_stats = true;
  std::string m_stats_log;
  std::string m_namespace;
  std::string m_temporary_dir;
  std::optional<mode_t> m_umask;

  bool m_temporary_dir_configured_explicitly = false;

  std::unordered_map<std::string /*key*/, std::string /*origin*/> m_origins;

  void set_item(const std::string& key,
                const std::string& value,
                const std::optional<std::string>& env_var_key,
                bool negate,
                const std::string& origin);
};

inline bool
Config::absolute_paths_in_stderr() const
{
  return m_absolute_paths_in_stderr;
}

inline const std::string&
Config::base_dir() const
{
  return m_base_dir;
}

inline const std::string&
Config::cache_dir() const
{
  return m_cache_dir;
}

inline const std::string&
Config::compiler() const
{
  return m_compiler;
}

inline const std::string&
Config::compiler_check() const
{
  return m_compiler_check;
}

inline CompilerType
Config::compiler_type() const
{
  return m_compiler_type;
}

inline bool
Config::is_compiler_group_clang() const
{
  return m_compiler_type == CompilerType::clang
         || m_compiler_type == CompilerType::clang_cl;
}

inline bool
Config::is_compiler_group_msvc() const
{
  return m_compiler_type == CompilerType::msvc
         || m_compiler_type == CompilerType::clang_cl
         || m_compiler_type == CompilerType::icl;
}

inline bool
Config::compression() const
{
  return m_compression;
}

inline int8_t
Config::compression_level() const
{
  return m_compression_level;
}

inline const std::string&
Config::cpp_extension() const
{
  return m_cpp_extension;
}

inline bool
Config::debug() const
{
  return m_debug;
}

inline const std::string&
Config::debug_dir() const
{
  return m_debug_dir;
}

inline bool
Config::depend_mode() const
{
  return m_depend_mode;
}

inline bool
Config::direct_mode() const
{
  return m_direct_mode;
}

inline bool
Config::disable() const
{
  return m_disable;
}

inline const std::string&
Config::extra_files_to_hash() const
{
  return m_extra_files_to_hash;
}

inline bool
Config::file_clone() const
{
  return m_file_clone;
}

inline bool
Config::hard_link() const
{
  return m_hard_link;
}

inline bool
Config::hash_dir() const
{
  return m_hash_dir;
}

inline const std::string&
Config::ignore_headers_in_manifest() const
{
  return m_ignore_headers_in_manifest;
}

inline const std::string&
Config::ignore_options() const
{
  return m_ignore_options;
}

inline bool
Config::inode_cache() const
{
  return m_inode_cache;
}

inline bool
Config::keep_comments_cpp() const
{
  return m_keep_comments_cpp;
}

inline const std::string&
Config::log_file() const
{
  return m_log_file;
}

inline uint64_t
Config::max_files() const
{
  return m_max_files;
}

inline uint64_t
Config::max_size() const
{
  return m_max_size;
}

inline const std::string&
Config::msvc_dep_prefix() const
{
  return m_msvc_dep_prefix;
}

inline const std::string&
Config::path() const
{
  return m_path;
}

inline bool
Config::pch_external_checksum() const
{
  return m_pch_external_checksum;
}

inline const std::string&
Config::prefix_command() const
{
  return m_prefix_command;
}

inline const std::string&
Config::prefix_command_cpp() const
{
  return m_prefix_command_cpp;
}

inline bool
Config::read_only() const
{
  return m_read_only;
}

inline bool
Config::read_only_direct() const
{
  return m_read_only_direct;
}

inline bool
Config::recache() const
{
  return m_recache;
}

inline bool
Config::reshare() const
{
  return m_reshare;
}

inline bool
Config::run_second_cpp() const
{
  return m_run_second_cpp;
}

inline bool
Config::remote_only() const
{
  return m_remote_only;
}

inline const std::string&
Config::remote_storage() const
{
  return m_remote_storage;
}

inline core::Sloppiness
Config::sloppiness() const
{
  return m_sloppiness;
}

inline bool
Config::stats() const
{
  return m_stats;
}

inline const std::string&
Config::stats_log() const
{
  return m_stats_log;
}

inline const std::string&
Config::namespace_() const
{
  return m_namespace;
}

inline const std::string&
Config::temporary_dir() const
{
  return m_temporary_dir;
}

inline std::optional<mode_t>
Config::umask() const
{
  return m_umask;
}

inline void
Config::set_base_dir(const std::string& value)
{
  m_base_dir = value;
}

inline void
Config::set_cache_dir(const std::string& value)
{
  m_cache_dir = value;
  if (!m_temporary_dir_configured_explicitly) {
    m_temporary_dir = default_temporary_dir();
  }
}

inline void
Config::set_cpp_extension(const std::string& value)
{
  m_cpp_extension = value;
}

inline void
Config::set_compiler(const std::string& value)
{
  m_compiler = value;
}

inline void
Config::set_compiler_type(CompilerType value)
{
  m_compiler_type = value;
}

inline void
Config::set_depend_mode(bool value)
{
  m_depend_mode = value;
}

inline void
Config::set_debug(bool value)
{
  m_debug = value;
}

inline void
Config::set_direct_mode(bool value)
{
  m_direct_mode = value;
}

inline void
Config::set_file_clone(const bool value)
{
  m_file_clone = value;
}

inline void
Config::set_hard_link(const bool value)
{
  m_hard_link = value;
}

inline void
Config::set_ignore_options(const std::string& value)
{
  m_ignore_options = value;
}

inline void
Config::set_inode_cache(bool value)
{
  m_inode_cache = value;
}

inline void
Config::set_max_files(uint64_t value)
{
  m_max_files = value;
}

inline void
Config::set_max_size(uint64_t value)
{
  m_max_size = value;
}

inline void
Config::set_msvc_dep_prefix(const std::string& value)
{
  m_msvc_dep_prefix = value;
}

inline void
Config::set_run_second_cpp(bool value)
{
  m_run_second_cpp = value;
}

inline void
Config::set_temporary_dir(const std::string& value)
{
  m_temporary_dir = value;
}