Skip to content
Draft
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
14 changes: 14 additions & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$<TARGET_PROPERTY:BINARY_DIR>")
endif()
target_compile_definitions(
codspeed
INTERFACE -DCODSPEED_COMPILE_DIR="${CODSPEED_COMPILE_DIR}"
)

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

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

Expand Down
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