Skip to content

Commit 2e8e019

Browse files
feat: add bazel support
1 parent 680721a commit 2e8e019

File tree

14 files changed

+246
-7
lines changed

14 files changed

+246
-7
lines changed

.bazelrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build --//core:codspeed_mode=instrumentation

.bazelversion

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.6.1

.github/workflows/ci.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,26 @@ jobs:
3939
with:
4040
name: test_results
4141
path: ${{runner.workspace}}/core/build-tests/test/test-results/**/*.json
42+
43+
bazel-build:
44+
name: Build Bazel benchmarks
45+
runs-on: ubuntu-latest
46+
steps:
47+
- uses: actions/checkout@v4
48+
49+
- name: Set up Bazel
50+
uses: bazel-contrib/setup-bazel@0.14.0
51+
with:
52+
# Avoid downloading Bazel every time.
53+
bazelisk-cache: true
54+
# Store build cache per workflow.
55+
disk-cache: ${{ github.workflow }}
56+
# Share repository cache between workflows.
57+
repository-cache: true
58+
59+
- name: Build and run benchmarks
60+
run: |
61+
bazel run //examples/google_benchmar:my_benchmark
4262
4363
instrumentation:
4464
runs-on: ubuntu-latest

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ build/
77

88
# Clangd cache
99
.cache/
10+
11+
# Bazel output
12+
/bazel-*

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bazel_dep(name = "rules_cc", version = "0.0.17")

MODULE.bazel.lock

Lines changed: 126 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/BUILD

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
2+
load("@rules_cc//cc:defs.bzl", "cc_library")
3+
4+
CODSPEED_VERSION = "1.0.0"
5+
6+
# Define the codspeed library
7+
cc_library(
8+
name = "codspeed",
9+
srcs = glob(["src/**/*.cpp"] + ["src/**/*.h"] + ["src/**/*.hpp"]),
10+
hdrs = ["include/codspeed.h"],
11+
includes = ["include"],
12+
defines = [
13+
"CODSPEED_VERSION=\\\"{}\\\"".format(CODSPEED_VERSION),
14+
] + select({
15+
":instrumentation_mode": ["CODSPEED_ENABLED", "CODSPEED_INSTRUMENTATION"],
16+
":walltime_mode": ["CODSPEED_ENABLED", "CODSPEED_WALLTIME"],
17+
"//conditions:default": [],
18+
}),
19+
visibility = ["//visibility:public"],
20+
)
21+
22+
# Codspeed mode
23+
string_flag(
24+
name = "codspeed_mode",
25+
build_setting_default = "off",
26+
values = [
27+
"off",
28+
"instrumentation",
29+
"walltime",
30+
],
31+
)
32+
33+
config_setting(
34+
name = "instrumentation_mode",
35+
flag_values = {":codspeed_mode": "instrumentation"},
36+
)
37+
38+
config_setting(
39+
name = "walltime_mode",
40+
flag_values = {":codspeed_mode": "walltime"},
41+
)

core/CMakeLists.txt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ set(CMAKE_CXX_STANDARD_REQUIRED True)
1212
include_directories(include)
1313

1414
# Add the library
15-
add_library(codspeed src/codspeed.cpp src/walltime.cpp src/uri.cpp)
15+
add_library(
16+
codspeed
17+
src/codspeed.cpp
18+
src/walltime.cpp
19+
src/uri.cpp
20+
src/workspace.cpp
21+
)
1622

1723
# Version
1824
add_compile_definitions(CODSPEED_VERSION="${CODSPEED_VERSION}")
@@ -31,7 +37,7 @@ endif()
3137

3238
execute_process(
3339
COMMAND git rev-parse --show-toplevel
34-
OUTPUT_VARIABLE GIT_ROOT_DIR
40+
OUTPUT_VARIABLE CODSPEED_ROOT_DIR
3541
OUTPUT_STRIP_TRAILING_WHITESPACE
3642
RESULT_VARIABLE GIT_COMMAND_RESULT
3743
)
@@ -44,13 +50,14 @@ if(NOT GIT_COMMAND_RESULT EQUAL 0)
4450
Continuing, but codspeed features will not be useable"
4551
)
4652
# Default to user's cmake source directory
47-
set(GIT_ROOT_DIR ${CMAKE_SOURCE_DIR})
53+
set(CODSPEED_ROOT_DIR ${CMAKE_SOURCE_DIR})
4854
endif()
4955

5056
target_compile_definitions(
5157
codspeed
52-
INTERFACE -DCODSPEED_GIT_ROOT_DIR="${GIT_ROOT_DIR}"
58+
PRIVATE -DCODSPEED_ROOT_DIR="${CODSPEED_ROOT_DIR}"
5359
)
60+
message(STATUS "Using codspeed root directory: ${CODSPEED_ROOT_DIR}")
5461

5562
set(CODSPEED_MODE_ALLOWED_VALUES "OFF" "instrumentation" "walltime")
5663
set(CODSPEED_MODE "OFF" CACHE STRING "Build mode for Codspeed")

core/include/codspeed.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ void generate_codspeed_walltime_report(
4646
std::string extract_lambda_namespace(const std::string &pretty_func);
4747
std::string sanitize_bench_args(std::string &text);
4848

49+
// Gets path relative to workspace root, expected to be called with __FILE__ as
50+
// an argument
51+
std::string get_path_relative_to_workspace(const std::string &path);
52+
4953
} // namespace codspeed
5054

5155
#endif // CODSPEED_H

core/src/workspace.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Implementation
2+
#include "codspeed.h"
3+
#include <cstdlib>
4+
#include <filesystem>
5+
6+
namespace codspeed {
7+
8+
std::string get_path_relative_to_workspace(const std::string &path) {
9+
// 1. Check for bazel usage, through the BUILD_WORKSPACE_DIRECTORY env var
10+
// If so, __FILE__ will already be relative to the bazel workspace root
11+
if (std::getenv("BUILD_WORKSPACE_DIRECTORY") != NULL) {
12+
return path;
13+
}
14+
15+
// 2. If defined, use the specificed value directly
16+
#ifdef CODSPEED_ROOT_DIR
17+
return std::filesystem::relative(path, CODSPEED_ROOT_DIR).string();
18+
#endif
19+
20+
// 3. Fallback to bath relative to PWD
21+
return std::filesystem::relative(path, std::filesystem::current_path())
22+
.string();
23+
}
24+
25+
} // namespace codspeed

examples/google_benchmark/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cc_binary(
2+
name = "my_benchmark",
3+
srcs = glob(["*.cpp", "*.hpp"]),
4+
deps = [
5+
"//google_benchmark:benchmark",
6+
],
7+
)

flake.nix

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
# Common build inputs for both shells
2121
commonBuildInputs = with pkgs; [
2222
gcc
23-
cmake
2423
pkg-config
24+
25+
# Build systems
26+
cmake
27+
bazelisk
2528
];
2629

2730
in

google_benchmark/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ cc_library(
7777
"_LARGEFILE_SOURCE",
7878
],
7979
visibility = ["//visibility:public"],
80-
deps = select({
80+
deps = ["//core:codspeed"] + select({
8181
":perfcounters": ["@libpfm"],
8282
"//conditions:default": [],
8383
}),

google_benchmark/include/benchmark/benchmark.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1444,7 +1444,7 @@ class Fixture : public internal::Benchmark {
14441444
#include <filesystem>
14451445

14461446
#define CUR_FILE \
1447-
std::filesystem::relative(__FILE__, CODSPEED_GIT_ROOT_DIR).string() + "::"
1447+
codspeed::get_path_relative_to_workspace(__FILE__) + "::"
14481448
#define NAMESPACE \
14491449
(([]() { return codspeed::extract_lambda_namespace(__PRETTY_FUNCTION__); })())
14501450
#define STATIC_NAMESPACE_STRING(name) static std::string name = NAMESPACE;

0 commit comments

Comments
 (0)