From 00962b0a6c024a78bebd07d1ff1a7476bcddf003 Mon Sep 17 00:00:00 2001 From: Dantali0n <info@dantalion.nl> Date: Thu, 16 Nov 2023 14:19:39 +0100 Subject: [PATCH] CWG-45: Finish cleaning up c++ versioning --- {{cookiecutter.project_slug}}/CMakeLists.txt | 3 --- {{cookiecutter.project_slug}}/README.md | 9 +++++++++ .../cmake/ArchiveVersionInfo.cmake.in | 1 + {{cookiecutter.project_slug}}/cmake/GitVersion.cc.in | 1 + {{cookiecutter.project_slug}}/cmake/GitVersion.cmake | 9 +++++++++ {{cookiecutter.project_slug}}/cmake/GitVersion.h.in | 1 + {{cookiecutter.project_slug}}/src/main.cpp | 1 + 7 files changed, 22 insertions(+), 3 deletions(-) diff --git a/{{cookiecutter.project_slug}}/CMakeLists.txt b/{{cookiecutter.project_slug}}/CMakeLists.txt index 8614c0d..15f25f4 100644 --- a/{{cookiecutter.project_slug}}/CMakeLists.txt +++ b/{{cookiecutter.project_slug}}/CMakeLists.txt @@ -7,12 +7,9 @@ include(GitVersion) get_version_info({{cookiecutter.project_slug}} "${CMAKE_CURRENT_SOURCE_DIR}") 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}) -message("VERSION: ${${project_slug}_VERSION}") - set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED YES) set(CMAKE_CXX_EXTENSIONS OFF) diff --git a/{{cookiecutter.project_slug}}/README.md b/{{cookiecutter.project_slug}}/README.md index 0aa8012..ec72500 100644 --- a/{{cookiecutter.project_slug}}/README.md +++ b/{{cookiecutter.project_slug}}/README.md @@ -36,6 +36,15 @@ For instance: - Install the project globally: ``ninja -C ./build/ install`` - 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 To contribute, please create a feature branch and a "Draft" merge request. Upon diff --git a/{{cookiecutter.project_slug}}/cmake/ArchiveVersionInfo.cmake.in b/{{cookiecutter.project_slug}}/cmake/ArchiveVersionInfo.cmake.in index f871b0e..cb43354 100644 --- a/{{cookiecutter.project_slug}}/cmake/ArchiveVersionInfo.cmake.in +++ b/{{cookiecutter.project_slug}}/cmake/ArchiveVersionInfo.cmake.in @@ -1,6 +1,7 @@ # Auto generated archive version information # Included in created source archives +set(@PREFIX@_VERSION "@VERSION@") set(@PREFIX@_VERSION_MAJOR "@VERSION_MAJOR@") set(@PREFIX@_VERSION_MINOR "@VERSION_MINOR@") set(@PREFIX@_VERSION_PATCH "@VERSION_PATCH@") diff --git a/{{cookiecutter.project_slug}}/cmake/GitVersion.cc.in b/{{cookiecutter.project_slug}}/cmake/GitVersion.cc.in index 6b51ba8..514debd 100644 --- a/{{cookiecutter.project_slug}}/cmake/GitVersion.cc.in +++ b/{{cookiecutter.project_slug}}/cmake/GitVersion.cc.in @@ -1,6 +1,7 @@ #include "@PREFIX@-GitVersion.h" namespace @PREFIX@ { + const char* version() { return "@VERSION@"; } const char* version_string() { return "@VERSION_STRING@"; } unsigned int version_major() { return @VERSION_MAJOR@; } unsigned int version_minor() { return @VERSION_MINOR@; } diff --git a/{{cookiecutter.project_slug}}/cmake/GitVersion.cmake b/{{cookiecutter.project_slug}}/cmake/GitVersion.cmake index 1ac39aa..1480f1d 100644 --- a/{{cookiecutter.project_slug}}/cmake/GitVersion.cmake +++ b/{{cookiecutter.project_slug}}/cmake/GitVersion.cmake @@ -45,6 +45,7 @@ set(_GitVersion_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}") # Helper method - usually never called directly: # 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 # ${prefix}_VERSION_MAJOR # ${prefix}_VERSION_MINOR # ${prefix}_VERSION_PATCH @@ -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 function(get_version_info prefix directory) set(${prefix}_VERSION_SUCCESS 0 PARENT_SCOPE) + set(${prefix}_VERSION "0.0.0") set(${prefix}_VERSION_MAJOR 0) set(${prefix}_VERSION_MINOR 0) set(${prefix}_VERSION_PATCH 0) @@ -303,6 +305,10 @@ function(get_version_info prefix directory) set(${prefix}_VERSION_PATCH ${${prefix}_VERSION_PATCH} 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... set(VERSION_STRING "${${prefix}_VERSION_MAJOR}.${${prefix}_VERSION_MINOR}") if(NOT ${${prefix}_VERSION_PATCH} EQUAL 0) @@ -365,6 +371,7 @@ function(add_version_info) endif() # Set default values, in case sth goes wrong + set(VERSION "0.0.0") set(VERSION_MAJOR 0) set(VERSION_MINOR 0) set(VERSION_PATCH 0) @@ -473,6 +480,7 @@ function(add_version_info) message(STATUS "Version-Info: Failure during version retrieval. Possible incomplete version information!") endif() # 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_MINOR ${${VI_PREFIX}_VERSION_MINOR}) set(VERSION_PATCH ${${VI_PREFIX}_VERSION_PATCH}) @@ -484,6 +492,7 @@ function(add_version_info) set(VERSION_ISDIRTY ${${VI_PREFIX}_VERSION_ISDIRTY}) set(VERSION_BRANCH ${${VI_PREFIX}_VERSION_BRANCH}) set_target_properties(${VI_TARGET} PROPERTIES + VERSION "${VERSION}" VERSION_MAJOR "${VERSION_MAJOR}" VERSION_MINOR "${VERSION_MINOR}" VERSION_PATCH "${VERSION_PATCH}" diff --git a/{{cookiecutter.project_slug}}/cmake/GitVersion.h.in b/{{cookiecutter.project_slug}}/cmake/GitVersion.h.in index 6e400fe..3b0aa0f 100644 --- a/{{cookiecutter.project_slug}}/cmake/GitVersion.h.in +++ b/{{cookiecutter.project_slug}}/cmake/GitVersion.h.in @@ -2,6 +2,7 @@ #define @PREFIX@_GITVERSION_H namespace @PREFIX@ { + const char* version(); const char* version_string(); unsigned int version_major(); unsigned int version_minor(); diff --git a/{{cookiecutter.project_slug}}/src/main.cpp b/{{cookiecutter.project_slug}}/src/main.cpp index 3677fef..89098e4 100644 --- a/{{cookiecutter.project_slug}}/src/main.cpp +++ b/{{cookiecutter.project_slug}}/src/main.cpp @@ -10,6 +10,7 @@ int main(int, const char **) { lib::Hello(); 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_branch: " << cpp::version_branch() << std::endl; std::cout << "- version_fullhash: " << cpp::version_fullhash() << std::endl; -- GitLab