Skip to content

Commit eba180a

Browse files
committed
cmake: support git info in released tarballs
1 parent eeffac6 commit eba180a

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
.git* export-ignore
2-
/CMakeLists.txt export-subst
2+
version.cmake export-subst

CMakeLists.txt

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -228,17 +228,7 @@ elseif(NOT MSVC)
228228
set(EXTRA_LIBRARIES ${RT} ${PTHREAD} ${DL})
229229
endif()
230230

231-
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/version")
232-
find_package(Git QUIET)
233-
if(Git_FOUND OR GIT_FOUND)
234-
message(STATUS "Found Git: ${GIT_EXECUTABLE}")
235-
add_custom_target(version ALL "${CMAKE_COMMAND}" "-D" "GIT=${GIT_EXECUTABLE}" "-D" "TO=${CMAKE_BINARY_DIR}/version/version.h" "-P" "src/version.cmake" WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
236-
else()
237-
message(STATUS "WARNING: Git was not found!")
238-
set(VERSIONTAG "unknown")
239-
configure_file("src/version.h.in" "version/version.h")
240-
add_custom_target(version ALL)
241-
endif()
231+
include(version.cmake)
242232

243233
add_subdirectory(src)
244234
add_subdirectory(tests)

version.cmake

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function (write_static_version_header hash)
2+
set(VERSIONTAG "${hash}")
3+
configure_file("src/version.h.in" "version/version.h")
4+
add_custom_target(version ALL)
5+
endfunction ()
6+
7+
file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/version")
8+
find_package(Git QUIET)
9+
if ("$Format:$" STREQUAL "")
10+
# We're in a tarball; use hard-coded variables.
11+
write_static_version_header("release")
12+
elseif (GIT_FOUND OR Git_FOUND)
13+
message(STATUS "Found Git: ${GIT_EXECUTABLE}")
14+
add_custom_target(version ALL
15+
COMMAND "${CMAKE_COMMAND}"
16+
"-D" "GIT=${GIT_EXECUTABLE}"
17+
"-D" "TO=${CMAKE_BINARY_DIR}/version/version.h"
18+
"-P" "src/version.cmake"
19+
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}")
20+
else()
21+
message(STATUS "WARNING: Git was not found!")
22+
write_static_version_header("unknown")
23+
endif ()

0 commit comments

Comments
 (0)