From 6a047088cb26248ec783713bb94925638aad8552 Mon Sep 17 00:00:00 2001 From: Manuel Bentele Date: Wed, 17 Mar 2021 09:04:04 +0100 Subject: [BUILD] Fix build issue if version information (Git tag) is missing The software version for packaging purposes is consituted from the following rules: - If the version information (from Git tags or the embedded version header file) is available, the version number for the packaging is set to those found version information. - If there isn't any version information available (e.g. missing Git tags), the version number for the packaging is set to '0.0' to represent an unkown version number. --- CMakeLists.txt | 2 +- cmake/GenerateVersion.cmake | 2 +- cmake/Version.cmake | 70 ++++++++++++++++++++++++++++++++++++--------- inc/dnbd3/version.h.in | 4 +-- 4 files changed, 60 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fb4d5d1..bb613df 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -122,7 +122,7 @@ endif(DNBD3_RELEASE_HARDEN AND CMAKE_BUILD_TYPE MATCHES "Release") # define packaging if Release build is enabled if(CMAKE_BUILD_TYPE MATCHES Release) # get version source package or Git repository - get_repository_version(REPOSITORY_VERSION ${INCLUDE_VERSION_HEADER} ${CMAKE_BUILD_TYPE} ${GIT_EXECUTABLE} ${CMAKE_SOURCE_DIR}) + get_repository_version(REPOSITORY_VERSION REPOSITORY_BRANCH ${INCLUDE_VERSION_HEADER} ${CMAKE_BUILD_TYPE} ${GIT_EXECUTABLE} ${CMAKE_SOURCE_DIR}) # define project version if(KernelHeaders_VERSION) diff --git a/cmake/GenerateVersion.cmake b/cmake/GenerateVersion.cmake index e7c551b..bddd672 100644 --- a/cmake/GenerateVersion.cmake +++ b/cmake/GenerateVersion.cmake @@ -11,7 +11,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} include(Version) # get Git version of Git repository -get_repository_version(DNBD3_VERSION ${VERSION_INPUT_FILE} ${VERSION_BUILD_TYPE} ${GIT_EXECUTABLE} ${REPOSITORY_DIR}) +get_repository_version(DNBD3_VERSION DNBD3_BRANCH ${VERSION_INPUT_FILE} ${VERSION_BUILD_TYPE} ${GIT_EXECUTABLE} ${REPOSITORY_DIR}) # generate version header if header does not exists if(NOT EXISTS ${VERSION_INPUT_FILE}) diff --git a/cmake/Version.cmake b/cmake/Version.cmake index 9beb338..8a6a2fd 100644 --- a/cmake/Version.cmake +++ b/cmake/Version.cmake @@ -31,40 +31,82 @@ macro(gen_project_version VERSION_INPUT_FILE VERSION_INPUT_FILE_TEMPLATE VERSION endmacro(gen_project_version VERSION_INPUT_FILE VERSION_INPUT_FILE_TEMPLATE VERSION_OUTPUT_FILE) # macro to get Git version information -macro(get_repository_version REPOSITORY_VERSION VERSION_HEADER_FILE VERSION_BUILD_TYPE GIT_EXECUTABLE REPOSITORY_DIR) +macro(get_repository_version REPOSITORY_VERSION REPOSITORY_BRANCH VERSION_HEADER_FILE VERSION_BUILD_TYPE GIT_EXECUTABLE REPOSITORY_DIR) + # set empty Git version information + set(GIT_VERSION "") + # set empty Git branch information + set(GIT_BRANCH "") + # check if generated version header from source package is available if(EXISTS ${VERSION_HEADER_FILE}) # get version information from the generated version header of the source package - file(READ ${VERSION_HEADER_FILE} GIT_VERSION) - string(REGEX MATCH "DNBD3_VERSION[ \t]+\"([0-9][A-Za-z0-9.+~-]*)\"" GIT_VERSION ${GIT_VERSION}) + file(READ ${VERSION_HEADER_FILE} GIT_VERSION_VERBOSE) + string(REGEX MATCH "DNBD3_VERSION[ \t]+\"([0-9][A-Za-z0-9.+~-]*)\"" GIT_VERSION ${GIT_VERSION_VERBOSE}) set(GIT_VERSION "${CMAKE_MATCH_1}") + + # get branch information from the generated version header of the source package + file(READ ${VERSION_HEADER_FILE} GIT_BRANCH_VERBOSE) + string(REGEX MATCH "DNBD3_BRANCH[ \t]+\"([0-9][A-Za-z0-9.+~-]*)\"" GIT_BRANCH ${GIT_BRANCH_VERBOSE}) + set(GIT_BRANCH "${CMAKE_MATCH_1}") else(EXISTS ${VERSION_HEADER_FILE}) # get detailed Git version information from Git repository execute_process(COMMAND ${GIT_EXECUTABLE} describe HEAD WORKING_DIRECTORY ${REPOSITORY_DIR} - OUTPUT_VARIABLE GIT_VERSION - OUTPUT_STRIP_TRAILING_WHITESPACE) - execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD - WORKING_DIRECTORY ${REPOSITORY_DIR} - OUTPUT_VARIABLE GIT_BRANCH + OUTPUT_VARIABLE GIT_VERSION_VERBOSE + RESULT_VARIABLE GIT_RETURN_CODE + ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) + # parse version information from repository if Git command succeeds + if(GIT_RETURN_CODE EQUAL 0) + # remove the first letter of the version to satisfy packaging rules + string(REGEX MATCH "([0-9]+:)?[0-9][A-Za-z0-9.+~-]*" GIT_VERSION ${GIT_VERSION_VERBOSE}) + endif(GIT_RETURN_CODE EQUAL 0) + # overwrite version from Git if version is unknown if(GIT_VERSION STREQUAL "") - set(GIT_VERSION "unknown") + # overwrite version information with unknown version 'v0.0' + set(GIT_VERSION "0.0") + + # print a message in Release build configuration to warn about the unknown version + if(${VERSION_BUILD_TYPE} MATCHES "Release") + message(WARNING "The version information from Git tags in this dnbd3 Git repository is missing! Please fetch all Git tags of this repository for a ${VERSION_BUILD_TYPE} build!") + endif(${VERSION_BUILD_TYPE} MATCHES "Release") endif(GIT_VERSION STREQUAL "") + + # get current branch of Git repository + execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD + WORKING_DIRECTORY ${REPOSITORY_DIR} + OUTPUT_VARIABLE GIT_BRANCH_VERBOSE + RESULT_VARIABLE GIT_RETURN_CODE + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE) + + # check output to get branch information + if(GIT_RETURN_CODE EQUAL 0) + set(GIT_BRANCH ${GIT_BRANCH_VERBOSE}) + endif(GIT_RETURN_CODE EQUAL 0) + if(GIT_BRANCH STREQUAL "") + # overwrite branch information with 'unknown' branch set(GIT_BRANCH "unknown") + + # print a message in Release build configuration to warn about the unknown branch + if(${VERSION_BUILD_TYPE} MATCHES "Release") + message(WARNING "The current branch in the dnbd3 Git repository is unknown! Please check the branches of this repository for a ${VERSION_BUILD_TYPE} build!") + endif(${VERSION_BUILD_TYPE} MATCHES "Release") endif(GIT_BRANCH STREQUAL "") # get status of Git repository execute_process(COMMAND ${GIT_EXECUTABLE} status --porcelain WORKING_DIRECTORY ${REPOSITORY_DIR} OUTPUT_VARIABLE GIT_STATUS + RESULT_VARIABLE GIT_RETURN_CODE + ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE) # check if Git repository is dirty - if(NOT GIT_STATUS STREQUAL "") + if(GIT_RETURN_CODE EQUAL 0 AND NOT GIT_STATUS STREQUAL "") # the Git repository is dirty, thus extend the version information set(GIT_VERSION "${GIT_VERSION}+MOD") @@ -72,10 +114,10 @@ macro(get_repository_version REPOSITORY_VERSION VERSION_HEADER_FILE VERSION_BUIL if(${VERSION_BUILD_TYPE} MATCHES "Release") message(WARNING "This dnbd3 Git repository is dirty! Please commit or revert all changes for a ${VERSION_BUILD_TYPE} build!") endif(${VERSION_BUILD_TYPE} MATCHES "Release") - endif(NOT GIT_STATUS STREQUAL "") + endif(GIT_RETURN_CODE EQUAL 0 AND NOT GIT_STATUS STREQUAL "") endif(EXISTS ${VERSION_HEADER_FILE}) - # remove the first letter of the version to satisfy packaging rules - # and return to caller - string(REGEX MATCH "([0-9]+:)?[0-9][A-Za-z0-9.+~-]*" ${REPOSITORY_VERSION} ${GIT_VERSION}) + # return version and branch to caller + set(${REPOSITORY_VERSION} ${GIT_VERSION}) + set(${REPOSITORY_BRANCH} ${GIT_BRANCH}) endmacro(get_repository_version) diff --git a/inc/dnbd3/version.h.in b/inc/dnbd3/version.h.in index a3eff45..727c8b8 100644 --- a/inc/dnbd3/version.h.in +++ b/inc/dnbd3/version.h.in @@ -6,7 +6,7 @@ #define VERSION_H_ #define DNBD3_VERSION "@DNBD3_VERSION@" -#define DNBD3_BRANCH "@GIT_BRANCH@" -#define DNBD3_VERSION_LONG "@GIT_VERSION@, branch @GIT_BRANCH@" +#define DNBD3_BRANCH "@DNBD3_BRANCH@" +#define DNBD3_VERSION_LONG "@GIT_VERSION@, branch @DNBD3_BRANCH@" #endif /* VERSION_H_ */ -- cgit v1.2.3-55-g7522