Skip to content

Commit

Permalink
cmake: allow board to change from image to image
Browse files Browse the repository at this point in the history
Signed-off-by: Håkon Øye Amundsen <[email protected]>
  • Loading branch information
hakonfam authored and SebastianBoe committed Feb 22, 2019
1 parent 8e4dfe8 commit c60bad2
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 79 deletions.
147 changes: 79 additions & 68 deletions cmake/app/boilerplate.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ if(FIRST_BOILERPLATE_EXECUTION)
# Equivalent to rm -rf build/*
)

# 'BOARD_ROOT' is a prioritized list of directories where boards may
# be found. It always includes ${ZEPHYR_BASE} at the lowest priority.
list(APPEND BOARD_ROOT ${ZEPHYR_BASE})

# The BOARD can be set by 3 sources. Through environment variables,
# through the cmake CLI, and through CMakeLists.txt.
#
Expand Down Expand Up @@ -191,7 +195,6 @@ if(FIRST_BOILERPLATE_EXECUTION)
endif()
endif()


set(BOARD ${CACHED_BOARD})
elseif(board_cli_argument)
set(BOARD ${board_cli_argument})
Expand All @@ -212,33 +215,6 @@ if(FIRST_BOILERPLATE_EXECUTION)
# Store the selected board in the cache
set(CACHED_BOARD ${BOARD} CACHE STRING "Selected board")

# 'BOARD_ROOT' is a prioritized list of directories where boards may
# be found. It always includes ${ZEPHYR_BASE} at the lowest priority.
list(APPEND BOARD_ROOT ${ZEPHYR_BASE})

# Use BOARD to search for a '_defconfig' file.
# e.g. zephyr/boards/arm/96b_carbon_nrf51/96b_carbon_nrf51_defconfig.
# When found, use that path to infer the ARCH we are building for.
foreach(root ${BOARD_ROOT})
# NB: find_path will return immediately if the output variable is
# already set
find_path(BOARD_DIR
NAMES ${BOARD}_defconfig
PATHS ${root}/boards/*/*
NO_DEFAULT_PATH
)
if(BOARD_DIR AND NOT (${root} STREQUAL ${ZEPHYR_BASE}))
set(USING_OUT_OF_TREE_BOARD 1)
endif()
endforeach()

if(NOT BOARD_DIR)
message("No board named '${BOARD}' found")
print_usage()
unset(CACHED_BOARD CACHE)
message(FATAL_ERROR "Invalid usage")
endif()

# The SHIELD can be set by 3 sources. Through environment variables,
# through the cmake CLI, and through CMakeLists.txt.
#
Expand Down Expand Up @@ -361,16 +337,25 @@ if(FIRST_BOILERPLATE_EXECUTION)
message(FATAL_ERROR "Invalid usage")
endif()

get_filename_component(BOARD_ARCH_DIR ${BOARD_DIR} DIRECTORY)
get_filename_component(BOARD_FAMILY ${BOARD_DIR} NAME)
get_filename_component(ARCH ${BOARD_ARCH_DIR} NAME)

# Prevent CMake from testing the toolchain
set(CMAKE_C_COMPILER_FORCED 1)
set(CMAKE_CXX_COMPILER_FORCED 1)

include(${ZEPHYR_BASE}/cmake/host-tools.cmake)

string(REPLACE ";" " " BOARD_ROOT_SPACE_SEPARATED "${BOARD_ROOT}")
string(REPLACE ";" " " SHIELD_LIST_SPACE_SEPARATED "${SHIELD_LIST}")

# NB: The reason it is 'usage' and not help is that CMake already
# defines a target 'help'
add_custom_target(
usage
${CMAKE_COMMAND}
-DBOARD_ROOT_SPACE_SEPARATED=${BOARD_ROOT_SPACE_SEPARATED}
-DSHIELD_LIST_SPACE_SEPARATED=${SHIELD_LIST_SPACE_SEPARATED}
-P ${ZEPHYR_BASE}/cmake/usage/usage.cmake
)

# DTS should be close to kconfig because CONFIG_ variables from
# kconfig and dts should be available at the same time.
#
Expand All @@ -386,42 +371,6 @@ if(FIRST_BOILERPLATE_EXECUTION)
# and possibly change the toolchain.
include(${ZEPHYR_BASE}/cmake/zephyr_module.cmake)
include(${ZEPHYR_BASE}/cmake/generic_toolchain.cmake)

string(REPLACE ";" " " BOARD_ROOT_SPACE_SEPARATED "${BOARD_ROOT}")
string(REPLACE ";" " " SHIELD_LIST_SPACE_SEPARATED "${SHIELD_LIST}")
# NB: The reason it is 'usage' and not help is that CMake already
# defines a target 'help'
add_custom_target(
usage
${CMAKE_COMMAND}
-DBOARD_ROOT_SPACE_SEPARATED=${BOARD_ROOT_SPACE_SEPARATED}
-DSHIELD_LIST_SPACE_SEPARATED=${SHIELD_LIST_SPACE_SEPARATED}
-P ${ZEPHYR_BASE}/cmake/usage/usage.cmake
)

if(CONF_FILE)
# # CONF_FILE has either been specified on the cmake CLI or is already
# # in the CMakeCache.txt. This has precedence over the environment
# # variable CONF_FILE and the default prj.conf
elseif(DEFINED ENV{CONF_FILE})
set(CONF_FILE $ENV{CONF_FILE})
elseif(COMMAND set_conf_file)
set_conf_file()
elseif(EXISTS ${APPLICATION_SOURCE_DIR}/prj_${BOARD}.conf)
set(CONF_FILE ${APPLICATION_SOURCE_DIR}/prj_${BOARD}.conf)
elseif(EXISTS ${APPLICATION_SOURCE_DIR}/prj.conf)
set(CONF_FILE ${APPLICATION_SOURCE_DIR}/prj.conf)
endif()

if(DTC_OVERLAY_FILE)
# # DTC_OVERLAY_FILE has either been specified on the cmake CLI or is already
# # in the CMakeCache.txt. This has precedence over the environment
# # variable DTC_OVERLAY_FILE
elseif(DEFINED ENV{DTC_OVERLAY_FILE})
set(DTC_OVERLAY_FILE $ENV{DTC_OVERLAY_FILE})
elseif(EXISTS ${APPLICATION_SOURCE_DIR}/${BOARD}.overlay)
set(DTC_OVERLAY_FILE ${APPLICATION_SOURCE_DIR}/${BOARD}.overlay)
endif()
else() # NOT FIRST_BOILERPLATE_EXECUTION

# Have the child image select the same BOARD that was selected by
Expand All @@ -439,6 +388,68 @@ else() # NOT FIRST_BOILERPLATE_EXECUTION
if(EXISTS ${APPLICATION_SOURCE_DIR}/${BOARD}.overlay)
set(DTC_OVERLAY_FILE ${APPLICATION_SOURCE_DIR}/${BOARD}.overlay)
endif()
endif(FIRST_BOILERPLATE_EXECUTION)

# Use BOARD to search for a '_defconfig' file.
# e.g. zephyr/boards/arm/96b_carbon_nrf51/96b_carbon_nrf51_defconfig.
# When found, use that path to infer the ARCH we are building for.
foreach(root ${BOARD_ROOT})
# NB: find_path will return immediately if the output variable is
# already set
find_path(TMP_BOARD_DIR
NAMES ${BOARD}_defconfig
PATHS ${root}/boards/*/*
NO_DEFAULT_PATH
)

# Ensure that BOARD_DIR is not in CACHE so that different images can use
# different BOARD_DIR.
get_property(BOARD_DIR CACHE TMP_BOARD_DIR PROPERTY VALUE)
unset(TMP_BOARD_DIR CACHE)

if(BOARD_DIR AND NOT (${root} STREQUAL ${ZEPHYR_BASE}))
set(USING_OUT_OF_TREE_BOARD 1)
endif()
endforeach()

if(NOT BOARD_DIR)
message("No board named '${BOARD}' found")
print_usage()
unset(CACHED_BOARD CACHE)
message(FATAL_ERROR "Invalid usage")
endif()

get_filename_component(BOARD_ARCH_DIR ${BOARD_DIR}} DIRECTORY)
get_filename_component(BOARD_FAMILY ${BOARD_DIR} NAME)
get_filename_component(ARCH ${BOARD_ARCH_DIR} NAME)

# Pick host system's toolchain if we are targeting posix
if((${ARCH} STREQUAL "posix") OR (${ARCH} STREQUAL "x86_64"))
set(ZEPHYR_TOOLCHAIN_VARIANT "host")
endif()

if(CONF_FILE)
# # CONF_FILE has either been specified on the cmake CLI or is already
# # in the CMakeCache.txt. This has precedence over the environment
# # variable CONF_FILE and the default prj.conf
elseif(DEFINED ENV{CONF_FILE})
set(CONF_FILE $ENV{CONF_FILE})
elseif(COMMAND set_conf_file)
set_conf_file()
elseif(EXISTS ${APPLICATION_SOURCE_DIR}/prj_${BOARD}.conf)
set(CONF_FILE ${APPLICATION_SOURCE_DIR}/prj_${BOARD}.conf)
elseif(EXISTS ${APPLICATION_SOURCE_DIR}/prj.conf)
set(CONF_FILE ${APPLICATION_SOURCE_DIR}/prj.conf)
endif()

if(DTC_OVERLAY_FILE)
# # DTC_OVERLAY_FILE has either been specified on the cmake CLI or is already
# # in the CMakeCache.txt. This has precedence over the environment
# # variable DTC_OVERLAY_FILE
elseif(DEFINED ENV{DTC_OVERLAY_FILE})
set(DTC_OVERLAY_FILE $ENV{DTC_OVERLAY_FILE})
elseif(EXISTS ${APPLICATION_SOURCE_DIR}/${BOARD}.overlay)
set(DTC_OVERLAY_FILE ${APPLICATION_SOURCE_DIR}/${BOARD}.overlay)
endif()

set(CONF_FILE ${CONF_FILE} CACHE STRING "If desired, you can build the application using\
Expand Down
13 changes: 7 additions & 6 deletions cmake/dts.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/include/generated)
# See ~/zephyr/doc/dts
set(GENERATED_DTS_BOARD_UNFIXED_H ${PROJECT_BINARY_DIR}/include/generated/generated_dts_board_unfixed.h)
set(GENERATED_DTS_BOARD_CONF ${PROJECT_BINARY_DIR}/include/generated/generated_dts_board.conf)
set_ifndef(DTS_SOURCE ${BOARD_DIR}/${BOARD}.dts)
set_ifndef(DTS_COMMON_OVERLAYS ${ZEPHYR_BASE}/dts/common/common.dts)
set_ifndef(DTS_APP_BINDINGS ${APPLICATION_SOURCE_DIR}/dts/bindings)
set_ifndef(DTS_APP_INCLUDE ${APPLICATION_SOURCE_DIR}/dts)

set_ifndef(${IMAGE}DTS_SOURCE ${BOARD_DIR}/${BOARD}.dts)
set_ifndef(${IMAGE}DTS_COMMON_OVERLAYS ${ZEPHYR_BASE}/dts/common/common.dts)
set_ifndef(${IMAGE}DTS_APP_BINDINGS ${APPLICATION_SOURCE_DIR}/dts/bindings)
set_ifndef(${IMAGE}DTS_APP_INCLUDE ${APPLICATION_SOURCE_DIR}/dts)

set(dts_files
${DTS_SOURCE}
${${IMAGE}DTS_SOURCE}
${DTS_COMMON_OVERLAYS}
${shield_dts_files}
)

# TODO: What to do about non-posix platforms where NOT CONFIG_HAS_DTS (xtensa)?
# Drop support for NOT CONFIG_HAS_DTS perhaps?
if(EXISTS ${DTS_SOURCE})
if(EXISTS ${${IMAGE}DTS_SOURCE})
set(SUPPORTS_DTS 1)
else()
set(SUPPORTS_DTS 0)
Expand Down
5 changes: 0 additions & 5 deletions cmake/generic_toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ assert(TOOLCHAIN_ROOT "Zephyr toolchain root path invalid: please set the TOOLCH
set(ZEPHYR_TOOLCHAIN_VARIANT ${ZEPHYR_TOOLCHAIN_VARIANT} CACHE STRING "Zephyr toolchain variant")
assert(ZEPHYR_TOOLCHAIN_VARIANT "Zephyr toolchain variant invalid: please set the ZEPHYR_TOOLCHAIN_VARIANT-variable")

# Pick host system's toolchain if we are targeting posix
if((${ARCH} STREQUAL "posix") OR (${ARCH} STREQUAL "x86_64"))
set(ZEPHYR_TOOLCHAIN_VARIANT "host")
endif()

# Configure the toolchain based on what SDK/toolchain is in use.
include(${TOOLCHAIN_ROOT}/cmake/toolchain/${ZEPHYR_TOOLCHAIN_VARIANT}/generic.cmake)

Expand Down

0 comments on commit c60bad2

Please sign in to comment.