aboutsummaryrefslogtreecommitdiff
path: root/build/b.cxx
blob: 5135761e7b16a3ffe777e9b0b1f241d24eb43ab3 (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// file      : build/b.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

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

#include <stdlib.h>    // getenv()
#include <unistd.h>    // getuid()
#include <sys/types.h> // uid_t
#include <pwd.h>       // struct passwd, getpwuid()

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

#include <build/scope>
#include <build/target>
#include <build/prerequisite>
#include <build/rule>
#include <build/algorithm>
#include <build/process>
#include <build/diagnostics>
#include <build/context>

#include <build/lexer>
#include <build/parser>

using namespace std;

namespace build
{
  bool
  match_recursive (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 ())
    {
      if (!match (t))
      {
        cerr << "error: no rule to update target " << t << endl;
        return false;
      }
    }

    for (prerequisite& p: t.prerequisites)
    {
      // Resolve prerequisite to target (prerequisite search). We
      // do this after matching since the rule can alter search
      // paths.
      //
      if (p.target == nullptr)
        search (p);

      if (!match_recursive (*p.target))
      {
        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 (prerequisite& p: t.prerequisites)
    {
      target& pt (*p.target);

      if (pt.state () == target_state::unknown)
      {
        pt.state ((ts = update (pt)));

        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;
  }

  void
  dump ()
  {
    cout << endl;

    for (const auto& pt: targets)
    {
      target& t (*pt);

      cout << t << ':';

      for (const auto& p: t.prerequisites)
      {
        cout << ' ' << p;
      }

      cout << endl;
    }

    cout << endl;
  }

}

#include <build/native>

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


using namespace build;

int
main (int argc, char* argv[])
{
  tracer tr ("main");

  // Initialize time conversion data that is used by localtime_r().
  //
  tzset ();

  // Trace verbosity.
  //
  verb = 5;

  // Register target types.
  //
  target_types.insert (file::static_type);

  target_types.insert (exe::static_type);
  target_types.insert (obj::static_type);

  target_types.insert (cxx::cxx::static_type);
  target_types.insert (cxx::hxx::static_type);
  target_types.insert (cxx::ixx::static_type);
  target_types.insert (cxx::txx::static_type);

  // Figure out directories: work, home, and {src,out}_{root,base}.
  //
  work = path::current ();

  if (const char* h = getenv ("HOME"))
    home = path (h);
  else
  {
    struct passwd* pw (getpwuid (getuid ()));

    if (pw == nullptr)
    {
      const char* msg (strerror (errno));
      cerr << "error: unable to determine home directory: " << msg << endl;
      return 1;
    }

    home = path (pw->pw_dir);
  }

  //@@ Must be normalized.
  //
  out_base = work;
  src_base = out_base;

  // The project's root directory is the one that contains the build/
  // sub-directory which contains the pre.build file.
  //
  for (path d (src_base); !d.root () && d != home; d = d.directory ())
  {
    path f (d / path ("build/pre.build"));
    if (path_mtime (f) != timestamp_nonexistent)
    {
      src_root = d;
      break;
    }
  }

  if (src_root.empty ())
  {
    src_root = src_base;
    out_root = out_base;
  }
  else
    out_root = out_base.directory (src_base.leaf (src_root));

  if (verb >= 4)
  {
    tr << "work dir: " << work.string ();
    tr << "home dir: " << home.string ();
    tr << "out_base: " << out_base.string ();
    tr << "src_base: " << src_base.string ();
    tr << "out_root: " << out_root.string ();
    tr << "src_root: " << src_root.string ();
  }

  // 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);
  parser p (cerr);

  try
  {
    p.parse (ifs, bf, scopes[path::current ()]);
  }
  catch (const lexer_error&)
  {
    return 1; // Diagnostics has already been issued.
  }
  catch (const parser_error&)
  {
    return 1; // Diagnostics has already been issued.
  }
  catch (const std::ios_base::failure&)
  {
    cerr << "error: failed to read from " << bf << endl;
    return 1;
  }

  dump ();

  // Register rules.
  //
  cxx::link cxx_link;
  rules[typeid (exe)].emplace ("cxx.gnu.link", cxx_link);

  cxx::compile cxx_compile;
  rules[typeid (obj)].emplace ("cxx.gnu.compile", cxx_compile);

  default_path_rule path_exists;
  rules[typeid (path_target)].emplace ("", path_exists);

  // Build.
  //
  if (default_target == nullptr)
  {
    cerr << "error: no default target" << endl;
    return 1;
  }

  try
  {
    target& d (*default_target);

    if (!match_recursive (d))
      return 1; // Diagnostics has already been issued.

    dump ();

    switch (update (d))
    {
    case target_state::uptodate:
      {
        cerr << "info: target " << d << " is up to date" << endl;
        break;
      }
    case target_state::updated:
      break;
    case target_state::failed:
      {
        cerr << "error: failed to update target " << d << 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;
  }
}