Properly persist git hash on each build (#1391)

Properly persist git hash on each build

Rebuild `git_hash.h` only when `.git` folder changes

`add_custom_target()` always considers its target out-of-date which
leads to rebuilding of `git_hash.h` on every build (which is
ironically what we wanted) and consequently rebuilding of
build_options.c and relinking of c3c even when no changes are made,
which is mildly annoying.

We are replacing `add_custom_target()` with `add_custom_command()`
which depends on `.git`, so `git_hash.h` is only rebuilt if any git
commands are performed. Which is less annoying.

In case of no `.git` we simply do not depend on it which leads to
`git_hash.h` being rebuilt only once.
This commit is contained in:
Alexey Kutepov
2024-08-30 20:01:08 +07:00
committed by GitHub
parent 99ace59b45
commit 6aea0f12cd
3 changed files with 31 additions and 17 deletions

View File

@@ -101,19 +101,6 @@ if(C3_USE_TB AND GIT_FOUND AND EXISTS "${CMAKE_SOURCE_DIR}/.git")
endif()
endif()
if(GIT_FOUND AND EXISTS "${CMAKE_SOURCE_DIR}/.git")
# Get git hash
execute_process(COMMAND git rev-parse HEAD
OUTPUT_VARIABLE GIT_HASH
RESULT_VARIABLE GIT_REVPARSE_RESULT
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(GIT_REVPARSE_RESULT EQUAL "0")
add_compile_definitions(GIT_HASH="${GIT_HASH}")
else()
message(WARNING "Cannot to get Git Hash: git rev-parse failed with ${GIT_REVPARSE_RESULT}")
endif()
endif()
if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
if (C3_LLVM_VERSION STREQUAL "auto")
set(C3_LLVM_VERSION "18")
@@ -349,8 +336,21 @@ add_executable(c3c
src/utils/time.c
src/utils/http.c
src/compiler/sema_liveness.c
src/build/common_build.c)
src/build/common_build.c
${CMAKE_BINARY_DIR}/git_hash.h)
if(GIT_FOUND AND EXISTS "${CMAKE_SOURCE_DIR}/.git")
# We are inside of a git repository so rebuilding the hash every time something changes.
add_custom_command(
OUTPUT ${CMAKE_BINARY_DIR}/git_hash.h
COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_LIST_DIR}/git_hash.cmake"
DEPENDS "${CMAKE_CURRENT_LIST_DIR}/.git")
else()
# We are NOT inside of a git repository. Building the has only once.
add_custom_command(
OUTPUT ${CMAKE_BINARY_DIR}/git_hash.h
COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_LIST_DIR}/git_hash.cmake")
endif()
if (C3_USE_TB)
file(GLOB tilde-sources
@@ -391,7 +391,8 @@ endif()
target_include_directories(c3c PRIVATE
"${CMAKE_SOURCE_DIR}/src/"
"${CMAKE_SOURCE_DIR}/wrapper/include/")
"${CMAKE_SOURCE_DIR}/wrapper/include/"
"${CMAKE_BINARY_DIR}")
target_include_directories(c3c_wrappers PRIVATE