From e240967c279122630d0359ac3e69fe516ea17be9 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Fri, 22 Jul 2022 13:47:39 +0200 Subject: [PATCH] code: Add support for requiring PATCH and TWEAK components These will be set to 0 if required instead of leaving them blank. --- docs/index.rst | 12 ++++++------ version.cmake | 27 +++++++++++++++++++++------ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 45690a6..109684f 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -8,9 +8,9 @@ Synopsis .. parsed-literal:: - version(`PARSE`_ ) + version(`PARSE`_ [REQUIRE [PATCH|TWEAK][;...]] ) version(`GENERATE`_ [COMPRESS] [MAJOR ] [MINOR ] [PATCH ] [TWEAK ] [PRERELEASE ] [BUILD ]) - version(`MODIFY`_ [COMPRESS] [MAJOR ] [MINOR ] [TWEAK ] [PRERELEASE ] [BUILD ]) + version(`MODIFY`_ [COMPRESS] [MAJOR ] [MINOR ] [TWEAK ] [PRERELEASE ] [BUILD ] [REQUIRE [PATCH|TWEAK][;...]]) version(`COMPARE`_ ) @@ -27,9 +27,9 @@ Parsing .. code-block:: cmake - version(PARSE ) + version(PARSE [REQUIRE [PATCH|TWEAK][;...]] ) -Attempts to parse the version in ```` and stores the individual compoents into ``_``. If a component is not present in the given version, it will be set to a false constant. If an error occurred, ``_ERROR`` will contain the error message otherwise it will be a false constant. The ``PRERELEASE`` and ``BUILD`` components support the dot-separation specifier and will be turned into a list if they are encountered. +Attempts to parse the version in ```` and stores the individual compoents into ``_``. If a component is not present in the given version, it will be set to a false constant. If an error occurred, ``_ERROR`` will contain the error message otherwise it will be a false constant. The ``PRERELEASE`` and ``BUILD`` components support the dot-separation specifier and will be turned into a list if they are encountered. The optional ``REQUIRE`` allows forcing the components ``PATCH`` and ``TWEAK`` to always be defined. Generating ^^^^^^^^^^ @@ -49,9 +49,9 @@ Modifying .. code-block:: cmake - version(MODIFY [COMPRESS] [MAJOR ] [MINOR ] [TWEAK ] [PRERELEASE ] [BUILD ]) + version(MODIFY [COMPRESS] [MAJOR ] [MINOR ] [TWEAK ] [PRERELEASE ] [BUILD ] [REQUIRE [PATCH|TWEAK][;...]]) -Modifies the version provided in ```` with the components provided. The components ````, ````, ```` and ```` may have a prefix of ``+`` or ``-`` to add and subtract the value, or no prefix to replace. The result of this operation will be stored as a string in ````. If a component did not exist in the original, it will be added to the version as a replace operation. If an error occurred, ``_ERROR`` will contain the error message otherwise it will be a false constant. +Modifies the version provided in ```` with the components provided. The components ````, ````, ```` and ```` may have a prefix of ``+`` or ``-`` to add and subtract the value, or no prefix to replace. The result of this operation will be stored as a string in ````. If a component did not exist in the original, it will be added to the version as a replace operation. If an error occurred, ``_ERROR`` will contain the error message otherwise it will be a false constant. The optional ``REQUIRE`` allows forcing the components ``PATCH`` and ``TWEAK`` to always be defined. Comparing ^^^^^^^^^ diff --git a/version.cmake b/version.cmake index f822ae8..c774752 100644 --- a/version.cmake +++ b/version.cmake @@ -98,12 +98,19 @@ function(version) string(TOUPPER "${ARGV0}" ARGV0) if(ARGV0 STREQUAL "PARSE") + cmake_parse_arguments( + PARSE_ARGV 2 + _992ae727 + "" + "" + "REQUIRE" + ) # "." ["." ["." ]] # "." ["." ["." ]] [["-"] ] ["+" ] # One regex to rule them all... - if(NOT (ARGV2 MATCHES [[^([0-9]+)\.([0-9]+)(\.[0-9]+|)(\.[0-9]+|)(-?[a-zA-Z0-9]+[a-zA-Z0-9\.\-]*|)(\+[a-zA-Z0-9]+[a-zA-Z0-9\.\-]*|)$]])) - set(${OUT_VAR}_ERROR "Version format not supported: '${ARGV2}'" PARENT_SCOPE) + if(NOT (_992ae727_UNPARSED_ARGUMENTS MATCHES [[^([0-9]+)\.([0-9]+)(\.[0-9]+|)(\.[0-9]+|)(-?[a-zA-Z0-9]+[a-zA-Z0-9\.\-]*|)(\+[a-zA-Z0-9]+[a-zA-Z0-9\.\-]*|)$]])) + set(${OUT_VAR}_ERROR "Version format not supported: '${_992ae727_UNPARSED_ARGUMENTS}'" PARENT_SCOPE) return() endif() @@ -116,14 +123,22 @@ function(version) string(SUBSTRING "${CMAKE_MATCH_3}" 1 -1 _TMP) set(${OUT_VAR}_PATCH "${_TMP}" PARENT_SCOPE) else() - set(${OUT_VAR}_PATCH "" PARENT_SCOPE) + if("PATCH" IN_LIST _992ae727_REQUIRE) + set(${OUT_VAR}_PATCH "0" PARENT_SCOPE) + else() + set(${OUT_VAR}_PATCH "" PARENT_SCOPE) + endif() endif() # {4} = Tweak if((DEFINED CMAKE_MATCH_4) AND (NOT "${CMAKE_MATCH_4}" STREQUAL "")) string(SUBSTRING "${CMAKE_MATCH_4}" 1 -1 _TMP) set(${OUT_VAR}_TWEAK "${_TMP}" PARENT_SCOPE) else() - set(${OUT_VAR}_TWEAK "" PARENT_SCOPE) + if("TWEAK" IN_LIST _992ae727_REQUIRE) + set(${OUT_VAR}_TWEAK "0" PARENT_SCOPE) + else() + set(${OUT_VAR}_TWEAK "" PARENT_SCOPE) + endif() endif() # {5} = PreRelease if((DEFINED CMAKE_MATCH_5) AND (NOT "${CMAKE_MATCH_5}" STREQUAL "")) @@ -270,7 +285,7 @@ function(version) _fe7bcddd "COMPRESS" "MAJOR;MINOR;PATCH;TWEAK;PRERELEASE;BUILD" - "" + "REQUIRE" ) # Helpers @@ -297,7 +312,7 @@ function(version) set(_df8abcce_PRERELEASE "") set(_df8abcce_BUILD "") set(_df8abcce_ERROR "") - version(PARSE _df8abcce "${ARGV2}") + version(PARSE _df8abcce "${ARGV2}" REQUIRE "${_df8abcce_REQUIRE}") if(_df8abcce_ERROR) set(${OUT_VAR}_ERROR ${_df8abcce_ERROR} PARENT_SCOPE) return()