aboutsummaryrefslogtreecommitdiff
path: root/build2/utility.hxx
blob: cc34d596f14b8d8762f9d660ca697a3e2ceadcad (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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
// file      : build2/utility.hxx -*- C++ -*-
// copyright : Copyright (c) 2014-2018 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#ifndef BUILD2_UTILITY_HXX
#define BUILD2_UTILITY_HXX

#include <tuple>      // make_tuple()
#include <memory>     // make_shared()
#include <string>     // to_string()
#include <utility>    // move(), forward(), declval(), make_pair()
#include <cassert>    // assert()
#include <iterator>   // make_move_iterator()
#include <algorithm>  // *
#include <functional> // ref(), cref()

#include <libbutl/ft/lang.hxx>

#include <libbutl/utility.mxx> // combine_hash(), reverse_iterate(), etc

#include <unordered_set>

#include <build2/types.hxx>
#include <build2/b-options.hxx>

// "Fake" version values used during bootstrap.
//
#ifdef BUILD2_BOOTSTRAP
#  define BUILD2_VERSION      9999999990000ULL
#  define BUILD2_VERSION_STR  "999.999.999"
#  define BUILD2_VERSION_ID   "999.999.999"
#  define LIBBUTL_VERSION_STR "999.999.999"
#  define LIBBUTL_VERSION_ID  "999.999.999"
#else
#  include <build2/version.hxx>
#endif

namespace build2
{
  using std::move;
  using std::forward;
  using std::declval;

  using std::ref;
  using std::cref;

  using std::make_pair;
  using std::make_tuple;
  using std::make_shared;
  using std::make_move_iterator;
  using std::to_string;
  using std::stoul;
  using std::stoull;

  // <libbutl/utility.mxx>
  //
  using butl::reverse_iterate;
  using butl::compare_c_string;
  using butl::compare_pointer_target;
  //using butl::hash_pointer_target;
  using butl::combine_hash;
  using butl::casecmp;
  using butl::case_compare_string;
  using butl::case_compare_c_string;
  using butl::lcase;
  using butl::alpha;
  using butl::alnum;
  using butl::digit;

  using butl::trim;
  using butl::next_word;

  using butl::make_guard;
  using butl::make_exception_guard;

  using butl::getenv;
  using butl::setenv;
  using butl::unsetenv;

  using butl::throw_generic_error;
  using butl::throw_system_error;

  using butl::eof;

  extern bool stderr_term; // True if stderr is a terminal.

  // Command line options.
  //
  extern options ops;

  // Build system driver process path (argv0.initial is argv[0]).
  //
  extern process_path argv0;

  // Build system driver version and check.
  //
  extern const standard_version build_version;

  class location;

  void
  check_build_version (const standard_version_constraint&, const location&);

  // Work/home directories (must be initialized in main()) and relative path
  // calculation.
  //
  extern dir_path work;
  extern dir_path home;

  // By default this points to work. Setting this to something else should
  // only be done in tightly controlled, non-concurrent situations (e.g.,
  // state dump). If it is empty, then relative() below returns the original
  // path.
  //
  extern const dir_path* relative_base;

  // If possible and beneficial, translate an absolute, normalized path into
  // relative to the relative_base directory, which is normally work. Note
  // that if the passed path is the same as relative_base, then this function
  // returns empty path.
  //
  template <typename K>
  basic_path<char, K>
  relative (const basic_path<char, K>&);

  class path_target;

  path
  relative (const path_target&);

  // In addition to calling relative(), this function also uses shorter
  // notations such as '~/'. For directories the result includes the trailing
  // slash. If the path is the same as base, returns "./" if current is true
  // and empty string otherwise.
  //
  string
  diag_relative (const path&, bool current = true);

  // Diagnostics forward declarations (see diagnostics.hxx).
  //
  extern uint16_t verb;
  const  uint16_t verb_never = 7;

  // Basic process utilities.
  //
  // The run*() functions with process_path assume that you are printing
  // the process command line yourself.

  // Search for a process executable. Issue diagnostics and throw failed in
  // case of an error.
  //
  process_path
  run_search (const char*& args0, const location& = location ());

  process_path
  run_search (const path&,
              bool init = false,
              const dir_path& fallback = dir_path (),
              const location& = location ());

  // Wait for process termination. Issue diagnostics and throw failed in case
  // of abnormal termination. If the process has terminated normally but with
  // a non-zero exit status, then, if error is true, assume the diagnostics
  // has already been issued and throw failed as well. Otherwise (error is
  // false), return false. The last argument is used in cooperation with
  // run_start() in case STDERR is redirected to STDOUT.
  //
  bool
  run_finish (const char* args[],
              process&,
              bool error = true,
              const string& = string (),
              const location& = location ());

  inline void
  run_finish (cstrings& args, process& pr, const location& l = location ())
  {
    run_finish (args.data (), pr, true, string (), l);
  }

  // Start a process with the specified arguments. If in is -1, then redirect
  // STDIN to a pipe (can also be -2 to redirect to /dev/null or equivalent).
  // If out is -1, redirect STDOUT to a pipe. If error is false, then
  // redirecting STDERR to STDOUT (this can be used to suppress diagnostics
  // from the child process). Issue diagnostics and throw failed in case of an
  // error.
  //
  process
  run_start (uint16_t verbosity,
             const process_env&, // Implicit-constructible from process_path.
             const char* args[],
             int in,
             int out,
             bool error = true,
             const dir_path& cwd = dir_path (),
             const location& = location ());

  inline process
  run_start (const process_env& pe, // Implicit-constructible from process_path.
             const char* args[],
             int in,
             int out,
             bool error = true,
             const dir_path& cwd = dir_path (),
             const location& l = location ())
  {
    return run_start (verb_never, pe, args, in, out, error, cwd, l);
  }

  inline void
  run (const process_path& p,
       const char* args[],
       const dir_path& cwd = dir_path ())
  {
    process pr (run_start (p, args, 0 /* stdin */, 1 /* stdout */, true, cwd));
    run_finish (args, pr);
  }

  inline void
  run (const process_path& p,
       cstrings& args,
       const dir_path& cwd = dir_path ())
  {
    run (p, args.data (), cwd);
  }

  // As above, but search for the process (including updating args[0]) and
  // print the process commands line at the specified verbosity level.
  //
  inline process
  run_start (uint16_t verbosity,
             const char* args[],
             int in,
             int out,
             bool error = true,
             const dir_path& cwd = dir_path (),
             const location& l = location ())
  {
    process_path pp (run_search (args[0], l));
    return run_start (verbosity, pp, args, in, out, error, cwd, l);
  }

  inline process
  run_start (uint16_t verbosity,
             cstrings& args,
             int in,
             int out,
             bool error = true,
             const dir_path& cwd = dir_path (),
             const location& l = location ())
  {
    return run_start (verbosity, args.data (), in, out, error, cwd, l);
  }

  inline void
  run (uint16_t verbosity,
       const char* args[],
       const dir_path& cwd = dir_path ())
  {
    process pr (run_start (verbosity,
                           args,
                           0 /* stdin */,
                           1 /* stdout */,
                           true,
                           cwd));
    run_finish (args, pr);
  }

  inline void
  run (uint16_t verbosity,
       cstrings& args,
       const dir_path& cwd = dir_path ())
  {
    run (verbosity, args.data (), cwd);
  }

  // Start the process as above and then call the specified function on each
  // trimmed line of the output until it returns a non-empty object T (tested
  // with T::empty()) which is then returned to the caller.
  //
  // The predicate can move the value out of the passed string but, if error
  // is false, only in case of a "content match" (so that any diagnostics
  // lines are left intact).
  //
  // If ignore_exit is true, then the program's exist status is ignored (if it
  // is false and the program exits with the non-zero status, then an empty T
  // instance is returned).
  //
  // If checksum is not NULL, then feed it the content of each trimmed line
  // (including those that come after the callback returns non-empty object).
  //
  template <typename T, typename F>
  T
  run (uint16_t verbosity,
       const process_env&, // Implicit-constructible from process_path.
       const char* args[],
       F&&,
       bool error = true,
       bool ignore_exit = false,
       sha256* checksum = nullptr);

  template <typename T, typename F>
  inline T
  run (const process_env& pe, // Implicit-constructible from process_path.
       const char* args[],
       F&& f,
       bool error = true,
       bool ignore_exit = false,
       sha256* checksum = nullptr)
  {
    return run<T> (
      verb_never, pe, args, forward<F> (f), error, ignore_exit, checksum);
  }

  template <typename T, typename F>
  inline T
  run (uint16_t verbosity,
       const char* args[],
       F&& f,
       bool error = true,
       bool ignore_exit = false,
       sha256* checksum = nullptr)
  {
    process_path pp (run_search (args[0]));
    return run<T> (
      verbosity, pp, args, forward<F> (f), error, ignore_exit, checksum);
  }

  // run <prog>
  //
  template <typename T, typename F>
  inline T
  run (uint16_t verbosity,
       const path& prog,
       F&& f,
       bool error = true,
       bool ignore_exit = false,
       sha256* checksum = nullptr)
  {
    const char* args[] = {prog.string ().c_str (), nullptr};
    return run<T> (
      verbosity, args, forward<F> (f), error, ignore_exit, checksum);
  }

  template <typename T, typename F>
  inline T
  run (uint16_t verbosity,
       const process_env& pe, // Implicit-constructible from process_path.
       F&& f,
       bool error = true,
       bool ignore_exit = false,
       sha256* checksum = nullptr)
  {
    const char* args[] = {pe.path->recall_string (), nullptr};
    return run<T> (
      verbosity, pe, args, forward<F> (f), error, ignore_exit, checksum);
  }

  // run <prog> <arg>
  //
  template <typename T, typename F>
  inline T
  run (uint16_t verbosity,
       const path& prog,
       const char* arg,
       F&& f,
       bool error = true,
       bool ignore_exit = false,
       sha256* checksum = nullptr)
  {
    const char* args[] = {prog.string ().c_str (), arg, nullptr};
    return run<T> (
      verbosity, args, forward<F> (f), error, ignore_exit, checksum);
  }

  template <typename T, typename F>
  inline T
  run (uint16_t verbosity,
       const process_env& pe, // Implicit-constructible from process_path.
       const char* arg,
       F&& f,
       bool error = true,
       bool ignore_exit = false,
       sha256* checksum = nullptr)
  {
    const char* args[] = {pe.path->recall_string (), arg, nullptr};
    return run<T> (
      verbosity, pe, args, forward<F> (f), error, ignore_exit, checksum);
  }

  // Empty/nullopt string, path, and project name.
  //
  extern const string       empty_string;
  extern const path         empty_path;
  extern const dir_path     empty_dir_path;
  extern const project_name empty_project_name;

  extern const optional<string>       nullopt_string;
  extern const optional<path>         nullopt_path;
  extern const optional<dir_path>     nullopt_dir_path;
  extern const optional<project_name> nullopt_project_name;

  // Hash a path potentially without the specific directory prefix.
  //
  // If prefix is not empty and is a super-path of the path to hash, then only
  // hash the suffix. Note that both paths are assumed to be normalized.
  //
  // This functionality is normally used to strip out_root from target paths
  // being hashed in order to avoid updates in case out_root was moved. Note
  // that this should only be done if the result of the update does not
  // include the out_root path in any form (as could be the case, for example,
  // for debug information, __FILE__ macro expansion, rpath, etc).
  //
  void
  hash_path (sha256&, const path&, const dir_path& prefix = dir_path ());

  // Append all the values from a variable to the C-string list. T is either
  // target or scope. The variable is expected to be of type strings.
  //
  // If excl is not NULL, then filter this option out (note: case sensitive).
  //
  struct variable;

  template <typename T>
  void
  append_options (cstrings&, T&, const variable&, const char* excl = nullptr);

  template <typename T>
  void
  append_options (cstrings&, T&, const char*, const char* excl = nullptr);

  template <typename T>
  void
  append_options (strings&, T&, const variable&, const char* excl = nullptr);

  template <typename T>
  void
  append_options (strings&, T&, const char*, const char* excl = nullptr);

  template <typename T>
  void
  hash_options (sha256&, T&, const variable&);

  template <typename T>
  void
  hash_options (sha256&, T&, const char*);

  // As above but from the strings value directly.
  //
  class value;
  struct lookup;

  void
  append_options (cstrings&, const lookup&, const char* excl = nullptr);

  void
  append_options (strings&, const lookup&, const char* excl = nullptr);

  void
  hash_options (sha256&, const lookup&);

  void
  append_options (cstrings&, const strings&, const char* excl = nullptr);

  void
  append_options (strings&, const strings&, const char* excl = nullptr);

  void
  hash_options (sha256&, const strings&);

  void
  append_options (cstrings&,
                  const strings&, size_t,
                  const char* excl = nullptr);

  void
  append_options (strings&,
                  const strings&, size_t,
                  const char* excl = nullptr);

  void
  hash_options (sha256&, const strings&, size_t);

  // As above but append/hash option values for the specified option (e.g.,
  // -I, -L).
  //
  template <typename I, typename F>
  void
  append_option_values (cstrings&,
                        const char* opt,
                        I begin, I end,
                        F&& get = [] (const string& s) {return s.c_str ();});

  template <typename I, typename F>
  void
  hash_option_values (sha256&,
                      const char* opt,
                      I begin, I end,
                      F&& get = [] (const string& s) {return s;});

  // Check if a specified option is present in the variable or value. T is
  // either target or scope.
  //
  template <typename T>
  bool
  find_option (const char* option,
               T&,
               const variable&,
               bool ignore_case = false);

  template <typename T>
  bool
  find_option (const char* option,
               T&,
               const char* variable,
               bool ignore_case = false);

  bool
  find_option (const char* option, const lookup&, bool ignore_case = false);

  bool
  find_option (const char* option, const strings&, bool ignore_case = false);

  bool
  find_option (const char* option, const cstrings&, bool ignore_case = false);

  // As above but look for several options returning true if any is present.
  //
  template <typename T>
  bool
  find_options (initializer_list<const char*>,
                T&,
                const variable&,
                bool = false);

  template <typename T>
  bool
  find_options (initializer_list<const char*>, T&, const char*, bool = false);

  bool
  find_options (initializer_list<const char*>, const lookup&, bool = false);

  bool
  find_options (initializer_list<const char*>, const strings&, bool = false);

  bool
  find_options (initializer_list<const char*>, const cstrings&, bool = false);

  // As above but look for an option that has the specified prefix. Return the
  // pointer to option or NULL if not found (thus can be used as bool).
  // Search backward (which is normall consistent with how options override
  // each other).
  //
  template <typename T>
  const string*
  find_option_prefix (const char* prefix, T&, const variable&, bool = false);

  template <typename T>
  const string*
  find_option_prefix (const char* prefix, T&, const char*, bool = false);

  const string*
  find_option_prefix (const char* prefix, const lookup&, bool = false);

  const string*
  find_option_prefix (const char* prefix, const strings&, bool = false);

  const char*
  find_option_prefix (const char* prefix, const cstrings&, bool = false);

  // As above but look for several option prefixes.
  //
  template <typename T>
  const string*
  find_option_prefixes (initializer_list<const char*>,
                        T&,
                        const variable&,
                        bool = false);

  template <typename T>
  const string*
  find_option_prefixes (initializer_list<const char*>,
                        T&,
                        const char*,
                        bool = false);

  const string*
  find_option_prefixes (initializer_list<const char*>,
                        const lookup&, bool = false);

  const string*
  find_option_prefixes (initializer_list<const char*>,
                        const strings&,
                        bool = false);

  const char*
  find_option_prefixes (initializer_list<const char*>,
                        const cstrings&,
                        bool = false);

  // Apply the specified substitution (stem) to a '*'-pattern. If pattern is
  // NULL or empty, then return the stem itself. Assume the pattern is valid,
  // i.e., contains a single '*' character.
  //
  string
  apply_pattern (const char* stem, const string* pattern);

  // Initialize build2 global state (verbosity, home/work directories, etc).
  // Should be called early in main() once.
  //
  void
  init (const char* argv0, uint16_t verbosity);
}

#include <build2/utility.ixx>
#include <build2/utility.txx>

#endif // BUILD2_UTILITY_HXX