Skip to content

Commit

Permalink
[nrfconnect] CMake improvements and cleanup (#23900)
Browse files Browse the repository at this point in the history
* Improve dependency handling for Matter OTA image build

Also, simplify the code for gn version checking by using
built-in CMake features.

Signed-off-by: Damian Krolik <[email protected]>

* Remove no longer needed zephyr_include_directories workaround

Signed-off-by: Damian Krolik <[email protected]>

* Fix dependency handling of CHIP libraries

Libraries that are built with GN are then referred to by
CMake targets using "-lLibraryName" flags instead of full
paths. It causes that CMake cannot easily figure out
dependencies on CHIP libraries and the application may not
be re-linked when only CHIP libraries are changed.

Signed-off-by: Damian Krolik <[email protected]>

* Fix build

Signed-off-by: Damian Krolik <[email protected]>
  • Loading branch information
Damian-Nordic authored and pull[bot] committed Mar 7, 2023
1 parent 977cf79 commit 8a7d10c
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 77 deletions.
92 changes: 45 additions & 47 deletions config/nrfconnect/chip-module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ if (NOT CHIP_ROOT)
get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../.. REALPATH)
endif()

set(GN_ROOT_TARGET ${CHIP_ROOT}/config/nrfconnect/chip-gn)
set(CHIP_LIB_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib)
set(CHIP_GN_ROOT_TARGET ${CHIP_ROOT}/config/nrfconnect/chip-gn)

# Prepare compiler flags

Expand All @@ -105,16 +106,16 @@ if (CONFIG_POSIX_API)
endif()

if (CONFIG_NORDIC_SECURITY_BACKEND)
zephyr_include_directories_raw($<TARGET_PROPERTY:mbedtls_external,INTERFACE_INCLUDE_DIRECTORIES>)
zephyr_include_directories_raw($<TARGET_PROPERTY:mbedcrypto_common,INTERFACE_INCLUDE_DIRECTORIES>)
zephyr_include_directories($<TARGET_PROPERTY:mbedtls_external,INTERFACE_INCLUDE_DIRECTORIES>)
zephyr_include_directories($<TARGET_PROPERTY:mbedcrypto_common,INTERFACE_INCLUDE_DIRECTORIES>)
if(TARGET platform_cc3xx)
zephyr_include_directories_raw($<TARGET_PROPERTY:platform_cc3xx,INTERFACE_INCLUDE_DIRECTORIES>)
zephyr_include_directories($<TARGET_PROPERTY:platform_cc3xx,INTERFACE_INCLUDE_DIRECTORIES>)
endif()
list(APPEND CHIP_CFLAGS -DMBEDTLS_CONFIG_FILE=<nrf-config.h>)
endif()

if (CONFIG_NRF_802154_RADIO_DRIVER)
zephyr_include_directories_raw($<TARGET_PROPERTY:nrf-802154-driver-interface,INTERFACE_INCLUDE_DIRECTORIES>)
zephyr_include_directories($<TARGET_PROPERTY:nrf-802154-driver-interface,INTERFACE_INCLUDE_DIRECTORIES>)
endif()

zephyr_get_compile_flags(CHIP_CFLAGS_C C)
Expand All @@ -131,6 +132,15 @@ if (NOT CHIP_LIBRARIES)
set(CHIP_LIBRARIES -lCHIP)
endif()

if (CONFIG_CHIP_EXAMPLE_DEVICE_INFO_PROVIDER)
list(APPEND CHIP_LIBRARIES -lMatterDeviceInfoProviderExample)
endif()

list(TRANSFORM CHIP_LIBRARIES REPLACE
"-l(.*)"
"${CHIP_LIB_DIR}/lib\\1.a"
)

# Set up CHIP project configuration file

if (CONFIG_CHIP_PROJECT_CONFIG)
Expand Down Expand Up @@ -167,30 +177,20 @@ get_property(CHIP_COMPILER_LAUNCHER GLOBAL PROPERTY RULE_LAUNCH_COMPILE)

# Find required programs

find_program(GN_EXECUTABLE gn)
if (${GN_EXECUTABLE} STREQUAL GN_EXECUTABLE-NOTFOUND)
message(FATAL_ERROR "The 'gn' command was not found. Make sure you have GN installed.")
else()
# Parse the 'gn --version' output to find the installed version.
set(MIN_GN_VERSION 1851)
execute_process(
COMMAND
${GN_EXECUTABLE} --version
OUTPUT_VARIABLE gn_version_output
ERROR_VARIABLE gn_error_output
RESULT_VARIABLE gn_status
)
find_package(Python3 REQUIRED)
find_program(GN_EXECUTABLE gn REQUIRED)

if(${gn_status} EQUAL 0)
if(gn_version_output VERSION_LESS ${MIN_GN_VERSION})
message(FATAL_ERROR "Found unsuitable version of 'gn'. Required is at least ${MIN_GN_VERSION}")
endif()
else()
message(FATAL_ERROR "Could NOT find working gn: Found gn (${GN_EXECUTABLE}), but failed to load with:\n ${gn_error_output}")
endif()
endif()
# Parse the 'gn --version' output to find the installed version.

find_package(Python3 REQUIRED)
set(MIN_GN_VERSION 1851)
execute_process(
COMMAND ${GN_EXECUTABLE} --version
OUTPUT_VARIABLE GN_VERSION
COMMAND_ERROR_IS_FATAL ANY
)
if (GN_VERSION VERSION_LESS MIN_GN_VERSION)
message(FATAL_ERROR "Found unsupported version of gn: ${MIN_GN_VERSION}+ is required")
endif()

# ==============================================================================
# Generate configuration for CHIP GN build system
Expand All @@ -217,20 +217,20 @@ chip_gn_arg_bool ("chip_build_libshell" CONFIG_CHIP_LIB_SHEL
chip_gn_arg_bool ("chip_error_logging" CONFIG_MATTER_LOG_LEVEL GREATER_EQUAL 1)
chip_gn_arg_bool ("chip_progress_logging" CONFIG_MATTER_LOG_LEVEL GREATER_EQUAL 3)
chip_gn_arg_bool ("chip_detail_logging" CONFIG_MATTER_LOG_LEVEL GREATER_EQUAL 4)
chip_gn_arg_bool ("chip_automation_logging" "false")
chip_gn_arg_bool ("chip_automation_logging" FALSE)
chip_gn_arg_bool ("chip_malloc_sys_heap" CONFIG_CHIP_MALLOC_SYS_HEAP)
chip_gn_arg_bool ("chip_enable_wifi" CONFIG_WIFI_NRF700X)

if (CONFIG_CHIP_FACTORY_DATA)
chip_gn_arg_bool ("chip_use_transitional_commissionable_data_provider" "false")
chip_gn_arg_bool ("chip_enable_factory_data" "true")
chip_gn_arg_bool("chip_use_transitional_commissionable_data_provider" FALSE)
chip_gn_arg_bool("chip_enable_factory_data" TRUE)
elseif (CONFIG_CHIP_FACTORY_DATA_CUSTOM_BACKEND)
chip_gn_arg_bool ("chip_use_transitional_commissionable_data_provider" "false")
chip_gn_arg_bool("chip_use_transitional_commissionable_data_provider" FALSE)
endif()

if (CONFIG_CHIP_ROTATING_DEVICE_ID)
chip_gn_arg_bool("chip_enable_rotating_device_id" "true")
chip_gn_arg_bool("chip_enable_additional_data_advertising" "true")
chip_gn_arg_bool("chip_enable_rotating_device_id" TRUE)
chip_gn_arg_bool("chip_enable_additional_data_advertising" TRUE)
endif()

if (CONFIG_NET_L2_OPENTHREAD)
Expand Down Expand Up @@ -272,8 +272,7 @@ if (CONFIG_CHIP_PW_RPC)
endif()

if (CONFIG_CHIP_EXAMPLE_DEVICE_INFO_PROVIDER)
chip_gn_arg_bool("chip_build_example_providers" "true")
list(APPEND CHIP_LIBRARIES -lMatterDeviceInfoProviderExample)
chip_gn_arg_bool("chip_build_example_providers" TRUE)
endif()

file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/args.tmp" CONTENT ${CHIP_GN_ARGS})
Expand All @@ -291,16 +290,15 @@ ExternalProject_Add(
@args.tmp > args.gn &&
${GN_EXECUTABLE}
--root=${CHIP_ROOT}
--root-target=${GN_ROOT_TARGET}
--dotfile=${GN_ROOT_TARGET}/.gn
--root-target=${CHIP_GN_ROOT_TARGET}
--dotfile=${CHIP_GN_ROOT_TARGET}/.gn
--script-executable=${Python3_EXECUTABLE}
--export-compile-commands
gen --check --fail-on-unused-args . &&
ninja
INSTALL_COMMAND ""
BUILD_BYPRODUCTS ${CHIP_LIBRARIES}
BUILD_ALWAYS TRUE
USES_TERMINAL_CONFIGURE TRUE
USES_TERMINAL_BUILD TRUE
)
add_dependencies(chip-gn kernel)
Expand All @@ -314,21 +312,24 @@ target_compile_definitions(chip INTERFACE CHIP_HAVE_CONFIG_H)
target_include_directories(chip INTERFACE
${CHIP_ROOT}/src
${CHIP_ROOT}/src/include
${CHIP_ROOT}/src/lib
${CHIP_ROOT}/third_party/nlassert/repo/include
${CHIP_ROOT}/third_party/nlio/repo/include
${CHIP_ROOT}/zzz_generated/app-common
${CMAKE_CURRENT_BINARY_DIR}/gen/include
)
target_link_directories(chip INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/lib)
if (CONFIG_CHIP_LIB_SHELL)
target_link_options(chip INTERFACE -Wl,--whole-archive -lCHIPShell -Wl,--no-whole-archive)
endif()

if (CONFIG_CHIP_EXAMPLE_DEVICE_INFO_PROVIDER)
target_include_directories(chip INTERFACE ${CHIP_ROOT}/examples/providers)
endif()

if (CONFIG_CHIP_LIB_SHELL)
target_link_options(chip INTERFACE -Wl,--whole-archive ${CHIP_LIB_DIR}/libCHIPShell.a -Wl,--no-whole-archive)
endif()

if (CONFIG_CHIP_BUILD_TESTS)
target_link_options(chip INTERFACE -Wl,--whole-archive ${CHIP_LIB_DIR}/libCHIP_tests.a -Wl,--no-whole-archive)
endif()

if (CONFIG_CHIP_MALLOC_SYS_HEAP_OVERRIDE)
target_link_options(chip INTERFACE
-Wl,--wrap=malloc
Expand All @@ -351,20 +352,17 @@ add_dependencies(chip chip-gn)
# ==============================================================================

if (CONFIG_CHIP_OTA_IMAGE_BUILD)

chip_ota_image(chip-ota-image
INPUT_FILES ${PROJECT_BINARY_DIR}/dfu_multi_image.bin
OUTPUT_FILE ${PROJECT_BINARY_DIR}/${CONFIG_CHIP_OTA_IMAGE_FILE_NAME}
)

add_dependencies(chip-ota-image dfu_multi_image_pkg)
endif()

# ==============================================================================
# Define 'factory_data' target for generating a factory data partition
# ==============================================================================

if(CONFIG_CHIP_FACTORY_DATA_BUILD)
if (CONFIG_CHIP_FACTORY_DATA_BUILD)
nrfconnect_generate_factory_data()
endif()

Expand Down
2 changes: 2 additions & 0 deletions config/telink/chip-module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ if (CONFIG_CHIP_OTA_IMAGE_BUILD)
add_custom_target(west_sign ALL
COMMAND
west sign -t imgtool -p ${ZEPHYR_BASE}/../bootloader/mcuboot/scripts/imgtool.py -d ${PROJECT_BINARY_DIR}/.. -- --pad-header --key ${ZEPHYR_BASE}/../bootloader/mcuboot/root-rsa-2048.pem
BYPRODUCTS
${PROJECT_BINARY_DIR}/zephyr.signed.bin
)

add_custom_target(merge_mcuboot ALL
Expand Down
12 changes: 9 additions & 3 deletions config/zephyr/ota-image.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ find_package(Python3 REQUIRED)

#
# Create CMake target for building Matter OTA (Over-the-air update) image.
#
# Required arguments:
# INPUT_FILES file1, [file2...] - binary files which Matter OTA image will be composed of
# OUTPUT_FILE file - where to store newly created Matter OTA image
# INPUT_FILES file1[, file2...] Binary files to be included in Matter OTA image
# OUTPUT_FILE file Location of generated Matter OTA image
#
function(chip_ota_image TARGET_NAME)
cmake_parse_arguments(ARG "" "OUTPUT_FILE" "INPUT_FILES" ${ARGN})
Expand Down Expand Up @@ -58,7 +59,12 @@ function(chip_ota_image TARGET_NAME)
CONTENT ${OTA_ARGS}
)

add_custom_target(${TARGET_NAME} ALL
add_custom_command(OUTPUT ${ARG_OUTPUT_FILE}
COMMAND ${Python3_EXECUTABLE} ${CHIP_ROOT}/src/app/ota_image_tool.py create @${ARG_OUTPUT_FILE}.args
DEPENDS ${ARG_INPUT_FILES} ${CHIP_ROOT}/src/app/ota_image_tool.py
)

add_custom_target(${TARGET_NAME} ALL
DEPENDS ${ARG_OUTPUT_FILE}
)
endfunction()
38 changes: 22 additions & 16 deletions config/zephyr/zephyr-util.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
# CMake utilities for managing and retrieving Zephyr build configuration
#

# Retrieve Zephyr compiler flags for the given language (C or CXX)
#
# Retrieve Zephyr compiler flags for the given language.
#
# Arguments:
# VAR Name of list variable in the parent scope to be appended with the result
# LANG Programming language: C or CXX
#
function(zephyr_get_compile_flags VAR LANG)
# We want to treat all zephyr-provided headers as system headers, so
# warnings in them don't trigger -Werror. That means that for the headers
# zephyr returns as "non-system" headers (the ones from
# zephyr_get_include_directories_for_lang) we need to manually replace "-I"
# with "-isystem".
# Replace "-I" with "-isystem" to treat all Zephyr headers as system headers
# that do not trigger -Werror.
zephyr_get_include_directories_for_lang_as_string(${LANG} INCLUDES)
string(REGEX REPLACE "(^| )-I" "\\1-isystem" INCLUDES ${INCLUDES})
zephyr_get_system_include_directories_for_lang_as_string(${LANG} SYSTEM_INCLUDES)
Expand All @@ -34,14 +37,12 @@ function(zephyr_get_compile_flags VAR LANG)
set(${VAR} ${INCLUDES} ${SYSTEM_INCLUDES} ${DEFINES} ${FLAGS} ${${VAR}} PARENT_SCOPE)
endfunction()

# Add include directories to the zephyr interface target.
# It works like zephyr_include_directories(), doesn't prepend the directories with
# the current directory. Hence it works correctly with generator expressions, too.
function(zephyr_include_directories_raw)
target_include_directories(zephyr_interface INTERFACE ${ARGN})
endfunction()

# Select gnu++<YY> standard based on Kconfig configuration
#
# Select gnu++<YY> standard matching C++ standard configured via Kconfig.
#
# Arguments:
# VAR Name of list variable to be appended with the selected "-std=gnu++<YY>" flag
#
macro(zephyr_get_gnu_cpp_standard VAR)
if (CONFIG_STD_CPP11)
list(APPEND ${VAR} -std=gnu++11)
Expand All @@ -67,11 +68,16 @@ function(zephyr_set_openthread_config_impl OT_DIR CONFIG_FILE)
set_property(DIRECTORY ${OT_DIR} PROPERTY COMPILE_DEFINITIONS ${DEFINES})

foreach(SUBDIR ${SUBDIRS})
zephyr_set_openthread_config_impl(${SUBDIR} ${CONFIG_FILE})
zephyr_set_openthread_config_impl(${SUBDIR} ${CONFIG_FILE})
endforeach()
endfunction()

# Replace Zephyr-supplied configuration file with a custom one
#
# Replace Zephyr-supplied configuration file with a custom one.
#
# Arguments:
# CONFIG_PATH Path to OpenThread configuration file
#
function(zephyr_set_openthread_config CONFIG_PATH)
get_filename_component(CONFIG_DIR ${CONFIG_PATH} DIRECTORY)
get_filename_component(CONFIG_FILE ${CONFIG_PATH} NAME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "AppEvent.h"
#include "LEDWidget.h"

#include <core/CHIPError.h>
#include <platform/CHIPDeviceLayer.h>

#if CONFIG_CHIP_FACTORY_DATA
Expand Down
10 changes: 0 additions & 10 deletions src/test_driver/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,6 @@ set(CHIP_CFLAGS
-isystem$ENV{ZEPHYR_BASE}/../mbedtls/include
)

set(CHIP_TESTS
-lCHIP_tests
)

set(CHIP_LIBRARIES
-lCHIP
${CHIP_TESTS}
)

# Load NCS/Zephyr build system
list(APPEND ZEPHYR_EXTRA_MODULES ${CHIP_ROOT}/config/nrfconnect/chip-module)
find_package(Zephyr HINTS $ENV{ZEPHYR_BASE})
Expand All @@ -68,7 +59,6 @@ project(AllChipTests)
enable_testing()

target_sources(app PRIVATE main/runner.cpp)
target_link_options(app PUBLIC -Wl,--whole-archive ${CHIP_TESTS} -Wl,--no-whole-archive)
target_link_libraries(app PUBLIC chip $<TARGET_FILE:kernel>)
target_compile_definitions(app PUBLIC CHIP_HAVE_CONFIG_H)

Expand Down

0 comments on commit 8a7d10c

Please sign in to comment.