1# Finds (or builds) the ELF2UF2 executable
2#
3# This will define the following variables
4#
5#    ELF2UF2_FOUND
6#
7# and the following imported targets
8#
9#     ELF2UF2
10#
11
12if (NOT ELF2UF2_FOUND)
13    # todo we would like to use pckgconfig to look for it first
14    # see https://pabloariasal.github.io/2018/02/19/its-time-to-do-cmake-right/
15
16    include(ExternalProject)
17
18    set(ELF2UF2_SOURCE_DIR ${PICO_SDK_PATH}/tools/elf2uf2)
19    set(ELF2UF2_BINARY_DIR ${CMAKE_BINARY_DIR}/elf2uf2)
20
21    set(ELF2UF2_BUILD_TARGET ELF2UF2Build)
22    set(ELF2UF2_TARGET ELF2UF2)
23
24    if (NOT TARGET ${ELF2UF2_BUILD_TARGET})
25        pico_message_debug("ELF2UF2 will need to be built")
26        ExternalProject_Add(${ELF2UF2_BUILD_TARGET}
27                PREFIX elf2uf2
28                SOURCE_DIR ${ELF2UF2_SOURCE_DIR}
29                BINARY_DIR ${ELF2UF2_BINARY_DIR}
30                CMAKE_ARGS "-DCMAKE_MAKE_PROGRAM:FILEPATH=${CMAKE_MAKE_PROGRAM}"
31                BUILD_ALWAYS 1 # force dependency checking
32                INSTALL_COMMAND ""
33                )
34    endif()
35
36    set(ELF2UF2_EXECUTABLE ${ELF2UF2_BINARY_DIR}/elf2uf2)
37    if(NOT TARGET ${ELF2UF2_TARGET})
38        add_executable(${ELF2UF2_TARGET} IMPORTED)
39    endif()
40    set_property(TARGET ${ELF2UF2_TARGET} PROPERTY IMPORTED_LOCATION
41            ${ELF2UF2_EXECUTABLE})
42
43    add_dependencies(${ELF2UF2_TARGET} ${ELF2UF2_BUILD_TARGET})
44    set(ELF2UF2_FOUND 1)
45endif()
46