From eba180a1c786c4a86ead7e46f6e19febef4dd940 Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Tue, 21 Oct 2014 13:52:24 -0400 Subject: [PATCH] cmake: support git info in released tarballs --- .gitattributes | 2 +- CMakeLists.txt | 12 +----------- version.cmake | 23 +++++++++++++++++++++++ 3 files changed, 25 insertions(+), 12 deletions(-) create mode 100644 version.cmake diff --git a/.gitattributes b/.gitattributes index 6afd357c2..f75097ecd 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,2 @@ .git* export-ignore -/CMakeLists.txt export-subst \ No newline at end of file +version.cmake export-subst diff --git a/CMakeLists.txt b/CMakeLists.txt index ac5cc395a..bdb347191 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -228,17 +228,7 @@ elseif(NOT MSVC) set(EXTRA_LIBRARIES ${RT} ${PTHREAD} ${DL}) endif() -file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/version") -find_package(Git QUIET) -if(Git_FOUND OR GIT_FOUND) - message(STATUS "Found Git: ${GIT_EXECUTABLE}") - 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}") -else() - message(STATUS "WARNING: Git was not found!") - set(VERSIONTAG "unknown") - configure_file("src/version.h.in" "version/version.h") - add_custom_target(version ALL) -endif() +include(version.cmake) add_subdirectory(src) add_subdirectory(tests) diff --git a/version.cmake b/version.cmake new file mode 100644 index 000000000..4dd6db6d4 --- /dev/null +++ b/version.cmake @@ -0,0 +1,23 @@ +function (write_static_version_header hash) + set(VERSIONTAG "${hash}") + configure_file("src/version.h.in" "version/version.h") + add_custom_target(version ALL) +endfunction () + +file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/version") +find_package(Git QUIET) +if ("$Format:$" STREQUAL "") + # We're in a tarball; use hard-coded variables. + write_static_version_header("release") +elseif (GIT_FOUND OR Git_FOUND) + message(STATUS "Found Git: ${GIT_EXECUTABLE}") + add_custom_target(version ALL + COMMAND "${CMAKE_COMMAND}" + "-D" "GIT=${GIT_EXECUTABLE}" + "-D" "TO=${CMAKE_BINARY_DIR}/version/version.h" + "-P" "src/version.cmake" + WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}") +else() + message(STATUS "WARNING: Git was not found!") + write_static_version_header("unknown") +endif ()