]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
fix: Don’t cache preexisting object file when using -fsyntax-only
authorJoel Rosdahl <joel@rosdahl.net>
Thu, 24 Mar 2022 20:17:17 +0000 (21:17 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Thu, 24 Mar 2022 20:30:14 +0000 (21:30 +0100)
Fixes #1034.

src/ccache.cpp
test/suites/base.bash

index a9e4b21951505a97631033b4e27cc8b053b25117..a3dade4ca8c81c372fab568dea83ea678fef807b 100644 (file)
@@ -1064,17 +1064,20 @@ to_cache(Context& ctx,
     Depfile::make_paths_relative_in_output_dep(ctx);
   }
 
-  const auto obj_stat = Stat::stat(ctx.args_info.output_obj);
-  if (!obj_stat) {
-    if (ctx.args_info.expect_output_obj) {
-      LOG_RAW("Compiler didn't produce an object file (unexpected)");
+  Stat obj_stat;
+  if (!ctx.args_info.expect_output_obj) {
+    // Don't probe for object file when we don't expect one since we otherwise
+    // will be fooled by an already existing object file.
+    LOG_RAW("Compiler not expected to produce an object file");
+  } else {
+    obj_stat = Stat::stat(ctx.args_info.output_obj);
+    if (!obj_stat) {
+      LOG_RAW("Compiler didn't produce an object file");
       return nonstd::make_unexpected(Statistic::compiler_produced_no_output);
-    } else {
-      LOG_RAW("Compiler didn't produce an object file (expected)");
+    } else if (obj_stat.size() == 0) {
+      LOG_RAW("Compiler produced an empty object file");
+      return nonstd::make_unexpected(Statistic::compiler_produced_empty_output);
     }
-  } else if (obj_stat.size() == 0) {
-    LOG_RAW("Compiler produced an empty object file");
-    return nonstd::make_unexpected(Statistic::compiler_produced_empty_output);
   }
 
   MTR_BEGIN("result", "result_put");
index 050d8bd07f1e2037ccb99a869cba825d1002bca3..b2d9149518597685b6c5ee57c21332edda4cd8b8 100644 (file)
@@ -1097,6 +1097,42 @@ EOF
     expect_stat files_in_cache 0
     expect_stat compiler_produced_no_output 2
 
+    # -------------------------------------------------------------------------
+    TEST "-fsyntax-only"
+
+    echo existing >test1.o
+    $CCACHE_COMPILE -fsyntax-only test1.c
+    expect_stat preprocessed_cache_hit 0
+    expect_stat cache_miss 1
+    expect_stat files_in_cache 1
+    expect_content test1.o existing
+
+    rm test1.o
+
+    $CCACHE_COMPILE -fsyntax-only test1.c
+    expect_stat preprocessed_cache_hit 1
+    expect_stat cache_miss 1
+    expect_stat files_in_cache 1
+    expect_missing test1.o
+
+    # -------------------------------------------------------------------------
+    TEST "-fsyntax-only /dev/null"
+
+    echo existing >null.o
+    $CCACHE_COMPILE -fsyntax-only -x c /dev/null
+    expect_stat preprocessed_cache_hit 0
+    expect_stat cache_miss 1
+    expect_stat files_in_cache 1
+    expect_content null.o existing
+
+    rm null.o
+
+    $CCACHE_COMPILE -fsyntax-only -x c /dev/null
+    expect_stat preprocessed_cache_hit 1
+    expect_stat cache_miss 1
+    expect_stat files_in_cache 1
+    expect_missing null.o
+
     # -------------------------------------------------------------------------
     TEST "No object file due to -fsyntax-only"