aboutsummaryrefslogtreecommitdiff
path: root/libbutl/default-options.txx
blob: dc809ade366b63727e795b4ce01ccc9781be95e9 (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// file      : libbutl/default-options.txx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

LIBBUTL_MODEXPORT namespace butl //@@ MOD Clang needs this for some reason.
{
  inline bool
  options_dir_exists (const dir_path& d)
  try
  {
    return dir_exists (d);
  }
  catch (std::system_error& e)
  {
    throw std::make_pair (path_cast<path> (d), std::move (e));
  }

  // Search for and parse the options files in the specified directory and
  // its local/ subdirectory, if exists, in the reverse order and append the
  // options to the resulting list. Return false if --no-default-options is
  // encountered.
  //
  // Note that by default we check for the local/ subdirectory even if we
  // don't think it belongs to the remote directory; the user may move things
  // around or it could be a VCS we don't yet recognize and there doesn't seem
  // to be any harm in doing so.
  //
  // Also note that if the directory is remote, then for now the options files
  // in both the directory itself and its local/ subdirectory are considered
  // remote (see load_default_options() for details).
  //
  template <typename O, typename S, typename U, typename F>
  bool
  load_default_options_files (const dir_path& d,
                              const std::string& opt,
                              bool args,
                              bool remote,
                              const small_vector<path, 2>& fs,
                              F&& fn,
                              default_options<O>& def_ops,
                              bool load_sub = true,
                              bool load_dir = true)
  {
    assert (load_sub || load_dir);

    bool r (true);

    auto load = [&opt, args, &fs, &fn, &def_ops, &r]
                (const dir_path& d, bool rem)
    {
      using namespace std;

      for (const path& f: reverse_iterate (fs))
      {
        path p (d / f);

        try
        {
          if (file_exists (p)) // Follows symlinks.
          {
            fn (p, rem, false /* overwrite */);

            S s (p.string (), opt);

            // @@ Note that the potentially thrown exceptions (unknown option,
            //    unexpected argument, etc) will not contain any location
            //    information. Intercepting exception handling to add the file
            //    attribution feels too hairy for now. Maybe we should support
            //    this in CLI.
            //
            O o;
            small_vector<string, 1> as;

            if (args)
            {
              while (s.more ())
              {
                if (!o.parse (s, U::fail, U::stop))
                  as.push_back (s.next ());
              }
            }
            else
              o.parse (s, U::fail, U::fail);

            if (o.no_default_options ())
              r = false;

            def_ops.push_back (default_options_entry<O> {move (p),
                                                         move (o),
                                                         move (as),
                                                         rem});
          }
        }
        catch (system_error& e)
        {
          throw make_pair (move (p), move (e));
        }
      }
    };

    dir_path ld (d / dir_path ("local"));

    if (load_sub && options_dir_exists (ld))
      load (ld, remote);

    // Don't load options from .build2/ if --no-default-options is encountered
    // in .build2/local/.
    //
    if (load_dir && r)
      load (d, remote);

    return r;
  }

  template <typename O, typename S, typename U, typename F>
  default_options<O>
  load_default_options (const optional<dir_path>& sys_dir,
                        const optional<dir_path>& home_dir,
                        const optional<dir_path>& extra_dir,
                        const default_options_files& ofs,
                        F&& fn,
                        const std::string& opt,
                        bool args)
  {
    if (sys_dir)
      assert (sys_dir->absolute () && sys_dir->normalized ());

    if (home_dir)
      assert (home_dir->absolute () && home_dir->normalized ());

    if (extra_dir)
      assert (extra_dir->absolute () && extra_dir->normalized ());

    default_options<O> r;

    // Search for and parse the options files in the start and outer
    // directories until root/home directory and in the extra and system
    // directories, stopping if --no-default-options is encountered and
    // reversing the resulting options entry list in the end.
    //
    // Note that the extra directory is handled either during the start/outer
    // directory traversal, if it turns out to be a subdirectory of any of the
    // traversed directories, or right after.
    //
    bool load       (true);
    bool load_extra (extra_dir && options_dir_exists (*extra_dir));

    if (ofs.start)
    {
      assert (ofs.start->absolute () && ofs.start->normalized ());

      for (dir_path d (*ofs.start);
           !(d.root () || (home_dir && d == *home_dir));
           d = d.directory ())
      {
        bool remote;

        try
        {
          remote = git_repository (d);
        }
        catch (std::system_error& e)
        {
          throw std::make_pair (d / ".git", std::move (e));
        }

        // If the directory is remote, then mark all the previously collected
        // local entries (that belong to its subdirectories but not to the
        // extra directory) as remote too.
        //
        // @@ Note that currently the local/ subdirectory of a remote
        //    directory is considered remote (see above for details). When
        //    changing that, skip entries from directories with the `local`
        //    name.
        //
        if (remote)
        {
          // We could optimize this, iterating in the reverse order until the
          // fist remote entry. However, let's preserve the function calls
          // order for entries being overwritten.
          //
          for (default_options_entry<O>& e: r)
          {
            if (!e.remote &&
                (!extra_dir || e.file.directory () != *extra_dir))
            {
              e.remote = true;

              fn (e.file, true /* remote */, true /* overwrite */);
            }
          }
        }

        // If --no-default-options is encountered, then stop the files search
        // but continue the directory traversal until the remote directory is
        // encountered and, if that's the case, mark the already collected
        // local entries as remote.
        //
        if (load)
        {
          dir_path od (d / dir_path (".build2"));

          // If the extra directory is a subdirectory of the current
          // directory, then load it first, but don't load .build2/ or
          // .build2/local/ if it matches any of them.
          //
          bool load_build2       (true);
          bool load_build2_local (true);

          if (load_extra && extra_dir->sub (d))
          {
            load = load_default_options_files<O, S, U> (*extra_dir,
                                                        opt,
                                                        args,
                                                        false /* remote */,
                                                        ofs.files,
                                                        std::forward<F> (fn),
                                                        r);

            load_extra        = false;
            load_build2       = *extra_dir != od;
            load_build2_local = *extra_dir != od / dir_path ("local");
          }

          if (load && options_dir_exists (od))
            load = load_default_options_files<O, S, U> (od,
                                                        opt,
                                                        args,
                                                        remote,
                                                        ofs.files,
                                                        std::forward<F> (fn),
                                                        r,
                                                        load_build2_local,
                                                        load_build2);
        }

        if (!load && remote)
          break;
      }
    }

    if (load && load_extra)
      load = load_default_options_files<O, S, U> (*extra_dir,
                                                  opt,
                                                  args,
                                                  false /* remote */,
                                                  ofs.files,
                                                  std::forward<F> (fn),
                                                  r);

    if (load && home_dir)
    {
      dir_path d (*home_dir / dir_path (".build2"));

      if (options_dir_exists (d))
        load = load_default_options_files<O, S, U> (d,
                                                    opt,
                                                    args,
                                                    false /* remote */,
                                                    ofs.files,
                                                    std::forward<F> (fn),
                                                    r);
    }

    if (load && sys_dir && options_dir_exists (*sys_dir))
      load_default_options_files<O, S, U> (*sys_dir,
                                           opt,
                                           args,
                                           false /* remote */,
                                           ofs.files,
                                           std::forward<F> (fn),
                                           r);

    std::reverse (r.begin (), r.end ());

    return r;
  }

  template <typename O, typename F>
  O
  merge_default_options (const default_options<O>& def_ops,
                         const O& cmd_ops,
                         F&& fn)
  {
    // Optimize for the common case.
    //
    if (def_ops.empty ())
      return cmd_ops;

    O r;
    for (const default_options_entry<O>& e: def_ops)
    {
      fn (e, cmd_ops);
      r.merge (e.options);
    }

    r.merge (cmd_ops);
    return r;
  }

  template <typename O, typename  AS, typename F>
  AS
  merge_default_arguments (const default_options<O>& def_ops,
                           const AS& cmd_args,
                           F&& fn)
  {
    AS r;
    for (const default_options_entry<O>& e: def_ops)
    {
      fn (e, cmd_args);
      r.insert (r.end (), e.arguments.begin (), e.arguments.end ());
    }

    // Optimize for the common case.
    //
    if (r.empty ())
      return cmd_args;

    r.insert (r.end (), cmd_args.begin (), cmd_args.end ());
    return r;
  }

  template <typename I, typename F>
  optional<dir_path>
  default_options_start (const optional<dir_path>& home, I b, I e, F&& f)
  {
    if (home)
      assert (home->absolute () && home->normalized ());

    if (b == e)
      return nullopt;

    // Use the first directory as a start.
    //
    I i (b);
    dir_path d (f (i));

    // Try to find a common prefix for each subsequent directory.
    //
    for (++i; i != e; ++i)
    {
      bool p (false);

      for (;
           !(d.root () || (home && d == *home));
           d = d.directory ())
      {
        if (f (i).sub (d))
        {
          p = true;
          break;
        }
      }

      if (!p)
        return nullopt;
    }

    return d;
  }
}