improve git-hash cmake script to detect pre-commit changes properly

This commit is contained in:
falsycat 2023-10-22 21:09:41 +09:00
parent 1d9fb74368
commit ac3efc6f3f

View File

@ -1,19 +1,21 @@
set(dir "${CMAKE_CURRENT_BINARY_DIR}/generated")
set(out "${dir}/git_hash.hh")
function(_git_hash_main)
set(dir "${CMAKE_CURRENT_BINARY_DIR}/generated")
set(out "${dir}/git_hash.hh")
find_program(SH sh REQUIRED)
make_directory("${dir}")
add_custom_command(
COMMAND ${SH} -c "echo constexpr const char\* GIT_HASH = \\\"`git rev-parse --short HEAD``git diff --quiet && echo \\\*`\\\"\\; > ${out}"
OUTPUT "${out}"
DEPENDS "${PROJECT_SOURCE_DIR}/.git/HEAD"
VERBATIM
)
add_library(git_hash INTERFACE)
target_sources(git_hash PUBLIC "${out}")
target_include_directories(git_hash INTERFACE ${CMAKE_CURRENT_BINARY_DIR})
unset(out)
unset(dir)
find_program(BASH bash REQUIRED)
find_program(GIT git REQUIRED)
make_directory("${dir}")
add_custom_target(git_hash_generate
COMMAND ${BASH} -c "echo constexpr const char\* GIT_HASH = \\\"$(${GIT} rev-parse --short HEAD)$([[ -z $(${GIT} status -s) ]] || echo \\\*)\\\"\\; > ${out}.temp"
COMMAND ${BASH} -c "diff '${out}.temp' '${out}' > /dev/null || mv '${out}.temp' '${out}'"
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
VERBATIM
)
add_library(git_hash INTERFACE)
add_dependencies(git_hash git_hash_generate)
target_sources(git_hash PUBLIC "${out}")
target_include_directories(git_hash INTERFACE ${CMAKE_CURRENT_BINARY_DIR})
endfunction()
_git_hash_main()