|
| 1 | +# Copyright (c) 2024 - 2025 Fraunhofer IOSB and contributors |
| 2 | +# |
| 3 | +# Redistribution and use in source and binary forms, with or without |
| 4 | +# modification, are permitted provided that the following conditions are met: |
| 5 | +# |
| 6 | +# * Redistributions of source code must retain the above copyright |
| 7 | +# notice, this list of conditions and the following disclaimer. |
| 8 | +# |
| 9 | +# * Redistributions in binary form must reproduce the above copyright |
| 10 | +# notice, this list of conditions and the following disclaimer in the |
| 11 | +# documentation and/or other materials provided with the distribution. |
| 12 | +# |
| 13 | +# * Neither the name of the Fraunhofer IOSB nor the names of its |
| 14 | +# contributors may be used to endorse or promote products derived from |
| 15 | +# this software without specific prior written permission. |
| 16 | +# |
| 17 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 21 | +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | +# POSSIBILITY OF SUCH DAMAGE. |
| 28 | + |
| 29 | +# Need to call git with manually specified paths to repository |
| 30 | +set(GIT_BASE_COMMAND git --git-dir "${CMAKE_CURRENT_LIST_DIR}/../.git" --work-tree "${CMAKE_CURRENT_LIST_DIR}/..") |
| 31 | + |
| 32 | +# Trying to get version from git tag / revision |
| 33 | +execute_process(COMMAND ${GIT_BASE_COMMAND} describe --always --tags OUTPUT_VARIABLE GIT_TAG) |
| 34 | +string(STRIP "${GIT_TAG}" GIT_VERSION) # strip leading and trailing whitespace |
| 35 | + |
| 36 | +# get commit count |
| 37 | +execute_process(COMMAND ${GIT_BASE_COMMAND} rev-list HEAD --count OUTPUT_VARIABLE GIT_COMMIT_COUNT) |
| 38 | +string(STRIP "${GIT_COMMIT_COUNT}" GIT_COMMIT_COUNT) # strip leading and trailing whitespace |
| 39 | +if ("${GIT_COMMIT_COUNT}" STREQUAL "") |
| 40 | + set(GIT_COMMIT_COUNT 0) |
| 41 | +endif() |
| 42 | + |
| 43 | +# Check if tag name does not consist of 'v' (optional) followed by three digits |
| 44 | +# if not append commit count to tag name |
| 45 | +if("${GIT_VERSION}" MATCHES "[v]*[0-9]+[.][0-9]+[.][0-9]+.*") |
| 46 | + |
| 47 | + # if preceeding 'v', remove |
| 48 | + if("${GIT_VERSION}" MATCHES "^[v].*") |
| 49 | + string(SUBSTRING "${GIT_VERSION}" 1 -1 GIT_VERSION) |
| 50 | + endif() |
| 51 | + |
| 52 | + # Turn describe output like 0.1.5-42-g652c397 into "0.1.5.42.652c397" |
| 53 | + string(REPLACE "-" "." GIT_VERSION "${GIT_VERSION}") |
| 54 | + |
| 55 | + # remove commit hash |
| 56 | + string(REGEX MATCH "[0-9]+[.][0-9]+[.][0-9]+[.]*[0-9]*" GIT_VERSION "${GIT_VERSION}") |
| 57 | + |
| 58 | +else() |
| 59 | + |
| 60 | + # append commit count to tag name |
| 61 | + set(GIT_VERSION "0.0.0.${GIT_COMMIT_COUNT}") |
| 62 | +endif() |
| 63 | + |
| 64 | +# set return variables |
| 65 | +string(REPLACE "." ";" VERSION_LIST "${GIT_VERSION}") |
| 66 | +set(VERSION_STR "${GIT_VERSION}") |
| 67 | +list(GET VERSION_LIST 0 VERSION_MAJOR) |
| 68 | +list(GET VERSION_LIST 1 VERSION_MINOR) |
| 69 | +list(GET VERSION_LIST 2 VERSION_PATCH) |
0 commit comments