Skip to content

Commit

Permalink
build: Generate ip_asn.h with Cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
fjahr committed Aug 29, 2024
1 parent d8ae8e3 commit e5a25fd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cmake/module/GenerateHeaders.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,17 @@ function(generate_header_from_raw raw_source_relpath)
VERBATIM
)
endfunction()

function(generate_ip_asn_header)
set(RAW_FILE "${CMAKE_SOURCE_DIR}/contrib/asmap/ip_asn.dat")
set(GENERATED_FILE "${CMAKE_CURRENT_SOURCE_DIR}/init/ip_asn.h")

add_custom_command(
OUTPUT ${GENERATED_FILE}
COMMAND ${CMAKE_COMMAND} -DGENERATED_FILE=${GENERATED_FILE} -DRAW_FILE=${RAW_FILE} -P ${PROJECT_SOURCE_DIR}/cmake/script/WriteIPASN.cmake
DEPENDS ${RAW_FILE} ${PROJECT_SOURCE_DIR}/cmake/script/WriteIPASN.cmake
VERBATIM
COMMENT "Generating embedded ASMap file init/ip_asn.h"
)
add_custom_target(generate_ip_asn_header ALL DEPENDS ${GENERATED_FILE})
endfunction()
16 changes: 16 additions & 0 deletions cmake/script/WriteIPASN.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
file(WRITE "${GENERATED_FILE}" "#ifndef BITCOIN_INIT_IP_ASN_H\n")
file(APPEND "${GENERATED_FILE}" "#define BITCOIN_INIT_IP_ASN_H\n")
file(APPEND "${GENERATED_FILE}" "/**\n")
file(APPEND "${GENERATED_FILE}" " * ASMap data, mapping IP prefixes to ASNs,\n")
file(APPEND "${GENERATED_FILE}" " * AUTOGENERATED by contrib/asmap/asmap-tool.py\n")
file(APPEND "${GENERATED_FILE}" " */\n")

execute_process(
COMMAND xxd -i "${RAW_FILE}"
COMMAND sed "s/unsigned char .*/static const unsigned char ip_asn[] = {/"
COMMAND sed "s/^unsigned int .*_len/static const unsigned int ip_asn_len/"
OUTPUT_VARIABLE HEX_DUMP
)
file(APPEND "${GENERATED_FILE}" "${HEX_DUMP}")

file(APPEND "${GENERATED_FILE}" "#endif // BITCOIN_INIT_IP_ASN_H\n")
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

include(GNUInstallDirs)
include(AddWindowsResources)
include(GenerateHeaders)

configure_file(${PROJECT_SOURCE_DIR}/cmake/bitcoin-config.h.in config/bitcoin-config.h @ONLY)
include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
Expand Down Expand Up @@ -298,6 +299,8 @@ target_link_libraries(bitcoin_node
$<TARGET_NAME_IF_EXISTS:bitcoin_zmq>
$<TARGET_NAME_IF_EXISTS:USDT::headers>
)
generate_ip_asn_header()
add_dependencies(bitcoin_node generate_ip_asn_header)


# Bitcoin Core bitcoind.
Expand Down

0 comments on commit e5a25fd

Please sign in to comment.