1function(_pico_init_pioasm)
2    # Assemble the version string from components instead of using PICO_SDK_VERSION_STRING, because the version string
3    # potentially has a PRE_RELEASE_ID suffix, which will trip up the find_package call.
4    find_package(pico-sdk-tools "${PICO_SDK_VERSION_MAJOR}.${PICO_SDK_VERSION_MINOR}.${PICO_SDK_VERSION_REVISION}" QUIET CONFIG CMAKE_FIND_ROOT_PATH_BOTH)
5    if (NOT Pioasm_FOUND)
6        set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PICO_SDK_PATH}/tools)
7        # todo CMAKE_CURRENT_FUNCTION_LIST_DIR ... what version?
8        find_package(Pioasm REQUIRED)
9    endif()
10endfunction()
11
12# PICO_CMAKE_CONFIG: PICO_DEFAULT_PIOASM_OUTPUT_FORMAT, default output format used by pioasm when using pico_generate_pio_header, default=c-sdk, group=build
13function(pico_generate_pio_header TARGET PIO)
14    _pico_init_pioasm()
15    cmake_parse_arguments(pico_generate_pio_header "" "OUTPUT_FORMAT;OUTPUT_DIR" "" ${ARGN} )
16
17    if (pico_generate_pio_header_OUTPUT_FORMAT)
18        set(OUTPUT_FORMAT "${pico_generate_pio_header_OUTPUT_FORMAT}")
19    elseif(DEFINED PICO_DEFAULT_PIOASM_OUTPUT_FORMAT)
20        set(OUTPUT_FORMAT "${PICO_DEFAULT_PIOASM_OUTPUT_FORMAT}")
21    else()
22        set(OUTPUT_FORMAT "c-sdk")
23    endif()
24
25    if (pico_generate_pio_header_OUTPUT_DIR)
26        get_filename_component(HEADER_DIR ${pico_generate_pio_header_OUTPUT_DIR} ABSOLUTE)
27    else()
28        set(HEADER_DIR "${CMAKE_CURRENT_BINARY_DIR}")
29    endif()
30    get_filename_component(PIO_NAME ${PIO} NAME)
31    set(HEADER "${HEADER_DIR}/${PIO_NAME}.h")
32    #message("Will generate ${HEADER}")
33    get_filename_component(HEADER_GEN_TARGET ${PIO} NAME_WE)
34    set(HEADER_GEN_TARGET "${TARGET}_${HEADER_GEN_TARGET}_pio_h")
35
36    add_custom_target(${HEADER_GEN_TARGET} DEPENDS ${HEADER})
37
38    add_custom_command(OUTPUT ${HEADER}
39            DEPENDS ${PIO}
40            COMMAND Pioasm -o ${OUTPUT_FORMAT} ${PIO} ${HEADER}
41            VERBATIM)
42    add_dependencies(${TARGET} ${HEADER_GEN_TARGET})
43    get_target_property(target_type ${TARGET} TYPE)
44    if ("INTERFACE_LIBRARY" STREQUAL "${target_type}")
45        target_include_directories(${TARGET} INTERFACE ${HEADER_DIR})
46    else()
47        target_include_directories(${TARGET} PUBLIC ${HEADER_DIR})
48    endif()
49endfunction()
50
51function(pico_add_uf2_output TARGET)
52    get_target_property(${TARGET}_archive_directory ${TARGET} ARCHIVE_OUTPUT_DIRECTORY)
53    if (${TARGET}_archive_directory)
54        get_filename_component(output_path "${${TARGET}_archive_directory}"
55                REALPATH BASE_DIR "${CMAKE_CURRENT_BINARY_DIR}")
56        file(MAKE_DIRECTORY "${output_path}")
57        set(output_path "${output_path}/")
58    else()
59        set(output_path "")
60    endif()
61    find_package(pico-sdk-tools "${PICO_SDK_VERSION_MAJOR}.${PICO_SDK_VERSION_MINOR}.${PICO_SDK_VERSION_REVISION}" QUIET CONFIG CMAKE_FIND_ROOT_PATH_BOTH)
62    if (NOT ELF2UF2_FOUND)
63        set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PICO_SDK_PATH}/tools)
64        find_package(ELF2UF2)
65    endif()
66    if (ELF2UF2_FOUND)
67        add_custom_command(TARGET ${TARGET} POST_BUILD
68            COMMAND ELF2UF2 $<TARGET_FILE:${TARGET}> ${output_path}$<IF:$<BOOL:$<TARGET_PROPERTY:${TARGET},OUTPUT_NAME>>,$<TARGET_PROPERTY:${TARGET},OUTPUT_NAME>,$<TARGET_PROPERTY:${TARGET},NAME>>.uf2
69            VERBATIM)
70    endif()
71endfunction()
72