Skip to content

Commit b50bc99

Browse files
committed
Use the git-version CMake module.
Use the newly added CMake module that determines the git hash of the source repository. Additionally, for the CMake-based build system, (1) reproduce the logic from file `git-hash.sh' for determining the appropriate git hash to build into C-Reduce; and (2) actually pass the git hash to the C/C++ compilers. This includes the ability to "burn the hash" into a release tarball.
1 parent a570f20 commit b50bc99

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

.gitattributes

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
* text=auto
2-
git-hash.sh export-subst
3-
tests/test0.bat -text
1+
* text=auto
2+
CMakeLists.txt export-subst
3+
git-hash.sh export-subst
4+
tests/test0.bat -text

CMakeLists.txt

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,38 @@
1111
cmake_minimum_required(VERSION 2.8.12)
1212
project(creduce)
1313

14+
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
15+
1416
include(CheckIncludeFile)
1517
include(CheckCXXCompilerFlag)
18+
include(GetGitRevisionDescription)
1619

1720
###############################################################################
1821

22+
# Determine the short git hash for the source tree. The logic here follows the
23+
# logic in the `git-hash.sh` script.
24+
#
25+
## METHOD 1: The source tree is the result of `git archive'.
26+
# `git archive' inserts the abbreviated hash of the archive's commit into this
27+
# file. (See the `.gitattributes' file.)
28+
#
29+
set(GIT_HASH "$Format:%h$")
30+
if(GIT_HASH MATCHES "^\\$")
31+
## METHOD 2: The source tree is a git repository.
32+
get_git_head_revision(GIT_REFSPEC GIT_HASH)
33+
if(NOT GIT_HASH STREQUAL "GITDIR-NOTFOUND")
34+
# Trim to the short hash.
35+
string(SUBSTRING "${GIT_HASH}" 0 7 GIT_HASH)
36+
else()
37+
## METHOD 3: Give up.
38+
set(GIT_HASH "unknown")
39+
endif()
40+
endif()
41+
42+
add_definitions("-DGIT_VERSION=\"${GIT_HASH}\"")
43+
44+
###
45+
1946
# Generate config.h
2047
#
2148
set(ENABLE_TRANS_ASSERT ON CACHE BOOL "Use assert() in clang_delta transformations.")
@@ -47,11 +74,6 @@ configure_file("cmake_config.h.in" "${PROJECT_BINARY_DIR}/config.h")
4774

4875
###
4976

50-
execute_process(COMMAND "git" "show" "-s" "--format=%h" WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" OUTPUT_VARIABLE creduce_GIT_HASH)
51-
string(STRIP "${creduce_GIT_HASH}" creduce_GIT_HASH)
52-
53-
###
54-
5577
# Include this flag if the C++ compiler supports it.
5678
# See LLVM file `share/llvm/cmake/HandleLLVMOptions.cmake`.
5779
check_cxx_compiler_flag(

creduce/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,10 @@ string(REPLACE "@PACKAGE_VERSION@" "${creduce_PACKAGE_VERSION}"
221221
CREDUCE_CONTENT "${CREDUCE_CONTENT}")
222222
string(REPLACE "@VERSION@" "${creduce_VERSION}"
223223
CREDUCE_CONTENT "${CREDUCE_CONTENT}")
224-
string(REPLACE "@GIT_HASH@" "${creduce_GIT_HASH}"
225-
CREDUCE_CONTENT "${CREDUCE_CONTENT}")
226224
# ENE: I do not understand why the \'s seem to be required in the following
227225
# match strings.
226+
string(REPLACE "\@GIT_HASH@" "${GIT_HASH}"
227+
CREDUCE_CONTENT "${CREDUCE_CONTENT}")
228228
string(REPLACE "\@ASTYLE@" "${ASTYLE}"
229229
CREDUCE_CONTENT "${CREDUCE_CONTENT}")
230230
string(REPLACE "\@CLANG_FORMAT@" "${CLANG_FORMAT}"

0 commit comments

Comments
 (0)