aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/cc/types.cxx
blob: 666b048f36c6d8cfa74adfbd02277fb31d2b9f8f (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
// file      : libbuild2/cc/types.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <libbuild2/cc/types.hxx>

#include <libbuild2/cc/utility.hxx>

using namespace std;

namespace build2
{
  namespace cc
  {
    const string header_group_all            ("all");
    const string header_group_all_importable ("all-importable");
    const string header_group_std            ("std");
    const string header_group_std_importable ("std-importable");

    // Find the position where the group should be inserted unless the group
    // is already there.
    //
    using groups = importable_headers::groups;

    static inline optional<groups::const_iterator>
    find_angle (const groups& gs, const string& g)
    {
      for (auto i (gs.begin ()); i != gs.end (); ++i)
      {
        // After last angle-bracket file.
        //
        if (i->front () != '<' || i->back () != '>' || path_pattern (*i))
          return i;

        if (*i == g)
          return nullopt;
      }

      return gs.begin ();
    }

    static inline optional<groups::const_iterator>
    find_angle_pattern (const groups& gs, const string& g)
    {
      for (auto i (gs.begin ()); i != gs.end (); ++i)
      {
        // After last angle-bracket file pattern.
        //
        if (i->front () != '<' || i->back () != '>')
          return i;

        if (*i == g)
          return nullopt;
      }

      return gs.begin ();
    }

    auto importable_headers::
    insert_angle (const dir_paths& sys_inc_dirs,
                   const string& s) -> pair<const path, groups>*
    {
      assert (s.front () == '<' && s.back () == '>');

      // First see if it has already been inserted.
      //
      auto i (group_map.find (s));
      if (i == group_map.end ())
      {
        path f (s, 1, s.size () - 2);

        path p;
        for (const dir_path& d: sys_inc_dirs)
        {
          if (file_exists ((p = d, p /= f),
                           true /* follow_symlinks */,
                           true /* ignore_errors */))
            goto found;
        }

        return nullptr;

      found:
        normalize_header (p);

        // Note that it's possible this header has already been entered as
        // part of a different group.
        //
        auto j (header_map.emplace (move (p), groups {}).first);

        if (auto p = find_angle (j->second, s))
          j->second.insert (*p, s);

        i = group_map.emplace (s, reinterpret_cast<uintptr_t> (&*j)).first;
      }

      return reinterpret_cast<pair<const path, groups>*> (i->second);
    }

    auto importable_headers::
    insert_angle (path p, const string& s) -> pair<const path, groups>&
    {
      assert (s.front () == '<' && s.back () == '>');

      // First see if it has already been inserted.
      //
      auto i (group_map.find (s));
      if (i == group_map.end ())
      {
        // Note that it's possible this header has already been entered as
        // part of a different group.
        //
        auto j (header_map.emplace (move (p), groups {}).first);

        if (auto p = find_angle (j->second, s))
          j->second.insert (*p, s);

        i = group_map.emplace (s, reinterpret_cast<uintptr_t> (&*j)).first;
      }

      return *reinterpret_cast<pair<const path, groups>*> (i->second);
    }

    size_t importable_headers::
    insert_angle_pattern (const dir_paths& sys_inc_dirs, const string& pat)
    {
      assert (pat.front () == '<' && pat.back () == '>' && path_pattern (pat));

      // First see if it has already been inserted.
      //
      auto i (group_map.find (pat));
      if (i == group_map.end ())
      {
        path f (pat, 1, pat.size () - 2);

        struct data
        {
          uintptr_t       n;
          const string&   pat;
          const dir_path* dir;
        } d {0, pat, nullptr};

        auto process = [&d, this] (path&& pe, const string&, bool interm)
        {
          if (interm)
            return true;

          path p (*d.dir / pe);
          normalize_header (p);

          string s (move (pe).string ());
          s.insert (0, 1, '<');
          s.push_back ('>');

          // Note that it's possible this header has already been entered as
          // part of a different group.
          //
          auto j (header_map.emplace (move (p), groups {}).first);

          if (auto p = find_angle (j->second, s))
            j->second.insert (*p, move (s));

          if (auto p = find_angle_pattern (j->second, d.pat))
            j->second.insert (*p, d.pat);

          d.n++;
          return true;
        };

        for (const dir_path& dir: sys_inc_dirs)
        {
          d.dir = &dir;

          try
          {
            path_search (f, process, dir);
          }
          catch (const system_error& e)
          {
            fail << "unable to scan " << dir << ": " << e;
          }
        }

        i = group_map.emplace (pat, d.n).first;
      }

      return static_cast<size_t> (i->second);
    }
  }
}