aboutsummaryrefslogtreecommitdiff
path: root/build2/cli/rule.cxx
blob: f621f02f3ed70e94ed80d9d0fa09578df5ae17a2 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// file      : build2/cli/rule.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2018 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <build2/cli/rule.hxx>

#include <build2/scope.hxx>
#include <build2/target.hxx>
#include <build2/context.hxx>
#include <build2/algorithm.hxx>
#include <build2/filesystem.hxx>
#include <build2/diagnostics.hxx>

#include <build2/cli/target.hxx>

using namespace std;
using namespace butl;

namespace build2
{
  namespace cli
  {
    // Figure out if name contains stem and, optionally, calculate prefix and
    // suffix.
    //
    static bool
    match_stem (const string& name, const string& stem,
                string* prefix = nullptr, string* suffix = nullptr)
    {
      size_t p (name.find (stem));

      if (p != string::npos)
      {
        if (prefix != nullptr)
          prefix->assign (name, 0, p);

        if (suffix != nullptr)
          suffix->assign (name, p + stem.size (), string::npos);

        return true;
      }

      return false;
    }

    bool compile_rule::
    match (action a, target& xt, const string&) const
    {
      tracer trace ("cli::compile_rule::match");

      if (cli_cxx* pt = xt.is_a<cli_cxx> ())
      {
        // The cli.cxx{} group.
        //
        cli_cxx& t (*pt);

        // See if we have a .cli source file.
        //
        bool r (false);
        for (prerequisite_member p: group_prerequisite_members (a, t))
        {
          if (p.is_a<cli> ())
          {
            // Check that the stem match.
            //
            if (!match_stem (t.name, p.name ()))
            {
              l4 ([&]{trace << ".cli file stem '" << p.name () << "' "
                            << "doesn't match target " << t;});
              return false;
            }

            r = true;
            break;
          }
        }

        if (!r)
        {
          l4 ([&]{trace << "no .cli source file for target " << t;});
          return r;
        }

        // Figure out the member list.
        //
        // At this stage, no further changes to cli.options are possible and
        // we can determine whether the --suppress-inline option is present.
        //
        // Passing the group as a "reference target" is a bit iffy,
        // conceptually.
        //
        t.h = &search<cxx::hxx> (t, t.dir, t.out, t.name);
        t.c = &search<cxx::cxx> (t, t.dir, t.out, t.name);
        t.i = find_option ("--suppress-inline", t, "cli.options")
          ? nullptr
          : &search<cxx::ixx> (t, t.dir, t.out, t.name);

        return r;
      }
      else
      {
        // One of the ?xx{} members.
        //
        target& t (xt);

        // Check if there is a corresponding cli.cxx{} group.
        //
        const cli_cxx* g (targets.find<cli_cxx> (t.dir, t.out, t.name));

        // If not or if it has no prerequisites (happens when we use it to
        // set cli.options) and this target has a cli{} prerequisite, then
        // synthesize the dependency.
        //
        if (g == nullptr || !g->has_prerequisites ())
        {
          for (prerequisite_member p: prerequisite_members (a, t))
          {
            if (p.is_a<cli> ())
            {
              // Check that the stems match.
              //
              if (match_stem (t.name, p.name ()))
              {
                if (g == nullptr)
                  g = &targets.insert<cli_cxx> (t.dir, t.out, t.name, trace);

                g->prerequisites (prerequisites {p.as_prerequisite ()});
              }
              else
                l4 ([&]{trace << ".cli file stem '" << p.name () << "' "
                              << "doesn't match target " << t;});
              break;
            }
          }
        }

        if (g == nullptr)
          return false;

        // For ixx{}, verify it is part of the group (i.e., not disabled
        // via --suppress-inline).
        //
        if (t.is_a<cxx::ixx> () &&
            find_option ("--suppress-inline", *g, "cli.options"))
          return false;

        t.group = g;
        return true;
      }
    }

    recipe compile_rule::
    apply (action a, target& xt) const
    {
      if (cli_cxx* pt = xt.is_a<cli_cxx> ())
      {
        cli_cxx& t (*pt);

        // Derive file names for the members.
        //
        t.h->derive_path ();
        t.c->derive_path ();
        if (t.i != nullptr)
          t.i->derive_path ();

        // Inject dependency on the output directory.
        //
        inject_fsdir (a, t);

        // Match prerequisites.
        //
        match_prerequisite_members (a, t);

        switch (a)
        {
        case perform_update_id: return &perform_update;
        case perform_clean_id:  return &perform_clean_group; // Standard impl.
        default:                return noop_recipe; // Configure update.
        }
      }
      else
      {
        const cli_cxx& g (xt.group->as<cli_cxx> ());
        build2::match (a, g);
        return group_recipe; // Execute the group's recipe.
      }
    }

    static void
    append_extension (cstrings& args,
                      const path_target& t,
                      const char* option,
                      const char* default_extension)
    {
      const string* e (t.ext ());
      assert (e != nullptr); // Should have been figured out in apply().

      if (*e != default_extension)
      {
        // CLI needs the extension with the leading dot (unless it is empty)
        // while we store the extension without. But if there is an extension,
        // then we can get it (with the dot) from the file name.
        //
        args.push_back (option);
        args.push_back (e->empty ()
                        ? e->c_str ()
                        : t.path ().extension_cstring () - 1);
      }
    }

    target_state compile_rule::
    perform_update (action a, const target& xt)
    {
      const cli_cxx& t (xt.as<cli_cxx> ());

      // Update prerequisites and determine if any relevant ones render us
      // out-of-date. Note that currently we treat all the prerequisites
      // as potentially affecting the result (think prologues/epilogues,
      // etc).
      //

      // The rule has been matched which means the members should be resolved
      // and paths assigned.
      //
      auto pr (
        execute_prerequisites<cli> (
          a, t, t.load_mtime (t.h->path ())));

      if (pr.first)
        return *pr.first;

      const cli& s (pr.second);

      // Translate paths to relative (to working directory). This
      // results in easier to read diagnostics.
      //
      path relo (relative (t.dir));
      path rels (relative (s.path ()));

      const scope& rs (t.root_scope ());

      const process_path& cli (cast<process_path> (rs["cli.path"]));

      cstrings args {cli.recall_string ()};

      // See if we need to pass --output-{prefix,suffix}
      //
      string prefix, suffix;
      match_stem (t.name, s.name, &prefix, &suffix);

      if (!prefix.empty ())
      {
        args.push_back ("--output-prefix");
        args.push_back (prefix.c_str ());
      }

      if (!suffix.empty ())
      {
        args.push_back ("--output-suffix");
        args.push_back (suffix.c_str ());
      }

      // See if we need to pass any --?xx-suffix options.
      //
      append_extension (args, *t.h, "--hxx-suffix", "hxx");
      append_extension (args, *t.c, "--cxx-suffix", "cxx");
      if (t.i != nullptr)
        append_extension (args, *t.i, "--ixx-suffix", "ixx");

      append_options (args, t, "cli.options");

      if (!relo.empty ())
      {
        args.push_back ("-o");
        args.push_back (relo.string ().c_str ());
      }

      args.push_back (rels.string ().c_str ());
      args.push_back (nullptr);

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

      run (cli, args);

      t.mtime (system_clock::now ());
      return target_state::changed;
    }
  }
}