|
| 1 | +# ProjectVersion.cmake |
| 2 | +# Derive the project version from the most recent git tag. |
| 3 | +# |
| 4 | +# liblsl tags look like `v1.18.0` or, for pre-releases, `v1.18.0b1`. This module |
| 5 | +# turns the nearest such tag into: |
| 6 | +# - a strict MAJOR.MINOR.PATCH string suitable for project(... VERSION ...) |
| 7 | +# - the full tag (sans leading `v`), preserving any pre-release suffix, for |
| 8 | +# packaging metadata. |
| 9 | +# |
| 10 | +# It must be included and called BEFORE project(), since project() needs the |
| 11 | +# numeric version. When git or a matching tag is unavailable (release tarballs, |
| 12 | +# shallow clones without tags, FetchContent on an exported tree) it falls back |
| 13 | +# to the supplied literal so the source tree always builds. |
| 14 | +# |
| 15 | +# Usage: |
| 16 | +# include(cmake/ProjectVersion.cmake) |
| 17 | +# lsl_version_from_git(LSL_VERSION LSL_VERSION_FULL FALLBACK "1.17.7") |
| 18 | +# project(liblsl VERSION ${LSL_VERSION} ...) |
| 19 | +# |
| 20 | +# A version can be forced (e.g. from CI) by passing -DLSL_VERSION_OVERRIDE=1.18.0b1. |
| 21 | + |
| 22 | +function(lsl_version_from_git out_numeric out_full) |
| 23 | + cmake_parse_arguments(ARG "" "FALLBACK" "" ${ARGN}) |
| 24 | + |
| 25 | + set(_ver "") |
| 26 | + if(LSL_VERSION_OVERRIDE) |
| 27 | + string(REGEX REPLACE "^v" "" _ver "${LSL_VERSION_OVERRIDE}") |
| 28 | + message(STATUS "lsl: using LSL_VERSION_OVERRIDE=${_ver}") |
| 29 | + else() |
| 30 | + find_package(Git QUIET) |
| 31 | + if(Git_FOUND) |
| 32 | + execute_process( |
| 33 | + COMMAND ${GIT_EXECUTABLE} describe --tags --abbrev=0 --match "v[0-9]*" |
| 34 | + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} |
| 35 | + OUTPUT_VARIABLE _tag |
| 36 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 37 | + ERROR_QUIET |
| 38 | + RESULT_VARIABLE _res |
| 39 | + ) |
| 40 | + if(_res EQUAL 0 AND _tag) |
| 41 | + string(REGEX REPLACE "^v" "" _ver "${_tag}") |
| 42 | + endif() |
| 43 | + endif() |
| 44 | + endif() |
| 45 | + |
| 46 | + if(NOT _ver) |
| 47 | + set(_ver "${ARG_FALLBACK}") |
| 48 | + message(STATUS "lsl: no git tag found; using fallback version ${_ver}") |
| 49 | + endif() |
| 50 | + |
| 51 | + # Strict numeric MAJOR.MINOR.PATCH for project(VERSION); ignore any suffix. |
| 52 | + if(_ver MATCHES "^([0-9]+)\\.([0-9]+)\\.([0-9]+)") |
| 53 | + set(${out_numeric} "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}.${CMAKE_MATCH_3}" PARENT_SCOPE) |
| 54 | + set(${out_full} "${_ver}" PARENT_SCOPE) |
| 55 | + message(STATUS "lsl version: ${_ver}") |
| 56 | + else() |
| 57 | + message(WARNING "lsl: version '${_ver}' is not MAJOR.MINOR.PATCH; using ${ARG_FALLBACK}") |
| 58 | + set(${out_numeric} "${ARG_FALLBACK}" PARENT_SCOPE) |
| 59 | + set(${out_full} "${ARG_FALLBACK}" PARENT_SCOPE) |
| 60 | + endif() |
| 61 | +endfunction() |
0 commit comments