cmake: support shortened file names for single SoC boards

This allow developers to create board files without the SoC name when
the board only defines a single SoC.

This means that a board, such as rpi_pico, which defines only a single
SoC, rp2040, and one variant, now allows the following file names:
Board target: rpi_pico/rp2040
- dts: rpi_pico_rp2040.dts, short: rpi_pico.dts
- defconfig: rpi_pico_rp2040_defconfig, short: rpi_pico_defconfig
- overlay: rpi_pico_rp2040.overlay, short: rpi_pico.overlay
- conf:  rpi_pico_rp2040.conf, short: rpi_pico.conf

Board target: rpi_pico/rp2040/w
- dts: rpi_pico_rp2040_w.dts, short: rpi_pico_w.dts
- defconfig: rpi_pico_rp2040_w_defconfig, short: rpi_pico_w_defconfig
- overlay: rpi_pico_rp2040_w.overlay, short: rpi_pico_w.overlay
- conf:  rpi_pico_rp2040_w.conf, short: rpi_pico_w.conf

A multi CPU cluster board, nrf5340dk:
Board target: nrf5340dk/nrf5340/cpunet
- dts: nrf5340dk_nrf5340_cpunet.dts, short: nrf5340dk_cpunet.dts
- defconfig: nrf5340dk_nrf5340_cpunet_defconfig,
             short: nrf5340dk_cpunet_defconfig
- overlay: nrf5340dk_nrf5340_cpunet.overlay,
           short: nrf5340dk_cpunet.overlay
- conf:  nrf5340dk_nrf5340_cpunet.conf, short: nrf5340dk_cpunet.conf

A multi SoC board, nrf5340dk (real: nrf52840, emulated: nrf52811):
Board target: nrf52840dk/nrf52840
- dts: nrf52840dk_nrf52840.dts, short: Not possible
- defconfig: nrf52840dk_nrf52840_defconfig, short: Not possible
- overlay: nrf52840dk_nrf52840.overlay, short: Not possible
- conf:  nrf52840dk_nrf52840.conf, short: Not possible

If two conflicting files are found, for example both
rpi_pico_rp2040.overlay and rpi_pico.overlay, then an error is raised.

If short form is detected for a board target with multiple SoCs, for
example nrf52840dk_nrf52840.overlay and nrf52840dk.overlay, then an
error is raised.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
This commit is contained in:
Torsten Rasmussen 2024-04-05 15:39:37 +02:00 committed by Carles Cufí
parent 006ea0f6fb
commit 4e3a01df41
4 changed files with 162 additions and 59 deletions

View file

@ -297,10 +297,14 @@ elseif(HWMv2)
if(LIST_BOARD_QUALIFIERS) if(LIST_BOARD_QUALIFIERS)
# Allow users to omit the SoC when building for a board with a single SoC. # Allow users to omit the SoC when building for a board with a single SoC.
list(LENGTH LIST_BOARD_SOCS socs_length) list(LENGTH LIST_BOARD_SOCS socs_length)
if(NOT DEFINED BOARD_QUALIFIERS AND socs_length EQUAL 1) if(socs_length EQUAL 1)
set(BOARD_QUALIFIERS "/${LIST_BOARD_SOCS}") set(BOARD_SINGLE_SOC TRUE)
elseif("${BOARD_QUALIFIERS}" MATCHES "^//.*" AND socs_length EQUAL 1) set(BOARD_${BOARD}_SINGLE_SOC TRUE)
string(REGEX REPLACE "^//" "/${LIST_BOARD_SOCS}/" BOARD_QUALIFIERS "${BOARD_QUALIFIERS}") if(NOT DEFINED BOARD_QUALIFIERS)
set(BOARD_QUALIFIERS "/${LIST_BOARD_SOCS}")
elseif("${BOARD_QUALIFIERS}" MATCHES "^//.*")
string(REGEX REPLACE "^//" "/${LIST_BOARD_SOCS}/" BOARD_QUALIFIERS "${BOARD_QUALIFIERS}")
endif()
endif() endif()
set(board_targets ${LIST_BOARD_QUALIFIERS}) set(board_targets ${LIST_BOARD_QUALIFIERS})

View file

@ -83,15 +83,14 @@ endif()
# If still not found, search for other overlays in the configuration directory. # If still not found, search for other overlays in the configuration directory.
if(NOT DEFINED DTC_OVERLAY_FILE) if(NOT DEFINED DTC_OVERLAY_FILE)
zephyr_build_string(board_overlay_strings zephyr_file(CONF_FILES ${APPLICATION_CONFIG_DIR} DTS DTC_OVERLAY_FILE)
BOARD ${BOARD}
BOARD_QUALIFIERS ${BOARD_QUALIFIERS} if(NOT DEFINED DTC_OVERLAY_FILE)
MERGE zephyr_file(CONF_FILES ${APPLICATION_CONFIG_DIR} DTS DTC_OVERLAY_FILE
) NAMES "app.overlay" SUFFIX ${FILE_SUFFIX}
list(TRANSFORM board_overlay_strings APPEND ".overlay") )
endif()
zephyr_file(CONF_FILES ${APPLICATION_CONFIG_DIR} DTS DTC_OVERLAY_FILE
NAMES "${board_overlay_strings};app.overlay" SUFFIX ${FILE_SUFFIX})
endif() endif()
set(DTC_OVERLAY_FILE ${DTC_OVERLAY_FILE} CACHE STRING "If desired, you can \ set(DTC_OVERLAY_FILE ${DTC_OVERLAY_FILE} CACHE STRING "If desired, you can \

View file

@ -123,13 +123,23 @@ set(DTS_CMAKE ${PROJECT_BINARY_DIR}/dts.cmake)
set(VENDOR_PREFIXES dts/bindings/vendor-prefixes.txt) set(VENDOR_PREFIXES dts/bindings/vendor-prefixes.txt)
if(NOT DEFINED DTS_SOURCE) if(NOT DEFINED DTS_SOURCE)
zephyr_build_string(dts_board_string BOARD ${BOARD} BOARD_QUALIFIERS ${BOARD_QUALIFIERS} MERGE) zephyr_build_string(board_string SHORT shortened_board_string
foreach(str ${dts_board_string}) BOARD ${BOARD} BOARD_QUALIFIERS ${BOARD_QUALIFIERS}
if(EXISTS ${BOARD_DIR}/${str}.dts) )
set(DTS_SOURCE ${BOARD_DIR}/${str}.dts) if(EXISTS ${BOARD_DIR}/${shortened_board_string}.dts AND NOT BOARD_${BOARD}_SINGLE_SOC)
break() message(FATAL_ERROR "Board ${ZFILE_BOARD} defines multiple SoCs.\nShortened file name "
endif() "(${shortened_board_string}.dts) not allowed, use '<board>_<soc>.dts' naming"
endforeach() )
elseif(EXISTS ${BOARD_DIR}/${board_string}.dts AND EXISTS ${BOARD_DIR}/${shortened_board_string}.dts)
message(FATAL_ERROR "Conflicting file names discovered. Cannot use both "
"${board_string}.dts and ${shortened_board_string}.dts. "
"Please choose one naming style, ${board_string}.dts is recommended."
)
elseif(EXISTS ${BOARD_DIR}/${board_string}.dts)
set(DTS_SOURCE ${BOARD_DIR}/${board_string}.dts)
elseif(EXISTS ${BOARD_DIR}/${shortened_board_string}.dts)
set(DTS_SOURCE ${BOARD_DIR}/${shortened_board_string}.dts)
endif()
endif() endif()
if(EXISTS ${DTS_SOURCE}) if(EXISTS ${DTS_SOURCE})

View file

@ -1519,7 +1519,8 @@ endfunction()
# build string as first item in the list. # build string as first item in the list.
# The full order of build strings returned in the list will be: # The full order of build strings returned in the list will be:
# - Normalized board target build string, this includes qualifiers and revision # - Normalized board target build string, this includes qualifiers and revision
# - Normalized board target build string, without revision # - Build string with board variants removed in addition
# - Build string with cpuset removed in addition
# - Build string with soc removed in addition # - Build string with soc removed in addition
# #
# If BUILD is supplied, then build type will be appended to each entry in the # If BUILD is supplied, then build type will be appended to each entry in the
@ -1538,6 +1539,7 @@ endfunction()
# ) # )
# #
# <out-variable>: Output variable where the build string will be returned. # <out-variable>: Output variable where the build string will be returned.
# SHORT <out-variable>: Output variable where the shortened build string will be returned.
# BOARD <board>: Board name to use when creating the build string. # BOARD <board>: Board name to use when creating the build string.
# BOARD_REVISION <revision>: Board revision to use when creating the build string. # BOARD_REVISION <revision>: Board revision to use when creating the build string.
# BUILD <type>: Build type to use when creating the build string. # BUILD <type>: Build type to use when creating the build string.
@ -1560,11 +1562,17 @@ endfunction()
# calling # calling
# zephyr_build_string(build_string BOARD alpha BOARD_REVISION 1.0.0 BOARD_QUALIFIERS /soc/bar MERGE) # zephyr_build_string(build_string BOARD alpha BOARD_REVISION 1.0.0 BOARD_QUALIFIERS /soc/bar MERGE)
# will return a list of the following strings # will return a list of the following strings
# `alpha_soc_bar_1_0_0;alpha_soc_bar;alpha_bar` in `build_string` parameter. # `alpha_soc_bar_1_0_0;alpha_soc_bar` in `build_string` parameter.
#
# calling
# zephyr_build_string(build_string SHORTENED short_build_string BOARD alpha BOARD_REVISION 1.0.0 BOARD_QUALIFIERS /soc/bar MERGE)
# will return two lists of the following strings
# `alpha_soc_bar_1_0_0;alpha_soc_bar` in `build_string` parameter.
# `alpha_bar_1_0_0;alpha_bar` in `short_build_string` parameter.
# #
function(zephyr_build_string outvar) function(zephyr_build_string outvar)
set(options MERGE REVERSE) set(options MERGE REVERSE)
set(single_args BOARD BOARD_QUALIFIERS BOARD_REVISION BUILD) set(single_args BOARD BOARD_QUALIFIERS BOARD_REVISION BUILD SHORT)
cmake_parse_arguments(BUILD_STR "${options}" "${single_args}" "" ${ARGN}) cmake_parse_arguments(BUILD_STR "${options}" "${single_args}" "" ${ARGN})
if(BUILD_STR_UNPARSED_ARGUMENTS) if(BUILD_STR_UNPARSED_ARGUMENTS)
@ -1588,28 +1596,44 @@ function(zephyr_build_string outvar)
) )
endif() endif()
string(REPLACE "/" ";" str_segment_list "${BUILD_STR_BOARD}${BUILD_STR_BOARD_QUALIFIERS}") string(REPLACE "/" ";" str_segment_list "${BUILD_STR_BOARD_QUALIFIERS}")
string(REPLACE "." "_" revision_string "${BUILD_STR_BOARD_REVISION}") string(REPLACE "." "_" revision_string "${BUILD_STR_BOARD_REVISION}")
string(JOIN "_" ${outvar} ${str_segment_list} ${revision_string} ${BUILD_STR_BUILD}) string(JOIN "_" ${outvar} ${BUILD_STR_BOARD} ${str_segment_list} ${revision_string} ${BUILD_STR_BUILD})
if(BUILD_STR_MERGE) if(BUILD_STR_MERGE)
string(JOIN "_" variant_string ${str_segment_list} ${BUILD_STR_BUILD}) string(JOIN "_" variant_string ${BUILD_STR_BOARD} ${str_segment_list} ${BUILD_STR_BUILD})
if(NOT "${variant_string}" IN_LIST ${outvar}) if(NOT "${variant_string}" IN_LIST ${outvar})
list(APPEND ${outvar} "${variant_string}") list(APPEND ${outvar} "${variant_string}")
endif() endif()
if(BUILD_STR_BOARD_QUALIFIERS)
string(REGEX REPLACE "^/[^/]*(.*)" "\\1" qualifiers_without_soc "${BUILD_STR_BOARD_QUALIFIERS}")
string(REPLACE "/" "_" qualifiers_without_soc "${qualifiers_without_soc}")
list(APPEND ${outvar} "${BUILD_STR_BOARD}${qualifiers_without_soc}")
endif()
endif() endif()
if(BUILD_STR_REVERSE) if(BUILD_STR_REVERSE)
list(REVERSE ${outvar}) list(REVERSE ${outvar})
endif() endif()
list(REMOVE_DUPLICATES ${outvar})
if(BUILD_STR_SHORT AND BUILD_STR_BOARD_QUALIFIERS)
string(REGEX REPLACE "^/[^/]*(.*)" "\\1" shortened_qualifiers "${BOARD_QUALIFIERS}")
string(REPLACE "/" ";" str_short_segment_list "${shortened_qualifiers}")
string(JOIN "_" ${BUILD_STR_SHORT}
${BUILD_STR_BOARD} ${str_short_segment_list} ${revision_string} ${BUILD_STR_BUILD}
)
if(BUILD_STR_MERGE)
string(JOIN "_" variant_string ${BUILD_STR_BOARD} ${str_short_segment_list} ${BUILD_STR_BUILD})
if(NOT "${variant_string}" IN_LIST ${BUILD_STR_SHORT})
list(APPEND ${BUILD_STR_SHORT} "${variant_string}")
endif()
endif()
if(BUILD_STR_REVERSE)
list(REVERSE ${BUILD_STR_SHORT})
endif()
list(REMOVE_DUPLICATES ${BUILD_STR_SHORT})
set(${BUILD_STR_SHORT} ${${BUILD_STR_SHORT}} PARENT_SCOPE)
endif()
# This updates the provided outvar in parent scope (callers scope) # This updates the provided outvar in parent scope (callers scope)
set(${outvar} ${${outvar}} PARENT_SCOPE) set(${outvar} ${${outvar}} PARENT_SCOPE)
@ -2522,7 +2546,6 @@ endfunction()
# creating file names based on board settings. # creating file names based on board settings.
# Only the first match found in <paths> will be # Only the first match found in <paths> will be
# returned in the <list> # returned in the <list>
#
# DTS <list>: List to append DTS overlay files in <path> to # DTS <list>: List to append DTS overlay files in <path> to
# KCONF <list>: List to append Kconfig fragment files in <path> to # KCONF <list>: List to append Kconfig fragment files in <path> to
# DEFCONF <list>: List to append _defconfig files in <path> to # DEFCONF <list>: List to append _defconfig files in <path> to
@ -2618,32 +2641,44 @@ Relative paths are only allowed with `-D${ARGV1}=<path>`")
set(kconf_filename_list ${ZFILE_NAMES}) set(kconf_filename_list ${ZFILE_NAMES})
else() else()
zephyr_build_string(filename_list zephyr_build_string(filename_list
SHORT shortened_filename_list
BOARD ${ZFILE_BOARD} BOARD ${ZFILE_BOARD}
BOARD_REVISION ${ZFILE_BOARD_REVISION} BOARD_REVISION ${ZFILE_BOARD_REVISION}
BOARD_QUALIFIERS ${ZFILE_BOARD_QUALIFIERS} BOARD_QUALIFIERS ${ZFILE_BOARD_QUALIFIERS}
BUILD ${ZFILE_BUILD} BUILD ${ZFILE_BUILD}
MERGE REVERSE MERGE REVERSE
) )
list(REMOVE_DUPLICATES filename_list)
set(dts_filename_list ${filename_list}) set(dts_filename_list ${filename_list})
set(dts_shortened_filename_list ${shortened_filename_list})
list(TRANSFORM dts_filename_list APPEND ".overlay") list(TRANSFORM dts_filename_list APPEND ".overlay")
list(TRANSFORM dts_shortened_filename_list APPEND ".overlay")
set(kconf_filename_list ${filename_list}) set(kconf_filename_list ${filename_list})
set(kconf_shortened_filename_list ${shortened_filename_list})
list(TRANSFORM kconf_filename_list APPEND ".conf") list(TRANSFORM kconf_filename_list APPEND ".conf")
list(TRANSFORM kconf_shortened_filename_list APPEND ".conf")
endif() endif()
if(ZFILE_DTS) if(ZFILE_DTS)
foreach(path ${ZFILE_CONF_FILES}) foreach(path ${ZFILE_CONF_FILES})
foreach(filename ${dts_filename_list}) foreach(filename IN ZIP_LISTS dts_filename_list dts_shortened_filename_list)
if(NOT IS_ABSOLUTE ${filename}) foreach(i RANGE 1)
set(test_file ${path}/${filename}) if(NOT IS_ABSOLUTE filename_${i} AND DEFINED filename_${i})
else() set(test_file_${i} ${path}/${filename_${i}})
set(test_file ${filename}) else()
endif() set(test_file_${i} ${filename_${i}})
zephyr_file_suffix(test_file SUFFIX ${ZFILE_SUFFIX}) endif()
zephyr_file_suffix(test_file_${i} SUFFIX ${ZFILE_SUFFIX})
if(EXISTS ${test_file}) if(NOT EXISTS ${test_file_${i}})
list(APPEND ${ZFILE_DTS} ${test_file}) set(test_file_${i})
endif()
endforeach()
if(test_file_0 OR test_file_1)
list(APPEND found_dts_files ${test_file_0})
list(APPEND found_dts_files ${test_file_1})
if(DEFINED ZFILE_BUILD) if(DEFINED ZFILE_BUILD)
set(deprecated_file_found y) set(deprecated_file_found y)
@ -2653,29 +2688,48 @@ Relative paths are only allowed with `-D${ARGV1}=<path>`")
break() break()
endif() endif()
endif() endif()
if(test_file_1 AND NOT BOARD_${ZFILE_BOARD}_SINGLE_SOC)
message(FATAL_ERROR "Board ${ZFILE_BOARD} defines multiple SoCs.\nShortened file name "
"(${filename_1}) not allowed, use '<board>_<soc>.overlay' naming"
)
endif()
if(test_file_0 AND test_file_1)
message(FATAL_ERROR "Conflicting file names discovered. Cannot use both ${filename_0} "
"and ${filename_1}. Please choose one naming style, "
"${filename_0} is recommended."
)
endif()
endforeach() endforeach()
endforeach() endforeach()
list(APPEND ${ZFILE_DTS} ${found_dts_files})
# This updates the provided list in parent scope (callers scope) # This updates the provided list in parent scope (callers scope)
set(${ZFILE_DTS} ${${ZFILE_DTS}} PARENT_SCOPE) set(${ZFILE_DTS} ${${ZFILE_DTS}} PARENT_SCOPE)
if(NOT ${ZFILE_DTS})
set(not_found ${dts_filename_list})
endif()
endif() endif()
if(ZFILE_KCONF) if(ZFILE_KCONF)
set(found_conf_files)
foreach(path ${ZFILE_CONF_FILES}) foreach(path ${ZFILE_CONF_FILES})
foreach(filename ${kconf_filename_list}) foreach(filename IN ZIP_LISTS kconf_filename_list kconf_shortened_filename_list)
if(NOT IS_ABSOLUTE ${filename}) foreach(i RANGE 1)
set(test_file ${path}/${filename}) if(NOT IS_ABSOLUTE filename_${i} AND DEFINED filename_${i})
else() set(test_file_${i} ${path}/${filename_${i}})
set(test_file ${filename}) else()
endif() set(test_file_${i} ${filename_${i}})
zephyr_file_suffix(test_file SUFFIX ${ZFILE_SUFFIX}) endif()
zephyr_file_suffix(test_file_${i} SUFFIX ${ZFILE_SUFFIX})
if(EXISTS ${test_file}) if(NOT EXISTS ${test_file_${i}})
list(APPEND ${ZFILE_KCONF} ${test_file}) set(test_file_${i})
endif()
endforeach()
if(test_file_0 OR test_file_1)
list(APPEND found_conf_files ${test_file_0})
list(APPEND found_conf_files ${test_file_1})
if(DEFINED ZFILE_BUILD) if(DEFINED ZFILE_BUILD)
set(deprecated_file_found y) set(deprecated_file_found y)
@ -2685,9 +2739,24 @@ Relative paths are only allowed with `-D${ARGV1}=<path>`")
break() break()
endif() endif()
endif() endif()
if(test_file_1 AND NOT BOARD_${ZFILE_BOARD}_SINGLE_SOC)
message(FATAL_ERROR "Board ${ZFILE_BOARD} defines multiple SoCs.\nShortened file name "
"(${filename_1}) not allowed, use '<board>_<soc>.conf' naming"
)
endif()
if(test_file_0 AND test_file_1)
message(FATAL_ERROR "Conflicting file names discovered. Cannot use both ${filename_0} "
"and ${filename_1}. Please choose one naming style, "
"${filename_0} is recommended."
)
endif()
endforeach() endforeach()
endforeach() endforeach()
list(APPEND ${ZFILE_KCONF} ${found_conf_files})
# This updates the provided list in parent scope (callers scope) # This updates the provided list in parent scope (callers scope)
set(${ZFILE_KCONF} ${${ZFILE_KCONF}} PARENT_SCOPE) set(${ZFILE_KCONF} ${${ZFILE_KCONF}} PARENT_SCOPE)
@ -2709,13 +2778,34 @@ Relative paths are only allowed with `-D${ARGV1}=<path>`")
endif() endif()
if(ZFILE_DEFCONFIG) if(ZFILE_DEFCONFIG)
set(found_defconf_files)
foreach(path ${ZFILE_CONF_FILES}) foreach(path ${ZFILE_CONF_FILES})
foreach(filename ${filename_list}) foreach(filename IN ZIP_LISTS filename_list shortened_filename_list)
if(EXISTS ${path}/${filename}_defconfig) foreach(i RANGE 1)
list(APPEND ${ZFILE_DEFCONFIG} ${path}/${filename}_defconfig) set(test_file_${i} ${path}/${filename_${i}}_defconfig)
if(EXISTS ${test_file_${i}})
list(APPEND found_defconf_files ${test_file_${i}})
else()
set(test_file_${i})
endif()
endforeach()
if(test_file_1 AND NOT BOARD_${ZFILE_BOARD}_SINGLE_SOC)
message(FATAL_ERROR "Board ${ZFILE_BOARD} defines multiple SoCs.\nShortened file name "
"(${filename_1}_defconfig) not allowed, use '<board>_<soc>_defconfig' naming"
)
endif()
if(test_file_0 AND test_file_1)
message(FATAL_ERROR "Conflicting file names discovered. Cannot use both "
"${filename_0}_defconfig and ${filename_1}_defconfig. Please choose one "
"naming style, ${filename_0}_defconfig is recommended."
)
endif() endif()
endforeach() endforeach()
endforeach() endforeach()
list(APPEND ${ZFILE_DEFCONFIG} ${found_defconf_files})
# This updates the provided list in parent scope (callers scope) # This updates the provided list in parent scope (callers scope)
set(${ZFILE_DEFCONFIG} ${${ZFILE_DEFCONFIG}} PARENT_SCOPE) set(${ZFILE_DEFCONFIG} ${${ZFILE_DEFCONFIG}} PARENT_SCOPE)
@ -4377,7 +4467,7 @@ function(zephyr_linker_dts_section)
if(DTS_SECTION_UNPARSED_ARGUMENTS) if(DTS_SECTION_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "zephyr_linker_dts_section(${ARGV0} ...) given unknown " message(FATAL_ERROR "zephyr_linker_dts_section(${ARGV0} ...) given unknown "
"arguments: ${DTS_SECTION_UNPARSED_ARGUMENTS}" "arguments: ${DTS_SECTION_UNPARSED_ARGUMENTS}"
) )
endif() endif()