Skip to content

Commit 7dffb0d

Browse files
committed
make git build info work with submodules
1 parent e06f9b8 commit 7dffb0d

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

CMakeLists.txt

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,31 @@ option(LLAMA_BUILD_EXAMPLES "llama: build examples" ${LLAMA_STANDALONE})
7676
# Build info header
7777
#
7878

79-
# Write header template to binary dir to keep source directory clean
80-
file(WRITE "${CMAKE_BINARY_DIR}/BUILD_INFO.h.in" "\
81-
#ifndef BUILD_INFO_H\n\
82-
#define BUILD_INFO_H\n\
83-
\n\
84-
#define BUILD_NUMBER @BUILD_NUMBER@\n\
85-
#define BUILD_COMMIT \"@BUILD_COMMIT@\"\n\
86-
\n\
87-
#endif // BUILD_INFO_H\n\
88-
")
89-
9079
# Generate initial build-info.h
9180
include(${CMAKE_CURRENT_SOURCE_DIR}/scripts/build-info.cmake)
9281

9382
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
83+
set(GIT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/.git")
84+
85+
# Is git submodule
86+
if(NOT IS_DIRECTORY "${GIT_DIR}")
87+
file(READ ${GIT_DIR} REAL_GIT_DIR_LINK)
88+
string(REGEX REPLACE "gitdir: (.*)\n$" "\\1" REAL_GIT_DIR ${REAL_GIT_DIR_LINK})
89+
set(GIT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${REAL_GIT_DIR}")
90+
endif()
91+
9492
# Add a custom target for build-info.h
9593
add_custom_target(BUILD_INFO ALL DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/build-info.h")
94+
95+
# Add a custom command to rebuild build-info.h when .git/index changes
96+
add_custom_command(
97+
OUTPUT "${CMAKE_CURRENT_SOURCE_DIR}/build-info.h"
98+
COMMENT "Generating build details from Git"
99+
COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_SOURCE_DIR}/scripts/build-info.cmake"
100+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
101+
DEPENDS "${GIT_DIR}/index"
102+
VERBATIM
103+
)
96104
else()
97105
message(WARNING "Git repository not found; to enable automatic generation of build info, make sure Git is installed and the project is a Git repository.")
98106
endif()

scripts/build-info.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
set(TEMPLATE_FILE "${CMAKE_BINARY_DIR}/BUILD_INFO.h.in")
1+
set(TEMPLATE_FILE "${CMAKE_CURRENT_SOURCE_DIR}/scripts/build-info.h.in")
22
set(HEADER_FILE "${CMAKE_CURRENT_SOURCE_DIR}/build-info.h")
33
set(BUILD_NUMBER 0)
44
set(BUILD_COMMIT "unknown")

scripts/build-info.h.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef BUILD_INFO_H
2+
#define BUILD_INFO_H
3+
4+
#define BUILD_NUMBER @BUILD_NUMBER@
5+
#define BUILD_COMMIT "@BUILD_COMMIT@"
6+
7+
#endif // BUILD_INFO_H

0 commit comments

Comments
 (0)