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"