]> git.treefish.org Git - cmake-modules/cmake-getgitversion.git/blob - GetGitVersion.cmake
Made versioning semantic versioning conform and added the ability to prepend build...
[cmake-modules/cmake-getgitversion.git] / GetGitVersion.cmake
1 # __________________________________________________________________________________ __ _
2 # get_git_version:
3 # Obtain version string from git tags via git_describe, create semantic versioning
4 # conform version numbers, and parse result into convenient version variables.
5 #
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.
9 #
10 # Usage:
11 # include(GetGitVersion)
12 # get_git_version(VERSION_VARIABLE_PREFIX [<additional args for git_describe>])
13 #
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
18 #
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]+).
22 #
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 # __________________________________________________________________________________ __ _
28
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}")
32
33     MATH(EXPR _VERSION_PATCH "${_VERSION_PATCH}+1")
34
35     set(VERSION_PATCH "${_VERSION_PATCH}" PARENT_SCOPE)
36     set(VERSION_EXTRA "${_VERSION_EXTRA}" PARENT_SCOPE)
37 endfunction()
38
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})
43     endif()
44 endfunction()
45
46 function(get_git_version _gitversionprefix)
47     include(GetGitRevisionDescription)
48     git_describe(GIT_DESCRIPTION ${ARGN})
49
50     string(REGEX REPLACE
51            "^[^0-9]*([0-9]+)\\..*" "\\1" VERSION_MAJOR "${GIT_DESCRIPTION}")
52     string(REGEX REPLACE
53            "^[^0-9]*[0-9]+\\.([0-9]+).*" "\\1" VERSION_MINOR "${GIT_DESCRIPTION}")
54     string(REGEX REPLACE
55            "^[^0-9]*[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" VERSION_PATCH "${GIT_DESCRIPTION}")
56     string(REGEX REPLACE
57            "^[^0-9]*[0-9]+\\.[0-9]+\\.[0-9]+-([0-9a-zA-Z-]+).*" "\\1" VERSION_EXTRA "${GIT_DESCRIPTION}")
58     string(REGEX REPLACE
59            "([^-]*-g)" "r\\1" VERSION_EXTRA "${VERSION_EXTRA}")
60     string(REGEX REPLACE
61            "-" "." VERSION_EXTRA "${VERSION_EXTRA}")
62
63     _set_untagged_commit_flag(${VERSION_EXTRA})
64
65     if(${IS_UNTAGGED_COMMIT})
66         _increment_patch_and_strip_package_release(${VERSION_PATCH} ${VERSION_EXTRA})
67     endif()
68
69     set(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}-${VERSION_EXTRA}")
70
71     if(DEFINED BUILD_METADATA)
72         set(VERSION "${VERSION}+${BUILD_METADATA}")
73     endif()
74
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]+)?$")
77
78         message(FATAL_ERROR 
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")
82     endif()
83
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)
89 endfunction()