aboutsummaryrefslogtreecommitdiff
path: root/build/cxx/module.cxx
blob: 846e3249a5faea0a418cbb52684b7c61bcd29bbc (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
// file      : build/cxx/module.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#include <build/cxx/module>

#include <istream>
#include <ext/stdio_filebuf.h>

#include <build/path>
#include <build/scope>
#include <build/process>
#include <build/diagnostics>

using namespace std;

namespace build
{
  namespace cxx
  {
    void
    init (scope& root, scope& base, const location& l)
    {
      //@@ TODO: avoid multiple inits (generally, for modules).
      //

      tracer trace ("cxx::init");

      //@@ Should it be this way?
      //
      if (&root != &base)
        fail (l) << "cxx module must be initialized in project root scope";

      //@@ TODO: need to register target types, rules here instead of main().

      const dir_path& out_root (root.path ());
      level4 ([&]{trace << "for " << out_root;});

      // Configure.
      //

      // config.cxx
      //
      for (bool f (true); f; f = false)
      {
        auto val (root["config.cxx"]);

        string v;

        if (val)
        {
          if (!val.belongs (*global_scope))
            break; // A value from (some) config.build.

          v = val.as<const string&> ();
        }
        else
          v = "g++"; // Default.

        // Test it by trying to execute.
        //
        const char* args[] = {v.c_str (), "-dumpversion", nullptr};

        if (verb)
          print_process (args);
        else
          text << "test " << v;

        string ver;
        try
        {
          process pr (args, false, false, true);

          __gnu_cxx::stdio_filebuf<char> fb (pr.in_ofd, ios_base::in);
          istream is (&fb);

          bool r (getline (is, ver));

          if (!pr.wait ())
            throw failed ();

          if (!r)
            fail << "unexpected output from " << v;
        }
        catch (const process_error& e)
        {
          error << "unable to execute " << v << ": " << e.what ();

          if (e.child ())
            exit (1);

          throw failed ();
        }

        //text << "toolchain version " << ver;

        // Set on the project root.
        //
        root.assign ("config.cxx") = move (v);
      }

      // config.cxx.{p,c,l}options
      // config.cxx.libs
      //
      // These are optional so all we need to do is "import" them
      // into the root scope if they were specified on the command
      // line and set them to NULL if unspecified (the last part
      // is important to distinguish between the "configured as
      // unspecified" and "not configured" cases).
      //
      {
        auto v (root["config.cxx.poptions"]);

        if (v.defined ())
        {
          if (v.belongs (*global_scope))
            root.assign ("config.cxx.poptions") = v;
        }
        else
          root.assign ("config.cxx.poptions") = nullptr;
      }

      {
        auto v (root["config.cxx.coptions"]);

        if (v.defined ())
        {
          if (v.belongs (*global_scope))
            root.assign ("config.cxx.coptions") = v;
        }
        else
          root.assign ("config.cxx.coptions") = nullptr;
      }

      {
        auto v (root["config.cxx.loptions"]);

        if (v.defined ())
        {
          if (v.belongs (*global_scope))
            root.assign ("config.cxx.loptions") = v;
        }
        else
          root.assign ("config.cxx.loptions") = nullptr;
      }

      {
        auto v (root["config.cxx.libs"]);

        if (v.defined ())
        {
          if (v.belongs (*global_scope))
            root.assign ("config.cxx.libs") = v;
        }
        else
          root.assign ("config.cxx.libs") = nullptr;
      }
    }
  }
}