1# Finds (or builds) the picotool executable
2#
3# This will define the following imported targets
4#
5#     picotool
6#
7cmake_minimum_required(VERSION 3.17)
8
9if (NOT TARGET picotool)
10    include(ExternalProject)
11
12    if (DEFINED ENV{PICOTOOL_FETCH_FROM_GIT_PATH} AND (NOT PICOTOOL_FETCH_FROM_GIT_PATH))
13        set(PICOTOOL_FETCH_FROM_GIT_PATH $ENV{PICOTOOL_FETCH_FROM_GIT_PATH})
14        message("Using PICOTOOL_FETCH_FROM_GIT_PATH from environment ('${PICOTOOL_FETCH_FROM_GIT_PATH}')")
15    endif ()
16
17    include(FetchContent)
18    set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
19    if (PICOTOOL_FETCH_FROM_GIT_PATH)
20        get_filename_component(FETCHCONTENT_BASE_DIR "${PICOTOOL_FETCH_FROM_GIT_PATH}" ABSOLUTE)
21    endif ()
22
23    set(picotool_BUILD_TARGET picotoolBuild)
24    set(picotool_TARGET picotool)
25
26    if (NOT TARGET ${picotool_BUILD_TARGET})
27        if (NOT PICOTOOL_FETCH_FROM_GIT_PATH)
28            message(WARNING
29                "No installed picotool with version ${picotool_VERSION_REQUIRED} found - building from source\n"
30                "It is recommended to build and install picotool separately, or to set PICOTOOL_FETCH_FROM_GIT_PATH "
31                "to a common directory for all your SDK projects"
32            )
33        endif()
34
35        FetchContent_Declare(
36                picotool
37                GIT_REPOSITORY https://github.com/raspberrypi/picotool.git
38                GIT_TAG develop
39                GIT_PROGRESS true
40        )
41
42        FetchContent_GetProperties(picotool)
43        set(picotool_INSTALL_DIR ${FETCHCONTENT_BASE_DIR} CACHE PATH "Directory where picotool has been installed" FORCE)
44        if (NOT picotool_POPULATED)
45            message("Downloading Picotool")
46            FetchContent_Populate(picotool)
47        endif ()
48        set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
49
50        add_custom_target(picotoolForceReconfigure
51            ${CMAKE_COMMAND} -E touch_nocreate "${CMAKE_SOURCE_DIR}/CMakeLists.txt"
52            VERBATIM)
53
54        ExternalProject_Add(${picotool_BUILD_TARGET}
55                PREFIX picotool
56                SOURCE_DIR ${picotool_SOURCE_DIR}
57                BINARY_DIR ${picotool_BINARY_DIR}
58                INSTALL_DIR ${picotool_INSTALL_DIR}
59                DEPENDS picotoolForceReconfigure
60                CMAKE_ARGS
61                    "--no-warn-unused-cli"
62                    "-DCMAKE_MAKE_PROGRAM:FILEPATH=${CMAKE_MAKE_PROGRAM}"
63                    "-DPICO_SDK_PATH:FILEPATH=${PICO_SDK_PATH}"
64                    "-DPICOTOOL_NO_LIBUSB=1"
65                    "-DPICOTOOL_FLAT_INSTALL=1"
66                    "-DCMAKE_INSTALL_PREFIX=${picotool_INSTALL_DIR}"
67                    "-DCMAKE_RULE_MESSAGES=OFF" # quieten the build
68                    "-DCMAKE_INSTALL_MESSAGE=NEVER" # quieten the install
69                BUILD_ALWAYS 1 # force dependency checking
70                EXCLUDE_FROM_ALL TRUE
71                TEST_COMMAND
72                    ${picotool_INSTALL_DIR}/picotool/picotool
73                    version ${picotool_VERSION_REQUIRED}
74                TEST_AFTER_INSTALL TRUE
75                )
76    endif()
77
78    set(picotool_EXECUTABLE ${picotool_INSTALL_DIR}/picotool/picotool)
79    add_executable(${picotool_TARGET} IMPORTED GLOBAL)
80    set_property(TARGET ${picotool_TARGET} PROPERTY IMPORTED_LOCATION
81            ${picotool_EXECUTABLE})
82
83    add_dependencies(${picotool_TARGET} ${picotool_BUILD_TARGET})
84endif()
85