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 diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 9d918cf..040b187 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -168,6 +168,22 @@ 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. +# 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() + set(CODSPEED_COMPILE_DIR "$") +endif() +target_compile_definitions( + codspeed + INTERFACE $ +) + 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"