aboutsummaryrefslogtreecommitdiff
path: root/build2/functions-path.cxx
blob: b76b27718040f992b1d897e24ee5e6746894c28a (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
// file      : build2/functions-path.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <build2/function>
#include <build2/variable>

using namespace std;

namespace build2
{
  static value
  path_thunk (vector_view<value> args, const function_overload& f)
  try
  {
    return function_family::default_thunk (move (args), f);
  }
  catch (const invalid_path& e)
  {
    error << "invalid path: '" << e.path << "'";
    throw failed ();
  }

  void
  path_functions ()
  {
    function_family f ("path", &path_thunk);

    // normalize
    //
    f["normalize"] = [](path p) {p.normalize (); return p;};
    f["normalize"] = [](paths v) {for (auto& p: v) p.normalize (); return v;};

    f["normalize"] = [](dir_path p) {p.normalize (); return p;};
    f["normalize"] = [](dir_paths v) {for (auto& p: v) p.normalize (); return v;};

    f[".normalize"] = [](names ns)
      {
        // For each path decide based on the presence of a trailing slash
        // whether it is a directory. Return as untyped list of (potentially
        // mixed) paths.
        //
        for (name& n: ns)
        {
          if (n.directory ())
            n.dir.normalize ();
          else
            n.value = convert<path> (move (n)).normalize ().string ();
        }
        return ns;
      };
  }
}