1# For boards without their own cmake file, we look for a header file
2
3# PICO_CMAKE_CONFIG: PICO_BOARD_HEADER_DIRS, List of directories to look for <PICO_BOARD>.h in. This may be specified the user environment, type=list, group=build
4if (DEFINED ENV{PICO_BOARD_HEADER_DIRS})
5    set(PICO_BOARD_HEADER_DIRS $ENV{PICO_BOARD_HEADER_DIRS})
6    message("Using PICO_BOARD_HEADER_DIRS from environment ('${PICO_BOARD_HEADER_DIRS}')")
7endif()
8set(PICO_BOARD_HEADER_DIRS ${PICO_BOARD_HEADER_DIRS} CACHE STRING "PICO board header directories" FORCE)
9
10list(APPEND PICO_BOARD_HEADER_DIRS ${CMAKE_CURRENT_LIST_DIR}/../src/boards/include/boards)
11pico_find_in_paths(PICO_BOARD_HEADER_FILE PICO_BOARD_HEADER_DIRS ${PICO_BOARD}.h)
12
13if (EXISTS ${PICO_BOARD_HEADER_FILE})
14    message("Using board configuration from ${PICO_BOARD_HEADER_FILE}")
15    list(APPEND PICO_CONFIG_HEADER_FILES ${PICO_BOARD_HEADER_FILE})
16
17    # we parse the header file to configure the defaults
18    file(STRINGS ${PICO_BOARD_HEADER_FILE} HEADER_FILE_CONTENTS)
19
20    while(HEADER_FILE_CONTENTS)
21        list(POP_FRONT HEADER_FILE_CONTENTS LINE)
22        if (LINE MATCHES "^[ \t\]*//[ \t\]*pico_cmake_set[ \t\]*([a-zA-Z_][a-zA-Z0-9_]*)[ \t\]*=[ \t\]*(.*)")
23            set("${CMAKE_MATCH_1}" "${CMAKE_MATCH_2}")
24        endif()
25        if (LINE MATCHES "^[ \t\]*//[ \t\]*pico_cmake_set_default[ \t\]*([a-zA-Z_][a-zA-Z0-9_]*)[ \t\]*=[ \t\]*(.*)")
26            if (NOT DEFINED "${CMAKE_MATCH_1}")
27                set("${CMAKE_MATCH_1}" "${CMAKE_MATCH_2}")
28            endif()
29        endif()
30    endwhile()
31else()
32    set(msg "Unable to find definition of board '${PICO_BOARD}' (specified by PICO_BOARD):\n")
33    list(JOIN PICO_BOARD_HEADER_DIRS ", " DIRS)
34    string(CONCAT msg ${msg} "   Looked for ${PICO_BOARD}.h in ${DIRS} (additional paths specified by PICO_BOARD_HEADER_DIRS)\n")
35    list(JOIN PICO_BOARD_CMAKE_DIRS ", " DIRS)
36    string(CONCAT msg ${msg} "   Looked for ${PICO_BOARD}.cmake in ${DIRS} (additional paths specified by PICO_BOARD_CMAKE_DIRS)")
37    message(FATAL_ERROR ${msg})
38endif()
39