Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
16 changes: 16 additions & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$<TARGET_PROPERTY:BINARY_DIR>")
endif()
target_compile_definitions(
codspeed
INTERFACE $<BUILD_INTERFACE:CODSPEED_COMPILE_DIR="${CODSPEED_COMPILE_DIR}">
)

set(CODSPEED_MODE_ALLOWED_VALUES
"off"
"instrumentation"
Expand Down
7 changes: 5 additions & 2 deletions core/include/codspeed.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 17 additions & 3 deletions core/src/workspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
8 changes: 7 additions & 1 deletion core/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
95 changes: 95 additions & 0 deletions core/test/workspace.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include <gtest/gtest.h>

#include <filesystem>
#include <fstream>
#include <random>
#include <string>

#include "codspeed.h"

namespace fs = std::filesystem;

// Builds a temporary tree, since the function checks that the file exists:
// <tmp>/src/bench.cpp the benchmark file
// <tmp>/build/ the compile directory
// <tmp>/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=<root>/=` 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);
}
7 changes: 7 additions & 0 deletions google_benchmark/include/benchmark/benchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading