From: Joel Rosdahl Date: Sun, 17 Jul 2022 11:43:22 +0000 (+0200) Subject: feat: Add support for GCC -fprofile-abs-path option X-Git-Tag: v4.7~156 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=195011ad1b1f0a9c43794532571261d8c0c5e1c6;p=thirdparty%2Fccache.git feat: Add support for GCC -fprofile-abs-path option 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. --- diff --git a/doc/MANUAL.adoc b/doc/MANUAL.adoc index 759590910..fc0eb1c35 100644 --- a/doc/MANUAL.adoc +++ b/doc/MANUAL.adoc @@ -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 diff --git a/src/argprocessing.cpp b/src/argprocessing.cpp index 1caf7e742..0d658aa9e 100644 --- a/src/argprocessing.cpp +++ b/src/argprocessing.cpp @@ -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, + }; } diff --git a/src/argprocessing.hpp b/src/argprocessing.hpp index 474a648e7..2f5c2f7e0 100644 --- a/src/argprocessing.hpp +++ b/src/argprocessing.hpp @@ -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_) { } diff --git a/src/ccache.cpp b/src/ccache.cpp index 53f59d361..1bc23430d 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -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); diff --git a/test/suites/profiling_gcc.bash b/test/suites/profiling_gcc.bash index cd892c3ea..cd0123450 100644 --- a/test/suites/profiling_gcc.bash +++ b/test/suites/profiling_gcc.bash @@ -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 }