Skip to content

Commit

Permalink
cmake: enhanced board entry file handling
Browse files Browse the repository at this point in the history
With a single board now covering what used to be several boards, and
with the ability to omit SoC when building for a single SoC board, then
<board>_defconfig and <board>.dts lookup is improved.

A single SoC board may prefer to keep its defconfig entry point as
<board>_defconfig instead of <board>_<soc>_defconfig.

Also, a multi-SoC board / multi-core SoC board, which used to be
implemented as n-boards may wish to have common _defconfig settings in
a common <board>_defconfig file, and the SoC / cpuset specifics in
<board>_<soc>_defconfig / <board>_<soc>_<core>_defconfig.

Such defconfig support allows also to place build variant specifics in
its own <board>_<soc>_<variant>_defconfig file.

This commit allows multiple _defconfigs for a board and its identifiers.

Similar is implemented for a board's dts file.
If a <board>_<soc>_<core>.dts file is not found, the build system will
instead use <board>_<soc>.dts, and finally fallback to <board>.dts.

This allows a board to have a shared dts file for all board identifiers
which are identical while still support specific dts where required.

A dts file is a devicetree starting point and thus two dts files cannot
be used in together. For such cases, an ordinary board overlay file must
be used.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
  • Loading branch information
tejlmand authored and gmarull committed Jan 11, 2024
1 parent e816dd4 commit a96a2b0
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 34 deletions.
11 changes: 9 additions & 2 deletions cmake/modules/dts.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,16 @@ set(DTS_CMAKE ${PROJECT_BINARY_DIR}/dts.cmake)
# modules.
set(VENDOR_PREFIXES dts/bindings/vendor-prefixes.txt)

zephyr_build_string(dts_board_string BOARD ${BOARD} BOARD_IDENTIFIER ${BOARD_IDENTIFIER})
if(NOT DEFINED DTS_SOURCE)
zephyr_build_string(dts_board_string BOARD ${BOARD} BOARD_IDENTIFIER ${BOARD_IDENTIFIER} MERGE)
foreach(str ${dts_board_string})
if(EXISTS ${BOARD_DIR}/${str}.dts)
set(DTS_SOURCE ${BOARD_DIR}/${str}.dts)
break()
endif()
endforeach()
endif()

set_ifndef(DTS_SOURCE ${BOARD_DIR}/${dts_board_string}.dts)
if(EXISTS ${DTS_SOURCE})
# We found a devicetree. Check for a board revision overlay.
if(DEFINED BOARD_REVISION)
Expand Down
106 changes: 79 additions & 27 deletions cmake/modules/extensions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1468,19 +1468,38 @@ endfunction()
#
# This is a common function to ensure that build strings are always created
# in a uniform way.
# A single string is returned containing the full build string constructed from
# all arguments.
#
# When MERGE is supplied a list of build strings will be returned with the full
# build string as first item in the list.
# The full order of build strings returned in the list will be:
# - Full build string, including identifier and revision
# - Build string with board variants removed in addition
# - Build string with cpuset 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
# list above.
# If REVISION is supplied or obtained as system wide setting a build string
# with the sanitized revision string will be added in addition to the
# non-revisioned entry for each entry.
#
# Usage:
# zephyr_build_string(<out-variable>
# BOARD <board>
# [BOARD_IDENTIFIER <identifier>]
# [BOARD_REVISION <revision>]
# [BUILD <type>]
# [MERGE [REVERSE]]
# )
#
# <out-variable>: Output variable where the build string will be returned.
# BOARD <board>: Board name 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.
# MERGE: Return a list of build identifiers instead of a single build string.
# REVERSE: Reverse the list before returning it.
#
# Examples
# calling
Expand All @@ -1491,10 +1510,20 @@ endfunction()
# zephyr_build_string(build_string BOARD alpha BOARD_REVISION 1.0.0 BUILD debug)
# will return the string `alpha_1_0_0_debug` in `build_string` parameter.
#
# calling
# zephyr_build_string(build_string BOARD alpha BOARD_IDENTIFIER /soc/bar)
# will return the string `alpha_soc_bar` in `build_string` parameter.
#
# calling
# zephyr_build_string(build_string BOARD alpha BOARD_REVISION 1.0.0 BOARD_IDENTIFIER /soc/bar MERGE)
# will return a list of the following strings
# `alpha_soc_bar_1_0_0;alpha_soc_bar;alpha_soc_1_0_0;alpha_soc;alpha_1_0_0;alpha` in `build_string` parameter.
#
function(zephyr_build_string outvar)
set(options MERGE REVERSE)
set(single_args BOARD BOARD_IDENTIFIER BOARD_REVISION BUILD)

cmake_parse_arguments(BUILD_STR "" "${single_args}" "" ${ARGN})
cmake_parse_arguments(BUILD_STR "${options}" "${single_args}" "" ${ARGN})
if(BUILD_STR_UNPARSED_ARGUMENTS)
message(FATAL_ERROR
"zephyr_build_string(${ARGV0} <val> ...) given unknown arguments:"
Expand All @@ -1516,20 +1545,30 @@ function(zephyr_build_string outvar)
)
endif()

set(${outvar} ${BUILD_STR_BOARD})
string(REPLACE "/" ";" str_segment_list "${BUILD_STR_BOARD}${BUILD_STR_BOARD_IDENTIFIER}")
string(REPLACE "." "_" revision_string "${BUILD_STR_BOARD_REVISION}")

if(DEFINED BUILD_STR_BOARD_IDENTIFIER)
string(REPLACE "/" "_" variant_string ${BUILD_STR_BOARD_IDENTIFIER})
set(${outvar} "${${outvar}}${variant_string}")
endif()
string(JOIN "_" ${outvar} ${str_segment_list} ${revision_string} ${BUILD_STR_BUILD})

if(DEFINED BUILD_STR_BOARD_REVISION)
string(REPLACE "." "_" revision_string ${BUILD_STR_BOARD_REVISION})
set(${outvar} "${${outvar}}_${revision_string}")
if(BUILD_STR_MERGE)
if(DEFINED BUILD_STR_BOARD_REVISION)
string(JOIN "_" variant_string ${str_segment_list} ${BUILD_STR_BUILD})
list(APPEND ${outvar} "${variant_string}")
endif()
list(POP_BACK str_segment_list)
while(NOT str_segment_list STREQUAL "")
if(DEFINED BUILD_STR_BOARD_REVISION)
string(JOIN "_" variant_string ${str_segment_list} ${revision_string} ${BUILD_STR_BUILD})
list(APPEND ${outvar} "${variant_string}")
endif()
string(JOIN "_" variant_string ${str_segment_list} ${BUILD_STR_BUILD})
list(APPEND ${outvar} "${variant_string}")
list(POP_BACK str_segment_list)
endwhile()
endif()

if(BUILD_STR_BUILD)
set(${outvar} "${${outvar}}_${BUILD_STR_BUILD}")
if(BUILD_STR_REVERSE)
list(REVERSE ${outvar})
endif()

# This updates the provided outvar in parent scope (callers scope)
Expand Down Expand Up @@ -2334,6 +2373,7 @@ endfunction()
# files are returned. Configuration files will be:
# - DTS: Overlay files (.overlay)
# - Kconfig: Config fragments (.conf)
# - defconfig: defconfig files (_defconfig)
# The conf file search will return existing configuration
# files for the current board.
# CONF_FILES takes the following additional arguments:
Expand All @@ -2344,12 +2384,13 @@ endfunction()
# If no board is given the current BOARD and
# BOARD_REVISION will be used.
#
# DTS <list>: List to append DTS overlay files in <path> to
# KCONF <list>: List to append Kconfig fragment files in <path> to
# BUILD <type>: Build type to include for search.
# For example:
# BUILD debug, will look for <board>_debug.conf
# and <board>_debug.overlay, instead of <board>.conf
# DTS <list>: List to append DTS overlay 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
# BUILD <type>: Build type to include for search.
# For example:
# BUILD debug, will look for <board>_debug.conf
# and <board>_debug.overlay, instead of <board>.conf
#
function(zephyr_file)
set(file_options APPLICATION_ROOT CONF_FILES)
Expand All @@ -2361,7 +2402,7 @@ Please provide one of following: APPLICATION_ROOT, CONF_FILES")
if(${ARGV0} STREQUAL APPLICATION_ROOT)
set(single_args APPLICATION_ROOT)
elseif(${ARGV0} STREQUAL CONF_FILES)
set(single_args BOARD BOARD_REVISION DTS KCONF BUILD)
set(single_args BOARD BOARD_REVISION BOARD_IDENTIFIER DTS KCONF DEFCONFIG BUILD)
set(multi_args CONF_FILES)
endif()

Expand Down Expand Up @@ -2420,21 +2461,19 @@ Relative paths are only allowed with `-D${ARGV1}=<path>`")
if(DEFINED BOARD_REVISION)
set(FILE_BOARD_REVISION ${BOARD_REVISION})
endif()
endif()

zephyr_build_string(filename
BOARD ${FILE_BOARD}
BUILD ${FILE_BUILD}
)
set(filename_list ${filename})
if(DEFINED BOARD_IDENTIFIER)
set(FILE_BOARD_IDENTIFIER ${BOARD_IDENTIFIER})
endif()
endif()

zephyr_build_string(filename
zephyr_build_string(filename_list
BOARD ${FILE_BOARD}
BOARD_REVISION ${FILE_BOARD_REVISION}
BOARD_IDENTIFIER ${FILE_BOARD_IDENTIFIER}
BUILD ${FILE_BUILD}
MERGE REVERSE
)
list(APPEND filename_list ${filename})
list(REMOVE_DUPLICATES filename_list)

if(FILE_DTS)
foreach(path ${FILE_CONF_FILES})
Expand All @@ -2461,6 +2500,19 @@ Relative paths are only allowed with `-D${ARGV1}=<path>`")
# This updates the provided list in parent scope (callers scope)
set(${FILE_KCONF} ${${FILE_KCONF}} PARENT_SCOPE)
endif()

if(FILE_DEFCONFIG)
foreach(path ${FILE_CONF_FILES})
foreach(filename ${filename_list})
if(EXISTS ${path}/${filename}_defconfig)
list(APPEND ${FILE_DEFCONFIG} ${path}/${filename}_defconfig)
endif()
endforeach()
endforeach()

# This updates the provided list in parent scope (callers scope)
set(${FILE_DEFCONFIG} ${${FILE_DEFCONFIG}} PARENT_SCOPE)
endif()
endif()
endfunction()

Expand Down
19 changes: 14 additions & 5 deletions cmake/modules/kconfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,23 @@ else()
set(KCONFIG_ROOT ${ZEPHYR_BASE}/Kconfig)
endif()

zephyr_build_string(config_board_string BOARD ${BOARD} BOARD_IDENTIFIER ${BOARD_IDENTIFIER})

if(NOT DEFINED BOARD_DEFCONFIG)
set(BOARD_DEFCONFIG ${BOARD_DIR}/${config_board_string}_defconfig)
zephyr_file(CONF_FILES ${BOARD_DIR} DEFCONFIG BOARD_DEFCONFIG)
endif()
if((DEFINED BOARD_REVISION) AND EXISTS ${BOARD_DIR}/${config_board_string}_${BOARD_REVISION_STRING}.conf)
set_ifndef(BOARD_REVISION_CONFIG ${BOARD_DIR}/${config_board_string}_${BOARD_REVISION_STRING}.conf)

if(DEFINED BOARD_REVISION)
zephyr_build_string(config_board_string
BOARD ${BOARD}
BOARD_IDENTIFIER ${BOARD_IDENTIFIER}
BOARD_REVISION ${BOARD_REVISION}
)
set(board_rev_file ${config_board_string})
if(EXISTS ${BOARD_DIR}/${board_rev_file}.conf)
message(DEPRECATION "Use of '${board_rev_file}.conf' is deprecated, please switch to '${board_rev_file}_defconfig'")
set_ifndef(BOARD_REVISION_CONFIG ${BOARD_DIR}/${board_rev_file}.conf)
endif()
endif()

set(DOTCONFIG ${PROJECT_BINARY_DIR}/.config)
set(PARSED_KCONFIG_SOURCES_TXT ${PROJECT_BINARY_DIR}/kconfig/sources.txt)

Expand Down

0 comments on commit a96a2b0

Please sign in to comment.