1
- # Copyright (c) 2018 Ribose Inc.
1
+ # Copyright (c) 2018-2021 Ribose Inc.
2
2
# All rights reserved.
3
3
#
4
4
# Redistribution and use in source and binary forms, with or without
@@ -52,7 +52,7 @@ function(extract_version_info version var_prefix)
52
52
# v1.9.0-3-g5b92266-dirty+1546836556
53
53
string (REGEX MATCH "^v?([0-9]+\\ .[0-9]+\\ .[0-9]+)(-([0-9]+)-g([0-9a-f]+)(-dirty)?)?(\\ +([0-9]+))?$" matches "${version} " )
54
54
if (NOT matches )
55
- message (FATAL_ERROR "Failed to extract version components." )
55
+ message (FATAL_ERROR "Failed to extract version components from ${version} ." )
56
56
endif ()
57
57
set (${var_prefix} _VERSION "${CMAKE_MATCH_1} " PARENT_SCOPE) # 1.9.0
58
58
if (NOT CMAKE_MATCH_3)
@@ -76,34 +76,62 @@ function(extract_version_info version var_prefix)
76
76
endfunction ()
77
77
78
78
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" )
79
84
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} " )
82
90
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} " )
95
107
# check if dirty (this won't detect untracked files, but should be ok)
96
108
_git(changes diff-index --quiet HEAD --)
97
109
if (NOT _git_ec EQUAL 0)
98
110
string (APPEND version "-dirty" )
99
111
endif ()
100
112
# append the commit timestamp of the most recent commit (only
101
113
# 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 ()
104
133
endif ()
105
134
endif ()
106
- set (local_prefix "_determine_ver" )
107
135
extract_version_info("${version} " "${local_prefix} " )
108
136
foreach (suffix VERSION VERSION_NCOMMITS VERSION_GIT_REV VERSION_IS_DIRTY VERSION_COMMIT_TIMESTAMP)
109
137
if (NOT DEFINED ${local_prefix} _${suffix} )
@@ -114,26 +142,24 @@ function(determine_version source_dir var_prefix)
114
142
endforeach ()
115
143
# Set VERSION_SUFFIX and VERSION_FULL. When making changes, be aware that
116
144
# 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 |
123
152
string (TIMESTAMP date "%Y%m%d" UTC)
124
153
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)
126
155
# 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
127
162
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 ()
137
163
endif ()
138
164
set (version_full "${${local_prefix} _VERSION}${version_suffix} " )
139
165
# set the results
0 commit comments