-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathcommand.h
More file actions
362 lines (286 loc) · 9.93 KB
/
command.h
File metadata and controls
362 lines (286 loc) · 9.93 KB
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
361
362
// Copyright (C) 2017-2018 Egor Pugin <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#pragma once
#include "node.h"
#include <primitives/command.h>
#include <primitives/executor.h>
#include <condition_variable>
#include <mutex>
#define SW_INTERNAL_ADD_COMMAND(name, target) \
(target).Storage.push_back(name)
#define SW_MAKE_CUSTOM_COMMAND(type, name, target, ...) \
auto name = std::make_shared<type>((target).getMainBuild().getContext(), __VA_ARGS__)
#ifdef _MSC_VER
#define SW_MAKE_CUSTOM_COMMAND_AND_ADD(type, name, target, ...) \
SW_MAKE_CUSTOM_COMMAND(type, name, target, __VA_ARGS__); \
SW_INTERNAL_ADD_COMMAND(name, target)
#else
#define SW_MAKE_CUSTOM_COMMAND_AND_ADD(type, name, target, ...) \
SW_MAKE_CUSTOM_COMMAND(type, name, target, ##__VA_ARGS__); \
SW_INTERNAL_ADD_COMMAND(name, target)
#endif
/*
#define SW_MAKE_COMMAND(name, target) \
SW_MAKE_CUSTOM_COMMAND(Command, name, target)
*/
#define SW_MAKE_COMMAND_AND_ADD(name, target) \
SW_MAKE_CUSTOM_COMMAND_AND_ADD(Command, name, target)
/*
#define _SW_MAKE_EXECUTE_COMMAND(name, target) \
SW_MAKE_CUSTOM_COMMAND(ExecuteCommand, name, target, __FILE__, __LINE__)
*/
#define _SW_MAKE_EXECUTE_COMMAND_AND_ADD(name, target) \
SW_MAKE_CUSTOM_COMMAND_AND_ADD(ExecuteCommand, name, target, __FILE__, __LINE__)
// ExecuteBuiltinCommand
#ifdef _MSC_VER
/*
#define SW_MAKE_EXECUTE_BUILTIN_COMMAND(var_name, target, func_name, ...) \
SW_MAKE_CUSTOM_COMMAND(::sw::builder::ExecuteBuiltinCommand, var_name, target, func_name, __VA_ARGS__)
*/
#define SW_MAKE_EXECUTE_BUILTIN_COMMAND_AND_ADD(var_name, target, func_name, ...) \
SW_MAKE_CUSTOM_COMMAND_AND_ADD(::sw::builder::ExecuteBuiltinCommand, var_name, target, func_name, __VA_ARGS__)
#else
/*
#define SW_MAKE_EXECUTE_BUILTIN_COMMAND(var_name, target, func_name, ...) \
SW_MAKE_CUSTOM_COMMAND(::sw::builder::ExecuteBuiltinCommand, var_name, target, func_name, ## __VA_ARGS__)
*/
#define SW_MAKE_EXECUTE_BUILTIN_COMMAND_AND_ADD(var_name, target, func_name, ...) \
SW_MAKE_CUSTOM_COMMAND_AND_ADD(::sw::builder::ExecuteBuiltinCommand, var_name, target, func_name, ## __VA_ARGS__)
#endif
namespace sw
{
struct FileStorage;
struct Program;
struct SwBuilderContext;
struct SW_BUILDER_API CommandNode : std::enable_shared_from_this<CommandNode>
{
using SPtr = std::shared_ptr<CommandNode>;
std::unordered_set<SPtr> dependencies;
std::atomic_size_t dependencies_left = 0;
std::unordered_set<SPtr> dependent_commands;
std::atomic_size_t *current_command = nullptr;
std::atomic_size_t *total_commands = nullptr;
CommandNode();
CommandNode(const CommandNode &);
CommandNode &operator=(const CommandNode &);
virtual ~CommandNode();
virtual String getName(bool short_name = false) const = 0;
virtual void execute() = 0;
virtual void prepare() = 0;
virtual bool lessDuringExecution(const CommandNode &) const = 0;
void clear()
{
dependent_commands.clear();
dependencies.clear();
}
};
struct SW_BUILDER_API ResourcePool
{
int n = -1; // unlimited
std::condition_variable cv;
std::mutex m;
void lock()
{
if (n == -1)
return;
std::unique_lock lk(m);
cv.wait(lk, [this] { return n > 0; });
--n;
}
void unlock()
{
if (n == -1)
return;
std::unique_lock lk(m);
++n;
lk.unlock();
cv.notify_one();
}
};
namespace builder
{
using ::primitives::command::QuoteType;
namespace detail
{
#pragma warning(push)
#pragma warning(disable:4275) // warning C4275: non dll-interface struct 'primitives::Command' used as base for dll-interface struct 'sw::builder::Command'
struct SW_BUILDER_API ResolvableCommand : ::primitives::Command
{
#pragma warning(pop)
using ::primitives::Command::push_back;
path resolveProgram(const path &p) const override;
};
}
struct SW_BUILDER_API Command : ICastable, CommandNode, detail::ResolvableCommand // hide?
{
using Base = detail::ResolvableCommand;
using Clock = std::chrono::high_resolution_clock;
String name;
String name_short;
Files inputs;
// byproducts
// used only to clean files and pre-create dirs
//Files intermediate;
// if some commands accept pairs of args, and specific outputs depend on specific inputs
// C I1 O1 I2 O2
// then split that command!
Files outputs;
Files implicit_inputs;
// additional create dirs
Files output_dirs;
fs::file_time_type mtime = fs::file_time_type::min();
std::optional<bool> use_response_files;
int first_response_file_argument = 0;
bool remove_outputs_before_execution = false; // was true
bool protect_args_with_quotes = true;
bool always = false;
bool do_not_save_command = false;
bool silent = false; // no log record
bool show_output = false; // no command output
bool write_output_to_file = false;
int strict_order = 0; // used to execute this before other commands
ResourcePool *pool = nullptr;
std::thread::id tid;
Clock::time_point t_begin;
Clock::time_point t_end;
enum
{
MU_FALSE = 0,
MU_TRUE = 1,
MU_ALWAYS = 2,
};
int maybe_unused = 0;
enum
{
CS_UNDEFINED,
CS_DO_NOT_SAVE,
CS_GLOBAL,
CS_LOCAL,
};
int command_storage = 0;
Command() = default;
Command(const SwBuilderContext &swctx);
virtual ~Command();
void prepare() override;
void execute() override;
void execute(std::error_code &ec) override;
void clean() const;
bool isExecuted() const { return pid != -1 || executed_; }
String getName(bool short_name = false) const override;
virtual bool isOutdated() const;
bool needsResponseFile() const;
bool needsResponseFile(size_t sz) const;
using Base::push_back;
using Base::setProgram;
void setProgram(std::shared_ptr<Program> p);
void addInput(const path &p);
void addInput(const Files &p);
void addImplicitInput(const path &p);
void addImplicitInput(const Files &p);
void addOutput(const path &p);
void addOutput(const Files &p);
path redirectStdin(const path &p);
path redirectStdout(const path &p, bool append = false);
path redirectStderr(const path &p, bool append = false);
size_t getHash() const;
void addPathDirectory(const path &p);
Files getGeneratedDirs() const; // used by generators
void addInputOutputDeps();
path writeCommand(const path &basename) const;
bool lessDuringExecution(const CommandNode &rhs) const override;
void onBeforeRun() noexcept override;
void onEnd() noexcept override;
path getResponseFilename() const;
virtual String getResponseFileContents(bool showIncludes = false) const;
int getFirstResponseFileArgument() const;
Arguments &getArguments() override;
const Arguments &getArguments() const override;
Command &operator|(Command &);
Command &operator|=(Command &);
const SwBuilderContext &getContext() const;
void setContext(const SwBuilderContext &);
protected:
bool prepared = false;
bool executed_ = false;
virtual bool check_if_file_newer(const path &, const String &what, bool throw_on_missing) const;
private:
const SwBuilderContext *swctx = nullptr;
mutable size_t hash = 0;
Arguments rsp_args;
mutable String log_string;
virtual void execute1(std::error_code *ec = nullptr);
virtual size_t getHash1() const;
void postProcess(bool ok = true);
virtual void postProcess1(bool ok) {}
bool beforeCommand();
void afterCommand();
bool isTimeChanged() const;
void printLog() const;
size_t getHashAndSave() const;
String makeErrorString();
String makeErrorString(const String &e);
String saveCommand() const;
void printOutputs();
};
struct SW_BUILDER_API CommandSequence : Command
{
using Command::Command;
void addCommand(const std::shared_ptr<Command> &c);
template <class C = Command, class ... Args>
std::shared_ptr<C> addCommand(Args && ... args)
{
auto c = std::make_shared<C>(getContext(), std::forward<Args>(args)...);
commands.push_back(c);
return c;
}
const std::vector<std::shared_ptr<Command>> &getCommands() { return commands; }
private:
std::vector<std::shared_ptr<Command>> commands;
void execute1(std::error_code *ec = nullptr) override;
size_t getHash1() const override;
void prepare() override;
};
// remove? probably no, just don't use it much
// we always can create executable commands that is not builtin into modules
struct SW_BUILDER_API ExecuteBuiltinCommand : Command
{
using F = std::function<void(void)>;
ExecuteBuiltinCommand(const SwBuilderContext &swctx);
ExecuteBuiltinCommand(const SwBuilderContext &swctx, const String &cmd_name, void *f, int version = 0);
virtual ~ExecuteBuiltinCommand() = default;
using Command::push_back;
void push_back(const Strings &strings);
void push_back(const Files &files);
private:
void execute1(std::error_code *ec = nullptr) override;
size_t getHash1() const override;
void prepare() override {}
};
SW_BUILDER_API
String getInternalCallBuiltinFunctionName();
} // namespace bulder
using builder::ExecuteBuiltinCommand;
using Commands = std::unordered_set<std::shared_ptr<builder::Command>>;
/// return input when file not found
SW_BUILDER_API
path resolveExecutable(const path &p);
SW_BUILDER_API
path resolveExecutable(const FilesOrdered &paths);
SW_BUILDER_API
std::map<path, String> &getMsvcIncludePrefixes();
SW_BUILDER_API
String detectMsvcPrefix(builder::detail::ResolvableCommand c, const path &idir);
} // namespace sw
namespace std
{
template<> struct hash<sw::builder::Command>
{
size_t operator()(const sw::builder::Command &c) const
{
return c.getHash();
}
};
}