aboutsummaryrefslogtreecommitdiff
path: root/build/bd.cxx
blob: e48de21b6380f8767f72eb27332f873f7a2c8187 (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
// 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 <cstdlib>      // exit
#include <cassert>
#include <iostream>
#include <typeinfo>
#include <system_error>

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

using namespace std;

namespace build
{
  bool
  match (target& t)
  {
    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);

          if (recipe re = ru.match (t))
          {
            t.recipe (re);
            break;
          }
        }
      }

      if (!t.recipe ())
      {
        cerr << "error: no rule to build 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;
      }
    }

    try
    {
      t.state ((ts = t.recipe () (t)));
      assert (ts != target_state::unknown);
      return ts;
    }
    catch (const process_error& e)
    {
      // Take care of failed children. In a multi-threaded program that
      // fork()'ed but did not exec(), it is unwise to try to do any kind
      // of cleanup (like unwinding the stack and running destructors).
      //
      assert (e.child ());
      exit (1);
    }
  }
}

#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 ();

  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"));

  hxx target ("target");
  target.path (path ("target"));

  bd_o.prerequisite (bd_cxx);
  bd_o.prerequisite (target);

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