| 1 |
|
| 2 |
if (EXISTS "${CMAKE_SOURCE_DIR}/.git") |
| 3 |
# If git directory is present, we require git executable and gather our info from git commands. |
| 4 |
find_package(Git) |
| 5 |
if (NOT ${GIT_FOUND}) |
| 6 |
message(FATAL_ERROR "Git not found. Cannot proceed with version info.") |
| 7 |
endif() |
| 8 |
|
| 9 |
# Set dependencies on git index to reconfigure on changed index/branch (like switching branches, commiting, etc.). |
| 10 |
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/.git/index") |
| 11 |
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/.git/HEAD") |
| 12 |
|
| 13 |
execute_process( |
| 14 |
COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD |
| 15 |
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} |
| 16 |
OUTPUT_VARIABLE VCS_BRANCH |
| 17 |
OUTPUT_STRIP_TRAILING_WHITESPACE |
| 18 |
) |
| 19 |
|
| 20 |
execute_process( |
| 21 |
COMMAND ${GIT_EXECUTABLE} rev-parse --short=12 HEAD |
| 22 |
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} |
| 23 |
OUTPUT_VARIABLE VCS_COMMIT |
| 24 |
OUTPUT_STRIP_TRAILING_WHITESPACE |
| 25 |
) |
| 26 |
|
| 27 |
execute_process( |
| 28 |
COMMAND ${GIT_EXECUTABLE} describe --tags --dirty |
| 29 |
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} |
| 30 |
OUTPUT_VARIABLE VCS_DESCRIBE |
| 31 |
OUTPUT_STRIP_TRAILING_WHITESPACE |
| 32 |
) |
| 33 |
|
| 34 |
execute_process( |
| 35 |
COMMAND ${GIT_EXECUTABLE} diff-index --quiet HEAD -- |
| 36 |
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR} |
| 37 |
RESULT_VARIABLE VCS_DIRTY |
| 38 |
) |
| 39 |
|
| 40 |
# Workaround to update dirty flag when anything changes, as CMake can not |
| 41 |
# depend on a command that would check it the git way. This check is only used |
| 42 |
# in not dirty state to avoid unnecessary reconfigures during development. |
| 43 |
# There are a few drawbacks: |
| 44 |
# - reconfigure is not triggered by any other change than in cpp/h file (or |
| 45 |
# CMakeLists files, that is the default) |
| 46 |
# - When changes are manually reverted from dirty to non-dirty state (without |
| 47 |
# touching index/HEAD, for example editor undo), the flag is not updated |
| 48 |
# properly |
| 49 |
# This workaround shall be replaced if a better way is found. |
| 50 |
if (NOT ${VCS_DIRTY}) |
| 51 |
file(GLOB_RECURSE DEP_FILES CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/*.cpp" "${CMAKE_SOURCE_DIR}/*.h") |
| 52 |
endif() |
| 53 |
|
| 54 |
else() |
| 55 |
# Git was not found, we will try to use snapshot info. |
| 56 |
include(${CMAKE_SOURCE_DIR}/cmake/vcs-snapshot.cmake) |
| 57 |
if (NOT ${VCS_SNAPSHOT}) |
| 58 |
message(FATAL_ERROR "VCS directory does not contain a .git subdirectory, but the snapshot info is not present. Cannot build.") |
| 59 |
endif() |
| 60 |
endif() |
| 61 |
|
| 62 |
|
| 63 |
# Describe has format 0.18.4-137-gXXX, or 0.18.4 if exactly on tag. We normalize that to: |
| 64 |
# 0.18.4 -> 0.18.4 |
| 65 |
# 0.18.4-137-gXXX -> 0.18.4.137 |
| 66 |
# 0.18.4-rcX-137-gXXX -> 0.18.4.137 |
| 67 |
# And use it as version that can be parsed by the cmake VERSION |
| 68 |
|
| 69 |
# Strip the -rcX and -SHAPSHOT version specifies |
| 70 |
string(REGEX REPLACE "(-rc[0-9]+|-SNAPSHOT)" "" VCS_VERSION ${VCS_DESCRIBE}) |
| 71 |
|
| 72 |
# Replace the -N-gXXX-dirty with .N for the patchlevel |
| 73 |
string(REGEX REPLACE "-([0-9]*)-g[0-9a-z]*" ".\\1" VCS_VERSION ${VCS_VERSION}) |
| 74 |
string(REGEX REPLACE "-dirty" "" VCS_VERSION ${VCS_VERSION}) |
| 75 |
|
| 76 |
if (NOT VCS_VERSION MATCHES "^[0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?$") |
| 77 |
message("VCS_DESCRIBE ${VCS_DESCRIBE}") |
| 78 |
message("VCS_VERSION ${VCS_VERSION}") |
| 79 |
message(FATAL_ERROR "Unable to parse version description into a version in major.minor.patch.tweak format. Is the latest tag ill-formed?") |
| 80 |
endif() |