From: Thomas Otto Date: Sat, 25 Jan 2020 09:38:06 +0000 (+0100) Subject: Context: move orig_args X-Git-Tag: v4.0~619^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F542%2Fhead;p=thirdparty%2Fccache.git Context: move orig_args --- diff --git a/src/Context.cpp b/src/Context.cpp index 27ab9c146..35380752d 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -18,6 +18,11 @@ #include "Context.hpp" +#include "args.hpp" + Context::~Context() { + if (free_orig_args) { + args_free(orig_args); + } } diff --git a/src/Context.hpp b/src/Context.hpp index 19821227b..afe8b4d56 100644 --- a/src/Context.hpp +++ b/src/Context.hpp @@ -24,6 +24,8 @@ #include "Config.hpp" #include "NonCopyable.hpp" +struct args; + struct Context : NonCopyable { Context() = default; @@ -35,4 +37,10 @@ struct Context : NonCopyable // Full path to the statistics file in the subdirectory where the cached // result belongs (//stats). std::string stats_file; + + // The original argument list. + struct args* orig_args = nullptr; + + // Whether to free orig_args in the destructor. + bool free_orig_args = true; }; diff --git a/src/ccache.cpp b/src/ccache.cpp index 12829cbc7..5bec417e8 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -1208,8 +1208,8 @@ to_cache(Context& ctx, // Use the original arguments (including dependency options) in depend // mode. - assert(orig_args); - struct args* depend_mode_args = args_copy(orig_args); + assert(ctx.orig_args); + struct args* depend_mode_args = args_copy(ctx.orig_args); args_strip(depend_mode_args, "--ccache-"); if (depend_extra_args) { args_extend(depend_mode_args, depend_extra_args); @@ -1528,7 +1528,7 @@ hash_compiler(Context& ctx, hash_file(hash, path); } else { // command string bool ok = hash_multicommand_output( - ctx, hash, ctx.config.compiler_check().c_str(), orig_args->argv[0]); + ctx, hash, ctx.config.compiler_check().c_str(), ctx.orig_args->argv[0]); if (!ok) { fatal("Failure running compiler check command: %s", ctx.config.compiler_check().c_str()); @@ -2148,12 +2148,12 @@ find_compiler(Context& ctx, char** argv) // We might be being invoked like "ccache gcc -c foo.c". std::string base(Util::base_name(argv[0])); if (same_executable_name(base.c_str(), MYNAME)) { - args_remove_first(orig_args); - if (is_full_path(orig_args->argv[0])) { + args_remove_first(ctx.orig_args); + if (is_full_path(ctx.orig_args->argv[0])) { // A full path was given. return; } - base = std::string(Util::base_name(orig_args->argv[0])); + base = std::string(Util::base_name(ctx.orig_args->argv[0])); } // Support user override of the compiler. @@ -2170,7 +2170,7 @@ find_compiler(Context& ctx, char** argv) fatal("Recursive invocation (the name of the ccache binary must be \"%s\")", MYNAME); } - orig_args->argv[0] = compiler; + ctx.orig_args->argv[0] = compiler; } bool @@ -3499,7 +3499,7 @@ set_up_config(Config& config) // Initialize ccache, must be called once before anything else is run. static Context& -initialize() +initialize(int argc, char* argv[]) { // This object is placed onto the heap so it is available in exit functions // which run after main(). It is cleaned up by the last exit function. @@ -3509,6 +3509,8 @@ initialize() init_log(ctx->config); + ctx->orig_args = args_init(argc, argv); + exitfn_init(); exitfn_delete_context(ctx); exitfn_add(stats_flush, ctx); @@ -3553,8 +3555,6 @@ cc_reset(Config& config) free_and_nullify(current_working_dir); free_and_nullify(included_pch_file); - args_free(orig_args); - orig_args = NULL; free_and_nullify(cached_result_name); free_and_nullify(cached_result_path); free_and_nullify(manifest_path); @@ -3623,9 +3623,7 @@ cache_compilation(int argc, char* argv[]) // Needed for portability when using localtime_r. tzset(); - orig_args = args_init(argc, argv); - - Context& ctx = initialize(); + Context& ctx = initialize(argc, argv); MTR_BEGIN("main", "find_compiler"); find_compiler(ctx, argv); @@ -3638,16 +3636,18 @@ cache_compilation(int argc, char* argv[]) stats_update(e.stat()); } - assert(orig_args); + assert(ctx.orig_args); - args_strip(orig_args, "--ccache-"); - add_prefix(ctx, orig_args, ctx.config.prefix_command().c_str()); + args_strip(ctx.orig_args, "--ccache-"); + add_prefix(ctx, ctx.orig_args, ctx.config.prefix_command().c_str()); cc_log("Failed; falling back to running the real compiler"); - cc_log_argv("Executing ", orig_args->argv); + cc_log_argv("Executing ", ctx.orig_args->argv); + ctx.free_orig_args = false; + struct args* orig_args_for_execv = ctx.orig_args; exitfn_call(); - execv(orig_args->argv[0], orig_args->argv); - fatal("execv of %s failed: %s", orig_args->argv[0], strerror(errno)); + execv(orig_args_for_execv->argv[0], orig_args_for_execv->argv); + fatal("execv of %s failed: %s", orig_args_for_execv->argv[0], strerror(errno)); } } @@ -3682,7 +3682,7 @@ do_cache_compilation(Context& ctx, char* argv[]) std::min(std::max(ctx.config.limit_multiple(), 0.0), 1.0)); MTR_BEGIN("main", "guess_compiler"); - guessed_compiler = guess_compiler(orig_args->argv[0]); + guessed_compiler = guess_compiler(ctx.orig_args->argv[0]); MTR_END("main", "guess_compiler"); // Arguments (except -E) to send to the preprocessor. @@ -3695,7 +3695,7 @@ do_cache_compilation(Context& ctx, char* argv[]) MTR_BEGIN("main", "process_args"); if (!cc_process_args(ctx, - orig_args, + ctx.orig_args, &preprocessor_args, &extra_args_to_hash, &compiler_args)) { @@ -3906,7 +3906,7 @@ handle_main_options(int argc, char* argv[]) {"zero-stats", no_argument, 0, 'z'}, {0, 0, 0, 0}}; - Context& ctx = initialize(); + Context& ctx = initialize(argc, argv); (void)ctx; int c; diff --git a/src/legacy_globals.hpp b/src/legacy_globals.hpp index 08affbd4a..614417194 100644 --- a/src/legacy_globals.hpp +++ b/src/legacy_globals.hpp @@ -32,8 +32,6 @@ extern char* current_working_dir; extern unsigned lock_staleness_limit; -extern struct args* orig_args; - #define MAX_ARCH_ARGS 10 extern size_t arch_args_size; extern char* arch_args[MAX_ARCH_ARGS];