Skip to content

Commit

Permalink
Use GitVersionDetect to automatically obtain version number
Browse files Browse the repository at this point in the history
  • Loading branch information
res2k committed May 12, 2024
1 parent f4b87ce commit 0c0fa53
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
11 changes: 7 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
#
cmake_minimum_required (VERSION 3.15...3.25)

include(GitVersionDetect.cmake)

project ("HDRTray")
set(VERSION_MAJOR 0)
set(VERSION_MINOR 3)
set(VERSION_REVISION 0)
set(VERSION_BUILD 0)
set(VERSION_MAJOR ${GITVERSIONDETECT_VERSION_MAJOR})
set(VERSION_MINOR ${GITVERSIONDETECT_VERSION_MINOR})
set(VERSION_REVISION ${GITVERSIONDETECT_VERSION_PATCH})
set(VERSION_BUILD ${GITVERSIONDETECT_VERSION_COMMIT_NUM})
set(VERSION_FULL ${GITVERSIONDETECT_VERSION})

configure_file(version.rc.in "${CMAKE_CURRENT_BINARY_DIR}/generated/version.rc")

Expand Down
71 changes: 71 additions & 0 deletions GitVersionDetect.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright 2023 Antonio Vázquez Blanco <[email protected]>
# Based on https://github.com/antoniovazquezblanco/cmake-gitversiondetect

#[=======================================================================[.rst:
GitVersionDetect
-------
Derives a program version from Git tags. Only tags starting with a "v" will be
used for version inference.
Result Variables
^^^^^^^^^^^^^^^^
This will define the following variables:
``GITVERSIONDETECT_VERSION``
Full git tag as git describe --dirty would produce.
``GITVERSIONDETECT_VERSION_MAJOR``
Major version matched from GITVERSIONDETECT_VERSION.
``GITVERSIONDETECT_VERSION_MINOR``
Minor version matched from GITVERSIONDETECT_VERSION.
``GITVERSIONDETECT_VERSION_PATCH``
Patch version matched from GITVERSIONDETECT_VERSION.
``GITVERSIONDETECT_VERSION_COMMIT_NUM``
Number of commits that separate current source from the detected version
tag.
``GITVERSIONDETECT_VERSION_COMMIT_SHA``
Latest commit sha.
#]=======================================================================]

# Required packages
find_package(Git)

# Check if a git executable was found
if(GIT_EXECUTABLE)
# Generate a git-describe version string from Git repository tags
execute_process(
COMMAND ${GIT_EXECUTABLE} describe --tags --dirty --match "v*" --long
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE GIT_DESCRIBE_VERSION
RESULT_VARIABLE GIT_DESCRIBE_ERROR_CODE
OUTPUT_STRIP_TRAILING_WHITESPACE
)
# If no error took place, save the version
if(NOT GIT_DESCRIBE_ERROR_CODE)
string(REGEX REPLACE "^v" "" GITVERSIONDETECT_VERSION "${GIT_DESCRIBE_VERSION}")
endif()
endif()


# Final fallback: Just use a bogus version string that is semantically older
# than anything else and spit out a warning to the developer.
if(NOT DEFINED GITVERSIONDETECT_VERSION)
set(GITVERSIONDETECT_VERSION 0.0.0-0-unknown)
message(WARNING "Failed to determine GITVERSIONDETECT_VERSION from Git tags. Using default version \"${GITVERSIONDETECT_VERSION}\".")
endif()

# Split the version into major, minor, patch and prerelease
string(REGEX MATCH "^([0-9]+)\\.([0-9]+)\\.?([0-9]+)?(-([0-9]+)-([a-z0-9]+))?" GITVERSIONDETECT_VERSION_MATCH ${GITVERSIONDETECT_VERSION})
set(GITVERSIONDETECT_VERSION_MAJOR ${CMAKE_MATCH_1})
set(GITVERSIONDETECT_VERSION_MINOR ${CMAKE_MATCH_2})
set(GITVERSIONDETECT_VERSION_PATCH ${CMAKE_MATCH_3})
set(GITVERSIONDETECT_VERSION_COMMIT_NUM ${CMAKE_MATCH_5})
set(GITVERSIONDETECT_VERSION_COMMIT_SHA ${CMAKE_MATCH_6})

if(NOT GITVERSIONDETECT_VERSION_PATCH)
set(GITVERSIONDETECT_VERSION_PATCH 0)
endif()
2 changes: 1 addition & 1 deletion version.rc.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ BEGIN
VALUE "InternalName", "HDRTray.exe"
VALUE "LegalCopyright", "Copyright (c) 2022 Frank Richter"
VALUE "ProductName", "HDRTray"
VALUE "ProductVersion", "@VERSION_MAJOR@.@VERSION_MINOR@.@VERSION_REVISION@.@VERSION_BUILD@"
VALUE "ProductVersion", "@VERSION_FULL@"
VALUE "Licence", "GNU General Public License, version 3"
//VALUE "Info", "https://res2k.github.io" // TODO link
END
Expand Down

0 comments on commit 0c0fa53

Please sign in to comment.