cmake: Generate properly formatted versions from git

This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2023-05-14 05:54:57 +02:00 committed by Xaymar
parent 7237d8826f
commit 0439fddabd

View file

@ -151,34 +151,6 @@ if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/.git")
) )
if(EXISTS "${GIT}") if(EXISTS "${GIT}")
# Determine the version by exist tag match.
execute_process(COMMAND "${GIT}" tag "--sort=-v:refname" "--points-at" HEAD WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} RESULT_VARIABLE GIT_RESULT OUTPUT_VARIABLE GIT_OUTPUT ERROR_VARIABLE GIT_ERROR OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
if((GIT_RESULT EQUAL 0) AND (NOT "${GIT_OUTPUT}" STREQUAL ""))
# Some minor string editing so the output conforms to SemVer 2.0.0.
string(REGEX REPLACE "[\r\n]+.*" "" GIT_OUTPUT "${GIT_OUTPUT}")
# Parse as SemVer 2.0.0
version(PARSE GIT_OUTPUT "${GIT_OUTPUT}" REQUIRE "PATCH;TWEAK")
if(GIT_OUTPUT_PRERELEASE)
string(SUBSTRING "${GIT_OUTPUT_PRERELEASE}" 1 -1 GIT_OUTPUT_TWEAK)
string(SUBSTRING "${GIT_OUTPUT_PRERELEASE}" 0 1 GIT_OUTPUT_PRERELEASE)
if(NOT GIT_OUTPUT_TWEAK STREQUAL GIT_OUTPUT_TWEAK)
message(WARNING "'git' tag mismatches detected version: '${GIT_OUTPUT_TWEAK}' != '${GIT_OUTPUT_TWEAK}'.")
endif()
endif()
# Update our global version.
version(GENERATE _VERSION COMPRESS
MAJOR "${GIT_OUTPUT_MAJOR}"
MINOR "${GIT_OUTPUT_MINOR}"
PATCH "${GIT_OUTPUT_PATCH}"
TWEAK "${GIT_OUTPUT_TWEAK}"
PRERELEASE "${GIT_OUTPUT_PRERELEASE}"
BUILD "${_VERSION_BUILD}"
REQUIRE "PATCH;TWEAK"
)
else()
# Try and calculate the exist version using git. # Try and calculate the exist version using git.
execute_process(COMMAND "${GIT}" describe --tags --long --abbrev=8 HEAD WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} RESULT_VARIABLE GIT_RESULT OUTPUT_VARIABLE GIT_OUTPUT ERROR_VARIABLE GIT_ERROR OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE ERROR_QUIET) execute_process(COMMAND "${GIT}" describe --tags --long --abbrev=8 HEAD WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} RESULT_VARIABLE GIT_RESULT OUTPUT_VARIABLE GIT_OUTPUT ERROR_VARIABLE GIT_ERROR OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
if((GIT_RESULT EQUAL 0) AND (NOT "${GIT_OUTPUT}" STREQUAL "")) if((GIT_RESULT EQUAL 0) AND (NOT "${GIT_OUTPUT}" STREQUAL ""))
@ -197,6 +169,11 @@ if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/.git")
if(GIT_OUTPUT_PATCH MATCHES "([0-9]+)([a-zA-Z]+)([0-9]*)") if(GIT_OUTPUT_PATCH MATCHES "([0-9]+)([a-zA-Z]+)([0-9]*)")
# Patch requires special parsing. # Patch requires special parsing.
set(GIT_OUTPUT_PATCH "${CMAKE_MATCH_1}") set(GIT_OUTPUT_PATCH "${CMAKE_MATCH_1}")
if(CMAKE_MATCH_3 GREATER 0)
set(GIT_OUTPUT_PRERELEASE "${CMAKE_MATCH_2}")
else()
set(GIT_OUTPUT_PRERELEASE "a")
endif()
MATH(EXPR GIT_OUTPUT_TWEAK "${GIT_OUTPUT_TWEAK} + ${CMAKE_MATCH_3}") MATH(EXPR GIT_OUTPUT_TWEAK "${GIT_OUTPUT_TWEAK} + ${CMAKE_MATCH_3}")
# Modify the global version # Modify the global version
@ -206,7 +183,7 @@ if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/.git")
PATCH "${GIT_OUTPUT_PATCH}" PATCH "${GIT_OUTPUT_PATCH}"
TWEAK "${GIT_OUTPUT_TWEAK}" TWEAK "${GIT_OUTPUT_TWEAK}"
BUILD "${GIT_OUTPUT_BUILD}" BUILD "${GIT_OUTPUT_BUILD}"
PRERELEASE "a" PRERELEASE "${GIT_OUTPUT_PRERELEASE}"
REQUIRE "PATCH;TWEAK" REQUIRE "PATCH;TWEAK"
) )
else() else()
@ -223,7 +200,6 @@ if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/.git")
endif() endif()
else() else()
execute_process(COMMAND "${GIT}" rev-list --count HEAD WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} RESULT_VARIABLE GIT_RESULT OUTPUT_VARIABLE GIT_OUTPUT ERROR_VARIABLE GIT_ERROR OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE ERROR_QUIET) execute_process(COMMAND "${GIT}" rev-list --count HEAD WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} RESULT_VARIABLE GIT_RESULT OUTPUT_VARIABLE GIT_OUTPUT ERROR_VARIABLE GIT_ERROR OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
message(STATUS "GIT=${GIT_RESULT},${GIT_OUTPUT},${GIT_ERROR}")
if((GIT_RESULT EQUAL 0) AND (NOT "${GIT_OUTPUT}" STREQUAL "")) if((GIT_RESULT EQUAL 0) AND (NOT "${GIT_OUTPUT}" STREQUAL ""))
version(MODIFY _VERSION "${_VERSION}" COMPRESS version(MODIFY _VERSION "${_VERSION}" COMPRESS
TWEAK "${GIT_OUTPUT}" TWEAK "${GIT_OUTPUT}"
@ -231,21 +207,20 @@ if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/.git")
REQUIRE "PATCH;TWEAK" REQUIRE "PATCH;TWEAK"
) )
# Determine the build using git.
execute_process(COMMAND "${GIT}" log -1 "--pretty=format:g%h" --abbrev=8 HEAD WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} RESULT_VARIABLE GIT_RESULT OUTPUT_VARIABLE GIT_OUTPUT ERROR_VARIABLE GIT_ERROR OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE ERROR_QUIET) execute_process(COMMAND "${GIT}" log -1 "--pretty=format:g%h" --abbrev=8 HEAD WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} RESULT_VARIABLE GIT_RESULT OUTPUT_VARIABLE GIT_OUTPUT ERROR_VARIABLE GIT_ERROR OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
if((GIT_RESULT EQUAL 0) AND (NOT "${GIT_OUTPUT}" STREQUAL "")) if((GIT_RESULT EQUAL 0) AND (NOT "${GIT_OUTPUT}" STREQUAL ""))
version(MODIFY _VERSION "${_VERSION}" COMPRESS version(MODIFY _VERSION "${_VERSION}" COMPRESS
BUILD "${GIT_OUTPUT}" BUILD "${GIT_OUTPUT}"
PRERELEASE "a"
REQUIRE "PATCH;TWEAK" REQUIRE "PATCH;TWEAK"
) )
else() else()
message(WARNING "Failed to detect full version with 'git'.") message(WARNING "Failed to detect build version with 'git'.")
endif() endif()
else() else()
message(WARNING "Failed to automatically detect version with 'git'.") message(WARNING "Failed to automatically detect version with 'git'.")
endif() endif()
endif() endif()
endif()
else() else()
message(WARNING "'git' not found, automatic version detection disabled.") message(WARNING "'git' not found, automatic version detection disabled.")
endif() endif()