aboutsummaryrefslogtreecommitdiff
path: root/build/cli/module.cxx
blob: c44d6707b8abec5bdb08f3c6a060e7fc5bb138b9 (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
// 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/config/utility>

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

using namespace std;
using namespace butl;

namespace build
{
  namespace cli
  {
    static compile compile_;

    extern "C" bool
    cli_init (scope& root,
              scope& base,
              const location& loc,
              std::unique_ptr<module>&,
              bool first,
              bool optional)
    {
      tracer trace ("cli::init");
      level5 ([&]{trace << "for " << base.out_path ();});

      // Make sure the cxx module has been loaded since we need its
      // targets types (?xx{}). Note that we don't try to load it
      // ourselves because of the non-trivial variable merging
      // semantics. So it is better to let the user load cxx
      // explicitly.
      //
      {
        auto l (base["cxx.loaded"]);

        if (!l || !as<bool> (*l))
          fail (loc) << "cxx module must be loaded before cli";
      }

      // Register target types.
      //
      {
        auto& tts (base.target_types);

        tts.insert<cli> ();
        tts.insert<cli_cxx> ();
      }

      // Enter module variables.
      //
      if (first)
      {
        var_pool.find ("config.cli.configured", bool_type);

        var_pool.find ("config.cli", string_type); //@@ VAR type

        var_pool.find ("config.cli.options", strings_type);
        var_pool.find ("cli.options", strings_type);
      }

      // Configure.
      //
      // The plan is as follows: try to configure the module. If this fails,
      // we are using default values, and the module is optional, leave it
      // unconfigured.
      //

      // We will only honor optional if the user didn't specify any cli
      // configuration explicitly.
      //
      optional = optional && !config::specified (root, "config.cli");

      // Don't re-run tests if the configuration says we are unconfigured.
      //
      if (optional)
      {
        auto l (root["config.cli.configured"]);

        if (l && !as<bool> (*l))
          return false;
      }

      // config.cli
      //
      if (first)
      {
        // Return version or empty string if unable to execute (e.g.,
        // the cli executable is not found).
        //
        auto test = [optional] (const char* cli) -> string
        {
          const char* args[] = {cli, "--version", nullptr};

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

          string ver;
          try
          {
            process pr (args, 0, -1); // Open pipe to stdout.
            ifdstream is (pr.in_ofd);

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

            is.close (); // Don't block the other end.

            if (!pr.wait ())
              return string (); // Not found.

            if (ver.empty ())
              fail << "unexpected output from " << cli;

            return ver;
          }
          catch (const process_error& e)
          {
            if (!optional)
              error << "unable to execute " << cli << ": " << e.what ();

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

            throw failed ();
          }
        };

        string ver;
        const char* cli ("cli"); // Default.

        if (optional)
        {
          // Test the default value before setting any config.cli.* values
          // so that if we fail to configure, nothing will be written to
          // config.build.
          //
          ver = test (cli);

          if (ver.empty ())
          {
            // Note that we are unconfigured so that we don't keep re-testing
            // this on each run.
            //
            root.assign ("config.cli.configured") = false;

            if (verb >= 2)
              text << cli << " not found, leaving cli module unconfigured";

            return false;
          }
          else
          {
            auto p (config::required (root, "config.cli", cli));
            assert (p.second && as<string> (p.first) == cli);
          }
        }
        else
        {
          auto p (config::required (root, "config.cli", cli));

          // If we actually set a new value, test it by trying to execute.
          //
          if (p.second)
          {
            cli = as<string> (p.first).c_str ();
            ver = test (cli);

            if (ver.empty ())
              throw failed ();
          }
        }

        // Clear the unconfigured flag, if any.
        //
        root.assign ("config.cli.configured") = true;

        if (!ver.empty () && verb >= 2)
          text << cli << " " << ver;
      }

      // config.cli.options
      //
      // This one is optional. We also merge it into the corresponding
      // cli.* variables. See the cxx module for more information on
      // this merging semantics and some of its tricky aspects.
      //
      if (const value& v = config::optional (root, "config.cli.options"))
        base.assign ("cli.options") += as<strings> (v);

      // Register our rules.
      //
      {
        auto& rs (base.rules);

        rs.insert<cli_cxx> (perform_id, update_id, "cli", compile_);
        rs.insert<cli_cxx> (perform_id, clean_id, "cli", compile_);

        rs.insert<cxx::hxx> (perform_id, update_id, "cli", compile_);
        rs.insert<cxx::hxx> (perform_id, clean_id, "cli", compile_);

        rs.insert<cxx::cxx> (perform_id, update_id, "cli", compile_);
        rs.insert<cxx::cxx> (perform_id, clean_id, "cli", compile_);

        rs.insert<cxx::ixx> (perform_id, update_id, "cli", compile_);
        rs.insert<cxx::ixx> (perform_id, clean_id, "cli", compile_);

        // Other rules (e.g., cxx::compile) may need to have the group
        // members resolved. Looks like a general pattern: groups should
        // resolve on configure(update).
        //
        rs.insert<cli_cxx> (configure_id, update_id, "cli", compile_);
      }

      return true;
    }
  }
}