aboutsummaryrefslogtreecommitdiff
path: root/build/bd.cxx
blob: 33ee02f86dd302955253629d48b6a08b11599abe (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// file      : build/bd.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#include <time.h> // tzset()

#include <vector>
#include <cassert>
#include <fstream>
#include <iostream>
#include <typeinfo>
#include <system_error>

#include <build/target>
#include <build/rule>
#include <build/process>
#include <build/diagnostics>

#include <build/token>
#include <build/lexer>

using namespace std;

namespace build
{
  bool
  match (target& t)
  {
    // Because we match the target first and then prerequisites,
    // any additional dependency information injected by the rule
    // will be covered as well.
    //
    if (!t.recipe ())
    {
      for (auto ti (&t.type_id ());
           ti != nullptr && !t.recipe ();
           ti = ti->base)
      {
        for (auto rs (rules.equal_range (ti->id));
             rs.first != rs.second;
             ++rs.first)
        {
          const rule& ru (rs.first->second);

          recipe re;

          {
            auto g (
              make_exception_guard (
                [] (target& t)
                {
                  cerr << "info: while matching a rule for target " << t << endl;
                },
                t));

            re = ru.match (t);
          }

          if (re)
          {
            t.recipe (re);
            break;
          }
        }
      }

      if (!t.recipe ())
      {
        cerr << "error: no rule to update target " << t << endl;
        return false;
      }
    }

    for (target& p: t.prerequisites ())
    {
      if (!match (p))
      {
        cerr << "info: required by " << t << endl;
        return false;
      }
    }

    return true;
  }

  target_state
  update (target& t)
  {
    assert (t.state () == target_state::unknown);

    target_state ts;

    for (target& p: t.prerequisites ())
    {
      if (p.state () == target_state::unknown)
      {
        p.state ((ts = update (p)));

        if (ts == target_state::failed)
          return ts;
      }
    }

    const recipe& r (t.recipe ());

    {
      auto g (
        make_exception_guard (
          [] (target& t)
          {
            cerr << "info: while building target " << t << endl;
          },
          t));

      ts = r (t);
    }

    assert (ts != target_state::unknown);
    t.state (ts);
    return ts;
  }
}

#include <build/native>

#include <build/cxx/target>
#include <build/cxx/rule>


using namespace build;

int
main (int argc, char* argv[])
{
  // Initialize time conversion data that is used by localtime_r().
  //
  tzset ();

  // Parse buildfile.
  //
  path bf ("buildfile");

  ifstream ifs (bf.string ().c_str ());
  if (!ifs.is_open ())
  {
    cerr << "error: unable to open " << bf << " in read mode" << endl;
    return 1;
  }

  ifs.exceptions (ifstream::failbit | ifstream::badbit);
  lexer l (ifs, bf.string ());

  try
  {
    for (token t (l.next ());; t = l.next ())
    {
      cout << t.line () << ':' << t.column () << ": ";

      switch (t.type ())
      {
      case token_type::eos: cout << "<eos>"; break;
      case token_type::punctuation:
        {
          switch (t.punctuation ())
          {
          case token_punctuation::newline: cout << "\\n"; break;
          case token_punctuation::colon:   cout << ':'; break;
          case token_punctuation::lcbrace: cout << '{'; break;
          case token_punctuation::rcbrace: cout << '}'; break;
          }
          break;
        }
      case token_type::name: cout << '\'' << t.name () << '\''; break;
      }

      cout << endl;

      if (t.type () == token_type::eos)
        break;
    }
  }
  catch (const lexer_error&)
  {
    return 1; // Diagnostics has already been issued.
  }
  catch (const std::ios_base::failure&)
  {
    cerr << "error: failed to read from " << bf << endl;
    return 1;
  }

  return 0;


  // Register rules.
  //
  cxx::link cxx_link;
  rules.emplace (typeid (exe), cxx_link);

  cxx::compile cxx_compile;
  rules.emplace (typeid (obj), cxx_compile);

  default_path_rule path_exists;
  rules.emplace (typeid (path_target), path_exists);

  //
  //
  using namespace build::cxx;

  exe bd ("bd");
  obj bd_o ("bd");
  bd.prerequisite (bd_o);

  cxx::cxx bd_cxx ("bd");
  bd_cxx.path (path ("bd.cxx"));

  bd_o.prerequisite (bd_cxx);

  //
  //
  try
  {
    if (!match (bd))
      return 1; // Diagnostics has already been issued.

    switch (update (bd))
    {
    case target_state::uptodate:
      {
        cerr << "info: target " << bd << " is up to date" << endl;
        break;
      }
    case target_state::updated:
      break;
    case target_state::failed:
      {
        cerr << "error: failed to update target " << bd << endl;
        return 1;
      }
    case target_state::unknown:
      assert (false);
    }
  }
  catch (const error&)
  {
    return 1; // Diagnostics has already been issued.
  }
  catch (const std::exception& e)
  {
    cerr << "error: " << e.what () << endl;
    return 1;
  }
}