Browse Source

refactor(build): Fixes for config/overlay loading.

* Move to ZephyrBuildConfig package based on Zephyr best practices.
* Support multiple shields properly, e.g. -DSHIELD="foo bar", which
  allows using existing upstream shields like `ssd1306_128x64` along
  with another ZMK shield.
* Properly *add* the "ZMK config" .conf file to the conf file list,
  so that existing board .conf files in `app/boards/<board>.conf` are
  properly honored still. Needed for overrides to usptream boards when
  combined with a local `<shield>.conf` file.
xmkb
Peter Johanson 3 years ago committed by Pete Johanson
parent
commit
532c4b9267
  1. 6
      app/CMakeLists.txt
  2. 104
      app/cmake/ZephyrBuildConfig.cmake

6
app/CMakeLists.txt

@ -1,18 +1,16 @@
cmake_minimum_required(VERSION 3.13.1) cmake_minimum_required(VERSION 3.13.1)
set(CONFIG_APPLICATION_DEFINED_SYSCALL true) set(CONFIG_APPLICATION_DEFINED_SYSCALL true)
list(APPEND BOARD_ROOT ${CMAKE_SOURCE_DIR})
list(APPEND DTS_ROOT ${CMAKE_SOURCE_DIR})
# Add our custom Zephyr module for drivers w/ syscalls, etc. # Add our custom Zephyr module for drivers w/ syscalls, etc.
list(APPEND DTS_ROOT ${CMAKE_SOURCE_DIR}/drivers/zephyr) list(APPEND DTS_ROOT ${CMAKE_SOURCE_DIR}/drivers/zephyr)
set(ZephyrBuildConfiguration_ROOT ${CMAKE_SOURCE_DIR}/cmake)
list(APPEND ZEPHYR_EXTRA_MODULES list(APPEND ZEPHYR_EXTRA_MODULES
${CMAKE_CURRENT_SOURCE_DIR}/drivers ${CMAKE_CURRENT_SOURCE_DIR}/drivers
) )
include(cmake/zmk_config.cmake)
# Find Zephyr. This also loads Zephyr's build system. # Find Zephyr. This also loads Zephyr's build system.
find_package(Zephyr REQUIRED HINTS ../zephyr) find_package(Zephyr REQUIRED HINTS ../zephyr)
project(zmk) project(zmk)

104
app/cmake/zmk_config.cmake → app/cmake/ZephyrBuildConfig.cmake

@ -7,6 +7,9 @@
cmake_minimum_required(VERSION 3.15) cmake_minimum_required(VERSION 3.15)
list(APPEND BOARD_ROOT ${APPLICATION_SOURCE_DIR})
list(APPEND DTS_ROOT ${APPLICATION_SOURCE_DIR})
get_property(cached_user_config_value CACHE ZMK_CONFIG PROPERTY VALUE) get_property(cached_user_config_value CACHE ZMK_CONFIG PROPERTY VALUE)
set(user_config_cli_argument ${cached_user_config_value}) # Either new or old set(user_config_cli_argument ${cached_user_config_value}) # Either new or old
@ -57,9 +60,29 @@ if (ZMK_CONFIG)
endif() endif()
endif() endif()
if(DEFINED SHIELD)
string(REPLACE " " ";" SHIELD_AS_LIST "${SHIELD}")
endif()
foreach(root ${BOARD_ROOT}) foreach(root ${BOARD_ROOT})
set(shield_dir ${root}/boards/shields)
# Match the Kconfig.shield files in the shield directories to make sure we are
# finding shields, e.g. x_nucleo_iks01a1/Kconfig.shield
file(GLOB_RECURSE shields_refs_list ${shield_dir}/*/Kconfig.shield)
unset(SHIELD_LIST)
foreach(shields_refs ${shields_refs_list})
get_filename_component(shield_path ${shields_refs} DIRECTORY)
file(GLOB shield_overlays RELATIVE ${shield_path} ${shield_path}/*.overlay)
foreach(overlay ${shield_overlays})
get_filename_component(shield ${overlay} NAME_WE)
list(APPEND SHIELD_LIST ${shield})
set(SHIELD_DIR_${shield} ${shield_path})
endforeach()
endforeach()
if (EXISTS "${root}/boards/${BOARD}.overlay") if (EXISTS "${root}/boards/${BOARD}.overlay")
list(APPEND ZMK_DTC_FILES "${root}/boards/${BOARD}.overlay") list(APPEND shield_dts_files "${root}/boards/${BOARD}.overlay")
endif() endif()
if (NOT DEFINED BOARD_DIR_NAME) if (NOT DEFINED BOARD_DIR_NAME)
find_path(BOARD_DIR find_path(BOARD_DIR
@ -74,17 +97,37 @@ foreach(root ${BOARD_ROOT})
endif() endif()
if(DEFINED SHIELD) if(DEFINED SHIELD)
find_path(shields_refs_list foreach(s ${SHIELD_AS_LIST})
NAMES ${SHIELD}.overlay if(NOT ${s} IN_LIST SHIELD_LIST)
PATHS ${root}/boards/shields/* message(WARNING "Didn't find ${s}")
NO_DEFAULT_PATH continue()
) endif()
foreach(shield_path ${shields_refs_list}) message(STATUS "Adding ${SHIELD_DIR_${s}}")
get_filename_component(SHIELD_DIR ${shield_path} NAME) list(APPEND KEYMAP_DIRS ${SHIELD_DIR_${s}})
list(APPEND KEYMAP_DIRS ${shield_path}) get_filename_component(shield_dir_name ${SHIELD_DIR_${s}} NAME)
list(APPEND SHIELD_DIR ${shield_dir_name})
endforeach()
endif()
endforeach() endforeach()
# Give a shield like `kyria_rev2_left` we want to use `kyria_rev2` and `kyria` as candidate names for
# overlay/conf/keymap files.
if(DEFINED SHIELD)
foreach(s ${SHIELD_AS_LIST})
message(STATUS ${SHIELD_DIR_${s}})
get_filename_component(shield_dir_name ${SHIELD_DIR_${s}} NAME)
string(REPLACE "_" ";" S_PIECES ${s})
list(LENGTH S_PIECES S_PIECES_LEN)
while(NOT S_PIECES STREQUAL "")
list(POP_BACK S_PIECES)
list(JOIN S_PIECES "_" S_SUBSTR)
if (S_SUBSTR STREQUAL ${shield_dir_name})
break()
endif() endif()
list(APPEND shield_candidate_names ${S_SUBSTR})
endwhile()
endforeach() endforeach()
endif()
if (ZMK_CONFIG) if (ZMK_CONFIG)
if (EXISTS ${ZMK_CONFIG}) if (EXISTS ${ZMK_CONFIG})
@ -92,16 +135,20 @@ if (ZMK_CONFIG)
list(APPEND DTS_ROOT ${ZMK_CONFIG}) list(APPEND DTS_ROOT ${ZMK_CONFIG})
list(PREPEND KEYMAP_DIRS "${ZMK_CONFIG}") list(PREPEND KEYMAP_DIRS "${ZMK_CONFIG}")
if (SHIELD) if (DEFINED SHIELD)
message(STATUS "Board: ${BOARD}, ${BOARD_DIR}, ${SHIELD}, ${SHIELD_DIR}") foreach (s ${shield_candidate_names})
list(APPEND overlay_candidates "${ZMK_CONFIG}/${SHIELD_DIR}.overlay") if (DEFINED $SHIELD_DIR_${s})
list(APPEND overlay_candidates "${ZMK_CONFIG}/${SHIELD_DIR}_${BOARD}.overlay") get_filename_component(shield_dir_name ${SHIELD_DIR_${s}} NAME)
list(APPEND overlay_candidates "${ZMK_CONFIG}/${SHIELD}_${BOARD}.overlay") endif()
list(APPEND overlay_candidates "${ZMK_CONFIG}/${SHIELD}.overlay") list(APPEND overlay_candidates "${ZMK_CONFIG}/${s}_${BOARD}.overlay")
list(APPEND config_candidates "${ZMK_CONFIG}/${SHIELD_DIR}.conf") list(APPEND overlay_candidates "${ZMK_CONFIG}/${s}.overlay")
list(APPEND config_candidates "${ZMK_CONFIG}/${SHIELD_DIR}_${BOARD}.conf") if (NOT "${shield_dir_name}" STREQUAL "${s}")
list(APPEND config_candidates "${ZMK_CONFIG}/${SHIELD}_${BOARD}.conf") list(APPEND config_candidates "${ZMK_CONFIG}/${shield_dir_name}_${BOARD}.conf")
list(APPEND config_candidates "${ZMK_CONFIG}/${SHIELD}.conf") list(APPEND config_candidates "${ZMK_CONFIG}/${shield_dir_name}.conf")
endif()
list(APPEND config_candidates "${ZMK_CONFIG}/${s}_${BOARD}.conf")
list(APPEND config_candidates "${ZMK_CONFIG}/${s}.conf")
endforeach()
endif() endif()
# TODO: Board revisions? # TODO: Board revisions?
@ -115,7 +162,7 @@ if (ZMK_CONFIG)
foreach(overlay ${overlay_candidates}) foreach(overlay ${overlay_candidates})
if (EXISTS "${overlay}") if (EXISTS "${overlay}")
message(STATUS "ZMK Config devicetree overlay: ${overlay}") message(STATUS "ZMK Config devicetree overlay: ${overlay}")
list(APPEND ZMK_DTC_FILES "${overlay}") list(APPEND shield_dts_files "${overlay}")
break() break()
endif() endif()
endforeach() endforeach()
@ -123,8 +170,7 @@ if (ZMK_CONFIG)
foreach(conf ${config_candidates}) foreach(conf ${config_candidates})
if (EXISTS "${conf}") if (EXISTS "${conf}")
message(STATUS "ZMK Config Kconfig: ${conf}") message(STATUS "ZMK Config Kconfig: ${conf}")
set(CONF_FILE "${conf}") list(APPEND shield_conf_files "${conf}")
break()
endif() endif()
endforeach() endforeach()
else() else()
@ -135,22 +181,20 @@ endif()
if(NOT KEYMAP_FILE) if(NOT KEYMAP_FILE)
foreach(keymap_dir ${KEYMAP_DIRS}) foreach(keymap_dir ${KEYMAP_DIRS})
foreach(keymap_prefix ${SHIELD} ${SHIELD_DIR} ${BOARD} ${BOARD_DIR_NAME}) foreach(keymap_prefix ${shield_candidate_names} ${SHIELD_AS_LIST} ${SHIELD_DIR} ${BOARD} ${BOARD_DIR_NAME})
if (EXISTS ${keymap_dir}/${keymap_prefix}.keymap) if (EXISTS ${keymap_dir}/${keymap_prefix}.keymap)
set(KEYMAP_FILE "${keymap_dir}/${keymap_prefix}.keymap" CACHE STRING "Selected keymap file") set(KEYMAP_FILE "${keymap_dir}/${keymap_prefix}.keymap" CACHE STRING "Selected keymap file")
message(STATUS "Using keymap file: ${KEYMAP_FILE}") message(STATUS "Using keymap file: ${KEYMAP_FILE}")
set(DTC_OVERLAY_FILE ${KEYMAP_FILE})
break() break()
endif() endif()
endforeach() endforeach()
endforeach() endforeach()
else()
message(STATUS "Using keymap file: ${KEYMAP_FILE}")
set(DTC_OVERLAY_FILE ${KEYMAP_FILE})
endif() endif()
if (NOT KEYMAP_FILE) if (NOT KEYMAP_FILE)
message(FATAL_ERROR "Failed to locate keymap file!") message(WARNING "Failed to locate keymap file!")
endif()
list(APPEND ZMK_DTC_FILES ${KEYMAP_FILE})
if (ZMK_DTC_FILES)
string(REPLACE ";" " " DTC_OVERLAY_FILE "${ZMK_DTC_FILES}")
endif() endif()
Loading…
Cancel
Save