Browse Source

ci: Auto-generate version numbers from git describe output

Stefan Profanter 4 years ago
parent
commit
7a651fcc86
4 changed files with 99 additions and 17 deletions
  1. 4 0
      .travis.yml
  2. 6 16
      CMakeLists.txt
  3. 3 1
      appveyor.yml
  4. 86 0
      tools/cmake/SetGitBasedVersion.cmake

+ 4 - 0
.travis.yml

@@ -9,6 +9,10 @@ env:
 
 dist: trusty
 
+# Make sure we have the git tags. Travis uses shallow checkout where git describe does not work
+git:
+  depth: false
+
 matrix:
   fast_finish: true
   include:

+ 6 - 16
CMakeLists.txt

@@ -32,22 +32,12 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
 # Version #
 ###########
 
-set(OPEN62541_VER_MAJOR 1)
-set(OPEN62541_VER_MINOR 0)
-set(OPEN62541_VER_PATCH 0)
-set(OPEN62541_VER_LABEL "-rc2") # Appended to the X.Y.Z version format. For example "-rc1" or an empty string
-
-# Set OPEN62541_VER_COMMIT
-if(GIT_FOUND)
-    execute_process(COMMAND ${GIT_EXECUTABLE} describe --always --tags
-                    RESULT_VARIABLE res_var OUTPUT_VARIABLE GIT_COM_ID WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
-    if(${res_var} EQUAL 0 AND NOT OPEN62541_VER_COMMIT STREQUAL "")
-        string(REPLACE "\n" "" OPEN62541_VER_COMMIT ${GIT_COM_ID} )
-    endif()
-endif()
-if(NOT OPEN62541_VER_COMMIT OR OPEN62541_VER_COMMIT STREQUAL "")
-    set(OPEN62541_VER_COMMIT "undefined")
-endif()
+include(SetGitBasedVersion)
+
+# The version string will be automatically set based on the git describe output.
+# This will automate the version strings. Just create a new tag, and the version will be set correctly.
+set_open62541_version()
+MESSAGE(STATUS "open62541 Version: ${OPEN62541_VER_MAJOR}.${OPEN62541_VER_MINOR}.${OPEN62541_VER_PATCH}${OPEN62541_VER_LABEL}")
 
 #################
 # Build Options #

+ 3 - 1
appveyor.yml

@@ -1,7 +1,9 @@
 version: '{build}'
 
 clone_folder: c:\projects\open62541
-clone_depth: 20
+
+# Do not set clone depth, we need all the tags to automatically detect the version based on git describe
+# clone_depth: 20
 
 # Avoid building branch if it is part of a PR and built anyways
 skip_branch_with_pr: true

+ 86 - 0
tools/cmake/SetGitBasedVersion.cmake

@@ -0,0 +1,86 @@
+get_filename_component(VERSION_SRC_DIR ${CMAKE_CURRENT_LIST_DIR} DIRECTORY)
+set(VERSION_SRC_DIR "${VERSION_SRC_DIR}/..")
+
+find_package(Git)
+
+function(set_open62541_version)
+
+    # Generate a git-describe version string from Git repository tags
+    if(GIT_EXECUTABLE AND NOT DEFINED OPEN62541_VERSION)
+        execute_process(
+            COMMAND ${GIT_EXECUTABLE} describe --tags --dirty --match "v*"
+            WORKING_DIRECTORY "${VERSION_SRC_DIR}"
+            OUTPUT_VARIABLE GIT_DESCRIBE_VERSION
+            RESULT_VARIABLE GIT_DESCRIBE_ERROR_CODE
+            OUTPUT_STRIP_TRAILING_WHITESPACE
+        )
+        if(NOT GIT_DESCRIBE_ERROR_CODE)
+            set(OPEN62541_VERSION ${GIT_DESCRIBE_VERSION})
+
+            # Example values can be:
+            # v1.2
+            # v1.2.3
+            # v1.2.3-rc1
+            # v1.2.3-rc1-dirty
+            # v1.2.3-5-g4538abcd
+            # v1.2.3-5-g4538abcd-dirty
+
+            STRING(REGEX REPLACE "^(v[0-9\\.]+)(.*)$"
+                   "\\1"
+                   GIT_VERSION_NUMBERS
+                   "${GIT_DESCRIBE_VERSION}" )
+            STRING(REGEX REPLACE "^v([0-9\\.]+)(.*)$"
+                   "\\2"
+                   GIT_VERSION_LABEL
+                   "${GIT_DESCRIBE_VERSION}" )
+
+            if("${GIT_VERSION_NUMBERS}" MATCHES "^v([0-9]+)(.*)$")
+                STRING(REGEX REPLACE "^v([0-9]+)\\.?(.*)$"
+                       "\\1"
+                       GIT_VER_MAJOR
+                       "${GIT_VERSION_NUMBERS}" )
+                if("${GIT_VERSION_NUMBERS}" MATCHES "^v([0-9]+)\\.([0-9]+)(.*)$")
+                    STRING(REGEX REPLACE "^v([0-9]+)\\.([0-9]+)(.*)$"
+                           "\\2"
+                           GIT_VER_MINOR
+                           "${GIT_VERSION_NUMBERS}" )
+                    if("${GIT_VERSION_NUMBERS}" MATCHES "^v([0-9]+)\\.([0-9]+)\\.([0-9]+)(.*)$")
+                        STRING(REGEX REPLACE "^v([0-9]+)\\.([0-9]+)\\.([0-9]+)(.*)$"
+                               "\\3"
+                               GIT_VER_PATCH
+                               "${GIT_VERSION_NUMBERS}" )
+                    else()
+                        set(GIT_VER_PATCH 0)
+                    endif()
+                else()
+                    set(GIT_VER_MINOR 0)
+                    set(GIT_VER_PATCH 0)
+                endif()
+
+            else()
+                set(GIT_VER_MAJOR 0)
+                set(GIT_VER_MINOR 0)
+                set(GIT_VER_PATCH 0)
+            endif()
+            set(OPEN62541_VER_MAJOR ${GIT_VER_MAJOR} PARENT_SCOPE)
+            set(OPEN62541_VER_MINOR ${GIT_VER_MINOR} PARENT_SCOPE)
+            set(OPEN62541_VER_PATCH ${GIT_VER_PATCH} PARENT_SCOPE)
+            set(OPEN62541_VER_LABEL "${GIT_VERSION_LABEL}" PARENT_SCOPE)
+        endif()
+        set(OPEN62541_VER_COMMIT ${GIT_DESCRIBE_VERSION} PARENT_SCOPE)
+
+
+    endif()
+
+    # Final fallback: Just use a bogus version string that is semantically older
+    # than anything else and spit out a warning to the developer.
+    if(NOT DEFINED OPEN62541_VERSION)
+        set(OPEN62541_VERSION "v0.0.0-unknown")
+        set(OPEN62541_VER_MAJOR 0 PARENT_SCOPE)
+        set(OPEN62541_VER_MINOR 0 PARENT_SCOPE)
+        set(OPEN62541_VER_PATCH 0 PARENT_SCOPE)
+        set(OPEN62541_VER_LABEL "-unknown" PARENT_SCOPE)
+        set(OPEN62541_VER_COMMIT "undefined" PARENT_SCOPE)
+        message(WARNING "Failed to determine OPEN62541_VERSION from repository tags. Using default version \"${OPEN62541_VERSION}\".")
+    endif()
+endfunction()