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,99 +151,74 @@ if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/.git")
) )
if(EXISTS "${GIT}") if(EXISTS "${GIT}")
# Determine the version by exist tag match. # Try and calculate the exist version using git.
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) 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 ""))
# Some minor string editing so the output conforms to SemVer 2.0.0. # Result will be MAJOR.MINOR.PATCH-TWEAK-gHASH
string(REGEX REPLACE "[\r\n]+.*" "" GIT_OUTPUT "${GIT_OUTPUT}") string(REPLACE "-" ";" GIT_OUTPUT "${GIT_OUTPUT}")
string(REPLACE "." ";" GIT_OUTPUT "${GIT_OUTPUT}")
# Parse as SemVer 2.0.0 # Split into components
version(PARSE GIT_OUTPUT "${GIT_OUTPUT}" REQUIRE "PATCH;TWEAK") list(GET GIT_OUTPUT 0 GIT_OUTPUT_MAJOR)
if(GIT_OUTPUT_PRERELEASE) list(GET GIT_OUTPUT 1 GIT_OUTPUT_MINOR)
string(SUBSTRING "${GIT_OUTPUT_PRERELEASE}" 1 -1 GIT_OUTPUT_TWEAK) list(GET GIT_OUTPUT 2 GIT_OUTPUT_PATCH)
string(SUBSTRING "${GIT_OUTPUT_PRERELEASE}" 0 1 GIT_OUTPUT_PRERELEASE) list(GET GIT_OUTPUT 3 GIT_OUTPUT_TWEAK)
list(GET GIT_OUTPUT 4 GIT_OUTPUT_BUILD)
if(NOT GIT_OUTPUT_TWEAK STREQUAL GIT_OUTPUT_TWEAK) # Special case: Tag contains prerelease
message(WARNING "'git' tag mismatches detected version: '${GIT_OUTPUT_TWEAK}' != '${GIT_OUTPUT_TWEAK}'.") if(GIT_OUTPUT_PATCH MATCHES "([0-9]+)([a-zA-Z]+)([0-9]*)")
endif() # Patch requires special parsing.
endif() set(GIT_OUTPUT_PATCH "${CMAKE_MATCH_1}")
if(CMAKE_MATCH_3 GREATER 0)
# Update our global version. set(GIT_OUTPUT_PRERELEASE "${CMAKE_MATCH_2}")
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.
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 ""))
# Result will be MAJOR.MINOR.PATCH-TWEAK-gHASH
string(REPLACE "-" ";" GIT_OUTPUT "${GIT_OUTPUT}")
string(REPLACE "." ";" GIT_OUTPUT "${GIT_OUTPUT}")
# Split into components
list(GET GIT_OUTPUT 0 GIT_OUTPUT_MAJOR)
list(GET GIT_OUTPUT 1 GIT_OUTPUT_MINOR)
list(GET GIT_OUTPUT 2 GIT_OUTPUT_PATCH)
list(GET GIT_OUTPUT 3 GIT_OUTPUT_TWEAK)
list(GET GIT_OUTPUT 4 GIT_OUTPUT_BUILD)
# Special case: Tag contains prerelease
if(GIT_OUTPUT_PATCH MATCHES "([0-9]+)([a-zA-Z]+)([0-9]*)")
# Patch requires special parsing.
set(GIT_OUTPUT_PATCH "${CMAKE_MATCH_1}")
MATH(EXPR GIT_OUTPUT_TWEAK "${GIT_OUTPUT_TWEAK} + ${CMAKE_MATCH_3}")
# Modify the global version
version(MODIFY _VERSION "${_VERSION}" COMPRESS
MAJOR "${GIT_OUTPUT_MAJOR}"
MINOR "${GIT_OUTPUT_MINOR}"
PATCH "${GIT_OUTPUT_PATCH}"
TWEAK "${GIT_OUTPUT_TWEAK}"
BUILD "${GIT_OUTPUT_BUILD}"
PRERELEASE "a"
REQUIRE "PATCH;TWEAK"
)
else() else()
# Modify the global version set(GIT_OUTPUT_PRERELEASE "a")
version(MODIFY _VERSION "${_VERSION}" COMPRESS
MAJOR "${GIT_OUTPUT_MAJOR}"
MINOR "${GIT_OUTPUT_MINOR}"
PATCH "${GIT_OUTPUT_PATCH}"
TWEAK "${GIT_OUTPUT_TWEAK}"
BUILD "${GIT_OUTPUT_BUILD}"
PRERELEASE "a"
REQUIRE "PATCH;TWEAK"
)
endif() endif()
MATH(EXPR GIT_OUTPUT_TWEAK "${GIT_OUTPUT_TWEAK} + ${CMAKE_MATCH_3}")
# Modify the global version
version(MODIFY _VERSION "${_VERSION}" COMPRESS
MAJOR "${GIT_OUTPUT_MAJOR}"
MINOR "${GIT_OUTPUT_MINOR}"
PATCH "${GIT_OUTPUT_PATCH}"
TWEAK "${GIT_OUTPUT_TWEAK}"
BUILD "${GIT_OUTPUT_BUILD}"
PRERELEASE "${GIT_OUTPUT_PRERELEASE}"
REQUIRE "PATCH;TWEAK"
)
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) # Modify the global version
message(STATUS "GIT=${GIT_RESULT},${GIT_OUTPUT},${GIT_ERROR}") version(MODIFY _VERSION "${_VERSION}" COMPRESS
MAJOR "${GIT_OUTPUT_MAJOR}"
MINOR "${GIT_OUTPUT_MINOR}"
PATCH "${GIT_OUTPUT_PATCH}"
TWEAK "${GIT_OUTPUT_TWEAK}"
BUILD "${GIT_OUTPUT_BUILD}"
PRERELEASE "a"
REQUIRE "PATCH;TWEAK"
)
endif()
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)
if((GIT_RESULT EQUAL 0) AND (NOT "${GIT_OUTPUT}" STREQUAL ""))
version(MODIFY _VERSION "${_VERSION}" COMPRESS
TWEAK "${GIT_OUTPUT}"
PRERELEASE "a"
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)
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}" BUILD "${GIT_OUTPUT}"
PRERELEASE "a"
REQUIRE "PATCH;TWEAK" REQUIRE "PATCH;TWEAK"
) )
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 ""))
version(MODIFY _VERSION "${_VERSION}" COMPRESS
BUILD "${GIT_OUTPUT}"
PRERELEASE "a"
REQUIRE "PATCH;TWEAK"
)
else()
message(WARNING "Failed to detect full version with 'git'.")
endif()
else() else()
message(WARNING "Failed to automatically detect version with 'git'.") message(WARNING "Failed to detect build version with 'git'.")
endif() endif()
else()
message(WARNING "Failed to automatically detect version with 'git'.")
endif() endif()
endif() endif()
else() else()