Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.

Commit c89e1de

Browse files
cmake: parse git describe output before setting it as SRCVERSION
and move this piece of code to a helper function.
1 parent 49586f7 commit c89e1de

2 files changed

Lines changed: 56 additions & 19 deletions

File tree

CMakeLists.txt

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,6 @@ if(VERSION_PRERELEASE)
2020
set(VERSION ${VERSION}-${VERSION_PRERELEASE})
2121
endif()
2222

23-
# Set ${SRCVERSION}
24-
if(EXISTS "${CMAKE_SOURCE_DIR}/.git")
25-
execute_process(COMMAND git describe
26-
OUTPUT_VARIABLE SRCVERSION
27-
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
28-
OUTPUT_STRIP_TRAILING_WHITESPACE
29-
ERROR_QUIET)
30-
if(NOT SRCVERSION)
31-
execute_process(COMMAND git log -1 --format=%h
32-
OUTPUT_VARIABLE SRCVERSION
33-
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
34-
OUTPUT_STRIP_TRAILING_WHITESPACE)
35-
endif()
36-
elseif(EXISTS "${CMAKE_SOURCE_DIR}/.version")
37-
file(STRINGS ${CMAKE_SOURCE_DIR}/.version SRCVERSION)
38-
else()
39-
set(SRCVERSION ${VERSION})
40-
endif()
41-
4223
set(LIBPMEMOBJ_REQUIRED_VERSION 1.9)
4324
set(LIBPMEM_REQUIRED_VERSION 1.7)
4425
# Only pmreorder in ver. >= 1.9 guarantees reliable output
@@ -112,7 +93,10 @@ include(CMakePackageConfigHelpers)
11293
include(CheckCXXSourceCompiles)
11394
include(CheckCXXCompilerFlag)
11495
include(GNUInstallDirs)
96+
11597
include(${CMAKE_SOURCE_DIR}/cmake/functions.cmake)
98+
# set SRCVERSION, it's more accurate and "current" than VERSION
99+
set_source_ver(SRCVERSION)
116100

117101
# Required for MSVC to correctly define __cplusplus
118102
add_flag("/Zc:__cplusplus")

cmake/functions.cmake

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,56 @@ function(find_pmemcheck)
162162
message(WARNING "Valgrind pmemcheck NOT found. Pmemcheck tests will not be performed.")
163163
endif()
164164
endfunction()
165+
166+
# src version shows the current version, as reported by git describe
167+
# unless git is not available, then it's set to the recently released VERSION
168+
function(set_source_ver SRCVERSION)
169+
# if there's version file commited, use it
170+
if(EXISTS "${CMAKE_SOURCE_DIR}/.version")
171+
file(STRINGS ${CMAKE_SOURCE_DIR}/.version FILE_VERSION)
172+
set(SRCVERSION ${FILE_VERSION} PARENT_SCOPE)
173+
return()
174+
endif()
175+
176+
# otherwise take it from git
177+
execute_process(COMMAND git describe
178+
OUTPUT_VARIABLE GIT_VERSION
179+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
180+
OUTPUT_STRIP_TRAILING_WHITESPACE
181+
ERROR_QUIET)
182+
if(GIT_VERSION)
183+
# 1.5-rc1-19-gb8f78a329 -> 1.5-rc1.git19.gb8f78a329
184+
string(REGEX MATCHALL
185+
"([0-9.]*)-rc([0-9]*)-([0-9]*)-([0-9a-g]*)"
186+
MATCHES
187+
${GIT_VERSION})
188+
if(MATCHES)
189+
set(SRCVERSION
190+
"${CMAKE_MATCH_1}-rc${CMAKE_MATCH_2}.git${CMAKE_MATCH_3}.${CMAKE_MATCH_4}"
191+
PARENT_SCOPE)
192+
return()
193+
endif()
194+
195+
# 1.5-19-gb8f78a329 -> 1.5-git19.gb8f78a329
196+
string(REGEX MATCHALL
197+
"([0-9.]*)-([0-9]*)-([0-9a-g]*)"
198+
MATCHES
199+
${GIT_VERSION})
200+
if(MATCHES)
201+
set(SRCVERSION
202+
"${CMAKE_MATCH_1}-git${CMAKE_MATCH_2}.${CMAKE_MATCH_3}"
203+
PARENT_SCOPE)
204+
return()
205+
endif()
206+
else()
207+
execute_process(COMMAND git log -1 --format=%h
208+
OUTPUT_VARIABLE GIT_COMMIT
209+
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
210+
OUTPUT_STRIP_TRAILING_WHITESPACE)
211+
set(SRCVERSION ${GIT_COMMIT} PARENT_SCOPE)
212+
return()
213+
endif()
214+
215+
# last chance: use version set up in the top-level CMake
216+
set(SRCVERSION ${VERSION} PARENT_SCOPE)
217+
endfunction()

0 commit comments

Comments
 (0)