Skip to content

Commit

Permalink
cmake: Add GenerateBuildInfo.cmake script
Browse files Browse the repository at this point in the history
  • Loading branch information
hebasto committed Nov 22, 2023
1 parent 4cf1051 commit 34a1718
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 1 deletion.
115 changes: 115 additions & 0 deletions cmake/script/GenerateBuildInfo.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# Copyright (c) 2023-present The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or https://opensource.org/license/mit/.

# This script is a multiplatform port of the share/genbuild.sh shell script.

macro(fatal_error)
message(FATAL_ERROR "\n"
"Usage:\n"
" cmake -D BUILD_INFO_HEADER_PATH=<path> [-D SOURCE_DIR=<path>] -P ${CMAKE_CURRENT_LIST_FILE}\n"
"All specified paths must be absolute ones.\n"
)
endmacro()

if(DEFINED BUILD_INFO_HEADER_PATH AND IS_ABSOLUTE "${BUILD_INFO_HEADER_PATH}")
if(EXISTS "${BUILD_INFO_HEADER_PATH}")
file(STRINGS ${BUILD_INFO_HEADER_PATH} INFO LIMIT_COUNT 1)
endif()
else()
fatal_error()
endif()

if(DEFINED SOURCE_DIR)
if(IS_ABSOLUTE "${SOURCE_DIR}" AND IS_DIRECTORY "${SOURCE_DIR}")
set(WORKING_DIR ${SOURCE_DIR})
else()
fatal_error()
endif()
else()
set(WORKING_DIR ${CMAKE_CURRENT_SOURCE_DIR})
endif()

set(GIT_TAG)
set(GIT_COMMIT)
if(NOT "$ENV{BITCOIN_GENBUILD_NO_GIT}" STREQUAL "1")
find_package(Git QUIET)
if(Git_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse --is-inside-work-tree
WORKING_DIRECTORY ${WORKING_DIR}
OUTPUT_VARIABLE IS_INSIDE_WORK_TREE
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
if(IS_INSIDE_WORK_TREE)
# Clean 'dirty' status of touched files that haven't been modified.
execute_process(
COMMAND ${GIT_EXECUTABLE} diff
WORKING_DIRECTORY ${WORKING_DIR}
OUTPUT_QUIET
ERROR_QUIET
)

execute_process(
COMMAND ${GIT_EXECUTABLE} describe --abbrev=0
WORKING_DIRECTORY ${WORKING_DIR}
OUTPUT_VARIABLE MOST_RECENT_TAG
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)

execute_process(
COMMAND ${GIT_EXECUTABLE} rev-list -1 ${MOST_RECENT_TAG}
WORKING_DIRECTORY ${WORKING_DIR}
OUTPUT_VARIABLE MOST_RECENT_TAG_COMMIT
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)

execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
WORKING_DIRECTORY ${WORKING_DIR}
OUTPUT_VARIABLE HEAD_COMMIT
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)

execute_process(
COMMAND ${GIT_EXECUTABLE} diff-index --quiet HEAD --
WORKING_DIRECTORY ${WORKING_DIR}
RESULT_VARIABLE IS_DIRTY
)

if(HEAD_COMMIT STREQUAL MOST_RECENT_TAG_COMMIT AND NOT IS_DIRTY)
# If latest commit is tagged and not dirty, then use the tag name.
set(GIT_TAG ${MOST_RECENT_TAG_COMMIT})
else()
# Otherwise, generate suffix from git, i.e. string like "0e0a5173fae3-dirty".
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse --short=12 HEAD
WORKING_DIRECTORY ${WORKING_DIR}
OUTPUT_VARIABLE GIT_COMMIT
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
if(IS_DIRTY)
string(APPEND GIT_COMMIT "-dirty")
endif()
endif()
endif()
endif()
endif()

if(GIT_TAG)
set(NEWINFO "#define BUILD_GIT_TAG \"${GIT_TAG}\"\n")
elseif(GIT_COMMIT)
set(NEWINFO "#define BUILD_GIT_COMMIT \"${GIT_COMMIT}\"\n")
else()
set(NEWINFO "// No build information available\n")
endif()

# Only update the header if necessary.
if(NOT "${INFO}" STREQUAL "${NEWINFO}")
file(WRITE ${BUILD_INFO_HEADER_PATH} ${NEWINFO})
endif()
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
add_custom_target(generate_build_info
BYPRODUCTS ${PROJECT_BINARY_DIR}/src/obj/build.h
COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}/src/obj
COMMAND ${PROJECT_SOURCE_DIR}/share/genbuild.sh ${PROJECT_BINARY_DIR}/src/obj/build.h ${PROJECT_SOURCE_DIR}
COMMAND ${CMAKE_COMMAND} -DBUILD_INFO_HEADER_PATH=${PROJECT_BINARY_DIR}/src/obj/build.h -DSOURCE_DIR=${PROJECT_SOURCE_DIR} -P ${CMAKE_SOURCE_DIR}/cmake/script/GenerateBuildInfo.cmake
COMMENT "Generating obj/build.h"
VERBATIM
)
Expand Down

0 comments on commit 34a1718

Please sign in to comment.