aboutsummaryrefslogtreecommitdiff
path: root/build2/functions-name.cxx
blob: 37549b39d628f019c5a3ac0271f13e41504f34a8 (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
// file      : build2/functions-name.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2018 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <build2/scope.hxx>
#include <build2/function.hxx>
#include <build2/variable.hxx>

using namespace std;

namespace build2
{
  // Convert name to target'ish name (see below for the 'ish part). Return
  // raw/unprocessed data in case this is an unknown target type (or called
  // out of scope). See scope::find_target_type() for details.
  //
  static pair<name, optional<string>>
  to_target (const scope* s, name&& n)
  {
    optional<string> e;

    if (s != nullptr)
    {
      auto rp (s->find_target_type (n, location ()));

      if (rp.first != nullptr)
        n.type = rp.first->name;

      e = move (rp.second);
    }

    return make_pair (move (n), move (e));
  }

  void
  name_functions ()
  {
    function_family f ("name");

    // These functions treat a name as a target/prerequisite name.
    //
    // While on one hand it feels like calling them target.name(), etc., would
    // have been more appropriate, on the other hand they can also be called
    // on prerequisite names. They also won't always return the same result as
    // if we were interrogating an actual target (e.g., the directory may be
    // relative).
    //
    f["name"] = [](const scope* s, name n)
    {
      return to_target (s, move (n)).first.value;
    };
    f["name"] = [](const scope* s, names ns)
    {
      return to_target (s, convert<name> (move (ns))).first.value;
    };

    // Note: returns NULL if extension is unspecified (default) and empty if
    // specified as no extension.
    //
    f["extension"] = [](const scope* s, name n)
    {
      return to_target (s, move (n)).second;
    };
    f["extension"] = [](const scope* s, names ns)
    {
      return to_target (s, convert<name> (move (ns))).second;
    };

    f["directory"] = [](const scope* s, name n)
    {
      return to_target (s, move (n)).first.dir;
    };
    f["directory"] = [](const scope* s, names ns)
    {
      return to_target (s, convert<name> (move (ns))).first.dir;
    };

    f["target_type"] = [](const scope* s, name n)
    {
      return to_target (s, move (n)).first.type;
    };
    f["target_type"] = [](const scope* s, names ns)
    {
      return to_target (s, convert<name> (move (ns))).first.type;
    };

    // Note: returns NULL if no project specified.
    //
    f["project"] = [](const scope* s, name n)
    {
      return to_target (s, move (n)).first.proj;
    };
    f["project"] = [](const scope* s, names ns)
    {
      return to_target (s, convert<name> (move (ns))).first.proj;
    };

    // Name-specific overloads from builtins.
    //
    function_family b ("builtin");

    b[".concat"] = [](dir_path d, name n)
    {
      d /= n.dir;
      n.dir = move (d);
      return n;
    };
  }
}