aboutsummaryrefslogtreecommitdiff
path: root/build/bd.cxx
blob: 03d0aa4ca148347ef52477f1fc2af68b2b9aa98d (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
// 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 <system_error>

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

using namespace std;

namespace build
{
  bool
  update (target& t)
  {
    auto tts (path_timestamp (t.name ()));
    cout << t.name () << ": " << tts << endl;

    bool u (tts == timestamp_nonexistent);
    for (target& p: t.prerequisites ())
    {
      if (!update (p))
        return false;

      if (!u)
      {
        auto tps (path_timestamp (p.name ()));

        if (tts <= tps) // Note: not just less.
        {
          cout << t.name () << " vs " << p.name () << ": " << (tps - tts)
               << " ahead" << endl;
          u = true;
        }
      }
    }

    if (!u) // Nothing to do.
      return true;

    try
    {
      auto r (t.rule ());
      return r != 0 ? r (t) : true;
    }
    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);
    }
  }
}

using namespace build;

bool
cxx_compile_rule (target& t)
{
  const targets& ps (t.prerequisites ());

  //@@ TODO: assuming .cxx is first.
  //
  const target& p0 (ps[0]);
  const char* args[] {
    "g++-4.9",
      "-std=c++11",
      "-I..",
      "-c",
      "-o", t.name ().c_str (),
      p0.name ().c_str (),
      nullptr};

  cerr << "c++ " << t.name () << endl;

  try
  {
    process pr (args);
    return pr.wait ();
  }
  catch (const process_error& e)
  {
    cerr << "error: unable to execute '" << args[0] << "': " <<
      e.what () << endl;

    if (e.child ())
      throw; // Let our caller terminate us quickly without causing a scene.

    return false;
  }
}

bool
cxx_link_rule (target& t)
{
  const targets& ps (t.prerequisites ());

  cerr << "ld " << t.name () << endl;
  return true;
}

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

  exe bd ("bd");
  obj bd_o ("bd.o");
  bd.prerequisite (bd_o);
  bd.rule (&cxx_link_rule);

  cxx bd_cxx ("bd.cxx");
  hxx target ("target");
  bd_o.prerequisite (bd_cxx);
  bd_o.prerequisite (target);
  bd_o.rule (&cxx_compile_rule);

  if (!update (bd))
  {
    cerr << "unable to update '" << bd.name () << "'" << endl;
    return 1;
  }
}