1 # __________________________________________________________________________________ __ _
3 # Obtain version string from git tags via git_describe, create semantic versioning
4 # conform version numbers, and parse result into convenient version variables.
6 # Tags have to match the regexp [^0-9]*([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9a-zA-Z]+),
7 # as in v1.2.3-rc5, where the expressions in parentheses are interpreted as major,
8 # minor, patch and extra (package release) version numbers.
11 # include(GetGitVersion)
12 # get_git_version(VERSION_VARIABLE_PREFIX [<additional args for git_describe>])
14 # Following variables are set by get_git_version:
15 # VERSION_VARIABLE_PREFIX_MAJOR, VERSION_VARIABLE_PREFIX_MINOR,
16 # VERSION_VARIABLE_PREFIX_PATCH, VERSION_VARIABLE_PREFIX_EXTRA,
17 # VERSION_VARIABLE_PREFIX
19 # If the CMake variable BUILD_METADATA is set, VERSION_VARIABLE_PREFIX_EXTRA is
20 # suffixed with "+${BUILD_METADATA}". Note that BUILD_METADATA has to match
21 # the regexp ([0-9a-zA-Z]+).
23 # "THE BEER-WARE LICENSE" (Revision 42):
24 # <cptiglo@treefish.org> wrote this file. As long as you retain this notice
25 # you can do whatever you want with this stuff. If we meet some day, and you
26 # think this stuff is worth it, you can buy me a beer in return.
27 # __________________________________________________________________________________ __ _
29 function(_increment_patch_and_strip_package_release _VERSION_PATCH _VERSION_EXTRA)
30 string(REGEX REPLACE "^[0-9a-zA-Z]+\\.(r[0-9]+\\.g[0-9a-zA-Z]+)$" "\\1"
31 _VERSION_EXTRA "${_VERSION_EXTRA}")
33 MATH(EXPR _VERSION_PATCH "${_VERSION_PATCH}+1")
35 set(VERSION_PATCH "${_VERSION_PATCH}" PARENT_SCOPE)
36 set(VERSION_EXTRA "${_VERSION_EXTRA}" PARENT_SCOPE)
39 function(_set_untagged_commit_flag _VERSION_EXTRA)
40 if("${_VERSION_EXTRA}" MATCHES "^[0-9a-zA-Z]+.r[0-9]+\\.g[0-9a-zA-Z]+$")
41 set(IS_UNTAGGED_COMMIT TRUE PARENT_SCOPE)
42 _increment_patch_and_strip_package_release(${VERSION_PATCH} ${VERSION_EXTRA})
46 function(get_git_version _gitversionprefix)
47 include(GetGitRevisionDescription)
48 git_describe(GIT_DESCRIPTION ${ARGN})
51 "^[^0-9]*([0-9]+)\\..*" "\\1" VERSION_MAJOR "${GIT_DESCRIPTION}")
53 "^[^0-9]*[0-9]+\\.([0-9]+).*" "\\1" VERSION_MINOR "${GIT_DESCRIPTION}")
55 "^[^0-9]*[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" VERSION_PATCH "${GIT_DESCRIPTION}")
57 "^[^0-9]*[0-9]+\\.[0-9]+\\.[0-9]+-([0-9a-zA-Z-]+).*" "\\1" VERSION_EXTRA "${GIT_DESCRIPTION}")
59 "([^-]*-g)" "r\\1" VERSION_EXTRA "${VERSION_EXTRA}")
61 "-" "." VERSION_EXTRA "${VERSION_EXTRA}")
63 _set_untagged_commit_flag(${VERSION_EXTRA})
65 if(${IS_UNTAGGED_COMMIT})
66 _increment_patch_and_strip_package_release(${VERSION_PATCH} ${VERSION_EXTRA})
69 set(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}-${VERSION_EXTRA}")
71 if(DEFINED BUILD_METADATA)
72 set(VERSION "${VERSION}+${BUILD_METADATA}")
75 if(NOT "${VERSION}" MATCHES
76 "^[0-9]+\\.[0-9]+\\.[0-9]+-(([0-9a-zA-Z]+)|(r[0-9]+\\.g[0-9a-zA-Z]+))(\\+[0-9a-zA-Z]+)?$")
79 "\nCOULD NOT EXTRACT VALID VERSION STRING FROM git_describe!"
80 "\n git_describe returned: \"${GIT_DESCRIPTION}\""
81 "\n Extracted invalid version string: \"${VERSION}\"\n")
84 set(${_gitversionprefix} "${VERSION}" PARENT_SCOPE)
85 set(${_gitversionprefix}_MAJOR "${VERSION_MAJOR}" PARENT_SCOPE)
86 set(${_gitversionprefix}_MINOR "${VERSION_MINOR}" PARENT_SCOPE)
87 set(${_gitversionprefix}_PATCH "${VERSION_PATCH}" PARENT_SCOPE)
88 set(${_gitversionprefix}_EXTRA "${VERSION_EXTRA}" PARENT_SCOPE)