From 9daff419f6be818595ddbea293a0ea7283af726e Mon Sep 17 00:00:00 2001 From: kuvaus <22169537+kuvaus@users.noreply.github.com> Date: Wed, 3 May 2023 03:43:43 +0300 Subject: [PATCH] fix build-info.h for git submodules (#1289) * make git build info work with submodules --------- Co-authored-by: Green Sky --- CMakeLists.txt | 22 ++++++++++------------ scripts/build-info.cmake | 2 +- scripts/build-info.h.in | 7 +++++++ 3 files changed, 18 insertions(+), 13 deletions(-) create mode 100644 scripts/build-info.h.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 53d48a6c5..48e3238df 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,21 +76,19 @@ option(LLAMA_BUILD_EXAMPLES "llama: build examples" ${LLAMA_STANDALONE}) # Build info header # -# Write header template to binary dir to keep source directory clean -file(WRITE "${CMAKE_BINARY_DIR}/BUILD_INFO.h.in" "\ -#ifndef BUILD_INFO_H\n\ -#define BUILD_INFO_H\n\ -\n\ -#define BUILD_NUMBER @BUILD_NUMBER@\n\ -#define BUILD_COMMIT \"@BUILD_COMMIT@\"\n\ -\n\ -#endif // BUILD_INFO_H\n\ -") - # Generate initial build-info.h include(${CMAKE_CURRENT_SOURCE_DIR}/scripts/build-info.cmake) if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git") + set(GIT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/.git") + + # Is git submodule + if(NOT IS_DIRECTORY "${GIT_DIR}") + file(READ ${GIT_DIR} REAL_GIT_DIR_LINK) + string(REGEX REPLACE "gitdir: (.*)\n$" "\\1" REAL_GIT_DIR ${REAL_GIT_DIR_LINK}) + set(GIT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/${REAL_GIT_DIR}") + endif() + # Add a custom target for build-info.h add_custom_target(BUILD_INFO ALL DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/build-info.h") @@ -100,7 +98,7 @@ if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git") COMMENT "Generating build details from Git" COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_SOURCE_DIR}/scripts/build-info.cmake" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/.git/index" + DEPENDS "${GIT_DIR}/index" VERBATIM ) else() diff --git a/scripts/build-info.cmake b/scripts/build-info.cmake index fb46ed2b5..5023b77ab 100644 --- a/scripts/build-info.cmake +++ b/scripts/build-info.cmake @@ -1,4 +1,4 @@ -set(TEMPLATE_FILE "${CMAKE_BINARY_DIR}/BUILD_INFO.h.in") +set(TEMPLATE_FILE "${CMAKE_CURRENT_SOURCE_DIR}/scripts/build-info.h.in") set(HEADER_FILE "${CMAKE_CURRENT_SOURCE_DIR}/build-info.h") set(BUILD_NUMBER 0) set(BUILD_COMMIT "unknown") diff --git a/scripts/build-info.h.in b/scripts/build-info.h.in new file mode 100644 index 000000000..75d1e16fd --- /dev/null +++ b/scripts/build-info.h.in @@ -0,0 +1,7 @@ +#ifndef BUILD_INFO_H +#define BUILD_INFO_H + +#define BUILD_NUMBER @BUILD_NUMBER@ +#define BUILD_COMMIT "@BUILD_COMMIT@" + +#endif // BUILD_INFO_H