Skip to content

Commit

Permalink
CI: get version from git tags, add suffix for custom builds.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart-Steensma committed Feb 3, 2024
1 parent b583dff commit 2abc4fe
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
10 changes: 6 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}")

Expand Down
2 changes: 1 addition & 1 deletion app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
74 changes: 74 additions & 0 deletions cmake/Version.cmake
Original file line number Diff line number Diff line change
@@ -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()
1 change: 0 additions & 1 deletion scripts/build-qt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ build-qt-static() {
-submodules qtbase,qtmultimedia,qtwayland \
"$use_pch" \
-no-gstreamer \
-no-alsa \
-- \
-S "$src" \
-B "$src/build" \
Expand Down

0 comments on commit 2abc4fe

Please sign in to comment.