From 2abc4fe730c437744140be995e84bd1d2a61c298 Mon Sep 17 00:00:00 2001 From: barts Date: Sat, 3 Feb 2024 10:32:56 +0100 Subject: [PATCH] CI: get version from git tags, add suffix for custom builds. --- CMakeLists.txt | 10 +++--- app/CMakeLists.txt | 2 +- cmake/Version.cmake | 74 +++++++++++++++++++++++++++++++++++++++++++++ scripts/build-qt | 1 - 4 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 cmake/Version.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index a291399..f0284d7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,12 +1,14 @@ +include(${CMAKE_SOURCE_DIR}/cmake/Version.cmake) + cmake_minimum_required(VERSION 3.5) -project( - snapshot - VERSION 0.0.0 - LANGUAGES CXX) +project(snapshot LANGUAGES CXX) +set_git_tag_as_project_version() +set(PROJECT_VERSION ${PROJECT_VERSION_TAG}) option(BUILD_TESTING "Build tests" OFF) option(INSTALL_GTEST OFF) +message(STATUS "Project version: ${PROJECT_VERSION}") message(STATUS "Build type: ${CMAKE_BUILD_TYPE}") message(STATUS "Build tests: ${BUILD_TESTING}") diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index ec829f9..f2e0a3a 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -20,7 +20,7 @@ if(QT6_LIB_TYPE STREQUAL "STATIC_LIBRARY") install(PROGRAMS ${CMAKE_SOURCE_DIR}/scripts/snapshot DESTINATION .) set(CPACK_PACKAGE_NAME ${PROJECT_NAME}) - set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION}) + set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION_FULL}) set(CPACK_GENERATOR "TGZ") include(CPack) diff --git a/cmake/Version.cmake b/cmake/Version.cmake new file mode 100644 index 0000000..af3f491 --- /dev/null +++ b/cmake/Version.cmake @@ -0,0 +1,74 @@ +# cmake-format: off +# Set the project version based on git tags +# This function sets the following variables: +# - PROJECT_VERSION: The version tag from git, or '0.0.0' if not a git +# repository or no version tag is found. +# - PROJECT_VERSION_SUFFIX: The version suffix which is the commit hash if not +# a tagged commit, and/or '-custom' if not a GitHub Actions build. +# - PROJECT_VERSION_FULL: The full version string +# cmake-format: on +function(set_git_tag_as_project_version) + find_package(Git QUIET) + if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git") + # Detect if building in GitHub Actions + set(BUILD_ENV "local") + if(DEFINED ENV{GITHUB_ACTIONS}) + set(BUILD_ENV "github") + endif() + + # Attempt to get version from git describe + execute_process( + COMMAND ${GIT_EXECUTABLE} describe --tags --abbrev=0 --contains --match + "v*[0-9].*[0-9].*[0-9]" + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + OUTPUT_VARIABLE project_version_tag + OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET) + + # If git describe fails, use the latest version tag and append commit hash + if(NOT project_version_tag) + execute_process( + COMMAND ${GIT_EXECUTABLE} describe --tags --abbrev=0 --match + "v*[0-9].*[0-9].*[0-9]" + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + OUTPUT_VARIABLE project_version_tag + OUTPUT_STRIP_TRAILING_WHITESPACE) + execute_process( + COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" + OUTPUT_VARIABLE project_commit_hash + OUTPUT_STRIP_TRAILING_WHITESPACE) + set(project_commit_hash "+${project_commit_hash}") + endif() + + # Append '-custom' if not a GitHub Actions build + if(BUILD_ENV STREQUAL "local") + set(project_version_suffix "${project_commit_hash}-custom") + endif() + + else() + message( + WARNING + "Git not found or not a git repository. Using default version v0.0.0-no-version-info" + ) + set(project_version_tag "0.0.0") + set(project_version_suffix "-no-version-info") + endif() + + # Optionally, strip the 'v' prefix if present + string(REGEX REPLACE "^v" "" project_version_tag "${project_version_tag}") + + # Make global variables + set(PROJECT_VERSION_TAG + "${project_version_tag}" + CACHE STRING "Project version") + set(PROJECT_VERSION_SUFFIX + "${project_version_suffix}" + CACHE STRING "Project version suffix") + set(PROJECT_VERSION_FULL + "${project_version_tag}${project_version_suffix}" + CACHE STRING "Project full version") + + message(STATUS "Project version tag: ${PROJECT_VERSION_TAG}") + message(STATUS "Project version suffix: ${PROJECT_VERSION_SUFFIX}") + message(STATUS "Project full version: ${PROJECT_VERSION_FULL}") +endfunction() diff --git a/scripts/build-qt b/scripts/build-qt index d82b597..9aa0714 100755 --- a/scripts/build-qt +++ b/scripts/build-qt @@ -88,7 +88,6 @@ build-qt-static() { -submodules qtbase,qtmultimedia,qtwayland \ "$use_pch" \ -no-gstreamer \ - -no-alsa \ -- \ -S "$src" \ -B "$src/build" \