build: gracefully handle error in git-version #19289

- only update git-version if both of these conditions are met:
    - `git` command succeeds
    - `versiondef_git.h` would change (SHA1-diff)
- else print a status/warning message

also move version generation out of Lua into cmake.
This commit is contained in:
kylo252
2022-07-14 09:12:27 +02:00
committed by GitHub
parent b93cb481a2
commit 912dbbdd77
3 changed files with 60 additions and 81 deletions

View File

@@ -0,0 +1,47 @@
# Handle generating version from Git.
set(use_git_version 0)
if(NVIM_VERSION_MEDIUM)
message(STATUS "USING NVIM_VERSION_MEDIUM: ${NVIM_VERSION_MEDIUM}")
return()
endif()
find_program(GIT_EXECUTABLE git)
if(NOT GIT_EXECUTABLE)
message(AUTHOR_WARNING "Skipping version-string generation (cannot find git)")
return()
endif()
execute_process(
COMMAND git describe --first-parent --tags --always --dirty
OUTPUT_VARIABLE GIT_TAG
ERROR_VARIABLE ERR
RESULT_VARIABLE RES
)
if("${RES}" EQUAL 1)
if(EXISTS ${OUTPUT})
message(STATUS "Unable to extract version-string from git: keeping the last known version")
else()
# this will only be executed once since the file will get generated afterwards
message(AUTHOR_WARNING "Git tag extraction failed with: " "${ERR}")
file(WRITE "${OUTPUT}" "")
endif()
return()
endif()
string(STRIP "${GIT_TAG}" GIT_TAG)
string(REGEX REPLACE "^v[0-9]+.[0-9]+.[0-9]+-" "" NVIM_VERSION_GIT "${GIT_TAG}")
set(NVIM_VERSION_MEDIUM
"v${NVIM_VERSION_MAJOR}.${NVIM_VERSION_MINOR}.${NVIM_VERSION_PATCH}-dev-${NVIM_VERSION_GIT}"
)
set(NVIM_VERSION_STRING "#define NVIM_VERSION_MEDIUM \"${NVIM_VERSION_MEDIUM}\"\n")
string(SHA1 CURRENT_VERSION_HASH "${NVIM_VERSION_STRING}")
if(EXISTS ${OUTPUT})
file(SHA1 "${OUTPUT}" NVIM_VERSION_HASH)
endif()
if(NOT "${NVIM_VERSION_HASH}" STREQUAL "${CURRENT_VERSION_HASH}")
message(STATUS "Updating NVIM_VERSION_MEDIUM: ${NVIM_VERSION_MEDIUM}")
file(WRITE "${OUTPUT}" "${NVIM_VERSION_STRING}")
endif()