|
| 1 | +#---------------------------------*-CMake-*----------------------------------# |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# https://github.com/sethrj/cmake-git-version |
| 5 | +# |
| 6 | +# Copyright 2021 UT-Battelle, LLC and Seth R Johnson |
| 7 | +# |
| 8 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +# you may not use this file except in compliance with the License. |
| 10 | +# You may obtain a copy of the License at |
| 11 | +# |
| 12 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +# |
| 14 | +# Unless required by applicable law or agreed to in writing, software |
| 15 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +# See the License for the specific language governing permissions and |
| 18 | +# limitations under the License. |
| 19 | +#[=======================================================================[.rst: |
| 20 | +
|
| 21 | +CgvFindVersion |
| 22 | +-------------- |
| 23 | +
|
| 24 | +.. command:: cgv_find_version |
| 25 | +
|
| 26 | + Get the project version using Git descriptions to ensure the version numbers |
| 27 | + are always synchronized between Git and CMake:: |
| 28 | +
|
| 29 | + cgv_find_version([<projname>]) |
| 30 | +
|
| 31 | + ``<projname>`` |
| 32 | + Name of the project. |
| 33 | +
|
| 34 | + This command sets the following variables in the parent package:: |
| 35 | +
|
| 36 | + ${projname}_VERSION |
| 37 | + ${projname}_VERSION_STRING |
| 38 | +
|
| 39 | + It takes the project name and version file path as optional arguments to |
| 40 | + support using it before the CMake ``project`` command. |
| 41 | +
|
| 42 | + The project version string uses an approximation to SemVer strings, appearing |
| 43 | + as v0.1.2 if the version is actually a tagged release, or v0.1.3+abcdef if |
| 44 | + it's not. |
| 45 | +
|
| 46 | + If a non-tagged version is exported, or an untagged shallow git clone is used, |
| 47 | + it's impossible to determine the version from the tag, so a warning will be |
| 48 | + issued and the version will be set to 0.0.0. |
| 49 | +
|
| 50 | + The exact regex used to match the version tag is:: |
| 51 | +
|
| 52 | + v([0-9.]+)(-dev[0-9.]+)? |
| 53 | +
|
| 54 | +
|
| 55 | + .. note:: In order for this script to work properly with archived git |
| 56 | + repositories (generated with ``git-archive`` or GitHub's release tarball |
| 57 | + feature), it's necessary to add to your ``.gitattributes`` file:: |
| 58 | +
|
| 59 | + CgvFindVersion.cmake export-subst |
| 60 | +
|
| 61 | +#]=======================================================================] |
| 62 | + |
| 63 | +if(CMAKE_SCRIPT_MODE_FILE) |
| 64 | + cmake_minimum_required(VERSION 3.8) |
| 65 | +endif() |
| 66 | + |
| 67 | +function(cgv_find_version) |
| 68 | + set(projname "${ARGV0}") |
| 69 | + if(NOT projname) |
| 70 | + set(projname "${CMAKE_PROJECT_NAME}") |
| 71 | + if(NOT projname) |
| 72 | + message(FATAL_ERROR "Project name is not defined") |
| 73 | + endif() |
| 74 | + endif() |
| 75 | + |
| 76 | + # Get a possible Git version generated using git-archive (see the |
| 77 | + # .gitattributes file) |
| 78 | + set(_ARCHIVE_TAG "$Format:%D$") |
| 79 | + set(_ARCHIVE_HASH "$Format:%h$") |
| 80 | + |
| 81 | + set(_TAG_REGEX "v([0-9.]+)(-dev[0-9.]+)?") |
| 82 | + set(_HASH_REGEX "([0-9a-f]+)") |
| 83 | + |
| 84 | + if(_ARCHIVE_HASH MATCHES "%h") |
| 85 | + # Not a "git archive": use live git information |
| 86 | + set(_CACHE_VAR "${projname}_GIT_DESCRIBE") |
| 87 | + set(_CACHED_VERSION "${${_CACHE_VAR}}") |
| 88 | + if(NOT _CACHED_VERSION) |
| 89 | + # Building from a git checkout rather than a distribution |
| 90 | + if(NOT GIT_EXECUTABLE) |
| 91 | + find_package(Git QUIET REQUIRED) |
| 92 | + endif() |
| 93 | + execute_process( |
| 94 | + COMMAND "${GIT_EXECUTABLE}" "describe" "--tags" "--match" "v*" |
| 95 | + WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}" |
| 96 | + ERROR_VARIABLE _GIT_ERR |
| 97 | + OUTPUT_VARIABLE _VERSION_STRING |
| 98 | + RESULT_VARIABLE _GIT_RESULT |
| 99 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 100 | + ) |
| 101 | + if(_GIT_RESULT) |
| 102 | + message(AUTHOR_WARNING "No git tags in ${projname} matched 'v*': " |
| 103 | + "${_GIT_ERR}") |
| 104 | + elseif(NOT _VERSION_STRING) |
| 105 | + message(WARNING "Failed to get ${projname} version from git: " |
| 106 | + "git describe returned an empty string") |
| 107 | + else() |
| 108 | + # Process description tag: e.g. v0.4.0-2-gc4af497 or v0.4.0 |
| 109 | + # or v2.0.0-dev2 |
| 110 | + string(REGEX MATCH "^${_TAG_REGEX}(-[0-9]+-g${_HASH_REGEX})?" _MATCH |
| 111 | + "${_VERSION_STRING}" |
| 112 | + ) |
| 113 | + if(_MATCH) |
| 114 | + set(_VERSION_STRING "${CMAKE_MATCH_1}") |
| 115 | + set(_VERSION_STRING_SUFFIX "${CMAKE_MATCH_2}") |
| 116 | + if(CMAKE_MATCH_3) |
| 117 | + # *not* a tagged release |
| 118 | + set(_VERSION_HASH "${CMAKE_MATCH_4}") |
| 119 | + endif() |
| 120 | + endif() |
| 121 | + endif() |
| 122 | + if(NOT _VERSION_STRING) |
| 123 | + execute_process( |
| 124 | + COMMAND "${GIT_EXECUTABLE}" "log" "-1" "--format=%h" "HEAD" |
| 125 | + WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}" |
| 126 | + OUTPUT_VARIABLE _VERSION_HASH |
| 127 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 128 | + ) |
| 129 | + endif() |
| 130 | + set(_CACHED_VERSION "${_VERSION_STRING}" "${_VERSION_STRING_SUFFIX}" "${_VERSION_HASH}") |
| 131 | + set("${_CACHE_VAR}" "${_CACHED_VERSION}" CACHE INTERNAL |
| 132 | + "Version string and hash for ${projname}") |
| 133 | + endif() |
| 134 | + list(GET _CACHED_VERSION 0 _VERSION_STRING) |
| 135 | + list(GET _CACHED_VERSION 1 _VERSION_STRING_SUFFIX) |
| 136 | + list(GET _CACHED_VERSION 2 _VERSION_HASH) |
| 137 | + else() |
| 138 | + string(REGEX MATCH "tag: *${_TAG_REGEX}" _MATCH "${_ARCHIVE_TAG}") |
| 139 | + if(_MATCH) |
| 140 | + set(_VERSION_STRING "${CMAKE_MATCH_1}") |
| 141 | + set(_VERSION_STRING_SUFFIX "${CMAKE_MATCH_2}") |
| 142 | + else() |
| 143 | + message(AUTHOR_WARNING "Could not match a version tag for " |
| 144 | + "git description '${_ARCHIVE_TAG}': perhaps this archive was not " |
| 145 | + "exported from a tagged commit?") |
| 146 | + string(REGEX MATCH " *${_HASH_REGEX}" _MATCH "${_ARCHIVE_HASH}") |
| 147 | + if(_MATCH) |
| 148 | + set(_VERSION_HASH "${CMAKE_MATCH_1}") |
| 149 | + endif() |
| 150 | + endif() |
| 151 | + endif() |
| 152 | + |
| 153 | + if(NOT _VERSION_STRING) |
| 154 | + set(_VERSION_STRING "0.0.0") |
| 155 | + endif() |
| 156 | + |
| 157 | + if(_VERSION_HASH) |
| 158 | + set(_FULL_VERSION_STRING "v${_VERSION_STRING}${_VERSION_STRING_SUFFIX}+${_VERSION_HASH}") |
| 159 | + else() |
| 160 | + set(_FULL_VERSION_STRING "v${_VERSION_STRING}${_VERSION_STRING_SUFFIX}") |
| 161 | + endif() |
| 162 | + |
| 163 | + set(${projname}_VERSION "${_VERSION_STRING}" PARENT_SCOPE) |
| 164 | + set(${projname}_VERSION_STRING "${_FULL_VERSION_STRING}" PARENT_SCOPE) |
| 165 | +endfunction() |
| 166 | + |
| 167 | +if(CMAKE_SCRIPT_MODE_FILE) |
| 168 | + cgv_find_version(TEMP) |
| 169 | + message("VERSION=\"${TEMP_VERSION}\"") |
| 170 | + message("VERSION_STRING=\"${TEMP_VERSION_STRING}\"") |
| 171 | +endif() |
0 commit comments