Generate GIT_COMMIT and GIT_DIRTY from CMake.

This commit is contained in:
levlam 2022-07-08 20:56:43 +03:00
parent 1dec0e203c
commit d37ad61f86
4 changed files with 43 additions and 19 deletions

29
CMake/GetGitInfo.cmake Normal file
View File

@ -0,0 +1,29 @@
# sets GIT_COMMIT and GIT_DIRTY CMake variables
function(get_git_info)
find_program(GIT_EXECUTABLE git)
unset(TD_GIT_COMMIT PARENT_SCOPE)
unset(TD_GIT_DIRTY PARENT_SCOPE)
execute_process(
COMMAND "${GIT_EXECUTABLE}" rev-parse HEAD
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
OUTPUT_VARIABLE GIT_COMMIT
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
if (NOT GIT_COMMIT)
return()
endif()
set(TD_GIT_COMMIT "${GIT_COMMIT}" PARENT_SCOPE)
execute_process(
COMMAND "${GIT_EXECUTABLE}" diff-index --quiet HEAD
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
RESULT_VARIABLE GIT_DIRTY_RESULT
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
if (GIT_DIRTY_RESULT)
set(TD_GIT_DIRTY "true" PARENT_SCOPE)
endif()
endfunction()

View File

@ -152,6 +152,11 @@ if (CLANG OR GCC)
endif() endif()
endif() endif()
if (NOT DEFINED TD_GIT_COMMIT)
include(GetGitInfo)
get_git_info()
endif()
add_subdirectory(tdtl) add_subdirectory(tdtl)
add_subdirectory(tdutils) add_subdirectory(tdutils)
@ -744,22 +749,6 @@ set(MEMPROF_STAT_SOURCE
memprof/memprof_stat.h memprof/memprof_stat.h
) )
#RULES
file(MAKE_DIRECTORY auto)
if (CMAKE_HOST_WIN32)
set(GIT_COMMIT_CMD powershell -ExecutionPolicy ByPass ./gen_git_commit_h.ps1)
else()
set(GIT_COMMIT_CMD ./gen_git_commit_h.sh)
endif()
add_custom_target(git_commit ALL
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
COMMAND ${GIT_COMMIT_CMD}
COMMENT "Generate git_commit.h"
)
#LIBRARIES #LIBRARIES
# memprof - simple library for memory usage profiling # memprof - simple library for memory usage profiling

View File

@ -100,6 +100,7 @@ set(TDUTILS_SOURCE
td/utils/find_boundary.cpp td/utils/find_boundary.cpp
td/utils/FlatHashTable.cpp td/utils/FlatHashTable.cpp
td/utils/FloodControlGlobal.cpp td/utils/FloodControlGlobal.cpp
td/utils/GitInfo.cpp
td/utils/Gzip.cpp td/utils/Gzip.cpp
td/utils/GzipByteFlow.cpp td/utils/GzipByteFlow.cpp
td/utils/Hints.cpp td/utils/Hints.cpp
@ -217,6 +218,7 @@ set(TDUTILS_SOURCE
td/utils/FloodControlGlobal.h td/utils/FloodControlGlobal.h
td/utils/FloodControlStrict.h td/utils/FloodControlStrict.h
td/utils/format.h td/utils/format.h
td/utils/GitInfo.h
td/utils/Gzip.h td/utils/Gzip.h
td/utils/GzipByteFlow.h td/utils/GzipByteFlow.h
td/utils/Hash.h td/utils/Hash.h
@ -337,10 +339,16 @@ set(TDUTILS_TEST_SOURCE
PARENT_SCOPE PARENT_SCOPE
) )
#RULES
#LIBRARIES #LIBRARIES
add_library(tdutils STATIC ${TDUTILS_SOURCE}) add_library(tdutils STATIC ${TDUTILS_SOURCE})
if (DEFINED TD_GIT_COMMIT)
set_property(SOURCE td/utils/GitInfo.cpp APPEND PROPERTY COMPILE_DEFINITIONS "GIT_COMMIT=\"${TD_GIT_COMMIT}\"")
endif()
if (DEFINED TD_GIT_DIRTY)
set_property(SOURCE td/utils/GitInfo.cpp APPEND PROPERTY COMPILE_DEFINITIONS "GIT_DIRTY=true")
endif()
if (NOT CMAKE_CROSSCOMPILING AND TDUTILS_MIME_TYPE) if (NOT CMAKE_CROSSCOMPILING AND TDUTILS_MIME_TYPE)
add_dependencies(tdutils tdmime_auto) add_dependencies(tdutils tdmime_auto)
endif() endif()

View File

@ -6,8 +6,6 @@
// //
#include "td/utils/GitInfo.h" #include "td/utils/GitInfo.h"
#include "auto/git_info.h"
#if !defined(GIT_COMMIT) #if !defined(GIT_COMMIT)
#define GIT_COMMIT "unknown" #define GIT_COMMIT "unknown"
#endif #endif