]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
fix: Avoid hashing CWD parts of -march=native expansion for Clang
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 9 Dec 2025 18:19:17 +0000 (19:19 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 9 Dec 2025 20:33:32 +0000 (21:33 +0100)
Fixes #1658.

src/ccache/ccache.cpp
test/fake-compilers/clang-march-native.sh
test/suites/base.bash

index b44296db2d5e939b02f1349c7958abcd6ff862fe..7aa40c659942d8f975d60dbacbe1449bc5f3b924 100644 (file)
@@ -2045,10 +2045,34 @@ hash_native_args(Context& ctx, const util::Args& native_args, Hash& hash)
 
   hash.hash_delimiter(native_args.to_string());
 
-  // We could potentially work out exactly which options -m*=native expand to
-  // and hash only those, but to keep things simple we include the full line
-  // where cc1 was found for now.
-  hash.hash(*line_to_hash);
+  if (ctx.config.is_compiler_group_clang()) {
+    // For Clang, extract and hash only architecture-related options to avoid
+    // hashing CWD-dependent paths like -fdebug-compilation-dir and
+    // -fcoverage-compilation-dir which cause cache misses when compiling from
+    // different directories.
+    //
+    // We specifically look for:
+    //
+    // - "-target-cpu" followed by CPU name (e.g., "alderlake")
+    // - "-tune-cpu" followed by CPU name
+    // - "-target-feature" followed by feature flag (e.g., "+avx2", "-avx512f")
+    bool hash_value = false;
+    for (const auto token : util::Tokenizer(*line_to_hash, " ")) {
+      if (hash_value) {
+        hash.hash(' ');
+        hash.hash(token);
+        hash_value = false;
+      } else if (token == "\"-target-cpu\"" || token == "\"-tune-cpu\""
+                 || token == "\"-target-feature\"") {
+        hash.hash(token);
+        hash_value = true;
+      }
+    }
+  } else {
+    // For GCC, the full line should be safe to hash as it doesn't contain
+    // CWD-dependent paths.
+    hash.hash(*line_to_hash);
+  }
 
   return {};
 }
index 32d6a7b50db8217638f9b4af690916817a7c8870..e7f2577679dce90816ffed9f8e06bac26ad0ea42 100755 (executable)
@@ -7,7 +7,7 @@ if [ "$1" = "-###" ]; then
 ...
 InstalledDir: /usr/bin
  (in-process)
- "/example/bin/clang" "-cc1" $CC1_ARGS
+ "/example/bin/clang" "-cc1" "-target-cpu" "$CC1_ARGS"
 EOF
     echo "bin/cc1"
 elif [ "$1" = "-E" ]; then
index cdb7daff6bfe6a4c2104b212b8a626eba80795ba..e6ed77e953034e2cf9e5a8bfcf3514d2bbe7e848 100644 (file)
@@ -1702,6 +1702,23 @@ EOF
     expect_stat preprocessed_cache_hit 2
     expect_stat cache_miss 2
 
+    # -------------------------------------------------------------------------
+    if $COMPILER -march=native -c test1.c 2>/dev/null; then
+        TEST "-march=native, no CWD in input hash"
+        mkdir a b
+        cp test1.c a
+        cp test1.c b
+        cd a
+        $CCACHE_COMPILE -march=native -c test1.c
+        expect_stat preprocessed_cache_hit 0
+        expect_stat cache_miss 1
+        cd ../b
+        $CCACHE_COMPILE -march=native -c test1.c
+        cd ..
+        expect_stat preprocessed_cache_hit 1
+        expect_stat cache_miss 1
+    fi
+
     # -------------------------------------------------------------------------
     TEST "Handling of compiler-only arguments"