@@ -146,6 +146,10 @@ if(FIRST_BOILERPLATE_EXECUTION)
146146 # Equivalent to rm -rf build/*
147147 )
148148
149+ # 'BOARD_ROOT' is a prioritized list of directories where boards may
150+ # be found. It always includes ${ZEPHYR_BASE} at the lowest priority.
151+ list (APPEND BOARD_ROOT ${ZEPHYR_BASE} )
152+
149153 # The BOARD can be set by 3 sources. Through environment variables,
150154 # through the cmake CLI, and through CMakeLists.txt.
151155 #
@@ -191,7 +195,6 @@ if(FIRST_BOILERPLATE_EXECUTION)
191195 endif ()
192196 endif ()
193197
194-
195198 set (BOARD ${CACHED_BOARD} )
196199 elseif (board_cli_argument)
197200 set (BOARD ${board_cli_argument} )
@@ -212,33 +215,6 @@ if(FIRST_BOILERPLATE_EXECUTION)
212215 # Store the selected board in the cache
213216 set (CACHED_BOARD ${BOARD} CACHE STRING "Selected board" )
214217
215- # 'BOARD_ROOT' is a prioritized list of directories where boards may
216- # be found. It always includes ${ZEPHYR_BASE} at the lowest priority.
217- list (APPEND BOARD_ROOT ${ZEPHYR_BASE} )
218-
219- # Use BOARD to search for a '_defconfig' file.
220- # e.g. zephyr/boards/arm/96b_carbon_nrf51/96b_carbon_nrf51_defconfig.
221- # When found, use that path to infer the ARCH we are building for.
222- foreach (root ${BOARD_ROOT} )
223- # NB: find_path will return immediately if the output variable is
224- # already set
225- find_path (BOARD_DIR
226- NAMES ${BOARD} _defconfig
227- PATHS ${root} /boards/*/*
228- NO_DEFAULT_PATH
229- )
230- if (BOARD_DIR AND NOT (${root} STREQUAL ${ZEPHYR_BASE} ))
231- set (USING_OUT_OF_TREE_BOARD 1)
232- endif ()
233- endforeach ()
234-
235- if (NOT BOARD_DIR)
236- message ("No board named '${BOARD} ' found" )
237- print_usage()
238- unset (CACHED_BOARD CACHE )
239- message (FATAL_ERROR "Invalid usage" )
240- endif ()
241-
242218 # The SHIELD can be set by 3 sources. Through environment variables,
243219 # through the cmake CLI, and through CMakeLists.txt.
244220 #
@@ -361,16 +337,25 @@ if(FIRST_BOILERPLATE_EXECUTION)
361337 message (FATAL_ERROR "Invalid usage" )
362338 endif ()
363339
364- get_filename_component (BOARD_ARCH_DIR ${BOARD_DIR} DIRECTORY )
365- get_filename_component (BOARD_FAMILY ${BOARD_DIR} NAME )
366- get_filename_component (ARCH ${BOARD_ARCH_DIR} NAME )
367-
368340 # Prevent CMake from testing the toolchain
369341 set (CMAKE_C_COMPILER_FORCED 1)
370342 set (CMAKE_CXX_COMPILER_FORCED 1)
371343
372344 include (${ZEPHYR_BASE} /cmake/host-tools.cmake)
373345
346+ string (REPLACE ";" " " BOARD_ROOT_SPACE_SEPARATED "${BOARD_ROOT} " )
347+ string (REPLACE ";" " " SHIELD_LIST_SPACE_SEPARATED "${SHIELD_LIST} " )
348+
349+ # NB: The reason it is 'usage' and not help is that CMake already
350+ # defines a target 'help'
351+ add_custom_target (
352+ usage
353+ ${CMAKE_COMMAND}
354+ -DBOARD_ROOT_SPACE_SEPARATED=${BOARD_ROOT_SPACE_SEPARATED}
355+ -DSHIELD_LIST_SPACE_SEPARATED=${SHIELD_LIST_SPACE_SEPARATED}
356+ -P ${ZEPHYR_BASE} /cmake/usage/usage.cmake
357+ )
358+
374359 # DTS should be close to kconfig because CONFIG_ variables from
375360 # kconfig and dts should be available at the same time.
376361 #
@@ -386,42 +371,6 @@ if(FIRST_BOILERPLATE_EXECUTION)
386371 # and possibly change the toolchain.
387372 include (${ZEPHYR_BASE} /cmake/zephyr_module.cmake)
388373 include (${ZEPHYR_BASE} /cmake/generic_toolchain.cmake)
389-
390- string (REPLACE ";" " " BOARD_ROOT_SPACE_SEPARATED "${BOARD_ROOT} " )
391- string (REPLACE ";" " " SHIELD_LIST_SPACE_SEPARATED "${SHIELD_LIST} " )
392- # NB: The reason it is 'usage' and not help is that CMake already
393- # defines a target 'help'
394- add_custom_target (
395- usage
396- ${CMAKE_COMMAND}
397- -DBOARD_ROOT_SPACE_SEPARATED=${BOARD_ROOT_SPACE_SEPARATED}
398- -DSHIELD_LIST_SPACE_SEPARATED=${SHIELD_LIST_SPACE_SEPARATED}
399- -P ${ZEPHYR_BASE} /cmake/usage/usage.cmake
400- )
401-
402- if (CONF_FILE)
403- # # CONF_FILE has either been specified on the cmake CLI or is already
404- # # in the CMakeCache.txt. This has precedence over the environment
405- # # variable CONF_FILE and the default prj.conf
406- elseif (DEFINED ENV{CONF_FILE})
407- set (CONF_FILE $ENV{CONF_FILE} )
408- elseif (COMMAND set_conf_file)
409- set_conf_file()
410- elseif (EXISTS ${APPLICATION_SOURCE_DIR} /prj_${BOARD} .conf)
411- set (CONF_FILE ${APPLICATION_SOURCE_DIR} /prj_${BOARD} .conf)
412- elseif (EXISTS ${APPLICATION_SOURCE_DIR} /prj.conf)
413- set (CONF_FILE ${APPLICATION_SOURCE_DIR} /prj.conf)
414- endif ()
415-
416- if (DTC_OVERLAY_FILE)
417- # # DTC_OVERLAY_FILE has either been specified on the cmake CLI or is already
418- # # in the CMakeCache.txt. This has precedence over the environment
419- # # variable DTC_OVERLAY_FILE
420- elseif (DEFINED ENV{DTC_OVERLAY_FILE})
421- set (DTC_OVERLAY_FILE $ENV{DTC_OVERLAY_FILE} )
422- elseif (EXISTS ${APPLICATION_SOURCE_DIR} /${BOARD} .overlay)
423- set (DTC_OVERLAY_FILE ${APPLICATION_SOURCE_DIR} /${BOARD} .overlay)
424- endif ()
425374else () # NOT FIRST_BOILERPLATE_EXECUTION
426375
427376 # Have the child image select the same BOARD that was selected by
@@ -439,6 +388,68 @@ else() # NOT FIRST_BOILERPLATE_EXECUTION
439388 if (EXISTS ${APPLICATION_SOURCE_DIR} /${BOARD} .overlay)
440389 set (DTC_OVERLAY_FILE ${APPLICATION_SOURCE_DIR} /${BOARD} .overlay)
441390 endif ()
391+ endif (FIRST_BOILERPLATE_EXECUTION)
392+
393+ # Use BOARD to search for a '_defconfig' file.
394+ # e.g. zephyr/boards/arm/96b_carbon_nrf51/96b_carbon_nrf51_defconfig.
395+ # When found, use that path to infer the ARCH we are building for.
396+ foreach (root ${BOARD_ROOT} )
397+ # NB: find_path will return immediately if the output variable is
398+ # already set
399+ find_path (TMP_BOARD_DIR
400+ NAMES ${BOARD} _defconfig
401+ PATHS ${root} /boards/*/*
402+ NO_DEFAULT_PATH
403+ )
404+
405+ # Ensure that BOARD_DIR is not in CACHE so that different images can use
406+ # different BOARD_DIR.
407+ get_property (BOARD_DIR CACHE TMP_BOARD_DIR PROPERTY VALUE )
408+ unset (TMP_BOARD_DIR CACHE )
409+
410+ if (BOARD_DIR AND NOT (${root} STREQUAL ${ZEPHYR_BASE} ))
411+ set (USING_OUT_OF_TREE_BOARD 1)
412+ endif ()
413+ endforeach ()
414+
415+ if (NOT BOARD_DIR)
416+ message ("No board named '${BOARD} ' found" )
417+ print_usage()
418+ unset (CACHED_BOARD CACHE )
419+ message (FATAL_ERROR "Invalid usage" )
420+ endif ()
421+
422+ get_filename_component (BOARD_ARCH_DIR ${BOARD_DIR} } DIRECTORY )
423+ get_filename_component (BOARD_FAMILY ${BOARD_DIR} NAME )
424+ get_filename_component (ARCH ${BOARD_ARCH_DIR} NAME )
425+
426+ # Pick host system's toolchain if we are targeting posix
427+ if ((${ARCH} STREQUAL "posix" ) OR (${ARCH} STREQUAL "x86_64" ))
428+ set (ZEPHYR_TOOLCHAIN_VARIANT "host" )
429+ endif ()
430+
431+ if (CONF_FILE)
432+ # # CONF_FILE has either been specified on the cmake CLI or is already
433+ # # in the CMakeCache.txt. This has precedence over the environment
434+ # # variable CONF_FILE and the default prj.conf
435+ elseif (DEFINED ENV{CONF_FILE})
436+ set (CONF_FILE $ENV{CONF_FILE} )
437+ elseif (COMMAND set_conf_file)
438+ set_conf_file()
439+ elseif (EXISTS ${APPLICATION_SOURCE_DIR} /prj_${BOARD} .conf)
440+ set (CONF_FILE ${APPLICATION_SOURCE_DIR} /prj_${BOARD} .conf)
441+ elseif (EXISTS ${APPLICATION_SOURCE_DIR} /prj.conf)
442+ set (CONF_FILE ${APPLICATION_SOURCE_DIR} /prj.conf)
443+ endif ()
444+
445+ if (DTC_OVERLAY_FILE)
446+ # # DTC_OVERLAY_FILE has either been specified on the cmake CLI or is already
447+ # # in the CMakeCache.txt. This has precedence over the environment
448+ # # variable DTC_OVERLAY_FILE
449+ elseif (DEFINED ENV{DTC_OVERLAY_FILE})
450+ set (DTC_OVERLAY_FILE $ENV{DTC_OVERLAY_FILE} )
451+ elseif (EXISTS ${APPLICATION_SOURCE_DIR} /${BOARD} .overlay)
452+ set (DTC_OVERLAY_FILE ${APPLICATION_SOURCE_DIR} /${BOARD} .overlay)
442453endif ()
443454
444455set (CONF_FILE ${CONF_FILE} CACHE STRING "If desired, you can build the application using\
0 commit comments