Skip to content

Commit 7a4427c

Browse files
committed
Add CMake script to determine project version based on git describe
1 parent ddf2654 commit 7a4427c

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}" CACHE STRING "CMake install prefi
2020
# Do not use a suffix for RelWithDebInfo
2121
set(CMAKE_DEBUG_POSTFIX "d" CACHE STRING "CMake debug suffix")
2222
set(CMAKE_RELWITHDEBINFO_POSTFIX "" CACHE STRING "CMake RelWithDebInfo suffix")
23-
2423
set(PDALC_ENABLE_CODE_COVERAGE ON CACHE BOOL "Enable code coverage calculation")
2524
set(PDALC_GCC_PARAM_GGC_MIN_HEAPSIZE "131072" CACHE STRING "GCC garbage collection minimum heap size")
2625

26+
include(ObtainProjectVersion)
2727
include_directories("${CMAKE_SOURCE_DIR}/source")
2828

2929
if(CMAKE_COMPILER_IS_GNUCXX)

cmake/ObtainProjectVersion.cmake

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# CMake script to derive project version from repository info
2+
3+
find_package(Git)
4+
5+
# Use `git describe` to derive patch and build info
6+
if(GIT_FOUND)
7+
execute_process(COMMAND "${GIT_EXECUTABLE}" "describe" "--tags" "--long"
8+
OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE GIT_DESCRIBE_OUTPUT
9+
ERROR_STRIP_TRAILING_WHITESPACE ERROR_VARIABLE GIT_DESCRIBE_ERROR)
10+
11+
# We use tags with the three version numbers
12+
# Use the remainder of the `git describe` output for the build ID
13+
if(GIT_DESCRIBE_OUTPUT MATCHES "^([0-9]+)\\.([0-9]+)\\.([0-9]+)-([0-9]+)-g([a-f0-9]+)$")
14+
string(REGEX REPLACE "([0-9]+).*" "\\1" MAJOR_VERSION ${GIT_DESCRIBE_OUTPUT})
15+
string(REGEX REPLACE "([0-9]+)\\.([0-9]+).*" "\\2" MINOR_VERSION ${GIT_DESCRIBE_OUTPUT})
16+
string(REGEX REPLACE "([0-9]+)\\.([0-9]+)\\.([0-9]+).*" "\\3" PATCH_VERSION ${GIT_DESCRIBE_OUTPUT})
17+
string(REGEX REPLACE ".*-(.*)-.*" "\\1" COMMIT_COUNT ${GIT_DESCRIBE_OUTPUT})
18+
string(REGEX REPLACE ".*-g(.*)" "\\1" COMMIT_ID ${GIT_DESCRIBE_OUTPUT})
19+
set(BUILD_ID "build ${COMMIT_COUNT} (${COMMIT_ID})")
20+
else()
21+
message(WARNING "Could not derive version and build ID: 'git describe' command output was \"${GIT_DESCRIBE_OUTPUT}\" - ${GIT_DESCRIBE_ERROR}")
22+
endif()
23+
24+
# Suffix BUILD_ID with " (with uncommitted changes)" if `git status --porcelain` does not return an empty string
25+
execute_process(COMMAND "${GIT_EXECUTABLE}" "status" "--porcelain"
26+
OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE GIT_STATUS_OUTPUT)
27+
28+
if(NOT "${GIT_STATUS_OUTPUT}" STREQUAL "")
29+
string(APPEND BUILD_ID " with uncommitted changes")
30+
endif()
31+
else()
32+
message(WARNING "Could not derive version and build ID: Git not found")
33+
endif()
34+
35+
set(${PROJECT_NAME}_VERSION_MAJOR ${MAJOR_VERSION})
36+
set(${PROJECT_NAME}_VERSION_MINOR ${MINOR_VERSION})
37+
set(${PROJECT_NAME}_VERSION_PATCH ${PATCH_VERSION})
38+
set(${PROJECT_NAME}_VERSION "${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION}")
39+
40+
string (TOUPPER "${PROJECT_NAME}" PROJECT_NAME_UPPERCASE)
41+
set(${PROJECT_NAME_UPPERCASE}_VERSION_MAJOR ${${PROJECT_NAME}_VERSION_MAJOR})
42+
set(${PROJECT_NAME_UPPERCASE}_VERSION_MINOR ${${PROJECT_NAME}_VERSION_MINOR})
43+
set(${PROJECT_NAME_UPPERCASE}_VERSION_PATCH ${${PROJECT_NAME}_VERSION_PATCH})
44+
set(${PROJECT_NAME_UPPERCASE}_VERSION ${${PROJECT_NAME}_VERSION})

0 commit comments

Comments
 (0)