aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/functions-builtin.cxx
blob: 44ae53401a0e6e557346f71d541d9706e3e6e2db (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
// file      : libbuild2/functions-builtin.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2019 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <libbuild2/function.hxx>
#include <libbuild2/variable.hxx>

namespace build2
{
  // Return NULL value if an environment variable is not set, untyped value
  // otherwise.
  //
  static inline value
  getenvvar (const string& name)
  {
    optional<string> v (getenv (name));

    if (!v)
      return value ();

    names r;
    r.emplace_back (to_name (move (*v)));
    return value (move (r));
  }

  void
  builtin_functions ()
  {
    function_family f ("builtin");

    f["type"] = [](value* v) {return v->type != nullptr ? v->type->name : "";};

    f["null"]  = [](value* v) {return v->null;};
    f["empty"] = [](value* v)  {return v->null || v->empty ();};

    f["identity"] = [](value* v) {return move (*v);};

    // string
    //
    f["string"] = [](bool b) {return b ? "true" : "false";};
    f["string"] = [](uint64_t i) {return to_string (i);};
    f["string"] = [](name n) {return to_string (n);};

    // getenv
    //
    f["getenv"] = [](string name)
    {
      return getenvvar (name);
    };

    f["getenv"] = [](names name)
    {
      return getenvvar (convert<string> (move (name)));
    };
  }
}