aboutsummaryrefslogtreecommitdiff
path: root/build/config/utility
blob: e1d81a7db6c2f0e785011d14d7c5b635fea793da (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
// file      : build/config/utility -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#ifndef BUILD_CONFIG_UTILITY
#define BUILD_CONFIG_UTILITY

#include <string>
#include <utility> // pair

#include <build/types>
#include <build/diagnostics>

namespace build
{
  class scope;
  class list_value;

  namespace config
  {
    // Set, if necessary, a required config.* variable.
    //
    // Return the reference to the value as well as the indication of
    // whether the variable has actually been set.
    //
    template <typename T>
    std::pair<const T&, bool>
    required (scope& root, const char* name, const T& default_value);

    std::pair<const std::string&, bool>
    required (scope& root, const char* name, const char* default_value);

    // Set, if necessary, an optional config.* variable. In particular,
    // an unspecified variable is set to NULL which is used to distinguish
    // between the "configured as unspecified" and "not yet configured"
    // cases.
    //
    // Return the pointer to the value, which can be NULL.
    //
    template <typename T>
    const T*
    optional (scope& root, const char* name);

    // Add all the values from a variable to the C-string list. T is
    // either target or scope.
    //
    template <typename T>
    void
    append_options (cstrings& args, T& s, const char* var)
    {
      if (auto val = s[var])
      {
        for (const name& n: val.template as<const list_value&> ())
        {
          if (n.simple ())
            args.push_back (n.value.c_str ());
          else if (n.directory ())
            args.push_back (n.dir.string ().c_str ());
          else
            fail << "expected option instead of " << n <<
              info << "in variable " << var;
        }
      }
    }

    // Check if a specified option is present. T is either target or scope.
    //
    template <typename T>
    bool
    find_option (const char* option, T& s, const char* var)
    {
      if (auto val = s[var])
      {
        for (const name& n: val.template as<const list_value&> ())
        {
          if (n.simple () && n.value == option)
            return true;
        }
      }

      return false;
    }
  }
}

#include <build/config/utility.txx>

#endif // BUILD_CONFIG_UTILITY