Skip to content

Commit

Permalink
cmake: get version from git, or git archive
Browse files Browse the repository at this point in the history
  • Loading branch information
ManuelHu committed Dec 20, 2024
1 parent 1db1765 commit d2b34ab
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
docs/rmg-commands.md linguist-generated=true
cmake/RMGGitVersion.cmake export-subst
8 changes: 7 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ jobs:
- G4.11.2
- slim

container: docker://gipert/remage-base:${{ matrix.container_version }}
container:
image: docker://gipert/remage-base:${{ matrix.container_version }}
# make git work inside container. reference:
# https://github.com/actions/runner/issues/2033#issuecomment-1598547465
options: "--user 1001"

steps:
- uses: actions/checkout@v4
with:
fetch-tags: true

- name: Build project
run: |
Expand Down
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/RMGGitVersion.cmake)

project(
remage
VERSION 0.5.0
VERSION ${RMG_GIT_VERSION}
DESCRIPTION "Simulation framework for HPGe-based experiments"
LANGUAGES C CXX) # C is needed for Geant4's HDF5 support

Expand Down
24 changes: 24 additions & 0 deletions cmake/RMGGitVersion.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# this is a special value replaced by git archive.
set(RMG_GIT_VERSION "$Format:%(describe:tags=1)$")

# try to get the tag ourselves from git describe, if we did not have it set above
# (i.e. we are not in a git-created archive).
if(RMG_GIT_VERSION MATCHES "Format:")
set(RMG_GIT_VERSION "v0.0.0")
find_package(Git)
if(Git_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} describe --tags --match "v*"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE GIT_OUTPUT
RESULT_VARIABLE GIT_STATUS
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(GIT_STATUS EQUAL 0)
set(RMG_GIT_VERSION ${GIT_OUTPUT})
endif()
endif()
endif()

# cleanup git describe output to match cmake's expectations.
string(REGEX REPLACE "^v" "" RMG_GIT_VERSION ${RMG_GIT_VERSION})
string(REGEX REPLACE "-.*$" "" RMG_GIT_VERSION ${RMG_GIT_VERSION})

0 comments on commit d2b34ab

Please sign in to comment.