1# Pre-initialize the Raspberry Pi Pico SDK, setting up the platform and toolchain and some CMake utility functions
2# This file must be included prior to the project() call
3
4# Note: this file is perhaps named badly, as it provides a method pico_sdk_init which
5# the enclosing project calls LATER to actually "initialize" the SDK (by including the CMakeLists.txt from this
6# same directory)
7
8if (NOT TARGET _pico_sdk_pre_init_marker)
9    add_library(_pico_sdk_pre_init_marker INTERFACE)
10
11    function(pico_is_top_level_project VAR)
12        string(TOLOWER ${CMAKE_CURRENT_LIST_DIR} __list_dir)
13        string(TOLOWER ${CMAKE_SOURCE_DIR} __source_dir)
14        if (__source_dir STREQUAL __list_dir)
15            set(${VAR} 1 PARENT_SCOPE)
16        else()
17            set(${VAR} 0 PARENT_SCOPE)
18        endif()
19    endfunction()
20
21    function(pico_message_debug MESSAGE)
22        # The log-level system was added in CMake 3.15.
23        if(${CMAKE_VERSION} VERSION_LESS "3.15.0")
24            message(${MESSAGE})
25        else()
26            message(DEBUG ${MESSAGE})
27        endif()
28    endfunction()
29
30    if (NOT PICO_SDK_PATH)
31        set(PICO_SDK_PATH ${CMAKE_CURRENT_LIST_DIR})
32    endif ()
33
34    get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
35
36    set(PICO_SDK_PATH ${CMAKE_CURRENT_LIST_DIR} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE)
37
38    list(APPEND CMAKE_MODULE_PATH ${PICO_SDK_PATH}/cmake)
39
40    include(${CMAKE_CURRENT_LIST_DIR}/pico_sdk_version.cmake)
41    include(pico_utils)
42
43    message("PICO_SDK_PATH is ${CMAKE_CURRENT_LIST_DIR}")
44
45    include(pico_pre_load_platform)
46
47    # We want to configure correct toolchain prior to project load
48    # todo perhaps this should be included by the platform instead?
49    include(pico_pre_load_toolchain)
50
51    macro(pico_sdk_init)
52        if (NOT CMAKE_PROJECT_NAME)
53            message(WARNING "pico_sdk_init() should be called after the project is created (and languages added)")
54        endif()
55        add_subdirectory(${PICO_SDK_PATH} pico-sdk)
56    endmacro()
57
58    macro(add_sub_list_dirs var)
59        foreach(LIST_DIR IN LISTS ${var})
60            get_filename_component(SHORT_NAME "${LIST_DIR}" NAME)
61            pico_message_debug("Including custom CMakeLists.txt ${SHORT_NAME}")
62            add_subdirectory(${LIST_DIR} ${SHORT_NAME})
63        endforeach()
64    endmacro()
65
66    macro(add_sub_list_files var)
67        foreach(LIST_FILE IN LISTS ${var})
68            pico_message_debug("Including custom CMake file ${LIST_FILE}")
69            include(${LIST_FILE})
70        endforeach()
71    endmacro()
72
73    macro(pico_register_common_scope_var NAME)
74        if (NOT ${NAME} IN_LIST PICO_PROMOTE_COMMON_SCOPE_VARS)
75            list(APPEND PICO_PROMOTE_COMMON_SCOPE_VARS ${NAME})
76        endif()
77    endmacro()
78
79    set(PICO_PROMOTE_COMMON_SCOPE_VARS
80            PICO_INCLUDE_DIRS
81            PICO_SDK_POST_LIST_DIRS
82            PICO_SDK_POST_LIST_FILES
83            PICO_CONFIG_HEADER_FILES
84            PICO_RP2040_CONFIG_HEADER_FILES
85    )
86
87    macro(pico_promote_common_scope_vars)
88        set(PICO_PROMOTE_COMMON_SCOPE_VARS ${PICO_PROMOTE_COMMON_SCOPE_VARS} PARENT_SCOPE)
89        foreach(VAR IN LISTS PICO_PROMOTE_COMMON_SCOPE_VARS)
90            SET(${VAR} ${${VAR}} PARENT_SCOPE)
91        endforeach()
92    endmacro()
93endif()
94