Skip to content

Commit a4bcd93

Browse files
authored
fix version recorded in releases (google#1047)
* cmake: fix handling the case where `git describe` fails * cmake: fix version recorded in releases If downloaded as a tarball release, there will be no info from git to determine the release, so it ends up v0.0.0. If that's the case, we'll now use the release specified in the project() command, which needs to be updated for each new release. * cmake: add `--tags` to `git describe` That way, lightweight tags will also be taken into account, which should never hurt, but it'll help in cases where, for some mysterious reason or other, annotated tags don't make it into a clone. * update releasing.md
1 parent a6a738c commit a4bcd93

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

CMakeLists.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ foreach(p
1313
endif()
1414
endforeach()
1515

16-
project (benchmark CXX)
16+
project (benchmark VERSION 1.5.3 LANGUAGES CXX)
1717

1818
option(BENCHMARK_ENABLE_TESTING "Enable testing of the benchmark library." ON)
1919
option(BENCHMARK_ENABLE_EXCEPTIONS "Enable the use of exceptions in the benchmark library." ON)
@@ -94,8 +94,14 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
9494
include(GetGitVersion)
9595
get_git_version(GIT_VERSION)
9696

97+
# If no git version can be determined, use the version
98+
# from the project() command
99+
if ("${GIT_VERSION}" STREQUAL "0.0.0")
100+
set(VERSION "${benchmark_VERSION}")
101+
else()
102+
set(VERSION "${GIT_VERSION}")
103+
endif()
97104
# Tell the user what versions we are using
98-
string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" VERSION ${GIT_VERSION})
99105
message(STATUS "Version: ${VERSION}")
100106

101107
# The version of the libraries

cmake/GetGitVersion.cmake

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

2121
function(get_git_version var)
2222
if(GIT_EXECUTABLE)
23-
execute_process(COMMAND ${GIT_EXECUTABLE} describe --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
2424
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
2525
RESULT_VARIABLE status
26-
OUTPUT_VARIABLE GIT_VERSION
26+
OUTPUT_VARIABLE GIT_DESCRIBE_VERSION
2727
ERROR_QUIET)
28-
if(${status})
29-
set(GIT_VERSION "v0.0.0")
28+
if(status)
29+
set(GIT_DESCRIBE_VERSION "v0.0.0")
30+
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})
3035
else()
31-
string(STRIP ${GIT_VERSION} GIT_VERSION)
32-
string(REGEX REPLACE "-[0-9]+-g" "-" GIT_VERSION ${GIT_VERSION})
36+
string(REGEX REPLACE "v(.*)" "\\1" GIT_VERSION ${GIT_DESCRIBE_VERSION})
3337
endif()
3438

3539
# Work out if the repository is dirty
@@ -43,12 +47,12 @@ function(get_git_version var)
4347
ERROR_QUIET)
4448
string(COMPARE NOTEQUAL "${GIT_DIFF_INDEX}" "" GIT_DIRTY)
4549
if (${GIT_DIRTY})
46-
set(GIT_VERSION "${GIT_VERSION}-dirty")
50+
set(GIT_DESCRIBE_VERSION "${GIT_DESCRIBE_VERSION}-dirty")
4751
endif()
52+
message(STATUS "git version: ${GIT_DESCRIBE_VERSION} normalized to ${GIT_VERSION}")
4853
else()
49-
set(GIT_VERSION "v0.0.0")
54+
set(GIT_VERSION "0.0.0")
5055
endif()
5156

52-
message(STATUS "git Version: ${GIT_VERSION}")
5357
set(${var} ${GIT_VERSION} PARENT_SCOPE)
5458
endfunction()

docs/releasing.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
* `git log $(git describe --abbrev=0 --tags)..HEAD` gives you the list of
99
commits between the last annotated tag and HEAD
1010
* Pick the most interesting.
11+
* Create one last commit that updates the version saved in `CMakeLists.txt` to the release version you're creating. (This version will be used if benchmark is installed from the archive you'll be creating in the next step.)
12+
13+
```
14+
project (benchmark VERSION 1.5.3 LANGUAGES CXX)
15+
```
16+
1117
* Create a release through github's interface
1218
* Note this will create a lightweight tag.
1319
* Update this to an annotated tag:

0 commit comments

Comments
 (0)