Skip to content

Commit b20ac28

Browse files
lvaroquiclaude
andcommitted
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=<root>/=` 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 <noreply@anthropic.com>
1 parent 493bce0 commit b20ac28

6 files changed

Lines changed: 145 additions & 6 deletions

File tree

‎core/CMakeLists.txt‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,20 @@ target_compile_definitions(
168168

169169
message(STATUS "Using codspeed root directory: ${CODSPEED_ROOT_DIR}")
170170

171+
# Gives each benchmark file the directory its compiler runs in. Some tools,
172+
# such as ccache with `base_dir`, make `__FILE__` relative to that directory.
173+
# Ninja runs every compiler in the top build directory; Makefiles run it in
174+
# the build directory of the target that owns the file.
175+
if(CMAKE_GENERATOR MATCHES "Ninja")
176+
set(CODSPEED_COMPILE_DIR "${CMAKE_BINARY_DIR}")
177+
else()
178+
set(CODSPEED_COMPILE_DIR "$<TARGET_PROPERTY:BINARY_DIR>")
179+
endif()
180+
target_compile_definitions(
181+
codspeed
182+
INTERFACE -DCODSPEED_COMPILE_DIR="${CODSPEED_COMPILE_DIR}"
183+
)
184+
171185
set(CODSPEED_MODE_ALLOWED_VALUES
172186
"off"
173187
"instrumentation"

‎core/include/codspeed.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ std::string extract_lambda_namespace(const std::string &pretty_func);
4747
std::string sanitize_bench_args(std::string &text);
4848

4949
// Gets path relative to workspace root, expected to be called with __FILE__ as
50-
// an argument
51-
std::string get_path_relative_to_workspace(const std::string &path);
50+
// an argument. A relative `path` is resolved against `compile_dir`, the
51+
// directory the compiler ran in, when the file exists there. Otherwise it is
52+
// resolved against the current directory.
53+
std::string get_path_relative_to_workspace(const std::string &path,
54+
const std::string &compile_dir = "");
5255

5356
} // namespace codspeed
5457

‎core/src/workspace.cpp‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,34 @@ std::string safe_getenv(const char* var_name) {
2525
#endif
2626
}
2727

28-
std::string get_path_relative_to_workspace(const std::string& path) {
28+
std::string get_path_relative_to_workspace(const std::string& path,
29+
const std::string& compile_dir) {
2930
// 1. Check for bazel usage, through the BUILD_WORKSPACE_DIRECTORY env var
3031
// If so, __FILE__ will already be relative to the bazel workspace root
3132
if (!safe_getenv("BUILD_WORKSPACE_DIRECTORY").empty()) {
3233
return path;
3334
}
3435

36+
// Tools like ccache with `base_dir` make __FILE__ relative to the
37+
// directory the compiler ran in, not to the one the benchmark runs in.
38+
// Other tools, like `-fmacro-prefix-map`, make it relative to another
39+
// directory, so this only uses `compile_dir` when the file exists there.
40+
std::filesystem::path file_path(path);
41+
if (file_path.is_relative() && !compile_dir.empty()) {
42+
auto in_compile_dir = std::filesystem::path(compile_dir) / file_path;
43+
std::error_code ec;
44+
if (std::filesystem::exists(in_compile_dir.lexically_normal(), ec)) {
45+
file_path = in_compile_dir;
46+
}
47+
}
48+
3549
// 2. If defined, use the specificed value directly
3650
#ifdef CODSPEED_ROOT_DIR
37-
return std::filesystem::relative(path, CODSPEED_ROOT_DIR).string();
51+
return std::filesystem::relative(file_path, CODSPEED_ROOT_DIR).string();
3852
#endif
3953

4054
// 3. Fallback to bath relative to PWD
41-
return std::filesystem::relative(path, std::filesystem::current_path())
55+
return std::filesystem::relative(file_path, std::filesystem::current_path())
4256
.string();
4357
}
4458

‎core/test/CMakeLists.txt‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ FetchContent_Declare(
66
)
77
FetchContent_MakeAvailable(googletest)
88

9-
add_executable(unit_tests uri.cpp codspeed.cpp walltime.cpp)
9+
add_executable(unit_tests uri.cpp codspeed.cpp walltime.cpp workspace.cpp)
10+
11+
# Gives the tests the root that get_path_relative_to_workspace uses
12+
target_compile_definitions(
13+
unit_tests
14+
PRIVATE CODSPEED_TEST_ROOT_DIR="${CODSPEED_ROOT_DIR}"
15+
)
1016

1117
target_include_directories(unit_tests PRIVATE ${CMAKE_SOURCE_DIR}/src)
1218

‎core/test/workspace.cpp‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <filesystem>
4+
#include <fstream>
5+
#include <random>
6+
#include <string>
7+
8+
#include "codspeed.h"
9+
10+
namespace fs = std::filesystem;
11+
12+
// Builds a temporary tree, since the function checks that the file exists:
13+
// <tmp>/src/bench.cpp the benchmark file
14+
// <tmp>/build/ the compile directory
15+
// <tmp>/other/deep/ a directory to run the benchmark from
16+
class WorkspaceTest : public ::testing::Test {
17+
protected:
18+
void SetUp() override {
19+
root = CODSPEED_TEST_ROOT_DIR;
20+
if (root.empty()) {
21+
GTEST_SKIP() << "CODSPEED_ROOT_DIR is not set (git detection failed)";
22+
}
23+
previous_cwd = fs::current_path();
24+
25+
// Canonical, so symlinks like /tmp -> /private/tmp on macOS don't change
26+
// the expected paths
27+
tmp = fs::canonical(fs::temp_directory_path()) /
28+
("codspeed-workspace-test-" + std::to_string(std::random_device{}()));
29+
fs::create_directories(tmp / "src");
30+
fs::create_directories(tmp / "build");
31+
fs::create_directories(tmp / "other" / "deep");
32+
std::ofstream(tmp / "src" / "bench.cpp").close();
33+
34+
file = tmp / "src" / "bench.cpp";
35+
expected = fs::relative(file, root);
36+
}
37+
38+
void TearDown() override {
39+
if (!previous_cwd.empty()) {
40+
fs::current_path(previous_cwd);
41+
}
42+
if (!tmp.empty()) {
43+
std::error_code ec;
44+
fs::remove_all(tmp, ec);
45+
}
46+
}
47+
48+
static fs::path relative(const std::string& path,
49+
const std::string& compile_dir = "") {
50+
return codspeed::get_path_relative_to_workspace(path, compile_dir);
51+
}
52+
53+
fs::path root;
54+
fs::path previous_cwd;
55+
fs::path tmp;
56+
fs::path file;
57+
fs::path expected;
58+
};
59+
60+
TEST_F(WorkspaceTest, AbsolutePath) {
61+
EXPECT_EQ(relative(file.string()), expected);
62+
EXPECT_EQ(relative(file.string(), (tmp / "build").string()), expected);
63+
}
64+
65+
// ccache with `base_dir`: __FILE__ is relative to the compile directory
66+
TEST_F(WorkspaceTest, RelativeToCompileDir) {
67+
fs::current_path(tmp);
68+
EXPECT_EQ(relative("../src/bench.cpp", (tmp / "build").string()), expected);
69+
}
70+
71+
// The benchmark runs from a directory that is not the compile directory's
72+
// parent
73+
TEST_F(WorkspaceTest, RelativeToCompileDirFromAnotherCwd) {
74+
fs::current_path(tmp / "other" / "deep");
75+
EXPECT_EQ(relative("../src/bench.cpp", (tmp / "build").string()), expected);
76+
}
77+
78+
// The build directory can be deleted after the build
79+
TEST_F(WorkspaceTest, RelativeToMissingCompileDir) {
80+
fs::current_path(tmp);
81+
EXPECT_EQ(relative("../src/bench.cpp", (tmp / "missing-build").string()),
82+
expected);
83+
}
84+
85+
// `-fmacro-prefix-map=<root>/=` makes __FILE__ relative to the root, so the
86+
// file does not exist in the compile directory
87+
TEST_F(WorkspaceTest, FallsBackToCwdWhenMissingInCompileDir) {
88+
fs::current_path(tmp);
89+
EXPECT_EQ(relative("src/bench.cpp", (tmp / "build").string()), expected);
90+
}
91+
92+
TEST_F(WorkspaceTest, RelativeToCwdWithoutCompileDir) {
93+
fs::current_path(tmp / "src");
94+
EXPECT_EQ(relative("bench.cpp"), expected);
95+
}

‎google_benchmark/include/benchmark/benchmark.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,14 @@ class Fixture : public internal::Benchmark {
14811481
static ::benchmark::internal::Benchmark const* const BENCHMARK_PRIVATE_NAME(n)
14821482

14831483
#ifdef CODSPEED_ENABLED
1484+
#ifdef CODSPEED_COMPILE_DIR
1485+
#define CUR_FILE \
1486+
codspeed::get_path_relative_to_workspace(__FILE__, \
1487+
CODSPEED_COMPILE_DIR) + \
1488+
"::"
1489+
#else
14841490
#define CUR_FILE codspeed::get_path_relative_to_workspace(__FILE__) + "::"
1491+
#endif
14851492
#ifdef _MSC_VER
14861493
// TODO: __PRETTY_FUNCTION__ is not available in MSVC and we dont support
14871494
#define __PRETTY_FUNCTION__ "unsupported"

0 commit comments

Comments
 (0)