1# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake
2
3# This can be dropped into an external project to help locate this SDK
4# It should be include()ed prior to project()
5
6if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
7    set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
8    message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
9endif ()
10
11if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
12    set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
13    message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
14endif ()
15
16if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
17    set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
18    message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
19endif ()
20
21set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK")
22set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable")
23set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")
24
25if (NOT PICO_SDK_PATH)
26    if (PICO_SDK_FETCH_FROM_GIT)
27        include(FetchContent)
28        set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
29        if (PICO_SDK_FETCH_FROM_GIT_PATH)
30            get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
31        endif ()
32        # GIT_SUBMODULES_RECURSE was added in 3.17
33        if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0")
34            FetchContent_Declare(
35                    pico_sdk
36                    GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
37                    GIT_TAG master
38                    GIT_SUBMODULES_RECURSE FALSE
39            )
40        else ()
41            FetchContent_Declare(
42                    pico_sdk
43                    GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
44                    GIT_TAG master
45            )
46        endif ()
47
48        if (NOT pico_sdk)
49            message("Downloading Raspberry Pi Pico SDK")
50            FetchContent_Populate(pico_sdk)
51            set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
52        endif ()
53        set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
54    else ()
55        message(FATAL_ERROR
56                "SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
57                )
58    endif ()
59endif ()
60
61get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
62if (NOT EXISTS ${PICO_SDK_PATH})
63    message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
64endif ()
65
66set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
67if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
68    message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK")
69endif ()
70
71set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE)
72
73include(${PICO_SDK_INIT_CMAKE_FILE})
74