Skip to content

fix: resolve relative __FILE__ against the compiler's directory - #58

Merged
lvaroqui merged 3 commits into
mainfrom
fix/resolve-relative-file-against-compile-dir
Nov 26, 2026
Merged

lvaroqui merged 3 commits into
mainfrom
fix/resolve-relative-file-against-compile-dir

Conversation

@lvaroqui

@lvaroqui lvaroqui commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Resolves a relative __FILE__ against the directory the compiler ran in, so benchmark URIs stay inside the repository when ccache rewrites paths.

With ccache and base_dir (CCACHE_BASEDIR) set, ccache rewrites the absolute source path CMake passes to the compiler into a path relative to the compiler's working directory. get_path_relative_to_workspace then resolved that relative path against the directory the benchmark runs in, so URIs became ../../../tests/benchmarks/bench.cpp::…. The number of .. also depended on how deep the checkout is.

  • core/CMakeLists.txt adds an INTERFACE definition CODSPEED_COMPILE_DIR to every target that links codspeed: CMAKE_BINARY_DIR with Ninja (which compiles everything from the top build directory), the consuming target's BINARY_DIR otherwise (Makefiles compile from each target's build directory).
  • CUR_FILE (benchmark.h) passes it to get_path_relative_to_workspace, which joins it to a relative path before making it relative to the workspace root.
  • get_path_relative_to_workspace uses the compile directory only when the file exists there. Otherwise it keeps the previous behavior. This matters for -fmacro-prefix-map=<root>/=, which also makes __FILE__ relative, but to the repository root.
  • The definition is wrapped in $<BUILD_INTERFACE:…>, so the installed package doesn't carry the compile directory of the build that compiled codspeed.
  • Absolute __FILE__, -fmacro-prefix-map, Bazel, installed packages, and build systems that don't set the definition behave as before.

Reproduced in Docker (checkout at /home/runner/work/repro/repro, benchmark at tests/benchmarks/bench.cpp, ccache 4.12.3):

Generator Fix __FILE__ URI
Make no ../../../tests/benchmarks/bench.cpp ../../../tests/benchmarks/bench.cpp::BM_sum
Ninja no ../tests/benchmarks/bench.cpp ../tests/benchmarks/bench.cpp::BM_sum
Make yes ../../../tests/benchmarks/bench.cpp tests/benchmarks/bench.cpp::BM_sum
Ninja yes ../tests/benchmarks/bench.cpp tests/benchmarks/bench.cpp::BM_sum

Accepted gaps

  • Only the Makefile and Ninja generators are verified. Visual Studio and Xcode use the BINARY_DIR branch.
  • The URIs of projects built with ccache base_dir change, so their benchmark history restarts.
  • Projects that use the installed package with ccache base_dir still get ../ URIs: the package can't know their compile directory.

Refs COD-4223 (the issue also covers fixes in other integrations and the platform, so it stays open)

@codspeed

codspeed Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Hooray! CodSpeed harness just leveled up!

The base and head of this comparison were measured with different runner settings, so their benchmark values are not directly comparable.

What changed between base and head:

  • CodSpeed runner v5 changed how benchmarks are measured (base 4.18.1 → head 5.8.0). View release notes

Re-run the base with the same settings to get a valid performance comparison.


Comparing fix/resolve-relative-file-against-compile-dir (35818bf) with main (493bce0)

Open in CodSpeed

@lvaroqui
lvaroqui force-pushed the fix/resolve-relative-file-against-compile-dir branch 2 times, most recently from 0993a15 to d848843 Compare November 26, 2026 10:28
@lvaroqui
lvaroqui marked this pull request as ready for review November 26, 2026 10:29
@greptile-apps

greptile-apps Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5 · The PR appears safe to merge; no new actionable issue was established.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable issue was established.

Summary

The PR passes the compiler’s working directory into benchmark path resolution so ccache-relative __FILE__ values can produce repository-relative URIs.

  • CMake benchmark URIs now use the compiler's directory to resolve relative source paths.
  • The macOS walltime jobs use the action's default runner.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A["Benchmark registration: __FILE__"] --> B{"Bazel workspace set?"}
  B -- Yes --> C["Keep supplied path"]
  B -- No --> D{"Relative path and compile directory supplied?"}
  D -- Yes --> E{"Candidate exists?"}
  E -- Yes --> F["Use compile-directory candidate"]
  E -- No --> G["Keep original path"]
  D -- No --> G
  F --> H["Make path relative to workspace root"]
  G --> H
Loading

Reviews (2) · Last reviewed commit: "ci: use the released runner in the macOS..." · Reviewed by Greptile

Comment thread core/test/workspace.cpp Resolved
lvaroqui and others added 3 commits November 26, 2026 12:12
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>
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
@lvaroqui
lvaroqui force-pushed the fix/resolve-relative-file-against-compile-dir branch from d848843 to 35818bf Compare November 26, 2026 11:12

@not-matthias not-matthias left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

@lvaroqui
lvaroqui merged commit 35818bf into main Nov 26, 2026
35 checks passed
@lvaroqui
lvaroqui deleted the fix/resolve-relative-file-against-compile-dir branch November 26, 2026 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants