Skip to content

Commit 6ee3d20

Browse files
feat(google_benchmark): expose codspeed mode as a cmake option
1 parent 9703c4f commit 6ee3d20

File tree

2 files changed

+67
-47
lines changed

2 files changed

+67
-47
lines changed

core/CMakeLists.txt

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,83 @@ add_library(codspeed src/codspeed.cpp src/walltime.cpp src/uri.cpp)
1717
# Version
1818
add_compile_definitions(CODSPEED_VERSION="${CODSPEED_VERSION}")
1919

20+
# Specify the include directories for users of the library
21+
target_include_directories(
22+
codspeed
23+
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
24+
)
25+
2026
# Disable valgrind compilation errors
2127
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
2228
# Disable the old-style-cast warning for the specific target
2329
target_compile_options(codspeed PRIVATE -Wno-old-style-cast)
2430
endif()
2531

26-
# Specify the include directories for users of the library
27-
target_include_directories(
32+
execute_process(
33+
COMMAND git rev-parse --show-toplevel
34+
OUTPUT_VARIABLE GIT_ROOT_DIR
35+
OUTPUT_STRIP_TRAILING_WHITESPACE
36+
RESULT_VARIABLE GIT_COMMAND_RESULT
37+
)
38+
39+
if(NOT GIT_COMMAND_RESULT EQUAL 0)
40+
message(
41+
WARNING
42+
"Failed to determine the git root directory.\
43+
Check that git is in your PATH, and that you are in a git repository.\
44+
Continuing, but codspeed features will not be useable"
45+
)
46+
# Default to user's cmake source directory
47+
set(GIT_ROOT_DIR ${CMAKE_SOURCE_DIR})
48+
endif()
49+
50+
target_compile_definitions(
2851
codspeed
29-
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
52+
INTERFACE -DCODSPEED_GIT_ROOT_DIR="${GIT_ROOT_DIR}"
53+
)
54+
55+
set(CODSPEED_MODE_ALLOWED_VALUES "OFF" "instrumentation" "walltime")
56+
set(CODSPEED_MODE "OFF" CACHE STRING "Build mode for Codspeed")
57+
set_property(
58+
CACHE CODSPEED_MODE
59+
PROPERTY STRINGS ${CODSPEED_MODE_ALLOWED_VALUES}
3060
)
3161

62+
if(NOT CODSPEED_MODE STREQUAL "OFF")
63+
target_compile_definitions(codspeed INTERFACE -DCODSPEED_ENABLED)
64+
65+
if(NOT CMAKE_BUILD_TYPE)
66+
message(
67+
WARNING
68+
"CMAKE_BUILD_TYPE is not set. Consider setting it to 'RelWithDebInfo'."
69+
)
70+
elseif(NOT CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
71+
message(
72+
WARNING
73+
"CMAKE_BUILD_TYPE is set to '${CMAKE_BUILD_TYPE}', but 'RelWithDebInfo' is recommended."
74+
)
75+
endif()
76+
77+
# Define a preprocessor macro based on the build mode
78+
if(CODSPEED_MODE STREQUAL "instrumentation")
79+
target_compile_definitions(
80+
codspeed
81+
INTERFACE -DCODSPEED_INSTRUMENTATION
82+
)
83+
elseif(CODSPEED_MODE STREQUAL "walltime")
84+
target_compile_definitions(codspeed INTERFACE -DCODSPEED_WALLTIME)
85+
else()
86+
message(
87+
FATAL_ERROR
88+
"Invalid build mode: ${CODSPEED_MODE}. Use one of ${CODSPEED_MODE_ALLOWED_VALUES}."
89+
)
90+
endif()
91+
endif()
92+
93+
message(STATUS "Codspeed mode: ${CODSPEED_MODE}")
94+
3295
option(ENABLE_TESTS "Enable building the unit tests which depend on gtest" OFF)
3396
if(ENABLE_TESTS)
34-
enable_testing()
97+
enable_testing()
3598
add_subdirectory(test)
3699
endif()

google_benchmark/cmake/Codspeed.cmake

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1 @@
11
add_subdirectory(${PROJECT_SOURCE_DIR}/../core codspeed)
2-
3-
execute_process(
4-
COMMAND git rev-parse --show-toplevel
5-
OUTPUT_VARIABLE GIT_ROOT_DIR
6-
OUTPUT_STRIP_TRAILING_WHITESPACE
7-
RESULT_VARIABLE GIT_COMMAND_RESULT
8-
)
9-
10-
if(NOT GIT_COMMAND_RESULT EQUAL 0)
11-
message(
12-
WARNING
13-
"Failed to determine the git root directory.\
14-
Check that git is in your PATH, and that you are in a git repository.\
15-
Continuing, but codspeed features will not be useable"
16-
)
17-
# Default to user's cmake source directory
18-
set(GIT_ROOT_DIR ${CMAKE_SOURCE_DIR})
19-
endif()
20-
21-
target_compile_definitions(
22-
codspeed
23-
INTERFACE -DCODSPEED_GIT_ROOT_DIR="${GIT_ROOT_DIR}"
24-
)
25-
26-
if(DEFINED CODSPEED_MODE)
27-
target_compile_definitions(codspeed INTERFACE -DCODSPEED_ENABLED)
28-
# Define a preprocessor macro based on the build mode
29-
if(CODSPEED_MODE STREQUAL "instrumentation")
30-
target_compile_definitions(
31-
codspeed
32-
INTERFACE -DCODSPEED_INSTRUMENTATION
33-
)
34-
elseif(CODSPEED_MODE STREQUAL "walltime")
35-
target_compile_definitions(codspeed INTERFACE -DCODSPEED_WALLTIME)
36-
else()
37-
message(
38-
FATAL_ERROR
39-
"Invalid build mode: ${CODSPEED_MODE}. Use 'instrumentation' or 'walltime'."
40-
)
41-
endif()
42-
endif()
43-
44-
message(STATUS "Build mode set to: ${CODSPEED_MODE}")

0 commit comments

Comments
 (0)