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 @@ @@ -1,18 +1,16 @@
cmake_minimum_required(VERSION 3.13.1)
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.
list(APPEND DTS_ROOT ${CMAKE_SOURCE_DIR}/drivers/zephyr)
set(ZephyrBuildConfiguration_ROOT ${CMAKE_SOURCE_DIR}/cmake)
list(APPEND ZEPHYR_EXTRA_MODULES
${CMAKE_CURRENT_SOURCE_DIR}/drivers
)
include(cmake/zmk_config.cmake)
# Find Zephyr. This also loads Zephyr's build system.
find_package(Zephyr REQUIRED HINTS ../zephyr)
project(zmk)

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

@ -7,6 +7,9 @@ @@ -7,6 +7,9 @@
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)
set(user_config_cli_argument ${cached_user_config_value}) # Either new or old
@ -57,9 +60,29 @@ if (ZMK_CONFIG) @@ -57,9 +60,29 @@ if (ZMK_CONFIG)
endif()
endif()
if(DEFINED SHIELD)
string(REPLACE " " ";" SHIELD_AS_LIST "${SHIELD}")
endif()
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")
list(APPEND ZMK_DTC_FILES "${root}/boards/${BOARD}.overlay")
list(APPEND shield_dts_files "${root}/boards/${BOARD}.overlay")
endif()
if (NOT DEFINED BOARD_DIR_NAME)
find_path(BOARD_DIR
@ -74,17 +97,37 @@ foreach(root ${BOARD_ROOT}) @@ -74,17 +97,37 @@ foreach(root ${BOARD_ROOT})
endif()
if(DEFINED SHIELD)
find_path(shields_refs_list
NAMES ${SHIELD}.overlay
PATHS ${root}/boards/shields/*
NO_DEFAULT_PATH
)
foreach(shield_path ${shields_refs_list})
get_filename_component(SHIELD_DIR ${shield_path} NAME)
list(APPEND KEYMAP_DIRS ${shield_path})
foreach(s ${SHIELD_AS_LIST})
if(NOT ${s} IN_LIST SHIELD_LIST)
message(WARNING "Didn't find ${s}")
continue()
endif()
message(STATUS "Adding ${SHIELD_DIR_${s}}")
list(APPEND KEYMAP_DIRS ${SHIELD_DIR_${s}})
get_filename_component(shield_dir_name ${SHIELD_DIR_${s}} NAME)
list(APPEND SHIELD_DIR ${shield_dir_name})
endforeach()
endif()
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()
list(APPEND shield_candidate_names ${S_SUBSTR})
endwhile()
endforeach()
endif()
if (ZMK_CONFIG)
if (EXISTS ${ZMK_CONFIG})
@ -92,16 +135,20 @@ if (ZMK_CONFIG) @@ -92,16 +135,20 @@ if (ZMK_CONFIG)
list(APPEND DTS_ROOT ${ZMK_CONFIG})
list(PREPEND KEYMAP_DIRS "${ZMK_CONFIG}")
if (SHIELD)
message(STATUS "Board: ${BOARD}, ${BOARD_DIR}, ${SHIELD}, ${SHIELD_DIR}")
list(APPEND overlay_candidates "${ZMK_CONFIG}/${SHIELD_DIR}.overlay")
list(APPEND overlay_candidates "${ZMK_CONFIG}/${SHIELD_DIR}_${BOARD}.overlay")
list(APPEND overlay_candidates "${ZMK_CONFIG}/${SHIELD}_${BOARD}.overlay")
list(APPEND overlay_candidates "${ZMK_CONFIG}/${SHIELD}.overlay")
list(APPEND config_candidates "${ZMK_CONFIG}/${SHIELD_DIR}.conf")
list(APPEND config_candidates "${ZMK_CONFIG}/${SHIELD_DIR}_${BOARD}.conf")
list(APPEND config_candidates "${ZMK_CONFIG}/${SHIELD}_${BOARD}.conf")
list(APPEND config_candidates "${ZMK_CONFIG}/${SHIELD}.conf")
if (DEFINED SHIELD)
foreach (s ${shield_candidate_names})
if (DEFINED $SHIELD_DIR_${s})
get_filename_component(shield_dir_name ${SHIELD_DIR_${s}} NAME)
endif()
list(APPEND overlay_candidates "${ZMK_CONFIG}/${s}_${BOARD}.overlay")
list(APPEND overlay_candidates "${ZMK_CONFIG}/${s}.overlay")
if (NOT "${shield_dir_name}" STREQUAL "${s}")
list(APPEND config_candidates "${ZMK_CONFIG}/${shield_dir_name}_${BOARD}.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()
# TODO: Board revisions?
@ -115,7 +162,7 @@ if (ZMK_CONFIG) @@ -115,7 +162,7 @@ if (ZMK_CONFIG)
foreach(overlay ${overlay_candidates})
if (EXISTS "${overlay}")
message(STATUS "ZMK Config devicetree overlay: ${overlay}")
list(APPEND ZMK_DTC_FILES "${overlay}")
list(APPEND shield_dts_files "${overlay}")
break()
endif()
endforeach()
@ -123,8 +170,7 @@ if (ZMK_CONFIG) @@ -123,8 +170,7 @@ if (ZMK_CONFIG)
foreach(conf ${config_candidates})
if (EXISTS "${conf}")
message(STATUS "ZMK Config Kconfig: ${conf}")
set(CONF_FILE "${conf}")
break()
list(APPEND shield_conf_files "${conf}")
endif()
endforeach()
else()
@ -135,22 +181,20 @@ endif() @@ -135,22 +181,20 @@ endif()
if(NOT KEYMAP_FILE)
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)
set(KEYMAP_FILE "${keymap_dir}/${keymap_prefix}.keymap" CACHE STRING "Selected keymap file")
message(STATUS "Using keymap file: ${KEYMAP_FILE}")
set(DTC_OVERLAY_FILE ${KEYMAP_FILE})
break()
endif()
endforeach()
endforeach()
else()
message(STATUS "Using keymap file: ${KEYMAP_FILE}")
set(DTC_OVERLAY_FILE ${KEYMAP_FILE})
endif()
if (NOT KEYMAP_FILE)
message(FATAL_ERROR "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}")
message(WARNING "Failed to locate keymap file!")
endif()
Loading…
Cancel
Save