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

#include <build/cli/module>

#include <butl/process>
#include <butl/fdstream>

#include <build/scope>
#include <build/target>
#include <build/variable>
#include <build/diagnostics>

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

#include <build/config/utility>

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

using namespace std;
using namespace butl;

namespace build
{
  namespace cli
  {
    static compile compile_;

    void
    init (scope& root, scope& base, const location& l)
    {
      //@@ TODO: avoid multiple inits (generally, for modules).
      //
      tracer trace ("cli::init");

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

      // Initialize the cxx module. We need its targets types (?xx{}).
      //
      cxx::init (root, base, l);

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

      // Register our target types.
      //
      target_types.insert (cli::static_type);
      target_types.insert (cli_cxx::static_type);

      // Register our rules.
      //
      rules[default_id][typeid (cli_cxx)].emplace ("cli.compile", compile_);
      rules[update_id][typeid (cli_cxx)].emplace ("cli.compile", compile_);
      rules[clean_id][typeid (cli_cxx)].emplace ("cli.compile", compile_);

      rules[default_id][typeid (cxx::cxx)].emplace ("cli.compile", compile_);
      rules[update_id][typeid (cxx::cxx)].emplace ("cli.compile", compile_);
      rules[clean_id][typeid (cxx::cxx)].emplace ("cli.compile", compile_);

      rules[default_id][typeid (cxx::hxx)].emplace ("cli.compile", compile_);
      rules[update_id][typeid (cxx::hxx)].emplace ("cli.compile", compile_);
      rules[clean_id][typeid (cxx::hxx)].emplace ("cli.compile", compile_);

      rules[default_id][typeid (cxx::ixx)].emplace ("cli.compile", compile_);
      rules[update_id][typeid (cxx::ixx)].emplace ("cli.compile", compile_);
      rules[clean_id][typeid (cxx::ixx)].emplace ("cli.compile", compile_);

      // Configure.
      //

      // config.cli
      //
      {
        auto r (config::required (root, "config.cli", "cli"));

        // If we actually set a new value, test it by trying to execute.
        //
        if (r.second)
        {
          const string& cli (r.first);
          const char* args[] = {cli.c_str (), "--version", nullptr};

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

          string ver;
          try
          {
            process pr (args, false, false, true);
            ifdstream is (pr.in_ofd);

            for (bool first (true); !is.eof (); )
            {
              string l;
              getline (is, l);

              if (first)
              {
                // The version is the last word on the first line.
                //
                auto p (l.rfind (' '));
                if (p != string::npos)
                  ver = string (l, p + 1);

                first = false;
              }
            }

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

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

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

            throw failed ();
          }

          if (verb)
            text << cli << " " << ver;
        }
      }

      // config.cli.options
      //
      // This one is optional. We also merge it into the corresponding
      // cli.* variables.
      //
      if (auto* v = config::optional<list_value> (root, "config.cli.options"))
        root.append ("cli.options") += *v;
    }
  }
}