Skip to content
Snippets Groups Projects
Commit 00962b0a authored by Corné Lukken's avatar Corné Lukken
Browse files

CWG-45: Finish cleaning up c++ versioning

parent 00fce5db
No related branches found
No related tags found
1 merge request!9CWG-45: Implement git tag based version extraction
Pipeline #63808 waiting for manual action
Pipeline: C++ project

#63809

    ...@@ -7,12 +7,9 @@ include(GitVersion) ...@@ -7,12 +7,9 @@ include(GitVersion)
    get_version_info({{cookiecutter.project_slug}} "${CMAKE_CURRENT_SOURCE_DIR}") get_version_info({{cookiecutter.project_slug}} "${CMAKE_CURRENT_SOURCE_DIR}")
    set(project_slug {{cookiecutter.project_slug}}) set(project_slug {{cookiecutter.project_slug}})
    set(${project_slug}_VERSION "${${project_slug}_VERSION_MAJOR}.${${project_slug}_VERSION_MINOR}.${${project_slug}_VERSION_PATCH}")
    project({{cookiecutter.project_slug}} VERSION ${${project_slug}_VERSION}) project({{cookiecutter.project_slug}} VERSION ${${project_slug}_VERSION})
    message("VERSION: ${${project_slug}_VERSION}")
    set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
    set(CMAKE_CXX_STANDARD_REQUIRED YES) set(CMAKE_CXX_STANDARD_REQUIRED YES)
    set(CMAKE_CXX_EXTENSIONS OFF) set(CMAKE_CXX_EXTENSIONS OFF)
    ......
    ...@@ -36,6 +36,15 @@ For instance: ...@@ -36,6 +36,15 @@ For instance:
    - Install the project globally: ``ninja -C ./build/ install`` - Install the project globally: ``ninja -C ./build/ install``
    - Clean caches: `ninja -C ./build/ clean` - Clean caches: `ninja -C ./build/ clean`
    ## Versioning
    This project uses git tags or branch names to determine the current version of the
    software. The version information is automatically made available in CMake and can be
    made available in C/C++ by dynamically adding a generated header.
    See for details:
    - https://github.com/jahnf/CMake-GitVersion/blob/develop/cmake/GitVersion.cmake
    ## Contributing ## Contributing
    To contribute, please create a feature branch and a "Draft" merge request. Upon To contribute, please create a feature branch and a "Draft" merge request. Upon
    ......
    # Auto generated archive version information # Auto generated archive version information
    # Included in created source archives # Included in created source archives
    set(@PREFIX@_VERSION "@VERSION@")
    set(@PREFIX@_VERSION_MAJOR "@VERSION_MAJOR@") set(@PREFIX@_VERSION_MAJOR "@VERSION_MAJOR@")
    set(@PREFIX@_VERSION_MINOR "@VERSION_MINOR@") set(@PREFIX@_VERSION_MINOR "@VERSION_MINOR@")
    set(@PREFIX@_VERSION_PATCH "@VERSION_PATCH@") set(@PREFIX@_VERSION_PATCH "@VERSION_PATCH@")
    ......
    #include "@PREFIX@-GitVersion.h" #include "@PREFIX@-GitVersion.h"
    namespace @PREFIX@ { namespace @PREFIX@ {
    const char* version() { return "@VERSION@"; }
    const char* version_string() { return "@VERSION_STRING@"; } const char* version_string() { return "@VERSION_STRING@"; }
    unsigned int version_major() { return @VERSION_MAJOR@; } unsigned int version_major() { return @VERSION_MAJOR@; }
    unsigned int version_minor() { return @VERSION_MINOR@; } unsigned int version_minor() { return @VERSION_MINOR@; }
    ......
    ...@@ -45,6 +45,7 @@ set(_GitVersion_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}") ...@@ -45,6 +45,7 @@ set(_GitVersion_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}")
    # Helper method - usually never called directly: # Helper method - usually never called directly:
    # Get the version information for a directory, sets the following variables # Get the version information for a directory, sets the following variables
    # ${prefix}_VERSION_SUCCESS // 0 on error (e.g. git not found), 1 on success # ${prefix}_VERSION_SUCCESS // 0 on error (e.g. git not found), 1 on success
    # ${prefix}_VERSION
    # ${prefix}_VERSION_MAJOR # ${prefix}_VERSION_MAJOR
    # ${prefix}_VERSION_MINOR # ${prefix}_VERSION_MINOR
    # ${prefix}_VERSION_PATCH # ${prefix}_VERSION_PATCH
    ...@@ -67,6 +68,7 @@ set(_GitVersion_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}") ...@@ -67,6 +68,7 @@ set(_GitVersion_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}")
    # The environment variable FALLBACK_BRANCH will be used if the branch cannot be determined # The environment variable FALLBACK_BRANCH will be used if the branch cannot be determined
    function(get_version_info prefix directory) function(get_version_info prefix directory)
    set(${prefix}_VERSION_SUCCESS 0 PARENT_SCOPE) set(${prefix}_VERSION_SUCCESS 0 PARENT_SCOPE)
    set(${prefix}_VERSION "0.0.0")
    set(${prefix}_VERSION_MAJOR 0) set(${prefix}_VERSION_MAJOR 0)
    set(${prefix}_VERSION_MINOR 0) set(${prefix}_VERSION_MINOR 0)
    set(${prefix}_VERSION_PATCH 0) set(${prefix}_VERSION_PATCH 0)
    ...@@ -303,6 +305,10 @@ function(get_version_info prefix directory) ...@@ -303,6 +305,10 @@ function(get_version_info prefix directory)
    set(${prefix}_VERSION_PATCH ${${prefix}_VERSION_PATCH} PARENT_SCOPE) set(${prefix}_VERSION_PATCH ${${prefix}_VERSION_PATCH} PARENT_SCOPE)
    set(${prefix}_VERSION_DISTANCE ${${prefix}_VERSION_DISTANCE} PARENT_SCOPE) set(${prefix}_VERSION_DISTANCE ${${prefix}_VERSION_DISTANCE} PARENT_SCOPE)
    # build version
    set(VERSION "${${prefix}_VERSION_MAJOR}.${${prefix}_VERSION_MINOR}.${${prefix}_VERSION_PATCH}")
    set(${prefix}_VERSION "${VERSION}" PARENT_SCOPE)
    # Build version string... # Build version string...
    set(VERSION_STRING "${${prefix}_VERSION_MAJOR}.${${prefix}_VERSION_MINOR}") set(VERSION_STRING "${${prefix}_VERSION_MAJOR}.${${prefix}_VERSION_MINOR}")
    if(NOT ${${prefix}_VERSION_PATCH} EQUAL 0) if(NOT ${${prefix}_VERSION_PATCH} EQUAL 0)
    ...@@ -365,6 +371,7 @@ function(add_version_info) ...@@ -365,6 +371,7 @@ function(add_version_info)
    endif() endif()
    # Set default values, in case sth goes wrong # Set default values, in case sth goes wrong
    set(VERSION "0.0.0")
    set(VERSION_MAJOR 0) set(VERSION_MAJOR 0)
    set(VERSION_MINOR 0) set(VERSION_MINOR 0)
    set(VERSION_PATCH 0) set(VERSION_PATCH 0)
    ...@@ -473,6 +480,7 @@ function(add_version_info) ...@@ -473,6 +480,7 @@ function(add_version_info)
    message(STATUS "Version-Info: Failure during version retrieval. Possible incomplete version information!") message(STATUS "Version-Info: Failure during version retrieval. Possible incomplete version information!")
    endif() endif()
    # Test if we are building from an archive that has generated version information # Test if we are building from an archive that has generated version information
    set(VERSION ${${VI_PREFIX}_VERSION})
    set(VERSION_MAJOR ${${VI_PREFIX}_VERSION_MAJOR}) set(VERSION_MAJOR ${${VI_PREFIX}_VERSION_MAJOR})
    set(VERSION_MINOR ${${VI_PREFIX}_VERSION_MINOR}) set(VERSION_MINOR ${${VI_PREFIX}_VERSION_MINOR})
    set(VERSION_PATCH ${${VI_PREFIX}_VERSION_PATCH}) set(VERSION_PATCH ${${VI_PREFIX}_VERSION_PATCH})
    ...@@ -484,6 +492,7 @@ function(add_version_info) ...@@ -484,6 +492,7 @@ function(add_version_info)
    set(VERSION_ISDIRTY ${${VI_PREFIX}_VERSION_ISDIRTY}) set(VERSION_ISDIRTY ${${VI_PREFIX}_VERSION_ISDIRTY})
    set(VERSION_BRANCH ${${VI_PREFIX}_VERSION_BRANCH}) set(VERSION_BRANCH ${${VI_PREFIX}_VERSION_BRANCH})
    set_target_properties(${VI_TARGET} PROPERTIES set_target_properties(${VI_TARGET} PROPERTIES
    VERSION "${VERSION}"
    VERSION_MAJOR "${VERSION_MAJOR}" VERSION_MAJOR "${VERSION_MAJOR}"
    VERSION_MINOR "${VERSION_MINOR}" VERSION_MINOR "${VERSION_MINOR}"
    VERSION_PATCH "${VERSION_PATCH}" VERSION_PATCH "${VERSION_PATCH}"
    ......
    ...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
    #define @PREFIX@_GITVERSION_H #define @PREFIX@_GITVERSION_H
    namespace @PREFIX@ { namespace @PREFIX@ {
    const char* version();
    const char* version_string(); const char* version_string();
    unsigned int version_major(); unsigned int version_major();
    unsigned int version_minor(); unsigned int version_minor();
    ......
    ...@@ -10,6 +10,7 @@ int main(int, const char **) { ...@@ -10,6 +10,7 @@ int main(int, const char **) {
    lib::Hello(); lib::Hello();
    std::cout << "CMake-GitVersion example"<< std::endl; std::cout << "CMake-GitVersion example"<< std::endl;
    std::cout << "- version: " << cpp::version() << std::endl;
    std::cout << "- version_string: " << cpp::version_string() << std::endl; std::cout << "- version_string: " << cpp::version_string() << std::endl;
    std::cout << "- version_branch: " << cpp::version_branch() << std::endl; std::cout << "- version_branch: " << cpp::version_branch() << std::endl;
    std::cout << "- version_fullhash: " << cpp::version_fullhash() << std::endl; std::cout << "- version_fullhash: " << cpp::version_fullhash() << std::endl;
    ......
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment