]> 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:01:29 +0000 (21:01 +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.

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

index 36b41bb232bd97857ee115fd8040a5a83a78f864..073efe16b3761fe1d636a5173b2bd4351d5d3447 100644 (file)
@@ -2009,7 +2009,8 @@ calculate_common_hash(struct args *args, struct hash *hash)
 // modes and calculate the object hash. Returns the object hash on success,
 // otherwise NULL. Caller frees.
 static struct file_hash *
-calculate_object_hash(struct args *args, struct hash *hash, int direct_mode)
+calculate_object_hash(struct args *args, struct args *preprocessor_args,
+                      struct hash *hash, int direct_mode)
 {
        bool found_ccbin = false;
 
@@ -2273,23 +2274,24 @@ calculate_object_hash(struct args *args, struct hash *hash, int direct_mode)
                        cc_log("Did not find object file hash in manifest");
                }
        } else {
+               assert(preprocessor_args);
                if (arch_args_size == 0) {
-                       object_hash = get_object_name_from_cpp(args, hash);
+                       object_hash = get_object_name_from_cpp(preprocessor_args, hash);
                        cc_log("Got object file hash 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]);
-                               object_hash = get_object_name_from_cpp(args, hash);
+                               args_add(preprocessor_args, arch_args[i]);
+                               object_hash = get_object_name_from_cpp(preprocessor_args, hash);
                                cc_log("Got object file hash from preprocessor with -arch %s",
                                       arch_args[i]);
                                if (i != arch_args_size - 1) {
                                        free(object_hash);
                                        object_hash = 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
@@ -4041,7 +4043,7 @@ ccache(int argc, char *argv[])
        if (conf->direct_mode) {
                cc_log("Trying direct lookup");
                MTR_BEGIN("hash", "direct_hash");
-               object_hash = calculate_object_hash(args_to_hash, direct_hash, 1);
+               object_hash = calculate_object_hash(args_to_hash, NULL, direct_hash, 1);
                MTR_END("hash", "direct_hash");
                if (object_hash) {
                        update_cached_result_globals(object_hash);
@@ -4074,7 +4076,8 @@ ccache(int argc, char *argv[])
                        cpp_hash, output_obj, 'p', "PREPROCESSOR MODE", debug_text_file);
 
                MTR_BEGIN("hash", "cpp_hash");
-               object_hash = calculate_object_hash(args_to_hash, cpp_hash, 0);
+               object_hash = calculate_object_hash(
+                       args_to_hash, preprocessor_args, cpp_hash, 0);
                MTR_END("hash", "cpp_hash");
                if (!object_hash) {
                        fatal("internal error: object hash from cpp returned NULL");
index 00664a9c6401a454b902429ac3207c72ebc0bc4a..7a864e9409859ffe0de30884e3a2931ea33b0009 100644 (file)
@@ -967,22 +967,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"