Skip to content

Commit 044ff37

Browse files
committed
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 YosysHQ/yosys#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.
1 parent 89ffb45 commit 044ff37

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.gitcommit export-subst

.gitversion

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
$Format:%cs-%h$

libtrellis/CMakeLists.txt

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,21 @@ endfunction()
117117
# Avoid perturbing build if git version hasn't changed
118118
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/generated")
119119
set(LAST_GIT_VERSION "")
120+
121+
# Get version string in following order of priority:
122+
# 1) Externally defined -DCURRENT_GIT_VERSION
123+
# 2) git-archive export-subst trick from .gitversion
124+
# 3) Call git describe (if available)
120125
if (NOT DEFINED CURRENT_GIT_VERSION)
121-
execute_process(COMMAND git describe --tags --always OUTPUT_VARIABLE CURRENT_GIT_VERSION WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
122-
endif()
123-
string(STRIP "${CURRENT_GIT_VERSION}" CURRENT_GIT_VERSION)
124-
if (EXISTS "${CMAKE_BINARY_DIR}/generated/last_git_version")
125-
file(READ "${CMAKE_BINARY_DIR}/generated/last_git_version" LAST_GIT_VERSION)
126-
endif()
127-
if (NOT ("${LAST_GIT_VERSION}" STREQUAL "${CURRENT_GIT_VERSION}") OR NOT GIT_EXECUTABLE)
128-
configure_file(
129-
${CMAKE_SOURCE_DIR}/tools/version.cpp.in
130-
${CMAKE_BINARY_DIR}/generated/version.cpp
131-
)
126+
file(READ "${CMAKE_SOURCE_DIR}/.gitversion" CURRENT_GIT_VERSION)
127+
if ("${CURRENT_GIT_VERSION}" STREQUAL "$Format:%cs-%h$") # if not substituted we're in a git checkout
128+
if (GIT_EXECUTABLE)
129+
execute_process(COMMAND ${GIT_EXECUTABLE} describe --tags --always OUTPUT_VARIABLE CURRENT_GIT_VERSION WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
130+
endif()
131+
else() # we're in a git-archive tarball
132+
file(READ "${CMAKE_SOURCE_DIR}/.gitversion" CURRENT_GIT_VERSION)
133+
endif()
132134
endif()
133-
file(WRITE "${CMAKE_BINARY_DIR}/generated/last_git_version" CURRENT_GIT_VERSION)
134135

135136
if (BUILD_ECPBRAM)
136137
add_executable(${PROGRAM_PREFIX}ecpbram ${INCLUDE_FILES} tools/ecpbram.cpp "${CMAKE_BINARY_DIR}/generated/version.cpp")

0 commit comments

Comments
 (0)