|
| 1 | +if(COMMAND generate_flow_graph) |
| 2 | + return() |
| 3 | +endif() |
| 4 | + |
| 5 | +function(generate_flow_graph) |
| 6 | + cmake_parse_arguments(FG "" "TARGET" "" ${ARGN}) |
| 7 | + message( |
| 8 | + STATUS |
| 9 | + "generate_flow_graph(${FG_TARGET}) is disabled because mmdc was not found." |
| 10 | + ) |
| 11 | +endfunction() |
| 12 | + |
| 13 | +find_program(MMDC_PROGRAM "mmdc") |
| 14 | +if(MMDC_PROGRAM) |
| 15 | + message(STATUS "mmdc found at ${MMDC_PROGRAM}.") |
| 16 | +else() |
| 17 | + message(STATUS "mmdc not found. generate_flow_graph will not be available.") |
| 18 | + return() |
| 19 | +endif() |
| 20 | + |
| 21 | +function(copy_target_property OLD NEW PROP) |
| 22 | + get_target_property(val ${OLD} ${PROP}) |
| 23 | + if(NOT "${val}" STREQUAL "val-NOTFOUND") |
| 24 | + set_target_properties(${NEW} PROPERTIES "${PROP}" "${val}") |
| 25 | + endif() |
| 26 | +endfunction(copy_target_property) |
| 27 | + |
| 28 | +function(copy_target_properties OLD NEW) |
| 29 | + foreach(prop ${ARGN}) |
| 30 | + copy_target_property(${OLD} ${NEW} ${prop}) |
| 31 | + copy_target_property(${OLD} ${NEW} INTERFACE_${prop}) |
| 32 | + endforeach(prop) |
| 33 | +endfunction(copy_target_properties) |
| 34 | + |
| 35 | +function(make_flow_debug_target OLD NEW) |
| 36 | + add_library(${NEW} STATIC EXCLUDE_FROM_ALL |
| 37 | + $<TARGET_PROPERTY:${FG_TARGET},SOURCES>) |
| 38 | + copy_target_properties( |
| 39 | + ${OLD} |
| 40 | + ${NEW} |
| 41 | + COMPILE_DEFINITIONS |
| 42 | + COMPILE_FEATURES |
| 43 | + COMPILE_OPTIONS |
| 44 | + INCLUDE_DIRECTORIES |
| 45 | + LINK_LIBRARIES |
| 46 | + PRECOMPILE_HEADERS) |
| 47 | +endfunction() |
| 48 | + |
| 49 | +function(generate_flow_graph) |
| 50 | + set(oneValueArgs TARGET FLOW_NAME START_NODE END_NODE) |
| 51 | + cmake_parse_arguments(FG "" "${oneValueArgs}" "" ${ARGN}) |
| 52 | + set(target_stem "${FG_TARGET}.${FG_FLOW_NAME}") |
| 53 | + |
| 54 | + # generate a header that overrides the flow service |
| 55 | + get_filename_component(header "${target_stem}.debug.hpp" ABSOLUTE BASE_DIR |
| 56 | + ${CMAKE_CURRENT_BINARY_DIR}) |
| 57 | + set(FLOW_NAME ${FG_FLOW_NAME}) |
| 58 | + set(START_NODE ${FG_START_NODE}) |
| 59 | + set(END_NODE ${FG_END_NODE}) |
| 60 | + set(RENDERER "mermaid") |
| 61 | + configure_file("${CMAKE_CURRENT_FUNCTION_LIST_DIR}/debug_flow.hpp.in" |
| 62 | + ${header} @ONLY) |
| 63 | + |
| 64 | + # set up a library that injects the header |
| 65 | + set(injection_lib "${target_stem}.lib") |
| 66 | + add_library(${injection_lib} INTERFACE) |
| 67 | + target_compile_options(${injection_lib} INTERFACE -include ${header}) |
| 68 | + |
| 69 | + # set up a new target from the existing one |
| 70 | + set(debug_target "${target_stem}.bin") |
| 71 | + make_flow_debug_target(${FG_TARGET} ${debug_target}) |
| 72 | + target_link_libraries(${debug_target} PRIVATE ${injection_lib}) |
| 73 | + |
| 74 | + # generate the graph source |
| 75 | + set(graph_stem "${target_stem}.graph") |
| 76 | + set(graph_source "${graph_stem}.mmd") |
| 77 | + set(graph "${graph_stem}.svg") |
| 78 | + set(awk_gcc_cmd "awk '{gsub(\"\\\\\\\\012\",\"\\n\")}\;1'") |
| 79 | + set(awk_clang_cmd "awk '{gsub(\"\\\\\\\\n\",\"\\n\")}\;1'") |
| 80 | + add_custom_command( |
| 81 | + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${graph_source} |
| 82 | + DEPENDS $<TARGET_PROPERTY:${debug_target},SOURCES> |
| 83 | + DEPENDS ${header} |
| 84 | + COMMAND |
| 85 | + ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target |
| 86 | + ${debug_target} | bash -c ${awk_gcc_cmd} | bash -c ${awk_clang_cmd} |
| 87 | + | tac | sed -n "/^~~~~END/,/^~~~~START/{p;/^~~~~START/q}" | tac | |
| 88 | + tail -n +2 | head -n -1 > |
| 89 | + "${CMAKE_CURRENT_BINARY_DIR}/${graph_source}" |
| 90 | + VERBATIM) |
| 91 | + |
| 92 | + # build the graph from the source |
| 93 | + if(EXISTS "${CMAKE_SOURCE_DIR}/docs/mermaid.conf") |
| 94 | + set(mm_conf "-c;${CMAKE_SOURCE_DIR}/docs/mermaid.conf") |
| 95 | + endif() |
| 96 | + if(EXISTS "${CMAKE_SOURCE_DIR}/docs/puppeteer_config.json") |
| 97 | + set(puppeteer_conf "-p;${CMAKE_SOURCE_DIR}/docs/puppeteer_config.json") |
| 98 | + endif() |
| 99 | + add_custom_command( |
| 100 | + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${graph} |
| 101 | + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${graph_source} |
| 102 | + COMMAND |
| 103 | + ${MMDC_PROGRAM} -i "${CMAKE_CURRENT_BINARY_DIR}/${graph_source}" -o |
| 104 | + "${CMAKE_CURRENT_BINARY_DIR}/${graph}" ${mm_conf} ${puppeteer_conf} |
| 105 | + COMMAND_EXPAND_LISTS) |
| 106 | + add_custom_target("${graph_stem}" |
| 107 | + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${graph}) |
| 108 | +endfunction() |
0 commit comments