|
| 1 | +############################################################################# |
| 2 | +# Alpine Terrain Renderer |
| 3 | +# Copyright (C) 2023 Adam Celarek <family name at cg tuwien ac at> |
| 4 | +# |
| 5 | +# This program is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +############################################################################# |
| 18 | + |
| 19 | +function(alp_get_version) |
| 20 | + # Usage: |
| 21 | + # alp_get_version( |
| 22 | + # GIT_DIR <path to git repo> |
| 23 | + # VERSION_VAR <name of var to hold the version string> |
| 24 | + # VERSION_INT_VAR <name of var to hold the commit count, optional> |
| 25 | + # ) |
| 26 | + |
| 27 | + cmake_parse_arguments(ALP "" "GIT_DIR;VERSION_VAR;VERSION_INT_VAR" "" ${ARGN}) |
| 28 | + |
| 29 | + if (NOT ALP_GIT_DIR) |
| 30 | + message(FATAL_ERROR "alp_get_version() requires GIT_DIR to be specified") |
| 31 | + endif() |
| 32 | + |
| 33 | + if (NOT ALP_VERSION_VAR) |
| 34 | + message(FATAL_ERROR "alp_get_version() requires ALP_VERSION_VAR to be specified") |
| 35 | + endif() |
| 36 | + |
| 37 | + find_package(Git 2.22 REQUIRED) |
| 38 | + |
| 39 | + #--- Retrieve the version string (e.g. "v1.2.3-dirty") --- |
| 40 | + execute_process( |
| 41 | + COMMAND ${GIT_EXECUTABLE} describe --tags --dirty=-d --abbrev=1 |
| 42 | + WORKING_DIRECTORY ${ALP_GIT_DIR} |
| 43 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 44 | + OUTPUT_VARIABLE _local_version |
| 45 | + ) |
| 46 | + |
| 47 | + if (_local_version STREQUAL "") |
| 48 | + message(WARNING "Retrieving version string from git failed; using 'vUnknown'") |
| 49 | + set(_local_version "vUnknown") |
| 50 | + else() |
| 51 | + string(REPLACE "-g" "." _local_version ${_local_version}) |
| 52 | + string(REPLACE "-" "." _local_version ${_local_version}) |
| 53 | + endif() |
| 54 | + set(${ALP_VERSION_VAR} "${_local_version}" PARENT_SCOPE) |
| 55 | + |
| 56 | + #--- Retrieve number of commits (rev-list HEAD --count) --- |
| 57 | + if (ALP_VERSION_INT_VAR) |
| 58 | + execute_process( |
| 59 | + COMMAND ${GIT_EXECUTABLE} rev-list HEAD --count |
| 60 | + WORKING_DIRECTORY ${ALP_GIT_DIR} |
| 61 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 62 | + OUTPUT_VARIABLE _local_version_integer |
| 63 | + ) |
| 64 | + |
| 65 | + if (_local_version_integer STREQUAL "") |
| 66 | + message(WARNING "Retrieving number of commits from git failed; using '0'") |
| 67 | + set(_local_version_integer "0") |
| 68 | + endif() |
| 69 | + |
| 70 | + set(${ALP_VERSION_INT_VAR} "${_local_version_integer}" PARENT_SCOPE) |
| 71 | + endif() |
| 72 | +endfunction() |
| 73 | + |
| 74 | + |
| 75 | +function(alp_generate_version_file) |
| 76 | + # Usage: |
| 77 | + # alp_generate_version_file( |
| 78 | + # GIT_DIR <path to git repo> |
| 79 | + # VERSION_TEMPLATE <path to .in template file> |
| 80 | + # VERSION_DESTINATION <destination file path to generate> |
| 81 | + # ) |
| 82 | + |
| 83 | + cmake_parse_arguments(ALP "" |
| 84 | + "GIT_DIR;VERSION_TEMPLATE;VERSION_DESTINATION" |
| 85 | + "" |
| 86 | + ${ARGN} |
| 87 | + ) |
| 88 | + |
| 89 | + if (NOT ALP_GIT_DIR) |
| 90 | + message(FATAL_ERROR "alp_generate_version_file() requires GIT_DIR") |
| 91 | + endif() |
| 92 | + if (NOT ALP_VERSION_TEMPLATE) |
| 93 | + message(FATAL_ERROR "alp_generate_version_file() requires VERSION_TEMPLATE") |
| 94 | + endif() |
| 95 | + if (NOT ALP_VERSION_DESTINATION) |
| 96 | + message(FATAL_ERROR "alp_generate_version_file() requires VERSION_DESTINATION") |
| 97 | + endif() |
| 98 | + |
| 99 | + # Retrieve version info |
| 100 | + alp_get_version( |
| 101 | + GIT_DIR "${ALP_GIT_DIR}" |
| 102 | + VERSION_VAR "_version" |
| 103 | + ) |
| 104 | + |
| 105 | + # Because ALP_VERSION_VAR is a *name of a variable*, we use indirect expansion: |
| 106 | + set(ALP_VERSION "${_version}") |
| 107 | + |
| 108 | + # Use configure_file() to generate your output |
| 109 | + configure_file( |
| 110 | + "${ALP_VERSION_TEMPLATE}" |
| 111 | + "${ALP_VERSION_DESTINATION}" |
| 112 | + @ONLY |
| 113 | + ) |
| 114 | +endfunction() |
| 115 | + |
0 commit comments