Skip to content

Commit 9daff41

Browse files
kuvausGreen-Sky
andauthored
fix build-info.h for git submodules (#1289)
* make git build info work with submodules --------- Co-authored-by: Green Sky <[email protected]>
1 parent bf4b22f commit 9daff41

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

CMakeLists.txt

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,19 @@ 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")
9694

@@ -100,7 +98,7 @@ if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
10098
COMMENT "Generating build details from Git"
10199
COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_SOURCE_DIR}/scripts/build-info.cmake"
102100
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
103-
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/.git/index"
101+
DEPENDS "${GIT_DIR}/index"
104102
VERBATIM
105103
)
106104
else()

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)