From c6d14b70d0b79d187acfbe7a670db16b91a8bbe6 Mon Sep 17 00:00:00 2001 From: Luc Varoqui Date: Wed, 25 Nov 2026 18:25:10 +0100 Subject: [PATCH] fix: resolve relative __FILE__ against the compiler's directory With ccache and `base_dir` set, `__FILE__` is relative to the directory the compiler ran in. `get_path_relative_to_workspace` resolved it against the directory the benchmark runs in instead, so benchmark URIs started with `../../../` and pointed outside the repository. CMake now gives every target that links codspeed a `CODSPEED_COMPILE_DIR` definition: the top build directory with Ninja, the target's build directory with other generators. `CUR_FILE` passes it to `get_path_relative_to_workspace`, which joins it to a relative path before making it relative to the workspace. Absolute paths, Bazel builds and build systems that don't set the definition behave as before. Benchmark URIs of projects built with ccache `base_dir` change, so their history restarts. Closes COD-4223 Co-Authored-By: Claude Opus 5.5 --- core/CMakeLists.txt | 14 ++++++++++++++ core/include/codspeed.h | 6 ++++-- core/src/workspace.cpp | 14 +++++++++++--- google_benchmark/include/benchmark/benchmark.h | 7 +++++++ 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 9d918cf..ac27039 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -168,6 +168,20 @@ target_compile_definitions( message(STATUS "Using codspeed root directory: ${CODSPEED_ROOT_DIR}") +# Gives each benchmark file the directory its compiler runs in. Some tools, +# such as ccache with `base_dir`, make `__FILE__` relative to that directory. +# Ninja runs every compiler in the top build directory; Makefiles run it in +# the build directory of the target that owns the file. +if(CMAKE_GENERATOR MATCHES "Ninja") + set(CODSPEED_COMPILE_DIR "${CMAKE_BINARY_DIR}") +else() + set(CODSPEED_COMPILE_DIR "$") +endif() +target_compile_definitions( + codspeed + INTERFACE -DCODSPEED_COMPILE_DIR="${CODSPEED_COMPILE_DIR}" +) + set(CODSPEED_MODE_ALLOWED_VALUES "off" "instrumentation" diff --git a/core/include/codspeed.h b/core/include/codspeed.h index ba43982..180fb81 100644 --- a/core/include/codspeed.h +++ b/core/include/codspeed.h @@ -47,8 +47,10 @@ std::string extract_lambda_namespace(const std::string &pretty_func); std::string sanitize_bench_args(std::string &text); // Gets path relative to workspace root, expected to be called with __FILE__ as -// an argument -std::string get_path_relative_to_workspace(const std::string &path); +// an argument. A relative `path` is resolved against `compile_dir`, the +// directory the compiler ran in, when it is given. +std::string get_path_relative_to_workspace(const std::string &path, + const std::string &compile_dir = ""); } // namespace codspeed diff --git a/core/src/workspace.cpp b/core/src/workspace.cpp index d2e6bcd..ba2ee10 100644 --- a/core/src/workspace.cpp +++ b/core/src/workspace.cpp @@ -25,20 +25,28 @@ std::string safe_getenv(const char* var_name) { #endif } -std::string get_path_relative_to_workspace(const std::string& path) { +std::string get_path_relative_to_workspace(const std::string& path, + const std::string& compile_dir) { // 1. Check for bazel usage, through the BUILD_WORKSPACE_DIRECTORY env var // If so, __FILE__ will already be relative to the bazel workspace root if (!safe_getenv("BUILD_WORKSPACE_DIRECTORY").empty()) { return path; } + // A relative __FILE__ is relative to the compiler's directory, not to the + // directory the benchmark runs in + std::filesystem::path file_path(path); + if (file_path.is_relative() && !compile_dir.empty()) { + file_path = std::filesystem::path(compile_dir) / file_path; + } + // 2. If defined, use the specificed value directly #ifdef CODSPEED_ROOT_DIR - return std::filesystem::relative(path, CODSPEED_ROOT_DIR).string(); + return std::filesystem::relative(file_path, CODSPEED_ROOT_DIR).string(); #endif // 3. Fallback to bath relative to PWD - return std::filesystem::relative(path, std::filesystem::current_path()) + return std::filesystem::relative(file_path, std::filesystem::current_path()) .string(); } diff --git a/google_benchmark/include/benchmark/benchmark.h b/google_benchmark/include/benchmark/benchmark.h index f55288d..aa74550 100644 --- a/google_benchmark/include/benchmark/benchmark.h +++ b/google_benchmark/include/benchmark/benchmark.h @@ -1481,7 +1481,14 @@ class Fixture : public internal::Benchmark { static ::benchmark::internal::Benchmark const* const BENCHMARK_PRIVATE_NAME(n) #ifdef CODSPEED_ENABLED +#ifdef CODSPEED_COMPILE_DIR +#define CUR_FILE \ + codspeed::get_path_relative_to_workspace(__FILE__, \ + CODSPEED_COMPILE_DIR) + \ + "::" +#else #define CUR_FILE codspeed::get_path_relative_to_workspace(__FILE__) + "::" +#endif #ifdef _MSC_VER // TODO: __PRETTY_FUNCTION__ is not available in MSVC and we dont support #define __PRETTY_FUNCTION__ "unsupported"