forked from chris2511/xca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_version.cmake
52 lines (47 loc) · 1.88 KB
/
git_version.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
find_package(Git)
string(REPLACE "." ";" VERSION_LIST ${PROJECT_VERSION})
list(GET VERSION_LIST 0 PROJECT_VERSION_MAJOR)
list(GET VERSION_LIST 1 PROJECT_VERSION_MINOR)
list(GET VERSION_LIST 2 PROJECT_VERSION_PATCH)
if(Git_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
message(STATUS "Git found: ${GIT_EXECUTABLE}")
execute_process(COMMAND git rev-parse HEAD
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
OUTPUT_VARIABLE GIT_REV
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
execute_process(COMMAND git diff-index --quiet HEAD --
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
OUTPUT_QUIET ERROR_QUIET
RESULT_VARIABLE GIT_LOCAL_CHANGES
OUTPUT_STRIP_TRAILING_WHITESPACE
)
execute_process(COMMAND git rev-list --count RELEASE.${PROJECT_VERSION}..HEAD
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
OUTPUT_VARIABLE GIT_COMMIT_COUNTER
RESULT_VARIABLE GIT_COMMIT_COUNTER_RESULT
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
if(NOT GIT_COMMIT_COUNTER_RESULT)
math(EXPR PROJECT_VERSION_PATCH "${PROJECT_VERSION_PATCH} + ${GIT_COMMIT_COUNTER}")
set(PROJECT_VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
message(STATUS "Commit counter: ${GIT_COMMIT_COUNTER} - ${GIT_REV} - ${PROJECT_VERSION}")
endif()
set(RELEASE_TAG "RELEASE.${PROJECT_VERSION}"
CACHE STRING "Release TAG for the release")
if (DEFINED ENV{RELEASE_TAG})
set(RELEASE_TAG "$ENV{RELEASE_TAG}")
endif()
set(RELEASE_NAME "${PROJECT_NAME}-${PROJECT_VERSION}")
add_custom_command(
OUTPUT "${RELEASE_NAME}.tar.gz"
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
COMMAND git archive --format=tar.gz --prefix=${RELEASE_NAME}/
${RELEASE_TAG} > ${CMAKE_BINARY_DIR}/${RELEASE_NAME}.tar.gz
COMMENT "Create ${RELEASE_NAME}.tar.gz from tag ${RELEASE_TAG}"
)
add_custom_target(release DEPENDS ${RELEASE_NAME}.tar.gz)
endif()
message(STATUS "VERSION: ${PROJECT_VERSION}")