Skip to content

Commit

Permalink
cable: external git info
Browse files Browse the repository at this point in the history
Problem:

A git commit sha or a branch name change causes relinking of executable targets with silkworm-buildinfo library even if nothing else has changed.

During development (e.g. local Debug builds) it is more important to have fast incremental builds than having the correct git commit sha and a branch name baked into the binary.

Solution:

Add arguments to cable_add_buildinfo_library:
* GIT_DESCRIBE
* GIT_BRANCH
* GIT_ORIGIN_URL

If passed they override corresponding git commands.
  • Loading branch information
battlmonstr committed Nov 4, 2024
1 parent d50af72 commit e3acb28
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 34 deletions.
15 changes: 13 additions & 2 deletions cmake/cable/CableBuildInfo.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@ include(GNUInstallDirs)
set(cable_buildinfo_template_dir ${CMAKE_CURRENT_LIST_DIR}/buildinfo)

function(cable_add_buildinfo_library)

cmake_parse_arguments("" "" PROJECT_NAME;EXPORT "" ${ARGN})
set(
ARG_NAMES
PROJECT_NAME
EXPORT
GIT_DESCRIBE
GIT_BRANCH
GIT_ORIGIN_URL
)
cmake_parse_arguments(PARSE_ARGV 0 "" "" "${ARG_NAMES}" "")

if(NOT _PROJECT_NAME)
message(FATAL_ERROR "The PROJECT_NAME argument missing")
Expand Down Expand Up @@ -55,10 +62,14 @@ function(cable_add_buildinfo_library)
${name}-git
COMMAND ${CMAKE_COMMAND}
-DGIT=${GIT_EXECUTABLE}
-DGIT_DESCRIBE=${_GIT_DESCRIBE}
-DGIT_BRANCH=${_GIT_BRANCH}
-DGIT_ORIGIN_URL=${_GIT_ORIGIN_URL}
-DSOURCE_DIR=${PROJECT_SOURCE_DIR}
-DOUTPUT_DIR=${output_dir}
-P ${cable_buildinfo_template_dir}/gitinfo.cmake
BYPRODUCTS ${output_dir}/gitinfo.txt
COMMENT "CableBuildInfo: updating gitinfo.txt"
)

add_custom_command(
Expand Down
74 changes: 42 additions & 32 deletions cmake/cable/buildinfo/gitinfo.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,55 @@

# Execute git only if the tool is available.
if(GIT)
execute_process(
COMMAND ${GIT} describe --always --long --tags --first-parent --match=v* --abbrev=40 --dirty
WORKING_DIRECTORY ${SOURCE_DIR}
OUTPUT_VARIABLE gitinfo
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_VARIABLE error
ERROR_STRIP_TRAILING_WHITESPACE
)
if(error)
message(WARNING "Git ${error}")
if (GIT_DESCRIBE)
set(gitdescribe "${GIT_DESCRIBE}")
else()
execute_process(
COMMAND ${GIT} describe --always --long --tags --first-parent --match=v* --abbrev=40 --dirty
WORKING_DIRECTORY ${SOURCE_DIR}
OUTPUT_VARIABLE gitdescribe
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_VARIABLE error
ERROR_STRIP_TRAILING_WHITESPACE
)
if(error)
message(WARNING "Git ${error}")
endif()
endif()

execute_process(
COMMAND ${GIT} rev-parse --abbrev-ref HEAD
WORKING_DIRECTORY ${SOURCE_DIR}
OUTPUT_VARIABLE gitbranch
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_VARIABLE error
ERROR_STRIP_TRAILING_WHITESPACE
)
if(error)
message(WARNING "Git ${error}")
if (GIT_BRANCH)
set(gitbranch "${GIT_BRANCH}")
else()
set(gitinfo "${gitinfo}\n${gitbranch}")
execute_process(
COMMAND ${GIT} rev-parse --abbrev-ref HEAD
WORKING_DIRECTORY ${SOURCE_DIR}
OUTPUT_VARIABLE gitbranch
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_VARIABLE error
ERROR_STRIP_TRAILING_WHITESPACE
)
if(error)
message(WARNING "Git ${error}")
endif()
endif()

execute_process(
COMMAND ${GIT} config --get remote.origin.url
WORKING_DIRECTORY ${SOURCE_DIR}
OUTPUT_VARIABLE gitorigin
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_VARIABLE error
ERROR_STRIP_TRAILING_WHITESPACE
)
if(error)
message(WARNING "Git ${error}")
if (GIT_ORIGIN_URL)
set(gitorigin "${GIT_ORIGIN_URL}")
else()
set(gitinfo "${gitinfo}\n${gitorigin}\n")
execute_process(
COMMAND ${GIT} config --get remote.origin.url
WORKING_DIRECTORY ${SOURCE_DIR}
OUTPUT_VARIABLE gitorigin
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_VARIABLE error
ERROR_STRIP_TRAILING_WHITESPACE
)
if(error)
message(WARNING "Git ${error}")
endif()
endif()

set(gitinfo "${gitdescribe}\n${gitbranch}\n${gitorigin}\n")
endif()

set(gitinfo_file ${OUTPUT_DIR}/gitinfo.txt)
Expand Down

0 comments on commit e3acb28

Please sign in to comment.