Graham Sanderson | 18c3985 | 2021-05-04 08:40:11 -0500 | [diff] [blame] | 1 | # Pre-initialize the Raspberry Pi Pico SDK, setting up the platform and toolchain and some CMake utility functions |
graham sanderson | 26653ea | 2021-01-20 10:44:27 -0600 | [diff] [blame] | 2 | # This file must be included prior to the project() call |
| 3 | |
Graham Sanderson | 18c3985 | 2021-05-04 08:40:11 -0500 | [diff] [blame] | 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) |
graham sanderson | 26653ea | 2021-01-20 10:44:27 -0600 | [diff] [blame] | 7 | |
Graham Sanderson | 18c3985 | 2021-05-04 08:40:11 -0500 | [diff] [blame] | 8 | if (NOT TARGET _pico_sdk_pre_init_marker) |
| 9 | add_library(_pico_sdk_pre_init_marker INTERFACE) |
graham sanderson | 26653ea | 2021-01-20 10:44:27 -0600 | [diff] [blame] | 10 | |
Graham Sanderson | 18c3985 | 2021-05-04 08:40:11 -0500 | [diff] [blame] | 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() |
graham sanderson | 26653ea | 2021-01-20 10:44:27 -0600 | [diff] [blame] | 20 | |
Jonathan Reichelt Gjertsen | a531123 | 2021-05-26 00:10:55 +0200 | [diff] [blame] | 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 | |
Graham Sanderson | 18c3985 | 2021-05-04 08:40:11 -0500 | [diff] [blame] | 30 | if (NOT PICO_SDK_PATH) |
| 31 | set(PICO_SDK_PATH ${CMAKE_CURRENT_LIST_DIR}) |
| 32 | endif () |
graham sanderson | 26653ea | 2021-01-20 10:44:27 -0600 | [diff] [blame] | 33 | |
Graham Sanderson | 18c3985 | 2021-05-04 08:40:11 -0500 | [diff] [blame] | 34 | get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}") |
graham sanderson | 26653ea | 2021-01-20 10:44:27 -0600 | [diff] [blame] | 35 | |
Graham Sanderson | 18c3985 | 2021-05-04 08:40:11 -0500 | [diff] [blame] | 36 | set(PICO_SDK_PATH ${CMAKE_CURRENT_LIST_DIR} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE) |
graham sanderson | 26653ea | 2021-01-20 10:44:27 -0600 | [diff] [blame] | 37 | |
Graham Sanderson | 18c3985 | 2021-05-04 08:40:11 -0500 | [diff] [blame] | 38 | list(APPEND CMAKE_MODULE_PATH ${PICO_SDK_PATH}/cmake) |
graham sanderson | 26653ea | 2021-01-20 10:44:27 -0600 | [diff] [blame] | 39 | |
Graham Sanderson | 18c3985 | 2021-05-04 08:40:11 -0500 | [diff] [blame] | 40 | include(${CMAKE_CURRENT_LIST_DIR}/pico_sdk_version.cmake) |
| 41 | include(pico_utils) |
graham sanderson | 26653ea | 2021-01-20 10:44:27 -0600 | [diff] [blame] | 42 | |
Graham Sanderson | 18c3985 | 2021-05-04 08:40:11 -0500 | [diff] [blame] | 43 | message("PICO_SDK_PATH is ${CMAKE_CURRENT_LIST_DIR}") |
graham sanderson | 26653ea | 2021-01-20 10:44:27 -0600 | [diff] [blame] | 44 | |
Graham Sanderson | 18c3985 | 2021-05-04 08:40:11 -0500 | [diff] [blame] | 45 | include(pico_pre_load_platform) |
graham sanderson | 26653ea | 2021-01-20 10:44:27 -0600 | [diff] [blame] | 46 | |
Graham Sanderson | 18c3985 | 2021-05-04 08:40:11 -0500 | [diff] [blame] | 47 | # We want to configure correct toolchain prior to project load |
Graham Sanderson | 6d87da4 | 2021-05-05 11:46:25 -0500 | [diff] [blame] | 48 | # todo perhaps this should be included by the platform instead? |
Graham Sanderson | 18c3985 | 2021-05-04 08:40:11 -0500 | [diff] [blame] | 49 | include(pico_pre_load_toolchain) |
graham sanderson | 26653ea | 2021-01-20 10:44:27 -0600 | [diff] [blame] | 50 | |
Graham Sanderson | 18c3985 | 2021-05-04 08:40:11 -0500 | [diff] [blame] | 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() |
graham sanderson | 26653ea | 2021-01-20 10:44:27 -0600 | [diff] [blame] | 57 | |
Graham Sanderson | 18c3985 | 2021-05-04 08:40:11 -0500 | [diff] [blame] | 58 | macro(add_sub_list_dirs var) |
| 59 | foreach(LIST_DIR IN LISTS ${var}) |
| 60 | get_filename_component(SHORT_NAME "${LIST_DIR}" NAME) |
Jonathan Reichelt Gjertsen | a531123 | 2021-05-26 00:10:55 +0200 | [diff] [blame] | 61 | pico_message_debug("Including custom CMakeLists.txt ${SHORT_NAME}") |
Graham Sanderson | 18c3985 | 2021-05-04 08:40:11 -0500 | [diff] [blame] | 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}) |
Jonathan Reichelt Gjertsen | a531123 | 2021-05-26 00:10:55 +0200 | [diff] [blame] | 68 | pico_message_debug("Including custom CMake file ${LIST_FILE}") |
Graham Sanderson | 18c3985 | 2021-05-04 08:40:11 -0500 | [diff] [blame] | 69 | include(${LIST_FILE}) |
| 70 | endforeach() |
| 71 | endmacro() |
Graham Sanderson | e57b99a | 2021-05-11 10:33:34 -0500 | [diff] [blame] | 72 | |
Graham Sanderson | 561502c | 2021-06-02 13:04:08 -0500 | [diff] [blame] | 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 | |
Graham Sanderson | e57b99a | 2021-05-11 10:33:34 -0500 | [diff] [blame] | 87 | macro(pico_promote_common_scope_vars) |
Graham Sanderson | 561502c | 2021-06-02 13:04:08 -0500 | [diff] [blame] | 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() |
Graham Sanderson | e57b99a | 2021-05-11 10:33:34 -0500 | [diff] [blame] | 92 | endmacro() |
Graham Sanderson | 18c3985 | 2021-05-04 08:40:11 -0500 | [diff] [blame] | 93 | endif() |