From 19ccf94978caffad0ca0a521f6df78f19c6cab45 Mon Sep 17 00:00:00 2001 From: Michael Scofield Date: Tue, 29 Jun 2021 14:44:07 +0200 Subject: [PATCH] Add major, minor, patch and build versions to the version header (#27) * Added major, minor, patch and build versions to the `version.h.in` Fixes #26. * made the build version a string * fixed the build folder being included in the git * Update CMakeLists.txt Co-authored-by: Lars Melchior Co-authored-by: Lars Melchior --- CMakeLists.txt | 28 +++++++++++++++++++ test/CMakeLists.txt | 2 +- test/dependency/CMakeLists.txt | 2 +- test/dependency/source/dependency.cpp | 4 +++ test/main.cpp | 16 +++++++++-- .../source/namespaced_dependency.cpp | 5 ++++ test/transitive_dependency/CMakeLists.txt | 2 +- .../source/transitive_dependency.cpp | 4 +++ version.h.in | 5 ++++ 9 files changed, 63 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c41657..125d673 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -49,6 +49,34 @@ function(packageProject) endif() if(DEFINED PROJECT_VERSION_HEADER) + # clear previous matches + unset(CMAKE_MATCH_1) + unset(CMAKE_MATCH_3) + unset(CMAKE_MATCH_5) + unset(CMAKE_MATCH_7) + + string(REGEX MATCH "^([0-9]+)(\\.([0-9]+))?(\\.([0-9]+))?(\\.([0-9]+))?$" _ + "${PROJECT_VERSION}" + ) + + set(PROJECT_VERSION_MAJOR ${CMAKE_MATCH_1}) + set(PROJECT_VERSION_MINOR ${CMAKE_MATCH_3}) + set(PROJECT_VERSION_PATCH ${CMAKE_MATCH_5}) + set(PROJECT_VERSION_TWEAK ${CMAKE_MATCH_7}) + + if(NOT DEFINED PROJECT_VERSION_MAJOR) + set(PROJECT_VERSION_MAJOR "0") + endif() + if(NOT DEFINED PROJECT_VERSION_MINOR) + set(PROJECT_VERSION_MINOR "0") + endif() + if(NOT DEFINED PROJECT_VERSION_PATCH) + set(PROJECT_VERSION_PATCH "0") + endif() + if(NOT DEFINED PROJECT_VERSION_TWEAK) + set(PROJECT_VERSION_TWEAK "0") + endif() + string(TOUPPER ${PROJECT_NAME} UPPERCASE_PROJECT_NAME) configure_file( ${PACKAGE_PROJECT_ROOT_PATH}/version.h.in diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 8c0201d..9ae215d 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -9,7 +9,7 @@ project( ) if(TEST_INSTALLED_VERSION) - find_package(dependency 1.2.3 REQUIRED) + find_package(dependency 1.2 REQUIRED) find_package(namespaced_dependency 4.5.6 REQUIRED) find_package(transitive_dependency 7.8.9 REQUIRED) else() diff --git a/test/dependency/CMakeLists.txt b/test/dependency/CMakeLists.txt index ad23315..c08de73 100644 --- a/test/dependency/CMakeLists.txt +++ b/test/dependency/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.14) project( dependency - VERSION 1.2.3 + VERSION 1.2 LANGUAGES CXX ) diff --git a/test/dependency/source/dependency.cpp b/test/dependency/source/dependency.cpp index 218cf54..9826b07 100644 --- a/test/dependency/source/dependency.cpp +++ b/test/dependency/source/dependency.cpp @@ -4,4 +4,8 @@ void dependencyFunction() { std::cout << "Using dependency version " << DEPENDENCY_VERSION << std::endl; + std::cout << "Dependency version major: " << DEPENDENCY_VERSION_MAJOR << std::endl; + std::cout << "Dependency version minor: " << DEPENDENCY_VERSION_MINOR << std::endl; + std::cout << "Dependency version patch: " << DEPENDENCY_VERSION_PATCH << std::endl; + std::cout << "Dependency version build: " << DEPENDENCY_VERSION_TWEAK << std::endl; } diff --git a/test/main.cpp b/test/main.cpp index d94976a..dcd4ed3 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -12,8 +12,20 @@ int main() { ns::namespacedDependencyFunction(); transitiveDependencyFunction(); auto result = true; - result &= DEPENDENCY_VERSION == std::string("1.2.3"); + result &= DEPENDENCY_VERSION == std::string("1.2"); + result &= DEPENDENCY_VERSION_MAJOR == 1; + result &= DEPENDENCY_VERSION_MINOR == 2; + result &= DEPENDENCY_VERSION_PATCH == 0; + result &= DEPENDENCY_VERSION_TWEAK == 0; result &= NAMESPACED_DEPENDENCY_VERSION == std::string("4.5.6"); - result &= TRANSITIVE_DEPENDENCY_VERSION == std::string("7.8.9"); + result &= NAMESPACED_DEPENDENCY_VERSION_MAJOR == 4; + result &= NAMESPACED_DEPENDENCY_VERSION_MINOR == 5; + result &= NAMESPACED_DEPENDENCY_VERSION_PATCH == 6; + result &= NAMESPACED_DEPENDENCY_VERSION_TWEAK == 0; + result &= TRANSITIVE_DEPENDENCY_VERSION == std::string("7.8.9.21948124"); + result &= TRANSITIVE_DEPENDENCY_VERSION_MAJOR == 7; + result &= TRANSITIVE_DEPENDENCY_VERSION_MINOR == 8; + result &= TRANSITIVE_DEPENDENCY_VERSION_PATCH == 9; + result &= TRANSITIVE_DEPENDENCY_VERSION_TWEAK == 21948124; return result ? 0 : 1; } diff --git a/test/namespaced_dependency/source/namespaced_dependency.cpp b/test/namespaced_dependency/source/namespaced_dependency.cpp index 4fceb92..93ae1cb 100644 --- a/test/namespaced_dependency/source/namespaced_dependency.cpp +++ b/test/namespaced_dependency/source/namespaced_dependency.cpp @@ -6,5 +6,10 @@ namespace ns { void namespacedDependencyFunction() { std::cout << "Using namespaced_dependency version " << NAMESPACED_DEPENDENCY_VERSION << std::endl; + + std::cout << "Dependency version major: " << NAMESPACED_DEPENDENCY_VERSION_MAJOR << std::endl; + std::cout << "Dependency version minor: " << NAMESPACED_DEPENDENCY_VERSION_MINOR << std::endl; + std::cout << "Dependency version patch: " << NAMESPACED_DEPENDENCY_VERSION_PATCH << std::endl; + std::cout << "Dependency version build: " << NAMESPACED_DEPENDENCY_VERSION_TWEAK << std::endl; } } // namespace ns diff --git a/test/transitive_dependency/CMakeLists.txt b/test/transitive_dependency/CMakeLists.txt index d2f9a0a..4abba0a 100644 --- a/test/transitive_dependency/CMakeLists.txt +++ b/test/transitive_dependency/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.14) project( transitive_dependency - VERSION 7.8.9 + VERSION 7.8.9.21948124 LANGUAGES CXX ) diff --git a/test/transitive_dependency/source/transitive_dependency.cpp b/test/transitive_dependency/source/transitive_dependency.cpp index 1e782fe..343c714 100644 --- a/test/transitive_dependency/source/transitive_dependency.cpp +++ b/test/transitive_dependency/source/transitive_dependency.cpp @@ -4,4 +4,8 @@ void TRANSITIVE_DEPENDENCY_EXPORT transitiveDependencyFunction() { fmt::print("Using transitive_dependency version {}\n", TRANSITIVE_DEPENDENCY_VERSION); + fmt::print("Dependency version major: {}\n", TRANSITIVE_DEPENDENCY_VERSION_MAJOR); + fmt::print("Dependency version minor: {}\n", TRANSITIVE_DEPENDENCY_VERSION_MINOR); + fmt::print("Dependency version patch: {}\n", TRANSITIVE_DEPENDENCY_VERSION_PATCH); + fmt::print("Dependency version build: {}\n", TRANSITIVE_DEPENDENCY_VERSION_TWEAK); } diff --git a/version.h.in b/version.h.in index a238bf8..e74e3eb 100644 --- a/version.h.in +++ b/version.h.in @@ -1,3 +1,8 @@ #pragma once #define @UPPERCASE_PROJECT_NAME@_VERSION "@PROJECT_VERSION@" + +#define @UPPERCASE_PROJECT_NAME@_VERSION_MAJOR @PROJECT_VERSION_MAJOR@ +#define @UPPERCASE_PROJECT_NAME@_VERSION_MINOR @PROJECT_VERSION_MINOR@ +#define @UPPERCASE_PROJECT_NAME@_VERSION_PATCH @PROJECT_VERSION_PATCH@ +#define @UPPERCASE_PROJECT_NAME@_VERSION_TWEAK @PROJECT_VERSION_TWEAK@