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
|
// file : libbuild2/functions-builtin.cxx -*- C++ -*-
// license : MIT; see accompanying LICENSE file
#include <sstream>
#include <libbuild2/scope.hxx>
#include <libbuild2/function.hxx>
#include <libbuild2/variable.hxx>
using namespace std;
namespace build2
{
// Note: not static since used by type-specific sort() implementations.
//
bool
functions_sort_flags (optional<names> fs)
{
bool r (false);
if (fs)
{
for (name& f: *fs)
{
string s (convert<string> (move (f)));
if (s == "dedup")
r = true;
else
throw invalid_argument ("invalid flag '" + s + '\'');
}
}
return r;
};
void
builtin_functions (function_map& m)
{
function_family f (m, "builtin");
// $defined(<variable>)
//
// Return true if the specified variable is defined in the calling scope or
// any outer scopes.
//
// Note that this function is not pure.
//
// Note that we may want to extend the scope argument to a more general
// notion of "lookup context" (scope, target, prerequisite).
//
f.insert ("defined", false) += [](const scope* s, names name)
{
if (s == nullptr)
fail << "defined() called out of scope" << endf;
return (*s)[convert<string> (move (name))].defined ();
};
// $visibility(<variable>)
//
// Return variable visibility if it is known and `null` otherwise.
//
// Possible visibility value are:
//
// global -- all outer scopes
// project -- this project (no outer projects)
// scope -- this scope (no outer scopes)
// target -- target and target type/pattern-specific
// prereq -- prerequisite-specific
//
// Note that this function is not pure.
//
f.insert ("visibility", false) += [](const scope* s, names name)
{
if (s == nullptr)
fail << "visibility() called out of scope" << endf;
const variable* var (
s->var_pool ().find (convert<string> (move (name))));
return (var != nullptr
? optional<string> (to_string (var->visibility))
: nullopt);
};
// $type(<value>)
//
// Return the type name of the value or empty string if untyped.
//
f["type"] += [](value* v) {return v->type != nullptr ? v->type->name : "";};
// $null(<value>)
//
// Return true if the value is `null`.
//
f["null"] += [](value* v) {return v->null;};
// $empty(<value>)
//
// Return true if the value is empty.
//
f["empty"] += [](value* v) {return v->null || v->empty ();};
// Leave this one undocumented for now since it's unclear why would anyone
// want to use it currently (we don't yet have any function composition
// facilities).
//
f["identity"] += [](value* v) {return move (*v);};
// $quote(<value>[, <escape>])
//
// Quote the value returning its string representation. If <escape> is
// true, then also escape (with a backslash) the quote characters being
// added (this is useful if the result will be re-parsed, for example as a
// script command line).
//
f["quote"] += [](value* v, optional<value> escape)
{
if (v->null)
return string ();
untypify (*v, true /* reduce */); // Reverse to names.
ostringstream os;
to_stream (os,
v->as<names> (),
quote_mode::normal,
'@' /* pair */,
escape && convert<bool> (move (*escape)));
return os.str ();
};
// $getenv(<name>)
//
// Get the value of the environment variable. Return `null` if the
// environment variable is not set.
//
// Note that if the build result can be affected by the variable being
// queried, then it should be reported with the `config.environment`
// directive.
//
// Note that this function is not pure.
//
f.insert ("getenv", false) += [](names name)
{
optional<string> v (getenv (convert<string> (move (name))));
if (!v)
return value ();
names r;
r.emplace_back (to_name (move (*v)));
return value (move (r));
};
}
}
|