Skip to content

Commit 27da4d3

Browse files
committed
Add version.txt to the repository and update CMake version detection code.
1 parent 42826fd commit 27da4d3

File tree

3 files changed

+62
-37
lines changed

3 files changed

+62
-37
lines changed

.gitignore

-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ CPackSourceConfig.cmake
2121
DartConfiguration.tcl
2222
_CPack_Packages/
2323

24-
version.txt
25-
2624
# Created by https://www.toptal.com/developers/gitignore/api/c,vim,c++,macos,linux,patch,cmake,emacs,vscode,python,windows,textmate,sublimetext
2725
# Edit at https://www.toptal.com/developers/gitignore?templates=c,vim,c++,macos,linux,patch,cmake,emacs,vscode,python,windows,textmate,sublimetext
2826

cmake/version.cmake

+61-35
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018 Ribose Inc.
1+
# Copyright (c) 2018-2021 Ribose Inc.
22
# All rights reserved.
33
#
44
# Redistribution and use in source and binary forms, with or without
@@ -52,7 +52,7 @@ function(extract_version_info version var_prefix)
5252
# v1.9.0-3-g5b92266-dirty+1546836556
5353
string(REGEX MATCH "^v?([0-9]+\\.[0-9]+\\.[0-9]+)(-([0-9]+)-g([0-9a-f]+)(-dirty)?)?(\\+([0-9]+))?$" matches "${version}")
5454
if (NOT matches)
55-
message(FATAL_ERROR "Failed to extract version components.")
55+
message(FATAL_ERROR "Failed to extract version components from ${version}.")
5656
endif()
5757
set(${var_prefix}_VERSION "${CMAKE_MATCH_1}" PARENT_SCOPE) # 1.9.0
5858
if (NOT CMAKE_MATCH_3)
@@ -76,34 +76,62 @@ function(extract_version_info version var_prefix)
7676
endfunction()
7777

7878
function(determine_version source_dir var_prefix)
79+
set(has_release_tag NO)
80+
set(has_version_txt NO)
81+
set(local_prefix "_determine_ver")
82+
# find out base version via version.txt
83+
set(base_version "v0.0.0")
7984
if (EXISTS "${source_dir}/version.txt")
80-
# same as below, but used for snapshots
81-
file(STRINGS "${source_dir}/version.txt" version)
85+
set(has_version_txt YES)
86+
file(STRINGS "${source_dir}/version.txt" version_file)
87+
extract_version_info("${version_file}" "${local_prefix}")
88+
set(base_version "v${${local_prefix}_VERSION}")
89+
message(STATUS "Found version.txt with ${version_file}")
8290
else()
83-
# for GIT_EXECUTABLE
84-
find_package(Git REQUIRED)
85-
# get a description of the version, something like:
86-
# v1.9.1-0-g38ffe82 (a tagged release)
87-
# v1.9.1-0-g38ffe82-dirty (a tagged release with local modifications)
88-
# v1.9.0-3-g5b92266 (post-release snapshot)
89-
# v1.9.0-3-g5b92266-dirty (post-release snapshot with local modifications)
90-
_git(version describe --abbrev=${GIT_REV_LEN} --match "v[0-9]*" --long --dirty)
91-
if (NOT _git_ec EQUAL 0)
92-
# no annotated tags, fake one
93-
git(revision rev-parse --short=${GIT_REV_LEN} --verify HEAD)
94-
set(version "v0.0.0-0-g${revision}")
91+
message(STATUS "Found no version.txt.")
92+
endif()
93+
# for GIT_EXECUTABLE
94+
find_package(Git REQUIRED)
95+
# get a description of the version, something like:
96+
# v1.9.1-0-g38ffe82 (a tagged release)
97+
# v1.9.1-0-g38ffe82-dirty (a tagged release with local modifications)
98+
# v1.9.0-3-g5b92266 (post-release snapshot)
99+
# v1.9.0-3-g5b92266-dirty (post-release snapshot with local modifications)
100+
_git(version describe --abbrev=${GIT_REV_LEN} --match "v[0-9]*" --long --dirty)
101+
if (NOT _git_ec EQUAL 0)
102+
# no annotated tags, fake one
103+
message(STATUS "Found no annotated tags.")
104+
_git(revision rev-parse --short=${GIT_REV_LEN} --verify HEAD)
105+
if (_git_ec EQUAL 0)
106+
set(version "${base_version}-0-g${revision}")
95107
# check if dirty (this won't detect untracked files, but should be ok)
96108
_git(changes diff-index --quiet HEAD --)
97109
if (NOT _git_ec EQUAL 0)
98110
string(APPEND version "-dirty")
99111
endif()
100112
# append the commit timestamp of the most recent commit (only
101113
# in non-release branches -- typically master)
102-
git(commit_timestamp show -s --format=%ct)
103-
string(APPEND version "+${commit_timestamp}")
114+
_git(commit_timestamp show -s --format=%ct)
115+
if (_git_ec EQUAL 0)
116+
string(APPEND version "+${commit_timestamp}")
117+
endif()
118+
elseif(has_version_txt)
119+
# Nothing to get from git - so use version.txt completely
120+
set(version "${version_file}")
121+
else()
122+
# Sad case - no git, no version.txt
123+
set(version "${base_version}")
124+
endif()
125+
else()
126+
set(has_release_tag YES)
127+
message(STATUS "Found annotated tag ${version}")
128+
if("${has_version_txt}")
129+
string(FIND "${version}" "${base_version}" _base_version_pos)
130+
if("${has_version_txt}" AND NOT "${_base_version_pos}" EQUAL 0)
131+
message(WARNING "Tagged version ${version} doesn't match one from the version.txt: ${base_version}")
132+
endif()
104133
endif()
105134
endif()
106-
set(local_prefix "_determine_ver")
107135
extract_version_info("${version}" "${local_prefix}")
108136
foreach(suffix VERSION VERSION_NCOMMITS VERSION_GIT_REV VERSION_IS_DIRTY VERSION_COMMIT_TIMESTAMP)
109137
if (NOT DEFINED ${local_prefix}_${suffix})
@@ -114,26 +142,24 @@ function(determine_version source_dir var_prefix)
114142
endforeach()
115143
# Set VERSION_SUFFIX and VERSION_FULL. When making changes, be aware that
116144
# this is used in packaging as well and will affect ordering.
117-
# | state | version_full |
118-
# |------------------------------------------------|
119-
# | exact tag | 0.9.0 |
120-
# | exact tag, dirty | 0.9.0+git20180604 |
121-
# | after tag | 0.9.0+git20180604.1.085039f |
122-
# | no tag | 0.0.0+git20180604.2ee02af |
145+
# | state | version_full |
146+
# |-----------------------------------------------------|
147+
# | exact tag | 0.9.0 |
148+
# | exact tag, dirty | 0.9.0+git20180604 |
149+
# | after tag | 0.9.0+git20180604.1.085039f |
150+
# | no tag, version.txt | 0.9.0+git20180604.2ee02af |
151+
# | no tag, no version.txt| 0.0.0+git20180604.2ee02af |
123152
string(TIMESTAMP date "%Y%m%d" UTC)
124153
set(version_suffix "")
125-
if ((NOT ${local_prefix}_VERSION_NCOMMITS EQUAL 0) OR (${local_prefix}_VERSION STREQUAL "0.0.0"))
154+
if (NOT ${local_prefix}_VERSION_NCOMMITS EQUAL 0)
126155
# 0.9.0+git20150604.4.289818b
156+
string(APPEND version_suffix "+git${date}.${${local_prefix}_VERSION_NCOMMITS}.${${local_prefix}_VERSION_GIT_REV}")
157+
elseif ((NOT has_release_tag) AND ((NOT has_version_txt) OR ("${base_version}" STREQUAL "v0.0.0") OR (NOT "${revision}" STREQUAL "")))
158+
# 0.9.0+git20150604.289818b
159+
string(APPEND version_suffix "+git${date}.${${local_prefix}_VERSION_GIT_REV}")
160+
elseif(${local_prefix}_VERSION_IS_DIRTY)
161+
# 0.9.0+git20150604
127162
string(APPEND version_suffix "+git${date}")
128-
if (NOT ${local_prefix}_VERSION_NCOMMITS EQUAL 0)
129-
string(APPEND version_suffix ".${${local_prefix}_VERSION_NCOMMITS}")
130-
endif()
131-
string(APPEND version_suffix ".${${local_prefix}_VERSION_GIT_REV}")
132-
else()
133-
if (${local_prefix}_VERSION_IS_DIRTY)
134-
# 0.9.0+git20150604
135-
string(APPEND version_suffix "+git${date}")
136-
endif()
137163
endif()
138164
set(version_full "${${local_prefix}_VERSION}${version_suffix}")
139165
# set the results

version.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.15.1

0 commit comments

Comments
 (0)