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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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()

View File

@ -1,54 +0,0 @@
#!/usr/bin/env lua
--
-- Script to update the Git version stamp during build.
-- This is called via the custom update_version_stamp target in
-- src/nvim/CMakeLists.txt.
--
-- arg[1]: file in which to update the version string
-- arg[2]: prefix to use always ("vX.Y.Z")
local function die(msg)
io.stderr:write(string.format('%s: %s\n', arg[0], msg))
-- No error, fall back to using generated "-dev" version.
os.exit(0)
end
local function iswin()
return package.config:sub(1,1) == '\\'
end
if #arg ~= 2 then
die(string.format("Expected two args, got %d", #arg))
end
local versiondeffile = arg[1]
local prefix = arg[2]
local dev_null = iswin() and 'NUL' or '/dev/null'
local described = io.popen('git describe --first-parent --dirty 2>'..dev_null):read('*l')
if not described then
described = io.popen('git describe --first-parent --tags --always --dirty'):read('*l')
end
if not described then
io.open(versiondeffile, 'w'):write('\n')
die('git-describe failed, using empty include file.')
end
-- `git describe` annotates the most recent tagged release; for pre-release
-- builds we append that to the dev version
local with_prefix = prefix
if prefix:match('-dev$') ~= nil then
with_prefix = prefix .. '+' .. described:gsub('^v%d+%.%d+%.%d+-', '')
end
-- Read existing include file.
local current = io.open(versiondeffile, 'r')
if current then
current = current:read('*l')
end
-- Write new include file, if different.
local new = '#define NVIM_VERSION_MEDIUM "'..with_prefix..'"'
if current ~= new then
io.open(versiondeffile, 'w'):write(new .. '\n')
end

View File

@ -222,31 +222,17 @@ function(get_preproc_output varname iname)
endif() endif()
endfunction() endfunction()
# Handle generating version from Git. set(NVIM_VERSION_GIT_H ${PROJECT_BINARY_DIR}/cmake.config/auto/versiondef_git.h)
set(use_git_version 0) add_custom_target(update_version_stamp
if(NVIM_VERSION_MEDIUM) COMMAND ${CMAKE_COMMAND}
message(STATUS "NVIM_VERSION_MEDIUM: ${NVIM_VERSION_MEDIUM}") -DNVIM_VERSION_MAJOR=${NVIM_VERSION_MAJOR}
elseif(EXISTS ${PROJECT_SOURCE_DIR}/.git) -DNVIM_VERSION_MINOR=${NVIM_VERSION_MINOR}
find_program(GIT_EXECUTABLE git) -DNVIM_VERSION_PATCH=${NVIM_VERSION_PATCH}
if(GIT_EXECUTABLE) -DNVIM_VERSION_PRERELEASE=${NVIM_VERSION_PRERELEASE}
message(STATUS "Using NVIM_VERSION_MEDIUM from Git") -DOUTPUT=${NVIM_VERSION_GIT_H}
set(use_git_version 1) -P ${PROJECT_SOURCE_DIR}/cmake/GenerateVersion.cmake
else() WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
message(STATUS "Skipping version-string generation (cannot find git)") BYPRODUCTS ${NVIM_VERSION_GIT_H})
endif()
endif()
if(use_git_version)
# Create a update_version_stamp target to update the version during build.
file(RELATIVE_PATH relbuild "${PROJECT_SOURCE_DIR}" "${CMAKE_BINARY_DIR}")
add_custom_target(update_version_stamp ALL
COMMAND ${LUA_PRG} scripts/update_version_stamp.lua
${relbuild}/cmake.config/auto/versiondef_git.h
"v${NVIM_VERSION_MAJOR}.${NVIM_VERSION_MINOR}.${NVIM_VERSION_PATCH}${NVIM_VERSION_PRERELEASE}"
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
BYPRODUCTS ${CMAKE_BINARY_DIR}/cmake.config/auto/versiondef_git.h)
else()
file(WRITE ${CMAKE_BINARY_DIR}/cmake.config/auto/versiondef_git.h "")
endif()
# NVIM_GENERATED_FOR_HEADERS: generated headers to be included in headers # NVIM_GENERATED_FOR_HEADERS: generated headers to be included in headers
# NVIM_GENERATED_FOR_SOURCES: generated headers to be included in sources # NVIM_GENERATED_FOR_SOURCES: generated headers to be included in sources
@ -280,9 +266,9 @@ foreach(sfile ${NVIM_SOURCES}
get_preproc_output(PREPROC_OUTPUT ${gf_i}) get_preproc_output(PREPROC_OUTPUT ${gf_i})
set(depends "${HEADER_GENERATOR}" "${sfile}") set(depends "${HEADER_GENERATOR}" "${sfile}")
if(use_git_version AND "${f}" STREQUAL "version.c") if("${f}" STREQUAL "version.c")
# Ensure auto/versiondef_git.h exists after "make clean". # Ensure auto/versiondef_git.h exists after "make clean".
list(APPEND depends update_version_stamp) list(APPEND depends "${NVIM_VERSION_GIT_H}")
endif() endif()
add_custom_command( add_custom_command(
OUTPUT "${gf_c_h}" "${gf_h_h}" OUTPUT "${gf_c_h}" "${gf_h_h}"