|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2016-2017 Theo Willows |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | +cmake_minimum_required( VERSION 3.0.0 ) |
| 24 | + |
| 25 | +include( CMakeParseArguments ) |
| 26 | + |
| 27 | +function( version_from_git ) |
| 28 | + # Parse arguments |
| 29 | + set( options OPTIONAL FAST ) |
| 30 | + set( oneValueArgs |
| 31 | + GIT_EXECUTABLE |
| 32 | + INCLUDE_HASH |
| 33 | + LOG |
| 34 | + TIMESTAMP |
| 35 | + ) |
| 36 | + set( multiValueArgs ) |
| 37 | + cmake_parse_arguments( ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} ) |
| 38 | + |
| 39 | + # Defaults |
| 40 | + if( NOT DEFINED ARG_INCLUDE_HASH ) |
| 41 | + set( ARG_INCLUDE_HASH ON ) |
| 42 | + endif() |
| 43 | + |
| 44 | + if( DEFINED ARG_GIT_EXECUTABLE ) |
| 45 | + set( GIT_EXECUTABLE "${ARG_GIT_EXECUTABLE}" ) |
| 46 | + else () |
| 47 | + # Find Git or bail out |
| 48 | + find_package( Git ) |
| 49 | + if( NOT GIT_FOUND ) |
| 50 | + message( FATAL_ERROR "[MunkeiVersionFromGit] Git not found" ) |
| 51 | + endif( NOT GIT_FOUND ) |
| 52 | + endif() |
| 53 | + |
| 54 | + # Git describe |
| 55 | + execute_process( |
| 56 | + COMMAND "${GIT_EXECUTABLE}" describe --tags |
| 57 | + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" |
| 58 | + RESULT_VARIABLE git_result |
| 59 | + OUTPUT_VARIABLE git_describe |
| 60 | + ERROR_VARIABLE git_error |
| 61 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 62 | + ERROR_STRIP_TRAILING_WHITESPACE |
| 63 | + ) |
| 64 | + if( NOT git_result EQUAL 0 ) |
| 65 | + message( FATAL_ERROR |
| 66 | + "[MunkeiVersionFromGit] Failed to execute Git: ${git_error}" |
| 67 | + ) |
| 68 | + endif() |
| 69 | + |
| 70 | + # Get Git tag |
| 71 | + execute_process( |
| 72 | + COMMAND "${GIT_EXECUTABLE}" describe --tags --abbrev=0 |
| 73 | + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" |
| 74 | + RESULT_VARIABLE git_result |
| 75 | + OUTPUT_VARIABLE git_tag |
| 76 | + ERROR_VARIABLE git_error |
| 77 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 78 | + ERROR_STRIP_TRAILING_WHITESPACE |
| 79 | + ) |
| 80 | + if( NOT git_result EQUAL 0 ) |
| 81 | + message( FATAL_ERROR |
| 82 | + "[MunkeiVersionFromGit] Failed to execute Git: ${git_error}" |
| 83 | + ) |
| 84 | + endif() |
| 85 | + |
| 86 | + if( git_tag MATCHES "^v(0|[1-9][0-9]*)[.](0|[1-9][0-9]*)[.](0|[1-9][0-9]*)(-[.0-9A-Za-z-]+)?([+][.0-9A-Za-z-]+)?$" ) |
| 87 | + set( version_major "${CMAKE_MATCH_1}" ) |
| 88 | + set( version_minor "${CMAKE_MATCH_2}" ) |
| 89 | + set( version_patch "${CMAKE_MATCH_3}" ) |
| 90 | + set( identifiers "${CMAKE_MATCH_4}" ) |
| 91 | + set( metadata "${CMAKE_MATCH_5}" ) |
| 92 | + else() |
| 93 | + message( FATAL_ERROR |
| 94 | + "[MunkeiVersionFromGit] Git tag isn't valid semantic version: [${git_tag}]" |
| 95 | + ) |
| 96 | + endif() |
| 97 | + |
| 98 | + if( "${git_tag}" STREQUAL "${git_describe}" ) |
| 99 | + set( git_at_a_tag ON ) |
| 100 | + endif() |
| 101 | + |
| 102 | + if( NOT git_at_a_tag ) |
| 103 | + # Extract the Git hash (if one exists) |
| 104 | + string( REGEX MATCH "g[0-9a-f]+$" git_hash "${git_describe}" ) |
| 105 | + endif() |
| 106 | + |
| 107 | + # Construct the version variables |
| 108 | + set( version ${version_major}.${version_minor}.${version_patch} ) |
| 109 | + set( semver ${version} ) |
| 110 | + |
| 111 | + # Identifiers |
| 112 | + if( identifiers MATCHES ".+" ) |
| 113 | + string( SUBSTRING "${identifiers}" 1 -1 identifiers ) |
| 114 | + set( semver "${semver}-${identifiers}") |
| 115 | + endif() |
| 116 | + |
| 117 | + # Metadata |
| 118 | + # TODO Split and join (add Git hash inbetween) |
| 119 | + if( metadata MATCHES ".+" ) |
| 120 | + string( SUBSTRING "${metadata}" 1 -1 metadata ) |
| 121 | + # Split |
| 122 | + string( REPLACE "." ";" metadata "${metadata}" ) |
| 123 | + endif() |
| 124 | + |
| 125 | + if( NOT git_at_a_tag ) |
| 126 | + |
| 127 | + if( ARG_INCLUDE_HASH ) |
| 128 | + list( APPEND metadata "${git_hash}" ) |
| 129 | + endif( ARG_INCLUDE_HASH ) |
| 130 | + |
| 131 | + # Timestamp |
| 132 | + if( DEFINED ARG_TIMESTAMP ) |
| 133 | + string( TIMESTAMP timestamp "${ARG_TIMESTAMP}" ${ARG_UTC} ) |
| 134 | + list( APPEND metadata "${timestamp}" ) |
| 135 | + endif( DEFINED ARG_TIMESTAMP ) |
| 136 | + |
| 137 | + endif() |
| 138 | + |
| 139 | + # Join |
| 140 | + string( REPLACE ";" "." metadata "${metadata}" ) |
| 141 | + |
| 142 | + if( metadata MATCHES ".+" ) |
| 143 | + set( semver "${semver}+${metadata}") |
| 144 | + endif() |
| 145 | + |
| 146 | + # Log the results |
| 147 | + if( ARG_LOG ) |
| 148 | + message( STATUS |
| 149 | + "[MunkeiVersionFromGit] Version: ${version} |
| 150 | + Git tag: [${git_tag}] |
| 151 | + Git hash: [${git_hash}] |
| 152 | + Decorated: [${git_describe}] |
| 153 | + Identifiers: [${identifiers}] |
| 154 | + Metadata: [${metadata}] |
| 155 | + SemVer: [${semver}]" |
| 156 | + ) |
| 157 | + endif( ARG_LOG ) |
| 158 | + |
| 159 | + # Set parent scope variables |
| 160 | + set( GIT_TAG ${git_tag} PARENT_SCOPE ) |
| 161 | + set( SEMVER ${semver} PARENT_SCOPE ) |
| 162 | + set( VERSION ${version} PARENT_SCOPE ) |
| 163 | + set( VERSION_MAJOR ${version_major} PARENT_SCOPE ) |
| 164 | + set( VERSION_MINOR ${version_minor} PARENT_SCOPE ) |
| 165 | + set( VERSION_PATCH ${version_patch} PARENT_SCOPE ) |
| 166 | + |
| 167 | +endfunction( version_from_git ) |
0 commit comments