Skip to content

Commit 93ab1ab

Browse files
feat: add bazel support
1 parent ea3bd00 commit 93ab1ab

File tree

15 files changed

+252
-7
lines changed

15 files changed

+252
-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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,28 @@ jobs:
3939
with:
4040
name: test_results
4141
path: ${{runner.workspace}}/core/build-tests/test/test-results/**/*.json
42+
43+
cmake-integration-tests:
44+
strategy:
45+
matrix:
46+
codspeed-mode:
47+
- "instrumentation"
48+
- "off"
49+
runs-on: ubuntu-latest
50+
51+
steps:
52+
- name: Checkout code
53+
uses: actions/checkout@v3
54+
55+
- name: Cache build
56+
uses: actions/cache@v3
57+
with:
58+
path: examples/google_benchmark_cmake/build
59+
key: ${{ runner.os }}-build-${{ matrix.codspeed-mode }}-${{ hashFiles('**/CMakeLists.txt', '**/examples/google_benchmark_cmake/**') }}
60+
61+
- name: Build and run benchmarks
62+
run: |
63+
bazel run //examples/google_benchmark:my_benchmark
4264
4365
instrumentation:
4466
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.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ This mono-repo contains the integration libraries for using CodSpeed in C++:
1111

1212
- [`codspeed-google-benchmark`](./google_benchmark/): Google Benchmark compatibility layer for CodSpeed
1313
- [`codspeed-core`](./core/): The CodSpeed core library used to integrate with Codspeed runners
14+
15+
## Usage
16+
17+
The library can be used with both [CMake](https://docs.codspeed.io/benchmarks/cpp#cmake) and [Bazel](https://docs.codspeed.io/benchmarks/cpp#bazel). If you need further information to integrate CodSpeed to your project, please feel free to open an issue or ask for help on our discord server.

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

0 commit comments

Comments
 (0)