diff --git a/{{cookiecutter.project_slug}}/CMakeLists.txt b/{{cookiecutter.project_slug}}/CMakeLists.txt index 8614c0d3e8c10cfe94ccfa1fddaca525cda6eebd..15f25f46aa971776e7b59dc495d2c6ef393b3dbd 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 0aa801286c7d7d0c7146a4f626b1a41e2c00eeca..ec725008ec5787351ab1371dd7b50b2ce206778f 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 f871b0e16df69d5d89f724851d6c02f1714fbeb5..cb43354392617bc91270c846c4d68711200fac7d 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 6b51ba8d51c20dc9138aaba2d30033ec0c856deb..514debdc9eca26daba3b9f008f434a7fa9cbaf12 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 1ac39aa14e7fb757f7655712d70ff65b887f81b1..1480f1d584098275d50b8fef70ef8299379c4f44 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 6e400fe3aea60f559d2e22453d57f72c7436e16c..3b0aa0fce8dfa78dfcbd66675811a36359a3e009 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 3677fef397f9f55388f94ec0eecb2a7d9317d99c..89098e418902670874f439d25610907f96531e78 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;