]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Context: move orig_args 542/head
authorThomas Otto <thomas.otto@pdv-fs.de>
Sat, 25 Jan 2020 09:38:06 +0000 (10:38 +0100)
committerThomas Otto <thomas.otto@pdv-fs.de>
Sun, 16 Feb 2020 12:43:08 +0000 (13:43 +0100)
src/Context.cpp
src/Context.hpp
src/ccache.cpp
src/legacy_globals.hpp

index 27ab9c146c2e3dcf557ee101df8a1ac0c3dac3f1..35380752d0d524b15189e4e68e80644e31c3e1a6 100644 (file)
 
 #include "Context.hpp"
 
+#include "args.hpp"
+
 Context::~Context()
 {
+  if (free_orig_args) {
+    args_free(orig_args);
+  }
 }
index 19821227b1c9cb64163a2b59a69e75af3e209bf3..afe8b4d565176909716c91edd19d66b449089d3f 100644 (file)
@@ -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 (<cache_dir>/<x>/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;
 };
index 12829cbc7470aa8a112b0c8adbda9de3d2fb0c4c..5bec417e8a65fddb3cc67dca4257089f1240d358 100644 (file)
@@ -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;
index 08affbd4aef2bc43f4649a7022c661b99add58a9..6144171949ad2141f59aa6397e7d9a3bf44b7695 100644 (file)
@@ -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];