]> git.treefish.org Git - cmake-modules/cmake-getgitversion.git/commitdiff
Made versioning semantic versioning conform and added the ability to prepend build... master
authorAlexander Schmidt <alex@treefish.org>
Wed, 25 Nov 2015 22:18:26 +0000 (23:18 +0100)
committerAlexander Schmidt <alex@treefish.org>
Wed, 25 Nov 2015 22:18:26 +0000 (23:18 +0100)
GetGitVersion.cmake

index 8f679d39138e56577cf5129a32b174113d7ebd70..45a5788760a402ebaaa2e9a25a1cb36ac495633e 100644 (file)
@@ -1,9 +1,9 @@
 # __________________________________________________________________________________ __ _
 # get_git_version:
-# Obtain version string from git tags via git_describe and parse result into
-# convenient version variables.
+# Obtain version string from git tags via git_describe, create semantic versioning
+# conform version numbers, and parse result into convenient version variables.
 #
-# Tags have to match the regex ^[^0-9]*([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9a-zA-Z]+),
+# Tags have to match the regex[^0-9]*([0-9]+)\.([0-9]+)\.([0-9]+)-([0-9a-zA-Z]+),
 # as in v1.2.3-rc5, where the expressions in parentheses are interpreted as major,
 # minor, patch and extra (package release) version numbers.
 #
 # VERSION_VARIABLE_PREFIX_PATCH, VERSION_VARIABLE_PREFIX_EXTRA,
 # VERSION_VARIABLE_PREFIX
 #
+# If the CMake variable BUILD_METADATA is set, VERSION_VARIABLE_PREFIX_EXTRA is
+# suffixed with "+${BUILD_METADATA}". Note that BUILD_METADATA has to match
+# the regexp ([0-9a-zA-Z]+).
+#
 # "THE BEER-WARE LICENSE" (Revision 42):
 # <cptiglo@treefish.org> wrote this file.  As long as you retain this notice
 # you can do whatever you want with this stuff. If we meet some day, and you
 # think this stuff is worth it, you can buy me a beer in return.
 # __________________________________________________________________________________ __ _
 
+function(_increment_patch_and_strip_package_release _VERSION_PATCH _VERSION_EXTRA)
+    string(REGEX REPLACE "^[0-9a-zA-Z]+\\.(r[0-9]+\\.g[0-9a-zA-Z]+)$" "\\1"
+           _VERSION_EXTRA "${_VERSION_EXTRA}")
+
+    MATH(EXPR _VERSION_PATCH "${_VERSION_PATCH}+1")
+
+    set(VERSION_PATCH "${_VERSION_PATCH}" PARENT_SCOPE)
+    set(VERSION_EXTRA "${_VERSION_EXTRA}" PARENT_SCOPE)
+endfunction()
+
+function(_set_untagged_commit_flag _VERSION_EXTRA)
+    if("${_VERSION_EXTRA}" MATCHES "^[0-9a-zA-Z]+.r[0-9]+\\.g[0-9a-zA-Z]+$")
+        set(IS_UNTAGGED_COMMIT TRUE PARENT_SCOPE)
+        _increment_patch_and_strip_package_release(${VERSION_PATCH} ${VERSION_EXTRA})
+    endif()
+endfunction()
+
 function(get_git_version _gitversionprefix)
     include(GetGitRevisionDescription)
     git_describe(GIT_DESCRIPTION ${ARGN})
@@ -38,19 +59,27 @@ function(get_git_version _gitversionprefix)
            "([^-]*-g)" "r\\1" VERSION_EXTRA "${VERSION_EXTRA}")
     string(REGEX REPLACE
            "-" "." VERSION_EXTRA "${VERSION_EXTRA}")
-          
+
+    _set_untagged_commit_flag(${VERSION_EXTRA})
+
+    if(${IS_UNTAGGED_COMMIT})
+        _increment_patch_and_strip_package_release(${VERSION_PATCH} ${VERSION_EXTRA})
+    endif()
+
     set(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}-${VERSION_EXTRA}")
 
+    if(DEFINED BUILD_METADATA)
+        set(VERSION "${VERSION}+${BUILD_METADATA}")
+    endif()
+
     if(NOT "${VERSION}" MATCHES
-       "^[0-9]+\\.[0-9]+\\.[0-9]+-[0-9a-zA-Z]+\\.?r?[0-9]*\\.?g?[0-9a-zA-Z]*$")
+       "^[0-9]+\\.[0-9]+\\.[0-9]+-(([0-9a-zA-Z]+)|(r[0-9]+\\.g[0-9a-zA-Z]+))(\\+[0-9a-zA-Z]+)?$")
 
         message(FATAL_ERROR 
                 "\nCOULD NOT EXTRACT VALID VERSION STRING FROM git_describe!"
                 "\n git_describe returned: \"${GIT_DESCRIPTION}\""
                 "\n Extracted invalid version string: \"${VERSION}\"\n")
-
-    endif(NOT "${VERSION}" MATCHES
-          "^[0-9]+\\.[0-9]+\\.[0-9]+-[0-9a-zA-Z]+\\.?r?[0-9]*\\.?g?[0-9a-zA-Z]*$")
+    endif()
 
     set(${_gitversionprefix}       "${VERSION}"       PARENT_SCOPE)
     set(${_gitversionprefix}_MAJOR "${VERSION_MAJOR}" PARENT_SCOPE)