fix(build): non-git ("tarball") build fails #19448

Problem:
Build fails without git or .git/.
ref #19289

Solution:
Fix the version generation logic.

Test cases:
If `git` is missing:
    -- Using NVIM_VERSION_MEDIUM: v0.8.0-dev
If `.git/` is missing:
    -- Git tag extraction failed:
       fatal: not a git repository (or any of the parent directories): .git
    -- Using NVIM_VERSION_MEDIUM: v0.8.0-dev
If `git describe` fails
    -- Git tag extraction failed:
       fatal: ...
    -- Using NVIM_VERSION_MEDIUM: v0.8.0-dev

Co-authored-by: Justin M. Keyes <justinkz@gmail.com>
This commit is contained in:
kylo252 2022-07-22 16:10:24 +02:00 committed by GitHub
parent 3f2e1c1db9
commit 790f8b0e16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 21 deletions

View File

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

View File

@ -229,6 +229,7 @@ add_custom_target(update_version_stamp
-DNVIM_VERSION_PATCH=${NVIM_VERSION_PATCH} -DNVIM_VERSION_PATCH=${NVIM_VERSION_PATCH}
-DNVIM_VERSION_PRERELEASE=${NVIM_VERSION_PRERELEASE} -DNVIM_VERSION_PRERELEASE=${NVIM_VERSION_PRERELEASE}
-DOUTPUT=${NVIM_VERSION_GIT_H} -DOUTPUT=${NVIM_VERSION_GIT_H}
-DCMAKE_MESSAGE_LOG_LEVEL=${CMAKE_MESSAGE_LOG_LEVEL}
-P ${PROJECT_SOURCE_DIR}/cmake/GenerateVersion.cmake -P ${PROJECT_SOURCE_DIR}/cmake/GenerateVersion.cmake
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
BYPRODUCTS ${NVIM_VERSION_GIT_H}) BYPRODUCTS ${NVIM_VERSION_GIT_H})