]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
feat: Add support for GCC -fprofile-abs-path option
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 17 Jul 2022 11:43:22 +0000 (13:43 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 17 Jul 2022 11:43:22 +0000 (13:43 +0200)
Note: -fprofile-abs-path makes the compiler include absolute paths based
on actual CWD in the .gcno file. We therefore need to include the actual
CWD in the hash in order not to get false positive cache hits. To opt
out of this, set "gcno_cwd" sloppiness.

Closes #1060.

doc/MANUAL.adoc
src/argprocessing.cpp
src/argprocessing.hpp
src/ccache.cpp
test/suites/profiling_gcc.bash

index 7595909105e9ed065276386b9b0e66a9e30a5d7a..fc0eb1c3582f7e72871c652cf778e12508f01d41 100644 (file)
@@ -936,7 +936,8 @@ Examples:
     in the `.gcno` file. The *gcno_cwd* sloppiness makes ccache not hash the
     current working directory so that you can get cache hits when compiling in
     different directories, with the tradeoff of potentially getting an incorrect
-    directory in the `.gcno` file.
+    directory in the `.gcno` file. *gcno_cwd* also disables hashing of the
+    current working directory if `-fprofile-abs-path` is used.
 *include_file_ctime*::
     By default, ccache will not cache a file if it includes a header whose ctime
     is too new. This sloppiness disables that check. See also
index 1caf7e742b3114bd87555b3364327bdb4c8fb243..0d658aa9efa18666fc12daf3e7b33d6e07574061 100644 (file)
@@ -95,6 +95,9 @@ struct ArgumentProcessingState
 
   // Whether to include the full command line in the hash.
   bool hash_full_command_line = false;
+
+  // Whether to include the actual CWD in the hash.
+  bool hash_actual_cwd = false;
 };
 
 bool
@@ -679,6 +682,15 @@ process_option_arg(const Context& ctx,
     return Statistic::none;
   }
 
+  if (args[i] == "-fprofile-abs-path") {
+    if (!config.sloppiness().is_enabled(core::Sloppy::gcno_cwd)) {
+      // -fprofile-abs-path makes the compiler include absolute paths based on
+      // the actual CWD in the .gcno file.
+      state.hash_actual_cwd = true;
+    }
+    return Statistic::none;
+  }
+
   if (util::starts_with(args[i], "-fprofile-")
       || util::starts_with(args[i], "-fauto-profile")
       || args[i] == "-fbranch-probabilities") {
@@ -1470,5 +1482,10 @@ process_args(Context& ctx)
     }
   }
 
-  return {preprocessor_args, extra_args_to_hash, compiler_args};
+  return {
+    preprocessor_args,
+    extra_args_to_hash,
+    compiler_args,
+    state.hash_actual_cwd,
+  };
 }
index 474a648e79b502abbccbb1805caddad638e5d6f1..2f5c2f7e074ceb231ca53ccdbc6ab05b3b9cc23d 100644 (file)
@@ -31,7 +31,8 @@ struct ProcessArgsResult
   ProcessArgsResult(core::Statistic error_);
   ProcessArgsResult(const Args& preprocessor_args_,
                     const Args& extra_args_to_hash_,
-                    const Args& compiler_args_);
+                    const Args& compiler_args_,
+                    bool hash_actual_cwd_);
 
   // nullopt on success, otherwise the statistics counter that should be
   // incremented.
@@ -45,6 +46,9 @@ struct ProcessArgsResult
 
   // Arguments to send to the real compiler.
   Args compiler_args;
+
+  // Whether to include the actual CWD in the hash.
+  bool hash_actual_cwd;
 };
 
 inline ProcessArgsResult::ProcessArgsResult(core::Statistic error_)
@@ -54,10 +58,12 @@ inline ProcessArgsResult::ProcessArgsResult(core::Statistic error_)
 
 inline ProcessArgsResult::ProcessArgsResult(const Args& preprocessor_args_,
                                             const Args& extra_args_to_hash_,
-                                            const Args& compiler_args_)
+                                            const Args& compiler_args_,
+                                            bool hash_actual_cwd_)
   : preprocessor_args(preprocessor_args_),
     extra_args_to_hash(extra_args_to_hash_),
-    compiler_args(compiler_args_)
+    compiler_args(compiler_args_),
+    hash_actual_cwd(hash_actual_cwd_)
 {
 }
 
index 53f59d3614bd0ddeffecab6cfa64d49b1f568283..1bc23430d27d33f2c7a0b8a8630fe18551b9b848 100644 (file)
@@ -2274,6 +2274,11 @@ do_cache_compilation(Context& ctx, const char* const* argv)
       ctx, processed.preprocessor_args, common_hash, ctx.args_info));
   }
 
+  if (processed.hash_actual_cwd) {
+    common_hash.hash_delimiter("actual_cwd");
+    common_hash.hash(ctx.actual_cwd);
+  }
+
   // Try to find the hash using the manifest.
   Hash direct_hash = common_hash;
   init_hash_debug(ctx, direct_hash, 'd', "DIRECT MODE", debug_text_file);
index cd892c3ea018c550fb0a5e89199c83f7aebcbd7e..cd0123450b1fbd777865cbe1cb0eb8440edc18d9 100644 (file)
@@ -115,4 +115,49 @@ SUITE_profiling_gcc() {
     $CCACHE_COMPILE -fprofile-dir=data2 -fprofile-use=data -c test.c
     expect_stat direct_cache_hit 1
     expect_stat cache_miss 3
+
+    # -------------------------------------------------------------------------
+    TEST "-fprofile-abs-path"
+
+    if $COMPILER -fprofile-abs-path -c test.c 2>/dev/null; then
+        mkdir a b
+
+        cd a
+
+        $CCACHE_COMPILE -fprofile-abs-path -ftest-coverage -c ../test.c
+        expect_stat direct_cache_hit 0
+        expect_stat cache_miss 1
+
+        $CCACHE_COMPILE -fprofile-abs-path -ftest-coverage -c ../test.c
+        expect_stat direct_cache_hit 1
+        expect_stat cache_miss 1
+
+        cd ../b
+
+        $CCACHE_COMPILE -fprofile-abs-path -ftest-coverage -c ../test.c
+        expect_stat direct_cache_hit 1
+        expect_stat cache_miss 2
+
+        $CCACHE_COMPILE -fprofile-abs-path -ftest-coverage -c ../test.c
+        expect_stat direct_cache_hit 2
+        expect_stat cache_miss 2
+
+        export CCACHE_SLOPPINESS="$CCACHE_SLOPPINESS gcno_cwd"
+
+        cd ../a
+
+        $CCACHE_COMPILE -fprofile-abs-path -ftest-coverage -c ../test.c
+        expect_stat direct_cache_hit 2
+        expect_stat cache_miss 3
+
+        $CCACHE_COMPILE -fprofile-abs-path -ftest-coverage -c ../test.c
+        expect_stat direct_cache_hit 3
+        expect_stat cache_miss 3
+
+        cd ../b
+
+        $CCACHE_COMPILE -fprofile-abs-path -ftest-coverage -c ../test.c
+        expect_stat direct_cache_hit 4
+        expect_stat cache_miss 3
+    fi
 }