blob: f6b6f81e075afe90e89b7fb841e69cc2b3b54dc2 [file] [log] [blame]
Austin Schuh208337d2022-01-01 14:29:11 -08001# 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 pico_is_top_level_project(ISTOP)
57 endmacro()
58
59 macro(add_sub_list_dirs var)
60 foreach(LIST_DIR IN LISTS ${var})
61 get_filename_component(SHORT_NAME "${LIST_DIR}" NAME)
62 pico_message_debug("Including custom CMakeLists.txt ${SHORT_NAME}")
63 add_subdirectory(${LIST_DIR} ${SHORT_NAME})
64 endforeach()
65 endmacro()
66
67 macro(add_sub_list_files var)
68 foreach(LIST_FILE IN LISTS ${var})
69 pico_message_debug("Including custom CMake file ${LIST_FILE}")
70 include(${LIST_FILE})
71 endforeach()
72 endmacro()
73
74 macro(pico_register_common_scope_var NAME)
75 if (NOT ${NAME} IN_LIST PICO_PROMOTE_COMMON_SCOPE_VARS)
76 list(APPEND PICO_PROMOTE_COMMON_SCOPE_VARS ${NAME})
77 endif()
78 endmacro()
79
80 set(PICO_PROMOTE_COMMON_SCOPE_VARS
81 PICO_INCLUDE_DIRS
82 PICO_SDK_POST_LIST_DIRS
83 PICO_SDK_POST_LIST_FILES
84 PICO_CONFIG_HEADER_FILES
85 PICO_RP2040_CONFIG_HEADER_FILES
86 )
87
88 macro(pico_promote_common_scope_vars)
89 set(PICO_PROMOTE_COMMON_SCOPE_VARS ${PICO_PROMOTE_COMMON_SCOPE_VARS} PARENT_SCOPE)
90 foreach(VAR IN LISTS PICO_PROMOTE_COMMON_SCOPE_VARS)
91 SET(${VAR} ${${VAR}} PARENT_SCOPE)
92 endforeach()
93 endmacro()
94endif()