Skip to content

Commit 82aac4b

Browse files
committed
Derive project version from git tag
The project version was hardcoded in project(VERSION ...) and in LSL_LIBRARY_VERSION, so release assets carried a stale version unless both were bumped by hand. - cmake/ProjectVersion.cmake: derive MAJOR.MINOR.PATCH (plus the full tag incl. pre-release suffix) from the nearest v[0-9]* git tag, with the literal as a fallback for git/tag-less builds and a LSL_VERSION_OVERRIDE escape hatch. - CMakeLists.txt: feed PROJECT_VERSION from the tag; use the full tag for CPACK_PACKAGE_VERSION so pre-release suffixes survive. - src/lsl_version.h.in + TargetObjLib.cmake: generate the version header so lsl_library_version() tracks the release. - CI: fetch-depth: 0 on the build-job checkouts so the tag is visible to git describe (default shallow clone fetches no tags).
1 parent bf652e7 commit 82aac4b

7 files changed

Lines changed: 105 additions & 4 deletions

File tree

‎.github/workflows/apple.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ jobs:
4343

4444
steps:
4545
- uses: actions/checkout@v4
46+
with:
47+
# Need full history + tags so the version can be derived from the git tag
48+
fetch-depth: 0
4649

4750
- name: Install certificates and provisioning profiles
4851
uses: ./.github/actions/install-apple-certs

‎.github/workflows/cppcmake.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ jobs:
6363

6464
steps:
6565
- uses: actions/checkout@v5
66+
with:
67+
# Need full history + tags so the version can be derived from the git tag
68+
fetch-depth: 0
6669

6770
# Set up cross-compilation toolchain for ARM on Linux
6871
- name: Install cross-compilation toolchain

‎CMakeLists.txt‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
cmake_minimum_required (VERSION 3.23)
2+
3+
# Derive the version from the most recent git tag (e.g. v1.18.0 -> 1.18.0).
4+
# The literal below is the single source of truth for builds without git/tags.
5+
include(cmake/ProjectVersion.cmake)
6+
lsl_version_from_git(LSL_VERSION LSL_VERSION_FULL FALLBACK "1.17.7")
7+
28
project (liblsl
3-
VERSION 1.17.7
9+
VERSION ${LSL_VERSION}
410
LANGUAGES C CXX
511
DESCRIPTION "Labstreaminglayer C/C++ library"
612
HOMEPAGE_URL "https://github.com/sccn/liblsl"
@@ -32,7 +38,9 @@ LSL_get_target_arch()
3238
LSL_get_os_name()
3339

3440
set(CPACK_PACKAGE_NAME "${PROJECT_NAME}")
35-
set(CPACK_PACKAGE_VERSION "${PROJECT_VERSION}")
41+
# Use the full tag (incl. any pre-release suffix like b1) for package metadata,
42+
# while PROJECT_VERSION stays strictly numeric as CMake requires.
43+
set(CPACK_PACKAGE_VERSION "${LSL_VERSION_FULL}")
3644
set(CPACK_PACKAGE_VENDOR "Labstreaminglayer")
3745
set(CPACK_STRIP_FILES ON)
3846

‎cmake/ProjectVersion.cmake‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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()

‎cmake/TargetObjLib.cmake‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1+
# Generate the version header from the (tag-derived) project version so that
2+
# lsl_library_version() tracks the release instead of a hand-maintained constant.
3+
configure_file(
4+
"${CMAKE_CURRENT_SOURCE_DIR}/src/lsl_version.h.in"
5+
"${CMAKE_CURRENT_BINARY_DIR}/lsl_version.h"
6+
@ONLY
7+
)
8+
19
# Create object library so all files are only compiled once
210
add_library(lslobj OBJECT
311
${lslsources}
412
${lslheaders}
513
)
614
set_target_properties(lslobj PROPERTIES FOLDER "liblsl")
715

16+
# Make the generated lsl_version.h visible to the sources (and to internal tests
17+
# that compile individual source files and link lslobj). Build-tree only; this is
18+
# an internal header and is not installed.
19+
target_include_directories(lslobj PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>)
20+
821
# Set the includes/headers for the lslobj target
922
# Note: We cannot use PUBLIC_HEADER because it flattens the include tree upon install
1023
# Note: We cannot use FILE_SET because it is not compatible with HEADERS.

‎src/common.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef COMMON_H
22
#define COMMON_H
33

4+
#include "lsl_version.h"
5+
46
extern "C" {
57
#include "api_types.hpp"
68
// api_types.h defines LSL_TYPES so it needs to be included before the next header
@@ -45,8 +47,9 @@ extern "C" {
4547
// * 110 is an alternative protocol that improves throughput, supported by library versions 1.10+
4648
const int LSL_PROTOCOL_VERSION = 110;
4749

48-
// the library version
49-
const int LSL_LIBRARY_VERSION = 117;
50+
// the library version, derived from the project version at configure time
51+
// (LSL_LIBRARY_VERSION_* come from the generated lsl_version.h)
52+
const int LSL_LIBRARY_VERSION = (LSL_LIBRARY_VERSION_MAJOR * 100) + LSL_LIBRARY_VERSION_MINOR;
5053

5154
/// size of the lsl_last_error() buffer size
5255
const int LAST_ERROR_SIZE = 512;

‎src/lsl_version.h.in‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Auto-generated by CMake from the project version (see cmake/ProjectVersion.cmake).
2+
// Do not edit; edit the version source (git tag / fallback) instead.
3+
#ifndef LSL_VERSION_GENERATED_H
4+
#define LSL_VERSION_GENERATED_H
5+
6+
#define LSL_LIBRARY_VERSION_MAJOR @PROJECT_VERSION_MAJOR@
7+
#define LSL_LIBRARY_VERSION_MINOR @PROJECT_VERSION_MINOR@
8+
#define LSL_LIBRARY_VERSION_PATCH @PROJECT_VERSION_PATCH@
9+
10+
#endif

0 commit comments

Comments
 (0)