Skip to content

Commit e3acb28

Browse files
committed
cable: external git info
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.
1 parent d50af72 commit e3acb28

File tree

2 files changed

+55
-34
lines changed

2 files changed

+55
-34
lines changed

cmake/cable/CableBuildInfo.cmake

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,15 @@ include(GNUInstallDirs)
2222
set(cable_buildinfo_template_dir ${CMAKE_CURRENT_LIST_DIR}/buildinfo)
2323

2424
function(cable_add_buildinfo_library)
25-
26-
cmake_parse_arguments("" "" PROJECT_NAME;EXPORT "" ${ARGN})
25+
set(
26+
ARG_NAMES
27+
PROJECT_NAME
28+
EXPORT
29+
GIT_DESCRIBE
30+
GIT_BRANCH
31+
GIT_ORIGIN_URL
32+
)
33+
cmake_parse_arguments(PARSE_ARGV 0 "" "" "${ARG_NAMES}" "")
2734

2835
if(NOT _PROJECT_NAME)
2936
message(FATAL_ERROR "The PROJECT_NAME argument missing")
@@ -55,10 +62,14 @@ function(cable_add_buildinfo_library)
5562
${name}-git
5663
COMMAND ${CMAKE_COMMAND}
5764
-DGIT=${GIT_EXECUTABLE}
65+
-DGIT_DESCRIBE=${_GIT_DESCRIBE}
66+
-DGIT_BRANCH=${_GIT_BRANCH}
67+
-DGIT_ORIGIN_URL=${_GIT_ORIGIN_URL}
5868
-DSOURCE_DIR=${PROJECT_SOURCE_DIR}
5969
-DOUTPUT_DIR=${output_dir}
6070
-P ${cable_buildinfo_template_dir}/gitinfo.cmake
6171
BYPRODUCTS ${output_dir}/gitinfo.txt
72+
COMMENT "CableBuildInfo: updating gitinfo.txt"
6273
)
6374

6475
add_custom_command(

cmake/cable/buildinfo/gitinfo.cmake

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,55 @@
44

55
# Execute git only if the tool is available.
66
if(GIT)
7-
execute_process(
8-
COMMAND ${GIT} describe --always --long --tags --first-parent --match=v* --abbrev=40 --dirty
9-
WORKING_DIRECTORY ${SOURCE_DIR}
10-
OUTPUT_VARIABLE gitinfo
11-
OUTPUT_STRIP_TRAILING_WHITESPACE
12-
ERROR_VARIABLE error
13-
ERROR_STRIP_TRAILING_WHITESPACE
14-
)
15-
if(error)
16-
message(WARNING "Git ${error}")
7+
if (GIT_DESCRIBE)
8+
set(gitdescribe "${GIT_DESCRIBE}")
9+
else()
10+
execute_process(
11+
COMMAND ${GIT} describe --always --long --tags --first-parent --match=v* --abbrev=40 --dirty
12+
WORKING_DIRECTORY ${SOURCE_DIR}
13+
OUTPUT_VARIABLE gitdescribe
14+
OUTPUT_STRIP_TRAILING_WHITESPACE
15+
ERROR_VARIABLE error
16+
ERROR_STRIP_TRAILING_WHITESPACE
17+
)
18+
if(error)
19+
message(WARNING "Git ${error}")
20+
endif()
1721
endif()
1822

19-
execute_process(
20-
COMMAND ${GIT} rev-parse --abbrev-ref HEAD
21-
WORKING_DIRECTORY ${SOURCE_DIR}
22-
OUTPUT_VARIABLE gitbranch
23-
OUTPUT_STRIP_TRAILING_WHITESPACE
24-
ERROR_VARIABLE error
25-
ERROR_STRIP_TRAILING_WHITESPACE
26-
)
27-
if(error)
28-
message(WARNING "Git ${error}")
23+
if (GIT_BRANCH)
24+
set(gitbranch "${GIT_BRANCH}")
2925
else()
30-
set(gitinfo "${gitinfo}\n${gitbranch}")
26+
execute_process(
27+
COMMAND ${GIT} rev-parse --abbrev-ref HEAD
28+
WORKING_DIRECTORY ${SOURCE_DIR}
29+
OUTPUT_VARIABLE gitbranch
30+
OUTPUT_STRIP_TRAILING_WHITESPACE
31+
ERROR_VARIABLE error
32+
ERROR_STRIP_TRAILING_WHITESPACE
33+
)
34+
if(error)
35+
message(WARNING "Git ${error}")
36+
endif()
3137
endif()
3238

33-
execute_process(
34-
COMMAND ${GIT} config --get remote.origin.url
35-
WORKING_DIRECTORY ${SOURCE_DIR}
36-
OUTPUT_VARIABLE gitorigin
37-
OUTPUT_STRIP_TRAILING_WHITESPACE
38-
ERROR_VARIABLE error
39-
ERROR_STRIP_TRAILING_WHITESPACE
40-
)
41-
if(error)
42-
message(WARNING "Git ${error}")
39+
if (GIT_ORIGIN_URL)
40+
set(gitorigin "${GIT_ORIGIN_URL}")
4341
else()
44-
set(gitinfo "${gitinfo}\n${gitorigin}\n")
42+
execute_process(
43+
COMMAND ${GIT} config --get remote.origin.url
44+
WORKING_DIRECTORY ${SOURCE_DIR}
45+
OUTPUT_VARIABLE gitorigin
46+
OUTPUT_STRIP_TRAILING_WHITESPACE
47+
ERROR_VARIABLE error
48+
ERROR_STRIP_TRAILING_WHITESPACE
49+
)
50+
if(error)
51+
message(WARNING "Git ${error}")
52+
endif()
4553
endif()
54+
55+
set(gitinfo "${gitdescribe}\n${gitbranch}\n${gitorigin}\n")
4656
endif()
4757

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

0 commit comments

Comments
 (0)