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

#include <build/context>

#include <ostream>
#include <cassert>
#include <system_error>

#include <build/scope>
#include <build/target>
#include <build/rule>
#include <build/diagnostics>

using namespace std;
using namespace butl;

namespace build
{
  dir_path work;
  dir_path home;

  const meta_operation_info* current_mif;
  const operation_info* current_oif;
  execution_mode current_mode;

  // Builtin rules.
  //
  static dir_rule dir_;
  static fsdir_rule fsdir_;
  static file_rule file_;

  void
  reset ()
  {
    targets.clear ();
    scopes.clear ();
    variable_pool.clear ();

    // Enter builtin variables.
    //
    variable_pool.insert (variable ("subprojects", '='));

    // Create global scope. For Win32 we use the empty path since there
    // is no "real" root path. On POSIX, however, this is a real path.
    // See the comment in <build/path-map> for details.
    //
#ifdef _WIN32
    global_scope = &scopes[dir_path ()];
#else
    global_scope = &scopes[dir_path ("/")];
#endif

    global_scope->assign ("work") = work;
    global_scope->assign ("home") = home;

    // Register builtin target types.
    //
    {
      target_type_map& tts (global_scope->target_types);

      tts.insert<file>  ();
      tts.insert<dir>   ();
      tts.insert<fsdir> ();
    }

    // Register builtin rules.
    //
    {
      rule_map& rs (global_scope->rules);

      rs.insert<dir> (default_id, "dir", dir_);
      rs.insert<dir> (update_id, "dir", dir_);
      rs.insert<dir> (clean_id, "dir", dir_);

      rs.insert<fsdir> (default_id, "fsdir", fsdir_);
      rs.insert<fsdir> (update_id, "fsdir", fsdir_);
      rs.insert<fsdir> (clean_id, "fsdir", fsdir_);

      rs.insert<file> (default_id, "file", file_);
      rs.insert<file> (update_id, "file", file_);
      rs.insert<file> (clean_id, "file", file_);
    }

  }

  fs_status<mkdir_status>
  mkdir (const dir_path& d)
  {
    // We don't want to print the command if the directory already
    // exists. This makes the below code a bit ugly.
    //
    mkdir_status ms;

    try
    {
      ms = try_mkdir (d);
    }
    catch (const system_error& e)
    {
      text << "mkdir " << d;
      fail << "unable to create directory " << d << ": " << e.what ();
    }

    if (ms == mkdir_status::success)
      text << "mkdir " << d;

    return ms;
  }

  fs_status<mkdir_status>
  mkdir_p (const dir_path& d)
  {
    // We don't want to print the command if the directory already
    // exists. This makes the below code a bit ugly.
    //
    mkdir_status ms;

    try
    {
      ms = try_mkdir_p (d);
    }
    catch (const system_error& e)
    {
      text << "mkdir -p " << d;
      fail << "unable to create directory " << d << ": " << e.what ();
    }

    if (ms == mkdir_status::success)
      text << "mkdir -p " << d;

    return ms;
  }

  dir_path
  src_out (const dir_path& out, scope& s)
  {
    scope& rs (*s.root_scope ());
    return src_out (out, rs.path (), rs.src_path ());
  }

  dir_path
  out_src (const dir_path& src, scope& s)
  {
    scope& rs (*s.root_scope ());
    return out_src (src, rs.path (), rs.src_path ());
  }

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

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

  const dir_path* relative_base = &work;
}