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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
|
// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef INCLUDED_SRC_BUILDTOOL_COMMON_CLI_HPP
#define INCLUDED_SRC_BUILDTOOL_COMMON_CLI_HPP
#include <chrono>
#include <cstdlib>
#include <filesystem>
#include <string>
#include <thread>
#include <vector>
#include "CLI/CLI.hpp"
#include "fmt/core.h"
#include "gsl-lite/gsl-lite.hpp"
#include "nlohmann/json.hpp"
#include "src/buildtool/build_engine/expression/evaluator.hpp"
#include "src/buildtool/common/clidefaults.hpp"
#include "src/buildtool/compatibility/compatibility.hpp"
#include "src/buildtool/logging/log_level.hpp"
constexpr auto kDefaultTimeout = std::chrono::milliseconds{300000};
/// \brief Arguments common to all commands.
struct CommonArguments {
std::optional<std::filesystem::path> workspace_root{};
std::optional<std::filesystem::path> repository_config{};
std::optional<std::string> main{};
std::size_t jobs{std::max(1U, std::thread::hardware_concurrency())};
};
struct LogArguments {
std::vector<std::filesystem::path> log_files{};
LogLevel log_limit{kDefaultLogLevel};
bool plain_log{false};
bool log_append{false};
};
/// \brief Arguments required for analysing targets.
struct AnalysisArguments {
std::optional<std::size_t> expression_log_limit{};
std::vector<std::string> defines{};
std::filesystem::path config_file{};
std::optional<nlohmann::json> target{};
std::optional<std::string> request_action_input{};
std::optional<std::string> target_file_name{};
std::optional<std::string> rule_file_name{};
std::optional<std::string> expression_file_name{};
std::optional<std::filesystem::path> target_root{};
std::optional<std::filesystem::path> rule_root{};
std::optional<std::filesystem::path> expression_root{};
std::optional<std::filesystem::path> graph_file{};
std::optional<std::filesystem::path> artifacts_to_build_file{};
};
/// \brief Arguments required for describing targets/rules.
struct DescribeArguments {
bool print_json{};
bool describe_rule{};
};
/// \brief Arguments required for running diagnostics.
struct DiagnosticArguments {
std::optional<std::string> dump_actions{std::nullopt};
std::optional<std::string> dump_blobs{std::nullopt};
std::optional<std::string> dump_trees{std::nullopt};
std::optional<std::string> dump_vars{std::nullopt};
std::optional<std::string> dump_targets{std::nullopt};
std::optional<std::string> dump_export_targets{std::nullopt};
std::optional<std::string> dump_targets_graph{std::nullopt};
std::optional<std::string> dump_anonymous{std::nullopt};
std::optional<std::string> dump_nodes{std::nullopt};
};
/// \brief Arguments required for specifying build endpoint.
struct EndpointArguments {
std::optional<std::filesystem::path> local_root{};
std::optional<std::string> remote_execution_address;
std::vector<std::string> platform_properties;
};
/// \brief Arguments required for building.
struct BuildArguments {
std::optional<std::vector<std::string>> local_launcher{std::nullopt};
std::chrono::milliseconds timeout{kDefaultTimeout};
std::size_t build_jobs{};
std::optional<std::string> dump_artifacts{std::nullopt};
std::optional<std::string> print_to_stdout{std::nullopt};
bool show_runfiles{false};
};
/// \brief Arguments required for staging.
struct StageArguments {
std::filesystem::path output_dir{};
bool remember{false};
};
/// \brief Arguments required for rebuilding.
struct RebuildArguments {
std::optional<std::string> cache_endpoint{};
std::optional<std::filesystem::path> dump_flaky{};
};
/// \brief Arguments for fetching artifacts from CAS.
struct FetchArguments {
std::string object_id{};
std::optional<std::filesystem::path> output_path{};
bool remember{false};
bool raw_tree{};
};
/// \brief Arguments required for running from graph file.
struct GraphArguments {
nlohmann::json artifacts{};
std::filesystem::path graph_file{};
std::optional<std::filesystem::path> git_cas{};
};
// Arguments for authentication methods.
/// \brief Arguments shared by both server and client
struct CommonAuthArguments {
std::optional<std::filesystem::path> tls_ca_cert{std::nullopt};
};
/// \brief Arguments used by the client
struct ClientAuthArguments {
std::optional<std::filesystem::path> tls_client_cert{std::nullopt};
std::optional<std::filesystem::path> tls_client_key{std::nullopt};
};
/// \brief Authentication arguments used by subcommand just execute
struct ServerAuthArguments {
std::optional<std::filesystem::path> tls_server_cert{std::nullopt};
std::optional<std::filesystem::path> tls_server_key{std::nullopt};
};
struct ExecutionServiceArguments {
std::optional<int> port{std::nullopt};
std::optional<std::filesystem::path> info_file{std::nullopt};
std::optional<std::string> interface{std::nullopt};
std::optional<std::string> pid_file{std::nullopt};
std::optional<uint8_t> op_exponent;
};
static inline auto SetupCommonArguments(
gsl::not_null<CLI::App*> const& app,
gsl::not_null<CommonArguments*> const& clargs) {
app->add_option("-C,--repository-config",
clargs->repository_config,
"Path to configuration file for multi-repository builds.")
->type_name("PATH");
app->add_option(
"--main", clargs->main, "The repository to take the target from.")
->type_name("NAME");
app->add_option_function<std::string>(
"-w,--workspace-root",
[clargs](auto const& workspace_root_raw) {
clargs->workspace_root = std::filesystem::canonical(
std::filesystem::absolute(workspace_root_raw));
},
"Path of the workspace's root directory.")
->type_name("PATH");
app->add_option("-j,--jobs",
clargs->jobs,
"Number of jobs to run (Default: Number of cores).")
->type_name("NUM");
}
static inline auto SetupLogArguments(
gsl::not_null<CLI::App*> const& app,
gsl::not_null<LogArguments*> const& clargs) {
app->add_option_function<std::string>(
"-f,--log-file",
[clargs](auto const& log_file_) {
clargs->log_files.emplace_back(log_file_);
},
"Path to local log file.")
->type_name("PATH")
->trigger_on_parse(); // run callback on all instances while parsing,
// not after all parsing is done
app->add_option_function<std::underlying_type_t<LogLevel>>(
"--log-limit",
[clargs](auto const& limit) {
clargs->log_limit = ToLogLevel(limit);
},
fmt::format("Log limit (higher is more verbose) in interval [{},{}] "
"(Default: {}).",
static_cast<int>(kFirstLogLevel),
static_cast<int>(kLastLogLevel),
static_cast<int>(kDefaultLogLevel)))
->type_name("NUM");
app->add_flag("--plain-log",
clargs->plain_log,
"Do not use ANSI escape sequences to highlight messages.");
app->add_flag(
"--log-append",
clargs->log_append,
"Append messages to log file instead of overwriting existing.");
}
static inline auto SetupAnalysisArguments(
gsl::not_null<CLI::App*> const& app,
gsl::not_null<AnalysisArguments*> const& clargs,
bool with_graph = true) {
app->add_option("--expression-log-limit",
clargs->expression_log_limit,
fmt::format("Maximal size for logging a single expression "
"in error messages (Default {})",
Evaluator::kDefaultExpressionLogLimit))
->type_name("NUM");
app->add_option_function<std::string>(
"-D,--defines",
[clargs](auto const& d) { clargs->defines.emplace_back(d); },
"Define an overlay configuration via an in-line JSON object."
" Multiple options overlay.")
->type_name("JSON")
->trigger_on_parse(); // run callback on all instances while parsing,
// not after all parsing is done
app->add_option(
"-c,--config", clargs->config_file, "Path to configuration file.")
->type_name("PATH");
app->add_option(
"--request-action-input",
clargs->request_action_input,
"Instead of the target result, request input for this action.")
->type_name("ACTION");
app->add_option_function<std::vector<std::string>>(
"target",
[clargs](auto const& target_raw) {
if (target_raw.size() == 1) {
clargs->target = nlohmann::json(target_raw[0]);
}
else {
clargs->target = nlohmann::json(target_raw);
}
},
"Module and target name to build.\n"
"Assumes current module if module name is omitted.");
app->add_option("--target-root",
clargs->target_root,
"Path of the target files' root directory.\n"
"Default: Same as --workspace-root")
->type_name("PATH");
app->add_option("--rule-root",
clargs->rule_root,
"Path of the rule files' root directory.\n"
"Default: Same as --target-root")
->type_name("PATH");
app->add_option("--expression-root",
clargs->expression_root,
"Path of the expression files' root directory.\n"
"Default: Same as --rule-root")
->type_name("PATH");
app->add_option("--target-file-name",
clargs->target_file_name,
"Name of the targets file.");
app->add_option(
"--rule-file-name", clargs->rule_file_name, "Name of the rules file.");
app->add_option("--expression-file-name",
clargs->expression_file_name,
"Name of the expressions file.");
if (with_graph) {
app->add_option(
"--dump-graph",
clargs->graph_file,
"File path for writing the action graph description to.")
->type_name("PATH");
app->add_option("--dump-artifacts-to-build",
clargs->artifacts_to_build_file,
"File path for writing the artifacts to build to.")
->type_name("PATH");
}
}
static inline auto SetupDescribeArguments(
gsl::not_null<CLI::App*> const& app,
gsl::not_null<DescribeArguments*> const& clargs) {
app->add_flag("--json", clargs->print_json, "Print description as JSON.");
app->add_flag("--rule",
clargs->describe_rule,
"Positional arguments refer to rule instead of target.");
}
static inline auto SetupDiagnosticArguments(
gsl::not_null<CLI::App*> const& app,
gsl::not_null<DiagnosticArguments*> const& clargs) {
app->add_option("--dump-actions",
clargs->dump_actions,
"Dump actions to file (use - for stdout).")
->type_name("PATH");
app->add_option("--dump-trees",
clargs->dump_trees,
"Dump trees to file (use - for stdout).")
->type_name("PATH");
app->add_option("--dump-blobs",
clargs->dump_blobs,
"Dump blobs to file (use - for stdout).")
->type_name("PATH");
app->add_option("--dump-vars",
clargs->dump_vars,
"Dump domain of the effective configuration to file (use - "
"for stdout).")
->type_name("PATH");
app->add_option("--dump-targets",
clargs->dump_targets,
"Dump targets to file (use - for stdout).")
->type_name("PATH");
app->add_option("--dump-export-targets",
clargs->dump_export_targets,
"Dump \"export\" targets to file (use - for stdout).")
->type_name("PATH");
app->add_option("--dump-targets-graph",
clargs->dump_targets_graph,
"Dump the graph of the configured targets to file.")
->type_name("PATH");
app->add_option("--dump-anonymous",
clargs->dump_anonymous,
"Dump anonymous targets to file (use - for stdout).")
->type_name("PATH");
app->add_option("--dump-nodes",
clargs->dump_nodes,
"Dump nodes of target to file (use - for stdout).")
->type_name("PATH");
}
static inline auto SetupCacheArguments(
gsl::not_null<CLI::App*> const& app,
gsl::not_null<EndpointArguments*> const& clargs) {
app->add_option_function<std::string>(
"--local-build-root",
[clargs](auto const& build_root_raw) {
clargs->local_root = std::filesystem::weakly_canonical(
std::filesystem::absolute(build_root_raw));
},
"Root for local CAS, cache, and build directories.")
->type_name("PATH");
}
static inline auto SetupEndpointArguments(
gsl::not_null<CLI::App*> const& app,
gsl::not_null<EndpointArguments*> const& clargs) {
app->add_option("-r,--remote-execution-address",
clargs->remote_execution_address,
"Address of the remote execution service.")
->type_name("NAME:PORT");
app->add_option(
"--remote-execution-property",
clargs->platform_properties,
"Property for remote execution as key-value pair. Specifying this "
"option multiple times will accumulate pairs (latest wins).")
->type_name("KEY:VAL")
->allow_extra_args(false)
->expected(1, 1);
}
static inline auto SetupCommonBuildArguments(
gsl::not_null<CLI::App*> const& app,
gsl::not_null<BuildArguments*> const& clargs) {
app->add_option_function<std::string>(
"-L,--local-launcher",
[clargs](auto const& launcher_raw) {
clargs->local_launcher =
nlohmann::json::parse(launcher_raw)
.template get<std::vector<std::string>>();
},
"JSON array with the list of strings representing the launcher to "
"prepend actions' commands before being executed locally.")
->type_name("JSON")
->default_val(nlohmann::json(kDefaultLauncher).dump());
}
static inline auto SetupBuildArguments(
gsl::not_null<CLI::App*> const& app,
gsl::not_null<BuildArguments*> const& clargs) {
app->add_option_function<unsigned int>(
"--action-timeout",
[clargs](auto const& seconds) {
clargs->timeout = seconds * std::chrono::seconds{1};
},
"Action timeout in seconds. (Default: 300).")
->type_name("NUM");
app->add_option(
"-J,--build-jobs",
clargs->build_jobs,
"Number of jobs to run during build phase (Default: same as jobs).")
->type_name("NUM");
app->add_option("--dump-artifacts",
clargs->dump_artifacts,
"Dump artifacts to file (use - for stdout).")
->type_name("PATH");
app->add_flag("-s,--show-runfiles",
clargs->show_runfiles,
"Do not omit runfiles in build report.");
app->add_option("-P,--print-to-stdout",
clargs->print_to_stdout,
"After building, print the specified artifact to stdout.")
->type_name("LOGICAL_PATH");
}
static inline auto SetupStageArguments(
gsl::not_null<CLI::App*> const& app,
gsl::not_null<StageArguments*> const& clargs) {
app->add_option_function<std::string>(
"-o,--output-dir",
[clargs](auto const& output_dir_raw) {
clargs->output_dir = std::filesystem::weakly_canonical(
std::filesystem::absolute(output_dir_raw));
},
"Path of the directory where outputs will be copied.")
->type_name("PATH")
->required();
app->add_flag(
"--remember", clargs->remember, "Copy object to local CAS first");
}
static inline auto SetupRebuildArguments(
gsl::not_null<CLI::App*> const& app,
gsl::not_null<RebuildArguments*> const& clargs) {
app->add_option_function<std::string>(
"--vs",
[clargs](auto const& cache_endpoint) {
clargs->cache_endpoint = cache_endpoint;
},
"Cache endpoint to compare against (use \"local\" for local cache).")
->type_name("NAME:PORT|\"local\"");
app->add_option(
"--dump-flaky", clargs->dump_flaky, "Dump flaky actions to file.")
->type_name("PATH");
}
static inline auto SetupFetchArguments(
gsl::not_null<CLI::App*> const& app,
gsl::not_null<FetchArguments*> const& clargs) {
app->add_option(
"object_id",
clargs->object_id,
"Object identifier with the format '[<hash>:<size>:<type>]'.")
->required();
app->add_option_function<std::string>(
"-o,--output-path",
[clargs](auto const& output_path_raw) {
clargs->output_path = std::filesystem::weakly_canonical(
std::filesystem::absolute(output_path_raw));
},
"Install path for the artifact. (omit to dump to stdout)")
->type_name("PATH");
app->add_flag("--raw-tree",
clargs->raw_tree,
"Dump raw tree object (omit pretty printing)");
app->add_flag(
"--remember", clargs->remember, "Copy object to local CAS first");
}
static inline auto SetupGraphArguments(
gsl::not_null<CLI::App*> const& app,
gsl::not_null<GraphArguments*> const& clargs) {
app->add_option_function<std::string>(
"-a,--artifacts",
[clargs](auto const& artifact_map_raw) {
clargs->artifacts = nlohmann::json::parse(artifact_map_raw);
},
"Json object with key/value pairs formed by the relative path in which "
"artifact is to be copied and the description of the artifact as json "
"object as well.");
app->add_option("-g,--graph-file",
clargs->graph_file,
"Path of the file containing the description of the "
"actions.")
->required();
app->add_option("--git-cas",
clargs->git_cas,
"Path to a Git repository, containing blobs of potentially "
"missing KNOWN artifacts.");
}
static inline auto SetupCompatibilityArguments(
gsl::not_null<CLI::App*> const& app) {
app->add_flag_function(
"--compatible",
[](auto /*unused*/) { Compatibility::SetCompatible(); },
"At increased computational effort, be compatible with the original "
"remote build execution protocol. As the change affects identifiers, "
"the flag must be used consistently for all related invocations.");
}
static inline auto SetupCommonAuthArguments(
gsl::not_null<CLI::App*> const& app,
gsl::not_null<CommonAuthArguments*> const& authargs) {
app->add_option("--tls-ca-cert",
authargs->tls_ca_cert,
"Path to a TLS CA certificate that is trusted to sign the "
"server certificate.");
}
static inline auto SetupClientAuthArguments(
gsl::not_null<CLI::App*> const& app,
gsl::not_null<ClientAuthArguments*> const& authargs) {
app->add_option("--tls-client-cert",
authargs->tls_client_cert,
"Path to the TLS client certificate.");
app->add_option("--tls-client-key",
authargs->tls_client_key,
"Path to the TLS client key.");
}
static inline auto SetupServerAuthArguments(
gsl::not_null<CLI::App*> const& app,
gsl::not_null<ServerAuthArguments*> const& authargs) {
app->add_option("--tls-server-cert",
authargs->tls_server_cert,
"Path to the TLS server certificate.");
app->add_option("--tls-server-key",
authargs->tls_server_key,
"Path to the TLS server key.");
}
static inline auto SetupExecutionServiceArguments(
gsl::not_null<CLI::App*> const& app,
gsl::not_null<ExecutionServiceArguments*> const& es_args) {
app->add_option("-p,--port",
es_args->port,
"Execution service will listen to this port. If unset, the "
"service will listen to the first available one.");
app->add_option("--info-file",
es_args->info_file,
"Write the used port, interface, and pid to this file in "
"JSON format. If the file exists, it "
"will be overwritten.");
app->add_option("-i,--interface",
es_args->interface,
"Interface to use. If unset, the loopback device is used.");
app->add_option(
"--pid-file",
es_args->pid_file,
"Write pid to this file in plain txt. If the file exists, it "
"will be overwritten.");
app->add_option(
"--log-operations-threshold",
es_args->op_exponent,
"Once the number of operations stored exceeds twice 2^n, where n is "
"given by the option --log-operations-threshold, at most 2^n "
"operations will be removed, in a FIFO scheme. If unset, defaults to "
"14. Must be in the range [0,255]");
}
#endif // INCLUDED_SRC_BUILDTOOL_COMMON_CLI_HPP
|