Skip to content

Commit

Permalink
Add major, minor, patch and build versions to the version header (#27)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

Co-authored-by: Lars Melchior <[email protected]>
  • Loading branch information
mscofield0 and TheLartians authored Jun 29, 2021
1 parent 18beef4 commit 19ccf94
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 5 deletions.
28 changes: 28 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion test/dependency/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.14)

project(
dependency
VERSION 1.2.3
VERSION 1.2
LANGUAGES CXX
)

Expand Down
4 changes: 4 additions & 0 deletions test/dependency/source/dependency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
16 changes: 14 additions & 2 deletions test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 5 additions & 0 deletions test/namespaced_dependency/source/namespaced_dependency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/transitive_dependency/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.14)

project(
transitive_dependency
VERSION 7.8.9
VERSION 7.8.9.21948124
LANGUAGES CXX
)

Expand Down
4 changes: 4 additions & 0 deletions test/transitive_dependency/source/transitive_dependency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
5 changes: 5 additions & 0 deletions version.h.in
Original file line number Diff line number Diff line change
@@ -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@

0 comments on commit 19ccf94

Please sign in to comment.