From f071c5dd5032240c02cdf1b5ec24c20d4657d352 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gr=C3=B6ber?= Date: Sat, 24 Jun 2023 23:25:51 +0200 Subject: [PATCH] Make CURRENT_GIT_VERSION logic robust in release tarballs This automatically embeds git version information into tarballs built with git-archive. This even works for tarballs downloaded from Github. Essentially the same trick as in https://github.com/YosysHQ/yosys/pull/3138 with one complication: git-tag information is not available to the export-subst (see gitattributes(5)) format string so we use the commit date instead. We retain the current git-describe based string for git checkouts but it might be advisable to align this to be the HEAD commit date plus commit hash too. --- .gitattributes | 1 + .gitversion | 1 + libtrellis/CMakeLists.txt | 26 ++++++++++++++------------ 3 files changed, 16 insertions(+), 12 deletions(-) create mode 100644 .gitattributes create mode 100644 .gitversion diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..68f8e7ec --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +/.gitversion export-subst diff --git a/.gitversion b/.gitversion new file mode 100644 index 00000000..36cb0fa8 --- /dev/null +++ b/.gitversion @@ -0,0 +1 @@ +$Format:%cs-%h$ diff --git a/libtrellis/CMakeLists.txt b/libtrellis/CMakeLists.txt index 93d3df52..0e72616f 100644 --- a/libtrellis/CMakeLists.txt +++ b/libtrellis/CMakeLists.txt @@ -117,20 +117,22 @@ endfunction() # Avoid perturbing build if git version hasn't changed file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/generated") set(LAST_GIT_VERSION "") + +# Get version string in following order of priority: +# 1) Externally defined -DCURRENT_GIT_VERSION +# 2) git-archive export-subst trick from .gitversion +# 3) Call git describe (if available) if (NOT DEFINED CURRENT_GIT_VERSION) - execute_process(COMMAND git describe --tags --always OUTPUT_VARIABLE CURRENT_GIT_VERSION WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}) -endif() -string(STRIP "${CURRENT_GIT_VERSION}" CURRENT_GIT_VERSION) -if (EXISTS "${CMAKE_BINARY_DIR}/generated/last_git_version") - file(READ "${CMAKE_BINARY_DIR}/generated/last_git_version" LAST_GIT_VERSION) -endif() -if (NOT ("${LAST_GIT_VERSION}" STREQUAL "${CURRENT_GIT_VERSION}") OR NOT GIT_EXECUTABLE) - configure_file( - ${CMAKE_SOURCE_DIR}/tools/version.cpp.in - ${CMAKE_BINARY_DIR}/generated/version.cpp - ) + file(READ "${CMAKE_SOURCE_DIR}/.gitversion" CURRENT_GIT_VERSION) + if ("${CURRENT_GIT_VERSION}" STREQUAL "$Format:%cs-%h$") # if not substituted we're in a git checkout + if (GIT_EXECUTABLE) + execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --always OUTPUT_VARIABLE CURRENT_GIT_VERSION WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}) + string(STRIP "${CURRENT_GIT_VERSION}" CURRENT_GIT_VERSION) + endif() + else() # we're in a git-archive tarball + file(READ "${CMAKE_SOURCE_DIR}/.gitversion" CURRENT_GIT_VERSION) + endif() endif() -file(WRITE "${CMAKE_BINARY_DIR}/generated/last_git_version" CURRENT_GIT_VERSION) if (BUILD_ECPBRAM) add_executable(${PROGRAM_PREFIX}ecpbram ${INCLUDE_FILES} tools/ecpbram.cpp "${CMAKE_BINARY_DIR}/generated/version.cpp")