{{ message }}
forked from asheplyakov/buildcache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
532 lines (482 loc) · 20.8 KB
/
Copy pathmain.cpp
File metadata and controls
532 lines (482 loc) · 20.8 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
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
//--------------------------------------------------------------------------------------------------
// Copyright (c) 2018 Marcus Geelnard
//
// This software is provided 'as-is', without any express or implied warranty. In no event will the
// authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose, including commercial
// applications, and to alter it and redistribute it freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not claim that you wrote
// the original software. If you use this software in a product, an acknowledgment in the
// product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be misrepresented as
// being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//--------------------------------------------------------------------------------------------------
#include <base/debug_utils.hpp>
#include <base/string_list.hpp>
#include <base/unicode_utils.hpp>
#include <cache/local_cache.hpp>
#include <config/configuration.hpp>
#include <sys/perf_utils.hpp>
#include <sys/sys_utils.hpp>
#include <wrappers/ccc_analyzer_wrapper.hpp>
#include <wrappers/clang_cl_wrapper.hpp>
#include <wrappers/gcc_wrapper.hpp>
#include <wrappers/ghs_wrapper.hpp>
#include <wrappers/lua_wrapper.hpp>
#include <wrappers/msvc_wrapper.hpp>
#include <wrappers/program_wrapper.hpp>
#include <wrappers/qcc_wrapper.hpp>
#include <wrappers/ti_arm_cgt_wrapper.hpp>
#include <wrappers/ti_arp32_wrapper.hpp>
#include <wrappers/ti_c6x_wrapper.hpp>
// These 3rd party includes are used for getting the version numbers.
#include "buildcache_version.h"
#include <cjson/cJSON.h>
#include <hiredis/hiredis.h>
#include <lua.h>
#include <lz4/lz4.h>
#include <xxHash/xxhash.h>
#include <zstd/zstd.h>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <string>
// For program binary identification (e.g. using the "strings", "ident" or "what" tools).
char PROGRAM_IDENTITY_1[] = "@(#)BuildCache version " BUILDCACHE_VERSION_STRING;
char PROGRAM_IDENTITY_2[] = "$BuildCacheVersion: " BUILDCACHE_VERSION_STRING " $";
namespace {
// The name of the BuildCache executable (excluding the file extension).
const std::string BUILDCACHE_EXE_NAME = "buildcache";
bool is_lua_script(const std::string& script_path) {
return (bcache::lower_case(bcache::file::get_extension(script_path)) == ".lua");
}
std::unique_ptr<bcache::program_wrapper_t> find_suitable_wrapper(
const bcache::file::exe_path_t& exe_path,
const bcache::string_list_t& args) {
std::unique_ptr<bcache::program_wrapper_t> wrapper;
// Try Lua wrappers first (so you can override internal wrappers).
// Iterate over the existing Lua paths.
for (const auto& lua_root_dir : bcache::config::lua_paths()) {
if (bcache::file::dir_exists(lua_root_dir)) {
// Find all .lua files in the given directory.
const auto lua_files = bcache::file::walk_directory(lua_root_dir);
for (const auto& file_info : lua_files) {
const auto& script_path = file_info.path();
if ((!file_info.is_dir()) && is_lua_script(script_path)) {
// Check if the given wrapper can handle this command (first match wins).
wrapper.reset(new bcache::lua_wrapper_t(exe_path, args, script_path));
if (wrapper->can_handle_command()) {
bcache::debug::log(bcache::debug::DEBUG)
<< "Found matching Lua wrapper for " << exe_path.virtual_path() << ": "
<< script_path;
break;
}
wrapper = nullptr;
}
}
}
if (wrapper) {
break;
}
}
// If no Lua wrappers were found, try built in wrappers.
if (!wrapper) {
wrapper.reset(new bcache::clang_cl_wrapper_t(exe_path, args));
if (!wrapper->can_handle_command()) {
wrapper.reset(new bcache::gcc_wrapper_t(exe_path, args));
if (!wrapper->can_handle_command()) {
wrapper.reset(new bcache::qcc_wrapper_t(exe_path, args));
if (!wrapper->can_handle_command()) {
wrapper.reset(new bcache::ghs_wrapper_t(exe_path, args));
if (!wrapper->can_handle_command()) {
wrapper.reset(new bcache::msvc_wrapper_t(exe_path, args));
if (!wrapper->can_handle_command()) {
wrapper.reset(new bcache::ti_c6x_wrapper_t(exe_path, args));
if (!wrapper->can_handle_command()) {
wrapper.reset(new bcache::ti_arm_cgt_wrapper_t(exe_path, args));
if (!wrapper->can_handle_command()) {
wrapper.reset(new bcache::ti_arp32_wrapper_t(exe_path, args));
if (!wrapper->can_handle_command()) {
wrapper.reset(new bcache::ccc_analyzer_wrapper_t(exe_path, args));
if (!wrapper->can_handle_command()) {
wrapper = nullptr;
}
}
}
}
}
}
}
}
}
}
return wrapper;
}
[[noreturn]] void clear_cache_and_exit() {
int return_code = 0;
try {
bcache::local_cache_t cache;
cache.clear();
} catch (const std::exception& e) {
std::cerr << "*** Unexpected error: " << e.what() << "\n";
return_code = 1;
} catch (...) {
std::cerr << "*** Unexpected error.\n";
return_code = 1;
}
std::exit(return_code);
}
[[noreturn]] void perform_housekeeping_and_exit() {
int return_code = 0;
try {
bcache::local_cache_t cache;
cache.perform_housekeeping();
} catch (const std::exception& e) {
std::cerr << "*** Unexpected error: " << e.what() << "\n";
return_code = 1;
} catch (...) {
std::cerr << "*** Unexpected error.\n";
return_code = 1;
}
std::exit(return_code);
}
[[noreturn]] void show_stats_and_exit() {
int return_code = 0;
try {
bcache::local_cache_t cache;
// Print the cache stats.
std::cout << "Cache status:\n";
cache.show_stats();
} catch (const std::exception& e) {
std::cerr << "*** Unexpected error: " << e.what() << "\n";
return_code = 1;
} catch (...) {
std::cerr << "*** Unexpected error.\n";
return_code = 1;
}
std::exit(return_code);
}
[[noreturn]] void show_config_and_exit() {
int return_code = 0;
try {
#ifdef _WIN32
const std::string PATH_DELIMITER = ";";
#else
const std::string PATH_DELIMITER = ":";
#endif
std::cout << "Configuration file: " << bcache::config::config_file() << "\n\n";
std::cout << " BUILDCACHE_ACCURACY: " << to_string(bcache::config::accuracy())
<< "\n";
std::cout << " BUILDCACHE_CACHE_LINK_COMMANDS: "
<< (bcache::config::cache_link_commands() ? "true" : "false") << "\n";
std::cout << " BUILDCACHE_COMPRESS: "
<< (bcache::config::compress() ? "true" : "false") << "\n";
std::cout << " BUILDCACHE_COMPRESS_FORMAT: "
<< to_string(bcache::config::compress_format()) << "\n";
std::cout << " BUILDCACHE_COMPRESS_LEVEL: " << bcache::config::compress_level()
<< "\n";
std::cout << " BUILDCACHE_DEBUG: " << bcache::config::debug() << "\n";
std::cout << " BUILDCACHE_DIR: " << bcache::config::dir() << "\n";
std::cout << " BUILDCACHE_DIRECT_MODE: "
<< (bcache::config::direct_mode() ? "true" : "false") << "\n";
std::cout << " BUILDCACHE_DISABLE: "
<< (bcache::config::disable() ? "true" : "false") << "\n";
std::cout << " BUILDCACHE_HARD_LINKS: "
<< (bcache::config::hard_links() ? "true" : "false") << "\n";
std::cout << " BUILDCACHE_HASH_EXTRA_FILES: "
<< bcache::config::hash_extra_files().join(PATH_DELIMITER, false) << "\n";
std::cout << " BUILDCACHE_IMPERSONATE: " << bcache::config::impersonate() << "\n";
std::cout << " BUILDCACHE_LOG_FILE: " << bcache::config::log_file() << "\n";
std::cout << " BUILDCACHE_LUA_PATH: "
<< bcache::config::lua_paths().join(PATH_DELIMITER, false) << "\n";
std::cout << " BUILDCACHE_MAX_CACHE_SIZE: " << bcache::config::max_cache_size() << " ("
<< bcache::file::human_readable_size(bcache::config::max_cache_size()) << ")\n";
std::cout << " BUILDCACHE_MAX_LOCAL_ENTRY_SIZE: " << bcache::config::max_local_entry_size()
<< " (" << bcache::file::human_readable_size(bcache::config::max_local_entry_size())
<< ")\n";
std::cout << " BUILDCACHE_MAX_REMOTE_ENTRY_SIZE: " << bcache::config::max_remote_entry_size()
<< " (" << bcache::file::human_readable_size(bcache::config::max_remote_entry_size())
<< ")\n";
std::cout << " BUILDCACHE_PERF: "
<< (bcache::config::perf() ? "true" : "false") << "\n";
std::cout << " BUILDCACHE_PREFIX: " << bcache::config::prefix() << "\n";
std::cout << " BUILDCACHE_READ_ONLY: "
<< (bcache::config::read_only() ? "true" : "false") << "\n";
std::cout << " BUILDCACHE_READ_ONLY_REMOTE: "
<< (bcache::config::read_only_remote() ? "true" : "false") << "\n";
std::cout << " BUILDCACHE_REDIS_USERNAME: " << bcache::config::redis_username()
<< "\n";
std::cout << " BUILDCACHE_REDIS_PASSWORD: "
<< (bcache::config::redis_password().empty() ? "" : "*******") << "\n";
std::cout << " BUILDCACHE_REMOTE: " << bcache::config::remote() << "\n";
std::cout << " BUILDCACHE_REMOTE_LOCKS: "
<< (bcache::config::remote_locks() ? "true" : "false") << "\n";
std::cout << " BUILDCACHE_S3_ACCESS: "
<< (bcache::config::s3_access().empty() ? "" : "*******") << "\n";
std::cout << " BUILDCACHE_S3_SECRET: "
<< (bcache::config::s3_secret().empty() ? "" : "*******") << "\n";
std::cout << " BUILDCACHE_TERMINATE_ON_MISS: "
<< (bcache::config::terminate_on_miss() ? "true" : "false") << "\n";
} catch (const std::exception& e) {
std::cerr << "*** Unexpected error: " << e.what() << "\n";
return_code = 1;
} catch (...) {
std::cerr << "*** Unexpected error.\n";
return_code = 1;
}
std::exit(return_code);
}
[[noreturn]] void zero_stats_and_exit() {
int return_code = 0;
try {
bcache::local_cache_t cache;
cache.zero_stats();
} catch (const std::exception& e) {
std::cerr << "*** Unexpected error: " << e.what() << "\n";
return_code = 1;
} catch (...) {
std::cerr << "*** Unexpected error.\n";
return_code = 1;
}
std::exit(return_code);
}
[[noreturn]] void print_version_and_exit() {
// Print the BuildCache version.
std::cout << "BuildCache version " << BUILDCACHE_VERSION_STRING << "\n"
<< BUILDCACHE_COPYRIGHT_STRING << "\n";
// Print the supported cache back ends.
std::cout << "\nSupported back ends:\n";
std::cout << " local - Local file system based cache (level 1)\n";
std::cout << " Redis - Remote in-memory cache (level 2)\n";
std::cout << " HTTP - Remote webdav cache (level 2)\n";
std::cout << " S3 - Remote object storage based cache (level 2)\n";
// Print a list of third party components.
std::cout << "\nThird party components:\n";
std::cout << " cJSON " << CJSON_VERSION_MAJOR << "." << CJSON_VERSION_MINOR << "."
<< CJSON_VERSION_PATCH << "\n";
std::cout << " cpp-base64 2.rc.04\n";
std::cout << " hiredis " << HIREDIS_MAJOR << "." << HIREDIS_MINOR << "." << HIREDIS_PATCH;
#ifdef ENABLE_SSL
std::cout << " (with SSL/TLS support)";
#endif
std::cout << "\n";
std::cout << " HTTPRequest\n";
std::cout << " lua " << LUA_VERSION_MAJOR << "." << LUA_VERSION_MINOR << "."
<< LUA_VERSION_RELEASE << "\n";
std::cout << " lz4 " << LZ4_VERSION_STRING << "\n";
#ifdef USE_MINGW_THREADS
std::cout << " mingw-std-threads\n";
#endif
std::cout << " xxhash " << XXH_VERSION_MAJOR << "." << XXH_VERSION_MINOR << "."
<< XXH_VERSION_RELEASE << "\n";
std::cout << " zstd " << ZSTD_VERSION_STRING << "\n";
std::exit(0);
}
[[noreturn]] void edit_config_and_exit() {
int return_code = 0;
try {
// This will ensure that the local cache directory exists.
bcache::local_cache_t cache;
// Get the name of the config file, and create an empty file if it does not already exist.
const auto& config_file = bcache::config::config_file();
if (!bcache::file::file_exists(config_file)) {
bcache::file::write("{\n}\n", config_file);
}
// Open the editor.
bcache::sys::open_in_default_editor(config_file);
} catch (const std::exception& e) {
std::cerr << "*** Unexpected error: " << e.what() << "\n";
return_code = 1;
} catch (...) {
std::cerr << "*** Unexpected error.\n";
return_code = 1;
}
std::exit(return_code);
}
[[noreturn]] void wrap_compiler_and_exit(int argc, const char** argv) {
auto args = bcache::string_list_t(argc, argv);
bool was_wrapped = false;
int return_code = 0;
PERF_START(TOTAL);
try {
if (args.size() < 1) {
// Should never happen.
throw std::runtime_error("Missing arguments.");
}
// Find the true path to the executable file. This affects things like if we can match the
// compiler name or not, and what version string we get. We also want to avoid incorrectly
// identifying other compiler accelerators (e.g. ccache) as actual compilers.
// TODO(m): This call may throw an exception, which currently means that we will not even try
// to run the original command. At the same time this is a protection against endless symlink
// recursion. Figure something out!
PERF_START(FIND_EXECUTABLE);
const auto exe_path = bcache::file::find_executable(args[0], BUILDCACHE_EXE_NAME);
PERF_STOP(FIND_EXECUTABLE);
// Replace the command with the virtual exe path (i.e. the path to the program that will be
// executed upon a cache miss). Most of the following operations rely on having a correct
// executable path. Also, this is important to avoid recursions when we are invoked from a
// symlink, for instance.
args[0] = exe_path.virtual_path();
// Is the caching mechanism disabled?
if (bcache::config::disable()) {
// Bypass all the cache logic and call the intended command directly.
auto result = bcache::sys::run(args, false);
return_code = result.return_code;
} else {
try {
return_code = 1;
// Select a matching compiler wrapper.
PERF_START(FIND_WRAPPER);
auto wrapper = find_suitable_wrapper(exe_path, args);
PERF_STOP(FIND_WRAPPER);
// Run the wrapper, if any.
if (wrapper) {
was_wrapped = wrapper->handle_command(return_code);
} else {
bcache::debug::log(bcache::debug::INFO)
<< "No suitable wrapper for " << exe_path.virtual_path();
}
} catch (const std::exception& e) {
bcache::debug::log(bcache::debug::ERROR) << "Unexpected error: " << e.what();
return_code = 1;
} catch (...) {
bcache::debug::log(bcache::debug::ERROR) << "Unexpected error.";
return_code = 1;
}
// Fall back to running the command as is.
if (!was_wrapped) {
PERF_START(RUN_FOR_FALLBACK);
auto result = bcache::sys::run_with_prefix(args, false);
PERF_STOP(RUN_FOR_FALLBACK);
return_code = result.return_code;
}
}
} catch (const std::exception& e) {
bcache::debug::log(bcache::debug::FATAL) << "Unexpected error: " << e.what();
return_code = 1;
} catch (...) {
bcache::debug::log(bcache::debug::FATAL) << "Unexpected error.";
return_code = 1;
}
PERF_STOP(TOTAL);
// Report performance timings.
if (!bcache::config::disable()) {
bcache::perf::report();
}
std::exit(return_code);
}
void init_config(const char* bcache_dir = nullptr) noexcept {
try {
// Initialize the configuration.
bcache::config::init(bcache_dir);
// Set up debug logging acccording to what is given by the configuration.
bcache::debug::set_log_level(bcache::config::debug());
bcache::debug::set_log_file(bcache::config::log_file());
} catch (const std::exception& e) {
bcache::debug::log(bcache::debug::FATAL) << e.what();
} catch (...) {
bcache::debug::log(bcache::debug::FATAL) << "Failed to initialize the configuration.";
}
}
bool compare_arg(const std::string& arg,
const std::string& short_form,
const std::string& long_form = "") {
return (arg == short_form) || (arg == long_form);
}
void print_help(const char* program_name) {
std::cout << "Usage:\n";
std::cout << " " << program_name << " [options] <command>\n";
std::cout << " " << program_name << " [options] <compiler> [compiler-options]\n";
std::cout << " BUILDCACHE_IMPERSONATE=<compiler> " << program_name << " [compiler-options]\n";
std::cout << " <symlink> [compiler-options]\n";
std::cout << "\n";
std::cout << "<compiler> the program to run (e.g. a C++ compiler)\n";
std::cout << "<symlink> a symlink to the BuildCache executable, and the name of\n";
std::cout << " the symlink is the program to run (e.g. \"g++\")\n";
std::cout << "\n";
std::cout << "Options:\n";
std::cout << " -d, --directory PATH operate on cache directory PATH instead\n";
std::cout << " of the default\n";
std::cout << "\n";
std::cout << "Commands:\n";
std::cout << " -C, --clear clear the local cache (except configuration)\n";
std::cout << " -s, --show-stats show statistics summary\n";
std::cout << " -c, --show-config show current configuration\n";
std::cout << " -z, --zero-stats zero statistics counters\n";
std::cout << " -H, --housekeeping perform housekeeping duties\n";
std::cout << " -e, --edit-config edit the configuration file\n";
std::cout << "\n";
std::cout << " -h, --help print this help text\n";
std::cout << " -V, --version print version and copyright information\n";
std::cout << "\n";
std::cout << "See also https://github.com/mbitsnbites/buildcache\n";
}
} // namespace
int main(int argc, const char** argv) {
// Initialize the configuration.
// NOTE: This must be the first thing that we do in order to have well defined config values
// (which are used throughout the code, e.g. for controlling log levels). We may override the
// configuration later if the -d option is used.
init_config();
// Handle BUILDCACHE_IMPERSONATE invocation.
const auto& impersonate = bcache::config::impersonate();
if (!impersonate.empty()) {
bcache::debug::log(bcache::debug::DEBUG) << "Impersonating: " << impersonate;
argv[0] = impersonate.c_str();
wrap_compiler_and_exit(argc, &argv[0]);
}
// Handle symlink invocation.
if (bcache::lower_case(bcache::file::get_file_part(std::string(argv[0]), false)) !=
BUILDCACHE_EXE_NAME) {
bcache::debug::log(bcache::debug::DEBUG) << "Invoked as symlink: " << argv[0];
wrap_compiler_and_exit(argc, &argv[0]);
}
// Parse BuildCache options (must be given before any command).
int arg_pos = 1;
if ((arg_pos < argc) && compare_arg(argv[arg_pos], "-d", "--directory")) {
if ((arg_pos + 1) >= argc || argv[arg_pos + 1][0] == '-') {
std::cerr << argv[0] << ": missing PATH for " << argv[arg_pos] << "\n";
print_help(argv[0]);
std::exit(1);
}
const auto* bcache_dir = argv[arg_pos + 1];
arg_pos += 2;
// Re-initialize with the cache dir specified in the command line.
init_config(bcache_dir);
}
if ((argc - arg_pos) < 1) {
print_help(argv[0]);
std::exit(1);
}
// Check if we are running any BuildCache commands.
const std::string arg_str(argv[arg_pos]);
if (compare_arg(arg_str, "-C", "--clear")) {
clear_cache_and_exit();
} else if (compare_arg(arg_str, "-s", "--show-stats")) {
show_stats_and_exit();
} else if (compare_arg(arg_str, "-c", "--show-config")) {
show_config_and_exit();
} else if (compare_arg(arg_str, "-z", "--zero-stats")) {
zero_stats_and_exit();
} else if (compare_arg(arg_str, "-H", "--housekeeping")) {
perform_housekeeping_and_exit();
} else if (compare_arg(arg_str, "-V", "--version")) {
print_version_and_exit();
} else if (compare_arg(arg_str, "-e", "--edit-config")) {
edit_config_and_exit();
} else if (compare_arg(arg_str, "-h", "--help")) {
print_help(argv[0]);
std::exit(0);
} else if (arg_str[0] == '-') {
std::cerr << argv[0] << ": invalid command -- " << arg_str << "\n";
print_help(argv[0]);
std::exit(1);
}
// We got this far, so we're running as a compiler wrapper.
wrap_compiler_and_exit(argc - arg_pos, &argv[arg_pos]);
}
You can’t perform that action at this time.
