Skip to content

Commit 17bc235

Browse files
LebedevRIdmah42
andauthored
Output library / schema versions in JSON context block (#1742)
* CMake: `get_git_version()`: just use `--dirty` flag of `git describe` * CMake: move version normalization out of `get_git_version()` Mainly, i want `get_git_version()` to return true version, not something sanitized. * JSON reporter: store library version and schema version in `context` * Tools: discard inputs with unexpected `json_schema_version` * Extract version string into `GetBenchmarkVersiom()` --------- Co-authored-by: dominic <510002+dmah42@users.noreply.github.com>
1 parent 8e2d258 commit 17bc235

8 files changed

Lines changed: 55 additions & 30 deletions

File tree

CMakeLists.txt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,26 @@ get_git_version(GIT_VERSION)
105105
# If no git version can be determined, use the version
106106
# from the project() command
107107
if ("${GIT_VERSION}" STREQUAL "0.0.0")
108-
set(VERSION "${benchmark_VERSION}")
108+
set(VERSION "v${benchmark_VERSION}")
109109
else()
110110
set(VERSION "${GIT_VERSION}")
111111
endif()
112+
113+
# Normalize version: drop "v" prefix, replace first "-" with ".",
114+
# drop everything after second "-" (including said "-").
115+
string(STRIP ${VERSION} VERSION)
116+
if(VERSION MATCHES v[^-]*-)
117+
string(REGEX REPLACE "v([^-]*)-([0-9]+)-.*" "\\1.\\2" NORMALIZED_VERSION ${VERSION})
118+
else()
119+
string(REGEX REPLACE "v(.*)" "\\1" NORMALIZED_VERSION ${VERSION})
120+
endif()
121+
112122
# Tell the user what versions we are using
113-
message(STATUS "Google Benchmark version: ${VERSION}")
123+
message(STATUS "Google Benchmark version: ${VERSION}, normalized to ${NORMALIZED_VERSION}")
114124

115125
# The version of the libraries
116-
set(GENERIC_LIB_VERSION ${VERSION})
117-
string(SUBSTRING ${VERSION} 0 1 GENERIC_LIB_SOVERSION)
126+
set(GENERIC_LIB_VERSION ${NORMALIZED_VERSION})
127+
string(SUBSTRING ${NORMALIZED_VERSION} 0 1 GENERIC_LIB_SOVERSION)
118128

119129
# Import our CMake modules
120130
include(AddCXXCompilerFlag)

cmake/GetGitVersion.cmake

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,16 @@ set(__get_git_version INCLUDED)
2020

2121
function(get_git_version var)
2222
if(GIT_EXECUTABLE)
23-
execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --match "v[0-9]*.[0-9]*.[0-9]*" --abbrev=8
23+
execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --match "v[0-9]*.[0-9]*.[0-9]*" --abbrev=8 --dirty
2424
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
2525
RESULT_VARIABLE status
26-
OUTPUT_VARIABLE GIT_DESCRIBE_VERSION
26+
OUTPUT_VARIABLE GIT_VERSION
2727
ERROR_QUIET)
2828
if(status)
29-
set(GIT_DESCRIBE_VERSION "v0.0.0")
29+
set(GIT_VERSION "v0.0.0")
3030
endif()
31-
32-
string(STRIP ${GIT_DESCRIBE_VERSION} GIT_DESCRIBE_VERSION)
33-
if(GIT_DESCRIBE_VERSION MATCHES v[^-]*-)
34-
string(REGEX REPLACE "v([^-]*)-([0-9]+)-.*" "\\1.\\2" GIT_VERSION ${GIT_DESCRIBE_VERSION})
35-
else()
36-
string(REGEX REPLACE "v(.*)" "\\1" GIT_VERSION ${GIT_DESCRIBE_VERSION})
37-
endif()
38-
39-
# Work out if the repository is dirty
40-
execute_process(COMMAND ${GIT_EXECUTABLE} update-index -q --refresh
41-
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
42-
OUTPUT_QUIET
43-
ERROR_QUIET)
44-
execute_process(COMMAND ${GIT_EXECUTABLE} diff-index --name-only HEAD --
45-
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
46-
OUTPUT_VARIABLE GIT_DIFF_INDEX
47-
ERROR_QUIET)
48-
string(COMPARE NOTEQUAL "${GIT_DIFF_INDEX}" "" GIT_DIRTY)
49-
if (${GIT_DIRTY})
50-
set(GIT_DESCRIBE_VERSION "${GIT_DESCRIBE_VERSION}-dirty")
51-
endif()
52-
message(STATUS "git version: ${GIT_DESCRIBE_VERSION} normalized to ${GIT_VERSION}")
5331
else()
54-
set(GIT_VERSION "0.0.0")
32+
set(GIT_VERSION "v0.0.0")
5533
endif()
5634

5735
set(${var} ${GIT_VERSION} PARENT_SCOPE)

include/benchmark/benchmark.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ class BenchmarkReporter;
302302
// Default number of minimum benchmark running time in seconds.
303303
const char kDefaultMinTimeStr[] = "0.5s";
304304

305+
// Returns the version of the library.
306+
BENCHMARK_EXPORT std::string GetBenchmarkVersiom();
307+
305308
BENCHMARK_EXPORT void PrintDefaultHelp();
306309

307310
BENCHMARK_EXPORT void Initialize(int* argc, char** argv,

src/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ target_include_directories(benchmark PUBLIC
2828
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
2929
)
3030

31+
set_property(
32+
SOURCE benchmark.cc
33+
APPEND
34+
PROPERTY COMPILE_DEFINITIONS
35+
BENCHMARK_VERSION="${VERSION}"
36+
)
37+
3138
# libpfm, if available
3239
if (PFM_FOUND)
3340
target_link_libraries(benchmark PRIVATE PFM::libpfm)

src/benchmark.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,14 @@ int InitializeStreams() {
748748

749749
} // end namespace internal
750750

751+
std::string GetBenchmarkVersiom() {
752+
#if defined(BENCHMARK_VERSION)
753+
return {BENCHMARK_VERSION};
754+
#else
755+
return "hello, bazel!";
756+
#endif
757+
}
758+
751759
void PrintDefaultHelp() {
752760
fprintf(stdout,
753761
"benchmark"

src/json_reporter.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,19 @@ bool JSONReporter::ReportContext(const Context& context) {
167167
}
168168
out << "],\n";
169169

170+
out << indent << FormatKV("library_version", GetBenchmarkVersiom());
171+
out << ",\n";
172+
170173
#if defined(NDEBUG)
171174
const char build_type[] = "release";
172175
#else
173176
const char build_type[] = "debug";
174177
#endif
175178
out << indent << FormatKV("library_build_type", build_type);
179+
out << ",\n";
180+
181+
// NOTE: our json schema is not strictly tied to the library version!
182+
out << indent << FormatKV("json_schema_version", int64_t(1));
176183

177184
std::map<std::string, std::string>* global_context =
178185
internal::GetGlobalContext();

test/reporter_output_test.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ static int AddContextCases() {
5555
{{"Load Average: (%float, ){0,2}%float$", MR_Next}});
5656
}
5757
AddCases(TC_JSONOut, {{"\"load_avg\": \\[(%float,?){0,3}],$", MR_Next}});
58+
AddCases(TC_JSONOut, {{"\"library_version\": \".*\",$", MR_Next}});
59+
AddCases(TC_JSONOut, {{"\"library_build_type\": \".*\",$", MR_Next}});
60+
AddCases(TC_JSONOut, {{"\"json_schema_version\": 1$", MR_Next}});
5861
return 0;
5962
}
6063
int dummy_register = AddContextCases();

tools/gbench/util.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,15 @@ def benchmark_wanted(benchmark):
135135

136136
with open(fname, "r") as f:
137137
results = json.load(f)
138+
if "context" in results:
139+
if "json_schema_version" in results["context"]:
140+
json_schema_version = results["context"]["json_schema_version"]
141+
if json_schema_version != 1:
142+
print(
143+
"In %s, got unnsupported JSON schema version: %i, expected 1"
144+
% (fname, json_schema_version)
145+
)
146+
sys.exit(1)
138147
if "benchmarks" in results:
139148
results["benchmarks"] = list(
140149
filter(benchmark_wanted, results["benchmarks"])

0 commit comments

Comments
 (0)