From b20ac28a31f0eb4420a37a24ef2385e0fbe8253b Mon Sep 17 00:00:00 2001 From: Luc Varoqui Date: Wed, 25 Nov 2026 18:25:10 +0100 Subject: [PATCH 1/3] 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. It only does so when the file exists there. `-fmacro-prefix-map=/=` also makes `__FILE__` relative, but to the repository root, and joining it to the compile directory would break URIs that are correct today. The check uses the lexically normalized path, so it still works when the build directory was deleted after the build. Otherwise, and for absolute paths, Bazel builds and build systems that don't set the definition, the behavior is unchanged. Benchmark URIs of projects built with ccache `base_dir` change, so their history restarts. Refs COD-4223 Co-Authored-By: Claude Opus 5.5 --- core/CMakeLists.txt | 14 +++ core/include/codspeed.h | 7 +- core/src/workspace.cpp | 20 +++- core/test/CMakeLists.txt | 8 +- core/test/workspace.cpp | 95 +++++++++++++++++++ .../include/benchmark/benchmark.h | 7 ++ 6 files changed, 145 insertions(+), 6 deletions(-) create mode 100644 core/test/workspace.cpp 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..0ae0430 100644 --- a/core/include/codspeed.h +++ b/core/include/codspeed.h @@ -47,8 +47,11 @@ 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 the file exists there. Otherwise it is +// resolved against the current directory. +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..a22b76a 100644 --- a/core/src/workspace.cpp +++ b/core/src/workspace.cpp @@ -25,20 +25,34 @@ 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; } + // Tools like ccache with `base_dir` make __FILE__ relative to the + // directory the compiler ran in, not to the one the benchmark runs in. + // Other tools, like `-fmacro-prefix-map`, make it relative to another + // directory, so this only uses `compile_dir` when the file exists there. + std::filesystem::path file_path(path); + if (file_path.is_relative() && !compile_dir.empty()) { + auto in_compile_dir = std::filesystem::path(compile_dir) / file_path; + std::error_code ec; + if (std::filesystem::exists(in_compile_dir.lexically_normal(), ec)) { + file_path = in_compile_dir; + } + } + // 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/core/test/CMakeLists.txt b/core/test/CMakeLists.txt index c67157f..6cbcb91 100644 --- a/core/test/CMakeLists.txt +++ b/core/test/CMakeLists.txt @@ -6,7 +6,13 @@ FetchContent_Declare( ) FetchContent_MakeAvailable(googletest) -add_executable(unit_tests uri.cpp codspeed.cpp walltime.cpp) +add_executable(unit_tests uri.cpp codspeed.cpp walltime.cpp workspace.cpp) + +# Gives the tests the root that get_path_relative_to_workspace uses +target_compile_definitions( + unit_tests + PRIVATE CODSPEED_TEST_ROOT_DIR="${CODSPEED_ROOT_DIR}" +) target_include_directories(unit_tests PRIVATE ${CMAKE_SOURCE_DIR}/src) diff --git a/core/test/workspace.cpp b/core/test/workspace.cpp new file mode 100644 index 0000000..0f99cb8 --- /dev/null +++ b/core/test/workspace.cpp @@ -0,0 +1,95 @@ +#include + +#include +#include +#include +#include + +#include "codspeed.h" + +namespace fs = std::filesystem; + +// Builds a temporary tree, since the function checks that the file exists: +// /src/bench.cpp the benchmark file +// /build/ the compile directory +// /other/deep/ a directory to run the benchmark from +class WorkspaceTest : public ::testing::Test { + protected: + void SetUp() override { + root = CODSPEED_TEST_ROOT_DIR; + if (root.empty()) { + GTEST_SKIP() << "CODSPEED_ROOT_DIR is not set (git detection failed)"; + } + previous_cwd = fs::current_path(); + + // Canonical, so symlinks like /tmp -> /private/tmp on macOS don't change + // the expected paths + tmp = fs::canonical(fs::temp_directory_path()) / + ("codspeed-workspace-test-" + std::to_string(std::random_device{}())); + fs::create_directories(tmp / "src"); + fs::create_directories(tmp / "build"); + fs::create_directories(tmp / "other" / "deep"); + std::ofstream(tmp / "src" / "bench.cpp").close(); + + file = tmp / "src" / "bench.cpp"; + expected = fs::relative(file, root); + } + + void TearDown() override { + if (!previous_cwd.empty()) { + fs::current_path(previous_cwd); + } + if (!tmp.empty()) { + std::error_code ec; + fs::remove_all(tmp, ec); + } + } + + static fs::path relative(const std::string& path, + const std::string& compile_dir = "") { + return codspeed::get_path_relative_to_workspace(path, compile_dir); + } + + fs::path root; + fs::path previous_cwd; + fs::path tmp; + fs::path file; + fs::path expected; +}; + +TEST_F(WorkspaceTest, AbsolutePath) { + EXPECT_EQ(relative(file.string()), expected); + EXPECT_EQ(relative(file.string(), (tmp / "build").string()), expected); +} + +// ccache with `base_dir`: __FILE__ is relative to the compile directory +TEST_F(WorkspaceTest, RelativeToCompileDir) { + fs::current_path(tmp); + EXPECT_EQ(relative("../src/bench.cpp", (tmp / "build").string()), expected); +} + +// The benchmark runs from a directory that is not the compile directory's +// parent +TEST_F(WorkspaceTest, RelativeToCompileDirFromAnotherCwd) { + fs::current_path(tmp / "other" / "deep"); + EXPECT_EQ(relative("../src/bench.cpp", (tmp / "build").string()), expected); +} + +// The build directory can be deleted after the build +TEST_F(WorkspaceTest, RelativeToMissingCompileDir) { + fs::current_path(tmp); + EXPECT_EQ(relative("../src/bench.cpp", (tmp / "missing-build").string()), + expected); +} + +// `-fmacro-prefix-map=/=` makes __FILE__ relative to the root, so the +// file does not exist in the compile directory +TEST_F(WorkspaceTest, FallsBackToCwdWhenMissingInCompileDir) { + fs::current_path(tmp); + EXPECT_EQ(relative("src/bench.cpp", (tmp / "build").string()), expected); +} + +TEST_F(WorkspaceTest, RelativeToCwdWithoutCompileDir) { + fs::current_path(tmp / "src"); + EXPECT_EQ(relative("bench.cpp"), expected); +} 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" From 05c8a74d69f33ca1d90adaa8d5791d5345ed11fa Mon Sep 17 00:00:00 2001 From: Luc Varoqui Date: Thu, 26 Nov 2026 11:17:46 +0100 Subject: [PATCH 2/3] build: keep CODSPEED_COMPILE_DIR out of the installed package The installed `benchmarkTargets.cmake` stored the compile directory of the build that compiled codspeed: an absolute path with Ninja, or a generator expression that is only right for Makefile consumers. A project using the installed package now gets no compile directory, so its URIs are computed as before this change. Refs COD-4223 Co-Authored-By: Claude Opus 5.5 --- core/CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index ac27039..040b187 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -172,6 +172,8 @@ message(STATUS "Using codspeed root directory: ${CODSPEED_ROOT_DIR}") # 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. +# The installed package doesn't get it: this directory belongs to the build +# that compiles codspeed, not to the project that uses the package. if(CMAKE_GENERATOR MATCHES "Ninja") set(CODSPEED_COMPILE_DIR "${CMAKE_BINARY_DIR}") else() @@ -179,7 +181,7 @@ else() endif() target_compile_definitions( codspeed - INTERFACE -DCODSPEED_COMPILE_DIR="${CODSPEED_COMPILE_DIR}" + INTERFACE $ ) set(CODSPEED_MODE_ALLOWED_VALUES From 35818bf7885948d8fef6fdd1fa2b5743911f04ca Mon Sep 17 00:00:00 2001 From: Luc Varoqui Date: Thu, 26 Nov 2026 11:21:37 +0100 Subject: [PATCH 3/3] ci: use the released runner in the macOS walltime jobs Both macOS jobs pinned the runner to `branch:main`. The action installs a branch by sourcing `~/.cargo/env`, which does not exist on `macos-latest`, so the jobs failed before running any benchmark. The released runner now supports macOS (v5.8.0 ships an `aarch64-apple-darwin` build), so drop the pin. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index af09a5b..b1c1043 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -227,8 +227,6 @@ jobs: with: run: examples/google_benchmark_cmake/build/benchmark_example --benchmark_filter=BM_FibonacciRecursive_Darwin mode: walltime - # TODO: Remove this once the runner has been released with macos support - runner-version: branch:main macos-walltime-bazel-test: runs-on: macos-latest @@ -259,4 +257,3 @@ jobs: # BUILD_WORKSPACE_DIRECTORY is normally set by `bazel run`; we set it manually so workspace-relative __FILE__ resolution works when invoking the binary directly. run: BUILD_WORKSPACE_DIRECTORY=$PWD ./bazel-bin/examples/google_benchmark_bazel/my_benchmark --benchmark_filter=BM_FibonacciRecursive_Darwin mode: walltime - runner-version: branch:main