summaryrefslogtreecommitdiff
path: root/src/Context.hpp
blob: 7af070569ef7ec170014c17cf27f5a25b31b28aa (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
// Copyright (C) 2020 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc., 51
// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

#pragma once

#include "system.hpp"

#include "Args.hpp"
#include "ArgsInfo.hpp"
#include "Config.hpp"
#include "Digest.hpp"
#include "File.hpp"
#include "MiniTrace.hpp"
#include "NonCopyable.hpp"
#include "ccache.hpp"

#ifdef INODE_CACHE_SUPPORTED
#  include "InodeCache.hpp"
#endif

#include "third_party/nonstd/optional.hpp"
#include "third_party/nonstd/string_view.hpp"

#include <string>
#include <unordered_map>
#include <vector>

class SignalHandler;

class Context : NonCopyable
{
public:
  Context();
  ~Context();

  ArgsInfo args_info;
  Config config;

  // Current working directory as returned by getcwd(3).
  std::string actual_cwd;

  // Current working directory according to $PWD (falling back to getcwd(3)).
  std::string apparent_cwd;

  // The original argument list.
  Args orig_args;

  // Name (represented as a hash) of the file containing the manifest for the
  // cached result.
  const nonstd::optional<Digest>& manifest_name() const;

  // Full path to the file containing the manifest (cachedir/a/b/cdef[...]M), if
  // any.
  const nonstd::optional<std::string>& manifest_path() const;

  // Name (represented as a hash) of the file containing the cached result.
  const nonstd::optional<Digest>& result_name() const;

  // Full path to the file containing the result (cachedir/a/b/cdef[...]R).
  const nonstd::optional<std::string>& result_path() const;

  // Time of compilation. Used to see if include files have changed after
  // compilation.
  time_t time_of_compilation = 0;

  // Files included by the preprocessor and their hashes.
  std::unordered_map<std::string, Digest> included_files;

  // Uses absolute path for some include files.
  bool has_absolute_include_headers = false;

  // Have we tried and failed to get colored diagnostics?
  bool diagnostics_color_failed = false;

  // The name of the temporary preprocessed file.
  std::string i_tmpfile;

  // The name of the cpp stderr file.
  std::string cpp_stderr;

  // The .gch/.pch/.pth file used for compilation.
  std::string included_pch_file;

  // Headers (or directories with headers) to ignore in manifest mode.
  std::vector<std::string> ignore_header_paths;

#ifdef INODE_CACHE_SUPPORTED
  // InodeCache that caches source file hashes when enabled.
  mutable InodeCache inode_cache;
#endif

  // Statistics updates which get written into the statistics file belonging to
  // the result.
  Counters counter_updates;

  // Statistics updates which get written into the statistics file belonging to
  // the manifest.
  Counters manifest_counter_updates;

  // PID of currently executing compiler that we have started, if any. 0 means
  // no ongoing compilation.
  pid_t compiler_pid = 0;

  // Files used by the hash debugging functionality.
  std::vector<File> hash_debug_files;

  // Options to ignore for the hash.
  const std::vector<std::string>& ignore_options() const;
  void set_ignore_options(const std::vector<std::string>& options);

  // Original umask before applying the `umask`/`CCACHE_UMASK` configuration, or
  // `nullopt` if there is no such configuration.
  nonstd::optional<mode_t> original_umask;

#ifdef MTR_ENABLED
  // Internal tracing.
  std::unique_ptr<MiniTrace> mini_trace;
#endif

  void set_manifest_name(const Digest& name);
  void set_manifest_path(const std::string& path);
  void set_result_name(const Digest& name);
  void set_result_path(const std::string& path);

  // Register a temporary file to remove at program exit.
  void register_pending_tmp_file(const std::string& path);

private:
  nonstd::optional<Digest> m_manifest_name;
  nonstd::optional<std::string> m_manifest_path;

  nonstd::optional<Digest> m_result_name;
  nonstd::optional<std::string> m_result_path;

  // Options to ignore for the hash.
  std::vector<std::string> m_ignore_options;

  // [Start of variables touched by the signal handler]

  // Temporary files to remove at program exit.
  std::vector<std::string> m_pending_tmp_files;

  // [End of variables touched by the signal handler]

  friend SignalHandler;
  void unlink_pending_tmp_files();
  void unlink_pending_tmp_files_signal_safe(); // called from signal handler
};

inline const nonstd::optional<Digest>&
Context::manifest_name() const
{
  return m_manifest_name;
}

inline const nonstd::optional<std::string>&
Context::manifest_path() const
{
  return m_manifest_path;
}

inline const nonstd::optional<Digest>&
Context::result_name() const
{
  return m_result_name;
}

inline const nonstd::optional<std::string>&
Context::result_path() const
{
  return m_result_path;
}

inline const std::vector<std::string>&
Context::ignore_options() const
{
  return m_ignore_options;
}

inline void
Context::set_manifest_name(const Digest& name)
{
  m_manifest_name = name;
}

inline void
Context::set_manifest_path(const std::string& path)
{
  m_manifest_path = path;
}

inline void
Context::set_result_name(const Digest& name)
{
  m_result_name = name;
}

inline void
Context::set_result_path(const std::string& path)
{
  m_result_path = path;
}