aboutsummaryrefslogtreecommitdiff
path: root/build/context.cxx
blob: 4d1d1e400dfbdb81fc04705dab8bd3529cf742c6 (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
// file      : build/context.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#include <build/context>

#include <ostream>
#include <cassert>

using namespace std;

namespace build
{
  path work;
  path home;

  path src_root;
  path out_root;

  path src_base;
  path out_base;

  path
  src_out (const path& o)
  {
    assert (o.sub (out_root));
    return src_root / o.leaf (out_root);
  }

  path
  out_src (const path& s)
  {
    assert (s.sub (src_root));
    return out_root / s.leaf (src_root);
  }

  path
  translate (const path& p)
  {
    if (p.sub (work))
      return p.leaf (work);

    // If work is a sub-path of {src,out}_root and this path is also a
    // sub-bath of it, then use '..' to form a relative path.
    //
    if (work.sub (src_root) && p.sub (src_root) ||
        work.sub (out_root) && p.sub (out_root)) // @@ cache
      return p.relative (work);

    return p;
  }

  std::string
  diagnostic_string (const path& p)
  {
    if (p.absolute ())
    {
      path rp (translate (p));

#ifndef _WIN32
      if (rp.absolute () && rp.sub (home))
        return "~/" + rp.leaf (home).string ();
#endif

      return rp.string ();
    }

    return p.string ();
  }
}