diff --git a/config/CMakeLists.txt b/config/CMakeLists.txt index 7bd48a1f1e..0ca41d5dfd 100644 --- a/config/CMakeLists.txt +++ b/config/CMakeLists.txt @@ -121,35 +121,6 @@ configure_file ( ) # generate version definitions -if(NVIM_VERSION_MEDIUM) - message(STATUS "NVIM_VERSION_MEDIUM: ${NVIM_VERSION_MEDIUM}") -elseif(${CMAKE_VERSION} VERSION_LESS "3.2.0") - message(STATUS "Skipping version-string generation (requires CMake 3.2.0+)") -elseif(EXISTS ${PROJECT_SOURCE_DIR}/.git) - find_program(GIT_EXECUTABLE git) - if(GIT_EXECUTABLE) - # Get current version. - execute_process( - COMMAND ${GIT_EXECUTABLE} describe --dirty - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} - OUTPUT_VARIABLE NVIM_VERSION_MEDIUM - OUTPUT_STRIP_TRAILING_WHITESPACE) - message(STATUS "NVIM_VERSION_MEDIUM (from git): ${NVIM_VERSION_MEDIUM}") - - # 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}/.version_stamp - ${relbuild}/config/auto/versiondef.h - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} - BYPRODUCTS ${CMAKE_BINARY_DIR}/config/auto/versiondef.h) - add_dependencies(nvim update_version_stamp) - else() - message(STATUS "Skipping version-string generation (cannot find git)") - endif() -endif() - configure_file ( "${PROJECT_SOURCE_DIR}/config/versiondef.h.in" "${PROJECT_BINARY_DIR}/config/auto/versiondef.h" diff --git a/config/versiondef.h.in b/config/versiondef.h.in index b9565735b3..22cad87249 100644 --- a/config/versiondef.h.in +++ b/config/versiondef.h.in @@ -5,7 +5,11 @@ #define NVIM_VERSION_MINOR @NVIM_VERSION_MINOR@ #define NVIM_VERSION_PATCH @NVIM_VERSION_PATCH@ #define NVIM_VERSION_PRERELEASE "@NVIM_VERSION_PRERELEASE@" + #cmakedefine NVIM_VERSION_MEDIUM "@NVIM_VERSION_MEDIUM@" +#ifndef NVIM_VERSION_MEDIUM +# include "auto/versiondef_git.h" +#endif #define NVIM_API_LEVEL @NVIM_API_LEVEL@ #define NVIM_API_LEVEL_COMPAT @NVIM_API_LEVEL_COMPAT@ diff --git a/scripts/update_version_stamp.lua b/scripts/update_version_stamp.lua old mode 100644 new mode 100755 index f01642043a..509e1f6fad --- a/scripts/update_version_stamp.lua +++ b/scripts/update_version_stamp.lua @@ -4,11 +4,11 @@ -- This is called via the custom update_version_stamp target in -- src/nvim/CMakeLists.txt. -- --- arg[1]: file containing the last git-describe output --- arg[2]: file in which to update the version string +-- arg[1]: file in which to update the version string +-- arg[2]: prefix to use always ("vX.Y.Z") local function die(msg) - print(string.format('%s: %s', arg[0], 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 @@ -17,29 +17,34 @@ if #arg ~= 2 then die(string.format("Expected two args, got %d", #arg)) end -local stampfile = arg[1] -local stamp = io.open(stampfile, 'r') -if stamp then - stamp = stamp:read('*l') +local versiondeffile = arg[1] +local prefix = arg[2] + +local described = io.popen('git describe --dirty'):read('*l') +if not described then + described = io.popen('git describe --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 -local current = io.popen('git describe --dirty'):read('*l') -if not current then - die('git-describe failed') +-- `git describe` annotates the most recent tagged release; for pre-release +-- builds we must replace that with the unreleased version. +local with_prefix = described:gsub("^v%d+%.%d+%.%d+", prefix) +if described == with_prefix then + -- Prepend the prefix always, e.g. with "nightly-12208-g4041b62b9". + with_prefix = prefix .. "-" .. described end -if stamp ~= current then - if stamp then - print(string.format('git version changed: %s -> %s', stamp, current)) - end - local new_lines = {} - local versiondeffile = arg[2] - for line in io.lines(versiondeffile) do - if line:match("NVIM_VERSION_MEDIUM") then - line = '#define NVIM_VERSION_MEDIUM "'..current..'"' - end - new_lines[#new_lines + 1] = line - end - io.open(versiondeffile, 'w'):write(table.concat(new_lines, '\n') .. '\n') - io.open(stampfile, 'w'):write(current) +-- 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 diff --git a/src/nvim/CMakeLists.txt b/src/nvim/CMakeLists.txt index 27977e3a40..a64944ab0d 100644 --- a/src/nvim/CMakeLists.txt +++ b/src/nvim/CMakeLists.txt @@ -217,6 +217,34 @@ function(get_preproc_output varname iname) endif() endfunction() +# Handle generating version from Git. +set(use_git_version 0) +if(NVIM_VERSION_MEDIUM) + message(STATUS "NVIM_VERSION_MEDIUM: ${NVIM_VERSION_MEDIUM}") +elseif(${CMAKE_VERSION} VERSION_LESS "3.2.0") + message(STATUS "Skipping version-string generation (requires CMake 3.2.0+)") +elseif(EXISTS ${PROJECT_SOURCE_DIR}/.git) + find_program(GIT_EXECUTABLE git) + if(GIT_EXECUTABLE) + message(STATUS "Using NVIM_VERSION_MEDIUM from Git") + set(use_git_version 1) + else() + message(STATUS "Skipping version-string generation (cannot find git)") + 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}/config/auto/versiondef_git.h + "v${NVIM_VERSION_MAJOR}.${NVIM_VERSION_MINOR}.${NVIM_VERSION_PATCH}" + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + BYPRODUCTS ${CMAKE_BINARY_DIR}/config/auto/versiondef_git.h) +else() + file(WRITE ${CMAKE_BINARY_DIR}/config/auto/versiondef_git.h "") +endif() + # NVIM_GENERATED_FOR_HEADERS: generated headers to be included in headers # NVIM_GENERATED_FOR_SOURCES: generated headers to be included in sources # NVIM_GENERATED_SOURCES: generated source files @@ -245,12 +273,16 @@ foreach(sfile ${NVIM_SOURCES} get_preproc_output(PREPROC_OUTPUT ${gf_i}) + set(depends "${HEADER_GENERATOR}" "${sfile}") + if(use_git_version AND "${f}" STREQUAL "version.c") + # Ensure auto/versiondef_git.h exists after "make clean". + list(APPEND depends update_version_stamp) + endif() add_custom_command( OUTPUT "${gf_c_h}" "${gf_h_h}" COMMAND ${CMAKE_C_COMPILER} ${sfile} ${PREPROC_OUTPUT} ${gen_cflags} ${C_FLAGS_ARRAY} COMMAND "${LUA_PRG}" "${HEADER_GENERATOR}" "${sfile}" "${gf_c_h}" "${gf_h_h}" "${gf_i}" - DEPENDS "${HEADER_GENERATOR}" "${sfile}" - ) + DEPENDS ${depends}) list(APPEND NVIM_GENERATED_FOR_SOURCES "${gf_c_h}") list(APPEND NVIM_GENERATED_FOR_HEADERS "${gf_h_h}") if(${d} MATCHES "^api$" AND NOT ${f} MATCHES "^api/helpers.c$")