|
| 1 | +##---------------------------------------------------------------------------## |
| 2 | +## File : flibcpp/cmake/FlibcppVersion.cmake |
| 3 | +#[=======================================================================[.rst: |
| 4 | +
|
| 5 | +FlibcppVersion |
| 6 | +-------------- |
| 7 | +
|
| 8 | +.. command:: flibcpp_find_version |
| 9 | +
|
| 10 | + Get the project version using Git descriptions to ensure the version numbers |
| 11 | + are always synchronized between Git and CMake:: |
| 12 | +
|
| 13 | + flibcpp_find_version(<projname> <git-version-file>) |
| 14 | +
|
| 15 | +
|
| 16 | + ``<projname>`` |
| 17 | + Name of the project. |
| 18 | +
|
| 19 | + This command sets the following variables in the parent package:: |
| 20 | +
|
| 21 | + ${PROJNAME}_VERSION |
| 22 | + ${PROJNAME}_VERSION_STRING |
| 23 | +
|
| 24 | +#]=======================================================================] |
| 25 | + |
| 26 | +function(flibcpp_find_version PROJNAME GIT_VERSION_FILE) |
| 27 | + # Get a possible Git version generated using git-archive (see the |
| 28 | + # .gitattributes file) |
| 29 | + file(STRINGS "${GIT_VERSION_FILE}" _TEXTFILE) |
| 30 | + |
| 31 | + if (_TEXTFILE MATCHES "\\$Format:") |
| 32 | + # Not a "git archive": use live git information |
| 33 | + set(_CACHE_VAR "${PROJNAME}_GIT_DESCRIBE") |
| 34 | + set(_VERSION_STRING "${${_CACHE_VAR}}") |
| 35 | + if (NOT _VERSION_STRING) |
| 36 | + # Building from a git checkout rather than a distribution |
| 37 | + find_package(Git REQUIRED) |
| 38 | + execute_process( |
| 39 | + COMMAND "${GIT_EXECUTABLE}" "describe" "--tags" "--match" "v*" |
| 40 | + WORKING_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}" |
| 41 | + ERROR_VARIABLE _GIT_ERR |
| 42 | + OUTPUT_VARIABLE _VERSION_STRING |
| 43 | + RESULT_VARIABLE _GIT_RESULT |
| 44 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 45 | + ) |
| 46 | + if (NOT _GIT_RESULT EQUAL "0") |
| 47 | + message(FATAL_ERROR "Failed to get ${PROJNAME} version from git: " |
| 48 | + "${_GIT_ERR}") |
| 49 | + endif() |
| 50 | + if (NOT _VERSION_STRING) |
| 51 | + message(FATAL_ERROR "Failed to get ${PROJNAME} version from git: " |
| 52 | + "git describe returned an empty string") |
| 53 | + endif() |
| 54 | + set("${_CACHE_VAR}" "${_VERSION_STRING}" CACHE INTERNAL |
| 55 | + "Git description for ${PROJNAME}") |
| 56 | + endif() |
| 57 | + # Process description tag: e.g. v0.4.0-2-gc4af497 or v0.4.0 |
| 58 | + string(REGEX MATCH "v([0-9.]+)(-[0-9]+-g([0-9a-f]+))?" _MATCH |
| 59 | + "${_VERSION_STRING}" |
| 60 | + ) |
| 61 | + if (_MATCH) |
| 62 | + set(_VERSION_STRING "${CMAKE_MATCH_1}") |
| 63 | + if (CMAKE_MATCH_2) |
| 64 | + # *not* a tagged release |
| 65 | + set(_VERSION_HASH "${CMAKE_MATCH_3}") |
| 66 | + endif() |
| 67 | + endif() |
| 68 | + else() |
| 69 | + # First line are decorators, second is hash. |
| 70 | + list(GET _TEXTFILE 0 _TAG) |
| 71 | + string(REGEX MATCH "tag: *v([0-9.]+)" _MATCH "${_TAG}") |
| 72 | + if (_MATCH) |
| 73 | + set(_VERSION_STRING "${CMAKE_MATCH_1}") |
| 74 | + else() |
| 75 | + # *not* a tagged release |
| 76 | + list(GET _TEXTFILE 1 _HASH) |
| 77 | + string(REGEX MATCH " *([0-9a-f]+)" _MATCH "${_HASH}") |
| 78 | + if (_MATCH) |
| 79 | + set(_VERSION_HASH "${CMAKE_MATCH_1}") |
| 80 | + endif() |
| 81 | + endif() |
| 82 | + endif() |
| 83 | + |
| 84 | + if (NOT _VERSION_STRING) |
| 85 | + message(FATAL_ERROR "Could not determine version for ${PROJNAME}") |
| 86 | + endif() |
| 87 | + |
| 88 | + if (_VERSION_HASH) |
| 89 | + set(_FULL_VERSION_STRING "v${_VERSION_STRING}+${_VERSION_HASH}") |
| 90 | + else() |
| 91 | + set(_FULL_VERSION_STRING "v${_VERSION_STRING}") |
| 92 | + endif() |
| 93 | + |
| 94 | + set(${PROJNAME}_VERSION "${_VERSION_STRING}" PARENT_SCOPE) |
| 95 | + set(${PROJNAME}_VERSION_STRING "${_FULL_VERSION_STRING}" PARENT_SCOPE) |
| 96 | +endfunction() |
| 97 | + |
| 98 | +##---------------------------------------------------------------------------## |
| 99 | +## end of flibcpp/cmake/FlibcppVersion.cmake |
| 100 | +##---------------------------------------------------------------------------## |
0 commit comments