]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Support ccache ccache gcc (#687)
authorAlexander Lanin <alex@lanin.de>
Tue, 20 Oct 2020 07:00:17 +0000 (09:00 +0200)
committerGitHub <noreply@github.com>
Tue, 20 Oct 2020 07:00:17 +0000 (09:00 +0200)
src/ccache.cpp
test/suites/base.bash
unittest/test_ccache.cpp

index 676f9c0b8ea171d156b88d5e010103b4fe5ba63b..4bcb9689e080076bb98dad44f456042e773796b5 100644 (file)
@@ -1844,18 +1844,24 @@ void
 find_compiler(Context& ctx,
               const FindExecutableFunction& find_executable_function)
 {
-  const nonstd::string_view first_param_base_name =
-    Util::base_name(ctx.orig_args[0]);
-  const bool first_param_is_ccache =
-    Util::same_program_name(first_param_base_name, CCACHE_NAME);
+  // gcc --> 0
+  // ccache gcc --> 1
+  // ccache ccache gcc --> 2
+  size_t compiler_pos = 0;
+  while (compiler_pos < ctx.orig_args.size()
+         && Util::same_program_name(
+           Util::base_name(ctx.orig_args[compiler_pos]), CCACHE_NAME)) {
+    compiler_pos++;
+  }
 
   // Support user override of the compiler.
   const std::string compiler =
     !ctx.config.compiler().empty()
       ? ctx.config.compiler()
-      : (first_param_is_ccache ? ctx.orig_args[1]
-                               // ccache is masquerading as compiler:
-                               : std::string(first_param_base_name));
+      // In case ccache is masquerading as compiler,
+      // use only base_name so the real compiler can be determined.
+      : compiler_pos == 0 ? std::string(Util::base_name(ctx.orig_args[0]))
+                          : ctx.orig_args[compiler_pos];
 
   const std::string resolved_compiler =
     Util::is_full_path(compiler)
@@ -1873,9 +1879,7 @@ find_compiler(Context& ctx,
       CCACHE_NAME);
   }
 
-  if (first_param_is_ccache) {
-    ctx.orig_args.pop_front();
-  }
+  ctx.orig_args.pop_front(compiler_pos);
   ctx.orig_args[0] = resolved_compiler;
 }
 
index e12d25cc9ecc6149a1b266c9792f40c9c6eedb03..765ef63bd94abf09836fd3fec89500746b9ebeed 100644 (file)
@@ -16,6 +16,31 @@ base_tests() {
     expect_stat 'files in cache' 1
     expect_equal_object_files reference_test1.o test1.o
 
+    # -------------------------------------------------------------------------
+    TEST "ccache ccache gcc"
+    # e.g. due to some suboptimal setup, scripts etc.
+    # Source: https://github.com/ccache/ccache/issues/686
+
+    $REAL_COMPILER -c -o reference_test1.o test1.c
+
+    $CCACHE $COMPILER -c test1.c
+    expect_stat 'cache hit (preprocessed)' 0
+    expect_stat 'cache miss' 1
+    expect_stat 'files in cache' 1
+    expect_equal_object_files reference_test1.o test1.o
+
+    $CCACHE $CCACHE $COMPILER -c test1.c
+    expect_stat 'cache hit (preprocessed)' 1
+    expect_stat 'cache miss' 1
+    expect_stat 'files in cache' 1
+    expect_equal_object_files reference_test1.o test1.o
+
+    $CCACHE $CCACHE $CCACHE $COMPILER -c test1.c
+    expect_stat 'cache hit (preprocessed)' 2
+    expect_stat 'cache miss' 1
+    expect_stat 'files in cache' 1
+    expect_equal_object_files reference_test1.o test1.o
+
     # -------------------------------------------------------------------------
     TEST "Version output readable"
 
index 3011d2fe4856af067d1430519d508d6b5722b6ab..f204a74f5675e67576b86b69088d22a6324b3eb0 100644 (file)
@@ -105,6 +105,16 @@ TEST_CASE("find_compiler")
     CHECK(helper("/abs/" CCACHE_NAME " /abs/gcc", "", "") == "/abs/gcc");
   }
 
+  SUBCASE("double ccache")
+  {
+    // e.g. due to some suboptimal setup, scripts etc.
+    // Source: https://github.com/ccache/ccache/issues/686
+    CHECK(helper(CCACHE_NAME " gcc", "") == "resolved_gcc");
+    CHECK(helper(CCACHE_NAME " " CCACHE_NAME " gcc", "") == "resolved_gcc");
+    CHECK(helper(CCACHE_NAME " " CCACHE_NAME " " CCACHE_NAME " gcc", "")
+          == "resolved_gcc");
+  }
+
   SUBCASE("config")
   {
     // In case the first parameter is gcc it must be a link to ccache so use