blob: 5153e2b01c619422ceedc37097b7819b01b563f0 [file] [log] [blame]
Graham Sanderson18c39852021-05-04 08:40:11 -05001# Pre-initialize the Raspberry Pi Pico SDK, setting up the platform and toolchain and some CMake utility functions
graham sanderson26653ea2021-01-20 10:44:27 -06002# This file must be included prior to the project() call
3
Graham Sanderson18c39852021-05-04 08:40:11 -05004# 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)
graham sanderson26653ea2021-01-20 10:44:27 -06007
Graham Sanderson18c39852021-05-04 08:40:11 -05008if (NOT TARGET _pico_sdk_pre_init_marker)
9 add_library(_pico_sdk_pre_init_marker INTERFACE)
graham sanderson26653ea2021-01-20 10:44:27 -060010
Graham Sanderson18c39852021-05-04 08:40:11 -050011 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()
graham sanderson26653ea2021-01-20 10:44:27 -060020
Jonathan Reichelt Gjertsena5311232021-05-26 00:10:55 +020021 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
Graham Sanderson18c39852021-05-04 08:40:11 -050030 if (NOT PICO_SDK_PATH)
31 set(PICO_SDK_PATH ${CMAKE_CURRENT_LIST_DIR})
32 endif ()
graham sanderson26653ea2021-01-20 10:44:27 -060033
Graham Sanderson18c39852021-05-04 08:40:11 -050034 get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
graham sanderson26653ea2021-01-20 10:44:27 -060035
Graham Sanderson18c39852021-05-04 08:40:11 -050036 set(PICO_SDK_PATH ${CMAKE_CURRENT_LIST_DIR} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE)
graham sanderson26653ea2021-01-20 10:44:27 -060037
Graham Sanderson18c39852021-05-04 08:40:11 -050038 list(APPEND CMAKE_MODULE_PATH ${PICO_SDK_PATH}/cmake)
graham sanderson26653ea2021-01-20 10:44:27 -060039
Graham Sanderson18c39852021-05-04 08:40:11 -050040 include(${CMAKE_CURRENT_LIST_DIR}/pico_sdk_version.cmake)
41 include(pico_utils)
graham sanderson26653ea2021-01-20 10:44:27 -060042
Graham Sanderson18c39852021-05-04 08:40:11 -050043 message("PICO_SDK_PATH is ${CMAKE_CURRENT_LIST_DIR}")
graham sanderson26653ea2021-01-20 10:44:27 -060044
Graham Sanderson18c39852021-05-04 08:40:11 -050045 include(pico_pre_load_platform)
graham sanderson26653ea2021-01-20 10:44:27 -060046
Graham Sanderson18c39852021-05-04 08:40:11 -050047 # We want to configure correct toolchain prior to project load
Graham Sanderson6d87da42021-05-05 11:46:25 -050048 # todo perhaps this should be included by the platform instead?
Graham Sanderson18c39852021-05-04 08:40:11 -050049 include(pico_pre_load_toolchain)
graham sanderson26653ea2021-01-20 10:44:27 -060050
Graham Sanderson18c39852021-05-04 08:40:11 -050051 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()
graham sanderson26653ea2021-01-20 10:44:27 -060057
Graham Sanderson18c39852021-05-04 08:40:11 -050058 macro(add_sub_list_dirs var)
59 foreach(LIST_DIR IN LISTS ${var})
60 get_filename_component(SHORT_NAME "${LIST_DIR}" NAME)
Jonathan Reichelt Gjertsena5311232021-05-26 00:10:55 +020061 pico_message_debug("Including custom CMakeLists.txt ${SHORT_NAME}")
Graham Sanderson18c39852021-05-04 08:40:11 -050062 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})
Jonathan Reichelt Gjertsena5311232021-05-26 00:10:55 +020068 pico_message_debug("Including custom CMake file ${LIST_FILE}")
Graham Sanderson18c39852021-05-04 08:40:11 -050069 include(${LIST_FILE})
70 endforeach()
71 endmacro()
Graham Sandersone57b99a2021-05-11 10:33:34 -050072
Graham Sanderson561502c2021-06-02 13:04:08 -050073 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
Graham Sandersone57b99a2021-05-11 10:33:34 -050087 macro(pico_promote_common_scope_vars)
Graham Sanderson561502c2021-06-02 13:04:08 -050088 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()
Graham Sandersone57b99a2021-05-11 10:33:34 -050092 endmacro()
Graham Sanderson18c39852021-05-04 08:40:11 -050093endif()