|
| 1 | +# Version derivation for moqx. |
| 2 | +# |
| 3 | +# Produces MOQX_VERSION_STRING: one canonical identifier consumed by every |
| 4 | +# binary (admin /info, --version, the startup log banner) and embedded in |
| 5 | +# build artifacts as a VERSION file, so a tarball or container can be |
| 6 | +# identified without its git history. |
| 7 | +# |
| 8 | +# Format is git-describe output with the "v" tag prefix retained, so the |
| 9 | +# string round-trips into `git checkout` / `gh release view`: |
| 10 | +# |
| 11 | +# v0.2.1 exact release tag |
| 12 | +# v0.2.1-14-gabc1234 14 commits past v0.2.1 |
| 13 | +# v0.2.1-14-gabc1234-dirty ...with uncommitted changes (local builds) |
| 14 | +# abc1234 no reachable v* tag (shallow clone, fork) |
| 15 | +# |
| 16 | +# Resolution order, first hit wins: |
| 17 | +# |
| 18 | +# 1. -DMOQX_VERSION_STRING=... explicit override (CI, docker --build-arg) |
| 19 | +# 2. <source root>/VERSION source tarballs and the docker build context |
| 20 | +# carry no .git; CI writes this file for them |
| 21 | +# 3. git describe any real clone, including local dev builds |
| 22 | +# 4. v${PROJECT_VERSION} last resort, e.g. v0.1.0 |
| 23 | +# |
| 24 | +# Only v-prefixed numeric tags are considered. The repo also carries moving |
| 25 | +# tags (snapshot-latest, build-*, archive/*) that are NOT versions; an |
| 26 | +# unfiltered `git describe` happily returns "snapshot-latest" and poisons |
| 27 | +# the version of every artifact built from main. |
| 28 | +# |
| 29 | +# Derivation happens at configure time. A dev who commits without re-running |
| 30 | +# cmake keeps the previous string until the next configure; CI configures |
| 31 | +# fresh every run, so published artifacts are always exact. |
| 32 | + |
| 33 | +function(_moqx_version_from_git out_var) |
| 34 | + set(${out_var} "" PARENT_SCOPE) |
| 35 | + |
| 36 | + find_package(Git QUIET) |
| 37 | + if(NOT GIT_FOUND) |
| 38 | + return() |
| 39 | + endif() |
| 40 | + |
| 41 | + # A worktree's .git is a file, not a directory — EXISTS covers both. |
| 42 | + if(NOT EXISTS "${PROJECT_SOURCE_DIR}/.git") |
| 43 | + return() |
| 44 | + endif() |
| 45 | + |
| 46 | + execute_process( |
| 47 | + COMMAND "${GIT_EXECUTABLE}" describe --tags --match "v[0-9]*" --always --dirty |
| 48 | + WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}" |
| 49 | + OUTPUT_VARIABLE _described |
| 50 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 51 | + ERROR_QUIET |
| 52 | + RESULT_VARIABLE _rc |
| 53 | + ) |
| 54 | + if(_rc EQUAL 0 AND _described) |
| 55 | + set(${out_var} "${_described}" PARENT_SCOPE) |
| 56 | + endif() |
| 57 | +endfunction() |
| 58 | + |
| 59 | +if(MOQX_VERSION_STRING) |
| 60 | + set(_moqx_version_source "explicit -DMOQX_VERSION_STRING") |
| 61 | +elseif(EXISTS "${PROJECT_SOURCE_DIR}/VERSION") |
| 62 | + file(READ "${PROJECT_SOURCE_DIR}/VERSION" MOQX_VERSION_STRING) |
| 63 | + string(STRIP "${MOQX_VERSION_STRING}" MOQX_VERSION_STRING) |
| 64 | + set(_moqx_version_source "VERSION file") |
| 65 | +else() |
| 66 | + _moqx_version_from_git(MOQX_VERSION_STRING) |
| 67 | + if(MOQX_VERSION_STRING) |
| 68 | + set(_moqx_version_source "git describe") |
| 69 | + else() |
| 70 | + set(MOQX_VERSION_STRING "v${PROJECT_VERSION}") |
| 71 | + set(_moqx_version_source "project() fallback") |
| 72 | + endif() |
| 73 | +endif() |
| 74 | + |
| 75 | +message(STATUS "moqx version: ${MOQX_VERSION_STRING} (from ${_moqx_version_source})") |
| 76 | + |
| 77 | +# Generated header — any target may link moqx_version to use it, so the |
| 78 | +# version is not welded to one library the way a target_compile_definitions |
| 79 | +# would be. |
| 80 | +set(MOQX_VERSION_HEADER_DIR "${CMAKE_BINARY_DIR}/generated") |
| 81 | +configure_file( |
| 82 | + "${PROJECT_SOURCE_DIR}/cmake/Version.h.in" |
| 83 | + "${MOQX_VERSION_HEADER_DIR}/moqx/Version.h" |
| 84 | + @ONLY |
| 85 | +) |
| 86 | + |
| 87 | +add_library(moqx_version INTERFACE) |
| 88 | +target_include_directories(moqx_version INTERFACE "${MOQX_VERSION_HEADER_DIR}") |
| 89 | + |
| 90 | +# Artifact-side copy: `cat VERSION` identifies an unpacked tarball or an |
| 91 | +# image layer with no binary to run and no git history to consult. |
| 92 | +file(WRITE "${CMAKE_BINARY_DIR}/VERSION" "${MOQX_VERSION_STRING}\n") |
0 commit comments