diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -3,33 +3,23 @@ project(bitcoin-test) # Process json files. -find_program(HEXDUMP "hexdump") -find_program(SED "sed") +find_program(PYTHON python) function(gen_json_header NAME) set(HEADERS "") foreach(f ${ARGN}) - set(hex "${CMAKE_CURRENT_BINARY_DIR}/${f}.hex") set(h "${CMAKE_CURRENT_BINARY_DIR}/${f}.h") - add_custom_command(OUTPUT ${hex} - COMMAND ${HEXDUMP} - ARGS - -v - -e "8/1 \"0x%02x, \"" - -e "\"\\n\"" - "${CMAKE_CURRENT_SOURCE_DIR}/${f}" > ${hex} - MAIN_DEPENDENCY ${f} - VERBATIM - ) + # Get the proper name for the test variable. get_filename_component(TEST_NAME ${f} NAME_WE) add_custom_command(OUTPUT ${h} - COMMAND ${SED} + COMMAND ${PYTHON} ARGS - -e "s/0x ,//g" - -e "1 s/^/namespace json_tests{\\nstatic unsigned const char ${TEST_NAME}[] = {\\n/" - -e "\${s:\$:\\n\\};\\n\\}; // namespace json_tests:}" - < ${hex} > ${h} - MAIN_DEPENDENCY ${hex} + "${CMAKE_CURRENT_SOURCE_DIR}/data/generate_header.py" + "${TEST_NAME}" + "${CMAKE_CURRENT_SOURCE_DIR}/${f}" > ${h} + MAIN_DEPENDENCY ${f} + DEPENDS + "data/generate_header.py" VERBATIM ) list(APPEND HEADERS ${h}) diff --git a/src/test/data/generate_header.py b/src/test/data/generate_header.py new file mode 100755 --- /dev/null +++ b/src/test/data/generate_header.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python +# Copyright (c) 2018 The Bitcoin developers + +import os +import sys + + +def main(test_name, input_file): + with open(input_file, "rb") as f: + contents = f.read() + + print("namespace json_tests{") + print(" static unsigned const char {}[] = {{".format(test_name)) + print(", ".join(map(lambda x: "0x{:02x}".format(ord(x)), contents))) + print(" };") + print("};") + + +if __name__ == "__main__": + if len(sys.argv) != 3: + print("We need additional pylons!") + os.exit(1) + + main(sys.argv[1], sys.argv[2])