From f46025f26ccbdbce62df2c1ffc8cf49d33305e47 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Mon, 4 Jul 2022 03:09:56 +0200 Subject: [PATCH] Version 0.1 --- .github/ISSUE_TEMPLATE/config.yml | 2 + .github/ISSUE_TEMPLATE/feature.yml | 21 ++ .github/ISSUE_TEMPLATE/issue.yml | 137 ++++++++++++ .github/pull_request_template.md | 8 + .vscode/tasks.json | 21 ++ cmake-version.code-workspace | 10 + docs/conf.py | 56 +++++ docs/index.rst | 7 + docs/version.rst | 76 +++++++ tests/CMakeLists.txt | 108 +++++++++ tests/helpers.cmake | 234 ++++++++++++++++++++ tools/Makefile | 20 ++ tools/make.bat | 35 +++ version.cmake | 340 +++++++++++++++++++++++++++++ 14 files changed, 1075 insertions(+) create mode 100644 .github/ISSUE_TEMPLATE/config.yml create mode 100644 .github/ISSUE_TEMPLATE/feature.yml create mode 100644 .github/ISSUE_TEMPLATE/issue.yml create mode 100644 .github/pull_request_template.md create mode 100644 .vscode/tasks.json create mode 100644 cmake-version.code-workspace create mode 100644 docs/conf.py create mode 100644 docs/index.rst create mode 100644 docs/version.rst create mode 100644 tests/CMakeLists.txt create mode 100644 tests/helpers.cmake create mode 100644 tools/Makefile create mode 100644 tools/make.bat create mode 100644 version.cmake diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 0000000..cc22735 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,2 @@ +blank_issues_enabled: false +contact_links: diff --git a/.github/ISSUE_TEMPLATE/feature.yml b/.github/ISSUE_TEMPLATE/feature.yml new file mode 100644 index 0000000..db29eea --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature.yml @@ -0,0 +1,21 @@ +--- +name: "Suggest an Enhancment to a Feature or a new Feature" +title: "Replace Me Or Have Your Request Closed" +description: "Is there something lacking that you would like to have supported?" +labels: ["enhancement", "help wanted"] +body: +- type: textarea + attributes: + label: "Explain the Feature/Enhancement" + description: "Describe the feature or enhancement in as much detail as possible, leave nothing out. If you think images or example videos help describe the Feature/Enhancement, include them." + validations: + required: true +- type: textarea + attributes: + label: "Examples" + description: "Provide some examples of how this feature would/should be used." + validations: + required: true +- type: textarea + attributes: + label: "Any additional information we need to know?" diff --git a/.github/ISSUE_TEMPLATE/issue.yml b/.github/ISSUE_TEMPLATE/issue.yml new file mode 100644 index 0000000..31892b5 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/issue.yml @@ -0,0 +1,137 @@ +name: "Report a Bug/Crash/Freeze" +title: "Replace Me Or Have Your Report Closed" +description: "Encountered a problem, a bug or a crash?" +labels: ["bug", "help wanted"] +body: +- type: dropdown + attributes: + label: "Operating System" + description: "What Operating System do you use for OBS Studio and StreamFX? Select 'Other' if your Operating System is not listed" + options: + - "Windows 10 1903 and higher" + - "MacOS 10.15 and higher" + - "Linux (like Debian/Ubuntu)" + - "Linux (like Arch Linux)" + - "Linux (like FreeBSD)" + - "(Other)" + validations: + required: true +- type: dropdown + attributes: + label: "OBS Studio Version?" + description: "Which OBS Studio version are you using to run StreamFX? Versions include all patches (the third element in 'A.B.C'), and release candidates for that version. Select 'Other' if you are using a custom build." + options: + - "27.1" + - "27.0" + - "26.1" + - "26.0" + - "25.0" + - "(Other)" + validations: + required: true +- type: dropdown + attributes: + label: "StreamFX Version" + description: "On which StreamFX version did you first encounter this issue?" + options: + - "0.12.0a1" + - "0.11.1" + - "0.11.1b1" + - "0.11.1a1" + - "0.11.0" + - "0.11.0c1" + - "0.11.0b3" + - "0.11.0b2" + - "0.11.0b1" + - "0.11.0a8" + - "0.11.0a7" + - "0.11.0a6" + - "0.11.0a5" + - "0.11.0a4" + - "0.11.0a3" + - "0.11.0a2" + - "0.11.0a1" + - "0.10.1" + - "0.10.0" + - "0.10.0b3" + - "0.10.0b2" + - "0.10.0b1" + - "0.10.0a2" + - "0.10.0a1" + - "0.10.0a0" + - "0.9.3" + - "0.9.2" + - "0.9.1" + - "0.9.0" + - "0.9.0a3" + - "0.9.0a2" + - "0.9.0a1" + - "0.8.3" + - "0.8.2" + - "0.8.1" + - "0.8.0" + - "0.8.0b3" + - "0.8.0b2" + - "0.8.0b2hf2" + - "0.8.0b2hf1" + - "0.8.0b1" + - "0.8.0a4" + - "0.8.0a3" + - "0.8.0a2" + - "0.8.0a1" + - "0.7.2" + - "0.7.1" + - "0.7.0" + - "0.6.3" + - "0.6.2" + - "0.6.1" + - "0.6.0" + - "0.5.2" + - "0.5.1" + - "0.5.0" + - "0.5.0preview3" + - "0.5.0preview2" + - "0.5.0preview1" + - "0.4.3" + - "0.4.3preview1" + - "0.4.2" + - "0.4.1" + - "0.4.0" + - "0.3.0" + - "0.2.0" + - "0.1.1" + - "0.1.0" + validations: + required: true +- type: input + attributes: + label: "OBS Studio Log" + description: "Upload a normal log file that showcases the issue happening. If you encountered a crash, also fill out the next field." + validations: + required: true +- type: textarea + attributes: + label: "OBS Studio Crash Log" + description: "If OBS Studio crashed (not froze) paste the crash log here, or upload it somewhere and paste the link here." +- type: textarea + attributes: + label: "Current Behavior" + description: "What actually happened when you used StreamFX?" + validations: + required: true +- type: textarea + attributes: + label: "Expected Behavior" + description: "What did you expect to happen when using StreamFX?" + validations: + required: true +- type: textarea + attributes: + label: "Steps to Reproduce the Bug" + description: "What steps are required to consistently reproduce the bug/crash/freeze?" + validations: + required: true +- type: textarea + attributes: + label: "Any additional Information we need to know?" + description: "If you answered '(Other)' to any system relevant information, explain what you mean by that here." diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 0000000..9e66251 --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,8 @@ +### Explain the Pull Request + + + + +### Checklist +- [ ] I will become the maintainer for this part of code. +- [ ] I have tested this code. diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..b8c0a7f --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,21 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "Test", + "type": "shell", + "command": "cmake", + "args": [ + "-Htests", + "-Bbuild/tests" + ], + "group": { + "kind": "test", + "isDefault": true + }, + "problemMatcher": [] + }, + ] +} \ No newline at end of file diff --git a/cmake-version.code-workspace b/cmake-version.code-workspace new file mode 100644 index 0000000..a4de677 --- /dev/null +++ b/cmake-version.code-workspace @@ -0,0 +1,10 @@ +{ + "folders": [ + { + "path": "." + } + ], + "settings": { + "editor.tabSize": 4 + } +} \ No newline at end of file diff --git a/docs/conf.py b/docs/conf.py new file mode 100644 index 0000000..08b9b95 --- /dev/null +++ b/docs/conf.py @@ -0,0 +1,56 @@ +# Configuration file for the Sphinx documentation builder. +# +# This file only contains a selection of the most common options. For a full +# list see the documentation: +# https://www.sphinx-doc.org/en/master/usage/configuration.html + +# -- Path setup -------------------------------------------------------------- + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +# +# import os +# import sys +# sys.path.insert(0, os.path.abspath('.')) + + +# -- Project information ----------------------------------------------------- + +project = 'version' +copyright = "2022, Michael Fabian Dirks" +author = "Michael Fabian 'Xaymar' Dirks" +version = "0.1" +release = "0.1" + + +# -- General configuration --------------------------------------------------- + +# Add any Sphinx extension module names here, as strings. They can be +# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom +# ones. +extensions = [ +] + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +# This pattern also affects html_static_path and html_extra_path. +exclude_patterns = [ + "Thumbs.db" + ".DS_Store" +] + +# -- Options for HTML output ------------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +# +html_theme = 'sphinx_rtd_theme' + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ['_static'] \ No newline at end of file diff --git a/docs/index.rst b/docs/index.rst new file mode 100644 index 0000000..69167dc --- /dev/null +++ b/docs/index.rst @@ -0,0 +1,7 @@ +Version handling for CMake +========================================= + +.. toctree:: + :maxdepth: 1 + + version diff --git a/docs/version.rst b/docs/version.rst new file mode 100644 index 0000000..23fc3d5 --- /dev/null +++ b/docs/version.rst @@ -0,0 +1,76 @@ +version +------- + +Generate, parse and modify versions purely with CMake. Supports Semantic Versioning 2.0.0, 1.0.0 and other version formats. + +Synopsis +^^^^^^^^ + +.. parsed-literal:: + + version(`PARSE`_ ) + version(`GENERATE`_ [COMPRESS] [MAJOR ] [MINOR ] [PATCH ] [TWEAK ] [PRERELEASE ] [BUILD ]) + version(`MODIFY`_ [COMPRESS] [MAJOR ] [MINOR ] [TWEAK ] [PRERELEASE ] [BUILD ]) + version(`COMPARE`_ ) + + +The following version constructs are currently supported: + +.. code-block:: + + "." ["." ["." ]] + "." ["." ["." ]] [["-"] ] ["+" ] + + +Parsing +^^^^^^^ + +.. _PARSE: + +.. code-block:: cmake + + version(PARSE ) + +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 undefined. If an error occurred, ``_ERROR`` will be defined and contain the error message. + +Generating +^^^^^^^^^^ + +.. _GENERATE: + +.. code-block:: cmake + + version(GENERATE [COMPRESS] [MAJOR ] [MINOR ] [PATCH ] [TWEAK ] [PRERELEASE ] [BUILD ]) + +Generates a version from the components provided and stores the result in ````. The components ```` and ```` will default to ``0`` if not provided. If an error occurred, ``_ERROR`` will be defined and contain the error message. + +Modifying +^^^^^^^^^ + +.. _MODIFY: + +.. code-block:: cmake + + version(MODIFY [COMPRESS] [MAJOR ] [MINOR ] [TWEAK ] [PRERELEASE ] [BUILD ]) + +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 be defined and contain the error message. + +Comparing +^^^^^^^^^ + +.. _COMPARE: + +.. code-block:: cmake + + version(COMPARE ) + +Compares the version ```` against ```` and stores the result in ````. The provided version will be evaluated in the order MAJOR, MINOR, PATCH, TWEAK, PRERELEASE, and then BUILD. The following results should be expected: + +- If a component is only in ````, ```` will contain the componenent name prefixed by ``+``. +- If a component is only in ````, ```` will contain the componenent name prefixed by ``-``. +- If a component is numerical and the value is larger in ````, ```` will contain the componenent name prefixed by ``>``. +- If a component is numerical and the value is larger in ````, ```` will contain the componenent name prefixed by ``<``. +- If a component is alphanumerical and the value is different in either, ```` will contain the component name with no prefix. +- In all other cases, ```` will be empty. +- If an error occurred, ``_ERROR`` will be defined and contain the error message, and ```` will be undefined. + diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..4eff2a6 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,108 @@ +cmake_minimum_required(VERSION 3.23) +project(cmake-version) + +include("helpers.cmake") + +# ToDo: Add even more test cases, as we don't want a burning bar because someone asked where the toilet is. + +# Generation +message(STATUS "\n\n===== Generation =====") +# - Valid Cases +test_generate("0.0") +test_generate("1.0" MAJOR 1) +test_generate("0.2" MINOR 2) +test_generate("0.0.3" PATCH 3) +test_generate("0.0.0.4" TWEAK 4) +test_generate("0.0-5" COMPRESS PRERELEASE 5) +test_generate("0.0a5" COMPRESS PRERELEASE a5) +test_generate("0.0+6" BUILD 6) +# - Invalid Cases +test_generate("-1.0.0" FAIL MAJOR "-1") +test_generate("0.-1.0" FAIL MINOR "-1") +test_generate("0.0.-1" FAIL PATCH "-1") +test_generate("0.0.0.-1" FAIL TWEAK "-1") +test_generate("0.0.0-#" FAIL PRERELEASE "#") +test_generate("0.0.0-0 0" FAIL PRERELEASE "0 0") +test_generate("0.0.0+#" FAIL BUILD "#") +test_generate("0.0.0+0 0" FAIL BUILD "0 0") + +# Parsing +message(STATUS "\n\n===== Parsing =====") +# - Valid Cases +test_parse("1.2" MAJOR 1 MINOR 2) +test_parse("1.2.3" MAJOR 1 MINOR 2 PATCH 3) +test_parse("1.2.3.4" MAJOR 1 MINOR 2 PATCH 3 TWEAK 4) +test_parse("1.2-5" MAJOR 1 MINOR 2 PRERELEASE 5) +test_parse("1.2.3-5" MAJOR 1 MINOR 2 PATCH 3 PRERELEASE 5) +test_parse("1.2.3.4-5" MAJOR 1 MINOR 2 PATCH 3 TWEAK 4 PRERELEASE 5) +test_parse("1.2a5" MAJOR 1 MINOR 2 PRERELEASE a5) +test_parse("1.2.3a5" MAJOR 1 MINOR 2 PATCH 3 PRERELEASE a5) +test_parse("1.2.3.4a5" MAJOR 1 MINOR 2 PATCH 3 TWEAK 4 PRERELEASE a5) +test_parse("1.2+6" MAJOR 1 MINOR 2 BUILD 6) +test_parse("1.2.3+6" MAJOR 1 MINOR 2 PATCH 3 BUILD 6) +test_parse("1.2.3.4+6" MAJOR 1 MINOR 2 PATCH 3 TWEAK 4 BUILD 6) +test_parse("1.2-5+6" MAJOR 1 MINOR 2 PRERELEASE 5 BUILD 6) +test_parse("1.2.3-5+6" MAJOR 1 MINOR 2 PATCH 3 PRERELEASE 5 BUILD 6) +test_parse("1.2.3.4-5+6" MAJOR 1 MINOR 2 PATCH 3 TWEAK 4 PRERELEASE 5 BUILD 6) +test_parse("1.2a5+6" MAJOR 1 MINOR 2 PRERELEASE a5 BUILD 6) +test_parse("1.2.3a5+6" MAJOR 1 MINOR 2 PATCH 3 PRERELEASE a5 BUILD 6) +test_parse("1.2.3.4a5+6" MAJOR 1 MINOR 2 PATCH 3 TWEAK 4 PRERELEASE a5 BUILD 6) +# - Invalid Cases +test_parse("" FAIL) +test_parse("cmake" FAIL) +test_parse("0" FAIL) +test_parse("-1.0.0" FAIL) +test_parse("11.bad.0" FAIL) +test_parse("0.0.0+.0" FAIL) + +# Modifying +message(STATUS "\n\n===== Modifying =====") +# - Valid Cases +test_modify("1.0" "0.0" MAJOR 1) +test_modify("1.0" "0.0" MAJOR "+1") +test_modify("1.0" "2.0" MAJOR "-1") +test_modify("0.2" "0.0" MINOR 2) +test_modify("0.2" "0.0" MINOR "+2") +test_modify("0.2" "0.3" MINOR "-1") +test_modify("0.0.3" "0.0" PATCH 3) +test_modify("0.0.3" "0.0.2" PATCH "+1") +test_modify("0.0.3" "0.0.4" PATCH "-1") +test_modify("0.0.0.4" "0.0" TWEAK 4) +test_modify("0.0.0.4" "0.0.0.3" TWEAK "+1") +test_modify("0.0.0.4" "0.0.0.5" TWEAK "-1") +test_modify("0.0-5" "0.0" COMPRESS PRERELEASE 5) +test_modify("0.0a5" "0.0" COMPRESS PRERELEASE a5) +test_modify("0.0+6" "0.0" COMPRESS BUILD 6) +test_modify("0.0-5+6" "0.0" COMPRESS PRERELEASE 5 BUILD 6) +test_modify("0.0a5+6" "0.0" COMPRESS PRERELEASE a5 BUILD 6) +# - Invalid Cases +test_modify("0.0" "0.0" FAIL MAJOR -1) +test_modify("0.0" "0.0" FAIL MINOR -1) +test_modify("0.0" "0.0" FAIL PATCH -1) +test_modify("0.0" "0.0" FAIL TWEAK -1) +test_modify("0.0" "0.0" FAIL COMPRESS PRERELEASE "#") +test_modify("0.0" "0.0" FAIL COMPRESS BUILD "#") + +# Comparing +message(STATUS "\n\n===== Comparing =====") +# - Base Components Comparison +test_compare("0.0" "" "0.0") +test_compare("1.0" ">MAJOR" "0.0") +test_compare("0.0" "MINOR" "0.0") +test_compare("0.0" "PATCH" "0.0.0") +test_compare("0.0.0" "TWEAK" "0.0.0.0") +test_compare("0.0.0.0" "NUL 2>NUL +if errorlevel 9009 ( + echo. + echo.The 'sphinx-build' command was not found. Make sure you have Sphinx + echo.installed, then set the SPHINXBUILD environment variable to point + echo.to the full path of the 'sphinx-build' executable. Alternatively you + echo.may add the Sphinx directory to PATH. + echo. + echo.If you don't have Sphinx installed, grab it from + echo.https://www.sphinx-doc.org/ + exit /b 1 +) + +if "%1" == "" goto help + +%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% +goto end + +:help +%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% + +:end +popd diff --git a/version.cmake b/version.cmake new file mode 100644 index 0000000..0ac4fa8 --- /dev/null +++ b/version.cmake @@ -0,0 +1,340 @@ +# Copyright (C) 2022, Michael Fabian 'Xaymar' Dirks . All Rights Reserved +# +# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. +# +# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +# For SemVer 2.0.0 + +function(version) + unset(${OUT_VAR} PARENT_SCOPE) + unset(${OUT_VAR}_ERROR PARENT_SCOPE) + set(OUT_VAR "${ARGV1}") + set(_ "") + + string(TOUPPER "${ARGV0}" ARGV0) + if(ARGV0 STREQUAL "PARSE") + # "." ["." ["." ]] + # "." ["." ["." ]] [["-"] ] ["+" ] + + # 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) + return() + endif() + + # {1} = Major + set(${OUT_VAR}_MAJOR "${CMAKE_MATCH_1}" PARENT_SCOPE) + # {2} = Minor + set(${OUT_VAR}_MINOR "${CMAKE_MATCH_2}" PARENT_SCOPE) + # {3} = Patch + if(CMAKE_MATCH_3) + string(SUBSTRING "${CMAKE_MATCH_3}" 1 -1 _TMP) + set(${OUT_VAR}_PATCH "${_TMP}" PARENT_SCOPE) + else() + unset(${OUT_VAR}_PATCH PARENT_SCOPE) + endif() + # {4} = Tweak + if(CMAKE_MATCH_4) + string(SUBSTRING "${CMAKE_MATCH_4}" 1 -1 _TMP) + set(${OUT_VAR}_TWEAK "${_TMP}" PARENT_SCOPE) + else() + unset(${OUT_VAR}_TWEAK PARENT_SCOPE) + endif() + # {5} = PreRelease + if(CMAKE_MATCH_5) + string(SUBSTRING "${CMAKE_MATCH_5}" 0 1 _TMP) + if(_TMP STREQUAL "-") + string(SUBSTRING "${CMAKE_MATCH_5}" 1 -1 _TMP) + set(${OUT_VAR}_PRERELEASE "${_TMP}" PARENT_SCOPE) + else() + set(${OUT_VAR}_PRERELEASE "${CMAKE_MATCH_5}" PARENT_SCOPE) + endif() + else() + unset(${OUT_VAR}_PRERELEASE PARENT_SCOPE) + endif() + # {6} = Build + if(CMAKE_MATCH_6) + string(SUBSTRING "${CMAKE_MATCH_6}" 1 -1 _TMP) + set(${OUT_VAR}_BUILD "${_TMP}" PARENT_SCOPE) + else() + unset(${OUT_VAR}_BUILD PARENT_SCOPE) + endif() + elseif(ARGV0 STREQUAL "GENERATE") + cmake_parse_arguments( + PARSE_ARGV 2 + _ARGS + "COMPRESS" + "MAJOR;MINOR;PATCH;TWEAK;PRERELEASE;BUILD" + "" + ) + + # Do we have the major component, and is it valid? + if(DEFINED _ARGS_MAJOR) + string(STRIP "${_ARGS_MAJOR}" _ARGS_MAJOR) + if(_ARGS_MAJOR STREQUAL "") + set(_ARGS_MAJOR 0) + elseif(NOT (_ARGS_MAJOR MATCHES [[^[0-9]+$]])) + set(${OUT_VAR}_ERROR "MAJOR component must be a numeric identifier, but is: '${_ARGS_MAJOR}'" PARENT_SCOPE) + return() + endif() + else() + set(_ARGS_MAJOR 0) + endif() + set(_ "${_}${_ARGS_MAJOR}") + + # Do we have the minor component, and is it valid? + if(DEFINED _ARGS_MINOR) + string(STRIP "${_ARGS_MINOR}" _ARGS_MINOR) + if(_ARGS_MINOR STREQUAL "") + set(_ARGS_MINOR 0) + elseif(NOT (_ARGS_MINOR MATCHES [[^[0-9]+$]])) + set(${OUT_VAR}_ERROR "MINOR component must be a numeric identifier, but is: '${_ARGS_MINOR}'" PARENT_SCOPE) + return() + endif() + else() + set(_ARGS_MINOR 0) + endif() + set(_ "${_}.${_ARGS_MINOR}") + + # Do we have the patch component, and is it valid? + if(DEFINED _ARGS_PATCH) + string(STRIP "${_ARGS_PATCH}" _ARGS_PATCH) + if(_ARGS_PATCH STREQUAL "") + unset(_ARGS_PATCH) + elseif(_ARGS_PATCH MATCHES [[^[0-9]+$]]) + set(_ "${_}.${_ARGS_PATCH}") + else() + set(${OUT_VAR}_ERROR "PATCH component must be a numeric identifier, but is: '${_ARGS_PATCH}'" PARENT_SCOPE) + return() + endif() + endif() + + # Do we have the tweak component, and is it valid? + if(DEFINED _ARGS_TWEAK) + string(STRIP "${_ARGS_TWEAK}" _ARGS_TWEAK) + if(_ARGS_TWEAK STREQUAL "") + unset(_ARGS_TWEAK) + elseif(_ARGS_TWEAK MATCHES [[^[0-9]+$]]) + if(NOT DEFINED _ARGS_PATCH) + set(_ "${_}.0") + endif() + set(_ "${_}.${_ARGS_TWEAK}") + else() + set(${OUT_VAR}_ERROR "TWEAK component must be a numeric identifier, but is: '${_ARGS_TWEAK}'" PARENT_SCOPE) + return() + endif() + endif() + + # Do we have the pre-release component, and is it valid? + if(DEFINED _ARGS_PRERELEASE) + string(STRIP "${_ARGS_PRERELEASE}" _ARGS_PRERELEASE) + if(_ARGS_PRERELEASE STREQUAL "") + unset(_ARGS_PRERELEASE) + elseif(_ARGS_PRERELEASE MATCHES [[^[a-zA-Z0-9]+[a-zA-Z0-9\-\.]*$]]) + if(_ARGS_COMPRESS AND (_ARGS_PRERELEASE MATCHES [[[a-zA-Z]+[a-zA-Z0-9\\-\\.]*$]])) + set(_ "${_}${_ARGS_PRERELEASE}") + else() + set(_ "${_}-${_ARGS_PRERELEASE}") + endif() + else() + set(${OUT_VAR}_ERROR "PRERELEASE component must be an alphanumeric identifier, but is: '${_ARGS_PRERELEASE}'" PARENT_SCOPE) + return() + endif() + endif() + + # Do we have the build component, and is it valid? + if(DEFINED _ARGS_BUILD) + string(STRIP "${_ARGS_BUILD}" _ARGS_PRERELEASE) + if(_ARGS_BUILD STREQUAL "") + unset(_ARGS_BUILD) + elseif(_ARGS_BUILD MATCHES [[^[a-zA-Z0-9]+[a-zA-Z0-9\-\.]*$]]) + set(_ "${_}+${_ARGS_BUILD}") + else() + set(${OUT_VAR}_ERROR "BUILD component must be an alphanumeric identifier, but is: '${_ARGS_BUILD}'" PARENT_SCOPE) + return() + endif() + endif() + + set(${OUT_VAR} "${_}" PARENT_SCOPE) + elseif(ARGV0 STREQUAL "MODIFY") + # Requires PARSE and GENERATE. + cmake_parse_arguments( + PARSE_ARGV 3 + _ARGS + "COMPRESS" + "MAJOR;MINOR;PATCH;BUILD;PRERELEASE;COMMIT" + "" + ) + + # Helpers + macro(modify_version_number) + if(DEFINED ${ARGV1}) + if(${ARGV1} MATCHES "^(\\+|\\-)([0-9]+)$") + if(DEFINED ${ARGV0}) + math(EXPR ${ARGV0} "${${ARGV0}} ${CMAKE_MATCH_1} ${CMAKE_MATCH_2}") + else() + math(EXPR ${ARGV0} "0 ${CMAKE_MATCH_1} ${CMAKE_MATCH_2}") + endif() + else() + set(${ARGV0} "${${ARGV1}}") + endif() + endif() + endmacro() + + # Parse the given version. + version(PARSE _TEMP "${ARGV2}") + if(DEFINED _TEMP_ERROR) + set(${OUT_VAR}_ERROR ${_TEMP_ERROR} PARENT_SCOPE) + return() + endif() + + # Modify core version components + modify_version_number(_TEMP_MAJOR _ARGS_MAJOR) + modify_version_number(_TEMP_MINOR _ARGS_MINOR) + modify_version_number(_TEMP_PATCH _ARGS_PATCH) + modify_version_number(_TEMP_TWEAK _ARGS_TWEAK) + + # Replace other components if defined. + if(DEFINED _ARGS_PRERELEASE) + set(_TEMP_PRERELEASE "${_ARGS_PRERELEASE}") + endif() + if(DEFINED _ARGS_BUILD) + set(_TEMP_BUILD "${_ARGS_BUILD}") + endif() + + # Generate a new version. + set(_ARGS "") + if(DEFINED _TEMP_MAJOR AND (NOT _TEMP_MAJOR STREQUAL "")) + set(_ARGS "${_ARGS} MAJOR \"${_TEMP_MAJOR}\"") + endif() + if(DEFINED _TEMP_MINOR AND (NOT _TEMP_MINOR STREQUAL "")) + set(_ARGS "${_ARGS} MINOR \"${_TEMP_MINOR}\"") + endif() + if(DEFINED _TEMP_PATCH AND (NOT _TEMP_PATCH STREQUAL "")) + set(_ARGS "${_ARGS} PATCH \"${_TEMP_PATCH}\"") + endif() + if(DEFINED _TEMP_TWEAK AND (NOT _TEMP_TWEAK STREQUAL "")) + set(_ARGS "${_ARGS} TWEAK \"${_TEMP_TWEAK}\"") + endif() + if(DEFINED _TEMP_PRERELEASE AND (NOT _TEMP_PRERELEASE STREQUAL "")) + set(_ARGS "${_ARGS} PRERELEASE \"${_TEMP_PRERELEASE}\"") + endif() + if(DEFINED _TEMP_BUILD AND (NOT _TEMP_BUILD STREQUAL "")) + set(_ARGS "${_ARGS} BUILD \"${_TEMP_BUILD}\"") + endif() + if(DEFINED _ARGS_COMPRESS) + set(_ARGS "${_ARGS} COMPRESS") + endif() + cmake_language(EVAL CODE "version(GENERATE _TEMP ${_ARGS})") + if(DEFINED _TEMP_ERROR) + set(${OUT_VAR}_ERROR ${_TEMP_ERROR} PARENT_SCOPE) + return() + endif() + set(${OUT_VAR} "${_TEMP}" PARENT_SCOPE) + elseif(ARGV0 STREQUAL "COMPARE") + # Requires PARSE. + + # Parse first version + version(PARSE _A "${ARGV2}") + if(DEFINED _A_ERROR) + set(${OUT_VAR}_ERROR ${_A_ERROR} PARENT_SCOPE) + return() + endif() + + # Parse second version + version(PARSE _B "${ARGV3}") + if(DEFINED _B_ERROR) + set(${OUT_VAR}_ERROR ${_B_ERROR} PARENT_SCOPE) + return() + endif() + + # Major + if(_A_MAJOR GREATER _B_MAJOR) + set(${OUT_VAR} ">MAJOR" PARENT_SCOPE) + return() + elseif(_A_MAJOR LESS _B_MAJOR) + set(${OUT_VAR} "MINOR" PARENT_SCOPE) + return() + elseif(_A_MINOR LESS _B_MINOR) + set(${OUT_VAR} "PATCH" PARENT_SCOPE) + return() + elseif(_A_PATCH LESS _B_PATCH) + set(${OUT_VAR} "TWEAK" PARENT_SCOPE) + return() + elseif(_A_TWEAK LESS _B_TWEAK) + set(${OUT_VAR} "