From: Joel Rosdahl Date: Thu, 24 Mar 2022 20:17:17 +0000 (+0100) Subject: fix: Don’t cache preexisting object file when using -fsyntax-only X-Git-Tag: v4.6.1~56 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d4fcdcf2d5bfd3f9ec416a303016eba9e7f42f86;p=thirdparty%2Fccache.git fix: Don’t cache preexisting object file when using -fsyntax-only Fixes #1034. --- diff --git a/src/ccache.cpp b/src/ccache.cpp index a9e4b2195..a3dade4ca 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -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"); diff --git a/test/suites/base.bash b/test/suites/base.bash index 050d8bd07..b2d914951 100644 --- a/test/suites/base.bash +++ b/test/suites/base.bash @@ -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"