]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Avoid passing compilation-only options to the preprocessor redux
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 14 Jan 2020 20:30:29 +0000 (21:30 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 15 Jan 2020 20:31:04 +0000 (21:31 +0100)
25e73c1f (“Include compiler-only arguments in the hash”) attempted to
fix a regression in 5d8585b5 (“Don’t pass -Werror and compilation-only
options to the preprocessor”). It succeeded fixing the regression all
right, but it also essentially reverted the essence of 5d8585b5 (#312)
since compiler-only arguments once again are passed to the preprocessor.
Sigh.

Fix this for real and also write a test that proves it.

(cherry picked from commit bc2005e994030f787c014ce2491cfc4c7057eac6)

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

index 7153917ebc2f354367236293398c5fb4e29c006d..480893b30951a409173fd2fbfdca2202ce94d4fc 100644 (file)
@@ -1877,7 +1877,10 @@ hash_common_info(struct args* args, struct hash* hash)
 // modes and calculate the result name. Returns the result name on success,
 // otherwise NULL. Caller frees.
 static struct digest*
-calculate_result_name(struct args* args, struct hash* hash, int direct_mode)
+calculate_result_name(struct args* args,
+                      struct args* preprocessor_args,
+                      struct hash* hash,
+                      bool direct_mode)
 {
   bool found_ccbin = false;
 
@@ -2151,22 +2154,23 @@ calculate_result_name(struct args* args, struct hash* hash, int direct_mode)
       cc_log("Did not find result name in manifest");
     }
   } else {
+    assert(preprocessor_args);
     if (arch_args_size == 0) {
-      result_name = get_result_name_from_cpp(args, hash);
+      result_name = get_result_name_from_cpp(preprocessor_args, hash);
       cc_log("Got result name from preprocessor");
     } else {
-      args_add(args, "-arch");
+      args_add(preprocessor_args, "-arch");
       for (size_t i = 0; i < arch_args_size; ++i) {
-        args_add(args, arch_args[i]);
-        result_name = get_result_name_from_cpp(args, hash);
+        args_add(preprocessor_args, arch_args[i]);
+        result_name = get_result_name_from_cpp(preprocessor_args, hash);
         cc_log("Got result name from preprocessor with -arch %s", arch_args[i]);
         if (i != arch_args_size - 1) {
           free(result_name);
           result_name = NULL;
         }
-        args_pop(args, 1);
+        args_pop(preprocessor_args, 1);
       }
-      args_pop(args, 1);
+      args_pop(preprocessor_args, 1);
     }
     if (generating_dependencies) {
       // Nothing is actually created with -MF /dev/null
@@ -3900,7 +3904,8 @@ ccache(int argc, char* argv[])
   if (g_config.direct_mode()) {
     cc_log("Trying direct lookup");
     MTR_BEGIN("hash", "direct_hash");
-    result_name = calculate_result_name(args_to_hash, direct_hash, 1);
+    result_name =
+      calculate_result_name(args_to_hash, nullptr, direct_hash, true);
     MTR_END("hash", "direct_hash");
     if (result_name) {
       update_cached_result_globals(result_name);
@@ -3933,7 +3938,8 @@ ccache(int argc, char* argv[])
       cpp_hash, output_obj, 'p', "PREPROCESSOR MODE", debug_text_file);
 
     MTR_BEGIN("hash", "cpp_hash");
-    result_name = calculate_result_name(args_to_hash, cpp_hash, 0);
+    result_name =
+      calculate_result_name(args_to_hash, preprocessor_args, cpp_hash, false);
     MTR_END("hash", "cpp_hash");
     if (!result_name) {
       fatal("internal error: calculate_result_name returned NULL for cpp");
index 36a99747979a2915437199d77edbb2f02104aed6..a98952dec0aabdcc72e82ef15731dbc23741738c 100644 (file)
@@ -991,22 +991,40 @@ EOF
     # -------------------------------------------------------------------------
     TEST "Handling of compiler-only arguments"
 
-    $CCACHE_COMPILE -c test1.c
+    cat >compiler.sh <<EOF
+#!/bin/sh
+printf "[%s]" "\$*" >>compiler.args
+[ \$1 = -E ] && echo test || echo test >test1.o
+EOF
+    chmod +x compiler.sh
+    backdate compiler.sh
+
+    $CCACHE ./compiler.sh -c test1.c
     expect_stat 'cache hit (preprocessed)' 0
     expect_stat 'cache miss' 1
     expect_stat 'files in cache' 1
+    if [ -z "$CCACHE_NOCPP2" ]; then
+        expect_file_content compiler.args "[-E test1.c][-c -o test1.o test1.c]"
+    fi
+    rm compiler.args
 
-    $CCACHE_COMPILE -c test1.c
+    $CCACHE ./compiler.sh -c test1.c
     expect_stat 'cache hit (preprocessed)' 1
     expect_stat 'cache miss' 1
     expect_stat 'files in cache' 1
+    expect_file_content compiler.args "[-E test1.c]"
+    rm compiler.args
 
     # Even though -Werror is not passed to the preprocessor, it should be part
     # of the hash, so we expect a cache miss:
-    $CCACHE_COMPILE -c -Werror test1.c
+    $CCACHE ./compiler.sh -c -Werror -rdynamic test1.c
     expect_stat 'cache hit (preprocessed)' 1
     expect_stat 'cache miss' 2
     expect_stat 'files in cache' 2
+    if [ -z "$CCACHE_NOCPP2" ]; then
+        expect_file_content compiler.args "[-E test1.c][-Werror -rdynamic -c -o test1.o test1.c]"
+    fi
+    rm compiler.args
 
     # -------------------------------------------------------------------------
     TEST "Dependency file content"