|
9 | 9 |
|
10 | 10 | cmake_minimum_required(VERSION 3.20) |
11 | 11 |
|
12 | | -# Detect cpp-library version from git tags |
| 12 | +# Extract latest semantic version from a list of refs/tags/vX.Y.Z entries |
| 13 | +function(extract_latest_cpp_library_version_from_tags TAG_REFS OUTPUT_VAR) |
| 14 | + string(REGEX MATCHALL "refs/tags/v[0-9]+\\.[0-9]+\\.[0-9]+" TAG_REFS_MATCHES "${TAG_REFS}") |
| 15 | + |
| 16 | + set(SEMVER_TAGS "") |
| 17 | + foreach(tag_ref IN LISTS TAG_REFS_MATCHES) |
| 18 | + string(REGEX REPLACE "^refs/tags/v" "" semver "${tag_ref}") |
| 19 | + list(APPEND SEMVER_TAGS "${semver}") |
| 20 | + endforeach() |
| 21 | + |
| 22 | + if(SEMVER_TAGS) |
| 23 | + list(REMOVE_DUPLICATES SEMVER_TAGS) |
| 24 | + list(SORT SEMVER_TAGS COMPARE NATURAL ORDER DESCENDING) |
| 25 | + list(GET SEMVER_TAGS 0 latest_version) |
| 26 | + set(${OUTPUT_VAR} "${latest_version}" PARENT_SCOPE) |
| 27 | + else() |
| 28 | + set(${OUTPUT_VAR} "" PARENT_SCOPE) |
| 29 | + endif() |
| 30 | +endfunction() |
| 31 | + |
| 32 | +# Detect cpp-library version from local git tags first |
| 33 | +set(CPP_LIBRARY_VERSION "") |
13 | 34 | execute_process( |
14 | 35 | COMMAND git describe --tags --abbrev=0 |
15 | 36 | WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} |
16 | 37 | OUTPUT_VARIABLE CPP_LIBRARY_GIT_VERSION |
17 | 38 | OUTPUT_STRIP_TRAILING_WHITESPACE |
18 | 39 | ERROR_QUIET |
19 | 40 | ) |
20 | | - |
21 | | -# Clean version (remove 'v' prefix if present) |
22 | 41 | if(CPP_LIBRARY_GIT_VERSION) |
23 | 42 | string(REGEX REPLACE "^v" "" CPP_LIBRARY_VERSION "${CPP_LIBRARY_GIT_VERSION}") |
24 | | -else() |
25 | | - # Fallback to X.Y.Z placeholder if no git tag found |
26 | | - set(CPP_LIBRARY_VERSION "X.Y.Z") |
27 | | - message(WARNING "No git tag found for cpp-library version. Using placeholder 'X.Y.Z'. Check https://github.com/stlab/cpp-library/releases for the latest version.") |
| 43 | +endif() |
| 44 | + |
| 45 | +# If setup.cmake is downloaded standalone (no local tags), detect from remote tags |
| 46 | +if(NOT CPP_LIBRARY_VERSION) |
| 47 | + execute_process( |
| 48 | + COMMAND git ls-remote --tags --refs https://github.com/stlab/cpp-library.git v[0-9]* |
| 49 | + OUTPUT_VARIABLE CPP_LIBRARY_REMOTE_TAGS |
| 50 | + OUTPUT_STRIP_TRAILING_WHITESPACE |
| 51 | + ERROR_QUIET |
| 52 | + ) |
| 53 | + extract_latest_cpp_library_version_from_tags("${CPP_LIBRARY_REMOTE_TAGS}" CPP_LIBRARY_VERSION) |
| 54 | +endif() |
| 55 | + |
| 56 | +# Last-resort fallback to main branch so initialization still works |
| 57 | +if(NOT CPP_LIBRARY_VERSION) |
| 58 | + set(CPP_LIBRARY_VERSION "main") |
| 59 | + message(WARNING "No git tag found for cpp-library version. Falling back to branch 'main'.") |
28 | 60 | endif() |
29 | 61 |
|
30 | 62 | message(STATUS "cpp-library version: ${CPP_LIBRARY_VERSION}") |
|
0 commit comments