From 54041178ce676a8407bad6abc2cd8f32b0472dad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20Kr=C3=B3lik?= <66667989+Damian-Nordic@users.noreply.github.com> Date: Mon, 5 Dec 2022 16:37:58 +0100 Subject: [PATCH] [nrfconnect] CMake improvements and cleanup (#23900) * 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 * Remove no longer needed zephyr_include_directories workaround Signed-off-by: Damian Krolik * 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 * Fix build Signed-off-by: Damian Krolik --- config/nrfconnect/chip-module/CMakeLists.txt | 92 +++++++++---------- config/telink/chip-module/CMakeLists.txt | 2 + config/zephyr/ota-image.cmake | 12 ++- config/zephyr/zephyr-util.cmake | 38 ++++---- .../nrfconnect/main/include/AppTask.h | 1 - src/test_driver/nrfconnect/CMakeLists.txt | 10 -- 6 files changed, 78 insertions(+), 77 deletions(-) diff --git a/config/nrfconnect/chip-module/CMakeLists.txt b/config/nrfconnect/chip-module/CMakeLists.txt index 68e0e31d19abb5..cef54d4bd014f6 100644 --- a/config/nrfconnect/chip-module/CMakeLists.txt +++ b/config/nrfconnect/chip-module/CMakeLists.txt @@ -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 @@ -105,16 +106,16 @@ if (CONFIG_POSIX_API) endif() if (CONFIG_NORDIC_SECURITY_BACKEND) - zephyr_include_directories_raw($) - zephyr_include_directories_raw($) + zephyr_include_directories($) + zephyr_include_directories($) if(TARGET platform_cc3xx) - zephyr_include_directories_raw($) + zephyr_include_directories($) endif() list(APPEND CHIP_CFLAGS -DMBEDTLS_CONFIG_FILE=) endif() if (CONFIG_NRF_802154_RADIO_DRIVER) - zephyr_include_directories_raw($) + zephyr_include_directories($) endif() zephyr_get_compile_flags(CHIP_CFLAGS_C C) @@ -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) @@ -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 @@ -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) @@ -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}) @@ -291,8 +290,8 @@ 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 . && @@ -300,7 +299,6 @@ ExternalProject_Add( INSTALL_COMMAND "" BUILD_BYPRODUCTS ${CHIP_LIBRARIES} BUILD_ALWAYS TRUE - USES_TERMINAL_CONFIGURE TRUE USES_TERMINAL_BUILD TRUE ) add_dependencies(chip-gn kernel) @@ -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 @@ -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() diff --git a/config/telink/chip-module/CMakeLists.txt b/config/telink/chip-module/CMakeLists.txt index b2f3da1eb9d135..6b1211134ddfd9 100644 --- a/config/telink/chip-module/CMakeLists.txt +++ b/config/telink/chip-module/CMakeLists.txt @@ -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 diff --git a/config/zephyr/ota-image.cmake b/config/zephyr/ota-image.cmake index 568cbd19faa9f6..bd7735648f8302 100644 --- a/config/zephyr/ota-image.cmake +++ b/config/zephyr/ota-image.cmake @@ -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}) @@ -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() diff --git a/config/zephyr/zephyr-util.cmake b/config/zephyr/zephyr-util.cmake index bab824dc0529d5..0d634914ee73fb 100644 --- a/config/zephyr/zephyr-util.cmake +++ b/config/zephyr/zephyr-util.cmake @@ -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) @@ -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++ standard based on Kconfig configuration +# +# Select gnu++ standard matching C++ standard configured via Kconfig. +# +# Arguments: +# VAR Name of list variable to be appended with the selected "-std=gnu++" flag +# macro(zephyr_get_gnu_cpp_standard VAR) if (CONFIG_STD_CPP11) list(APPEND ${VAR} -std=gnu++11) @@ -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) diff --git a/examples/light-switch-app/nrfconnect/main/include/AppTask.h b/examples/light-switch-app/nrfconnect/main/include/AppTask.h index 6d464638f11e7a..9bd5ac4f5d98c0 100644 --- a/examples/light-switch-app/nrfconnect/main/include/AppTask.h +++ b/examples/light-switch-app/nrfconnect/main/include/AppTask.h @@ -21,7 +21,6 @@ #include "AppEvent.h" #include "LEDWidget.h" -#include #include #if CONFIG_CHIP_FACTORY_DATA diff --git a/src/test_driver/nrfconnect/CMakeLists.txt b/src/test_driver/nrfconnect/CMakeLists.txt index 7d145d6df9e0ed..39648928a5dde2 100644 --- a/src/test_driver/nrfconnect/CMakeLists.txt +++ b/src/test_driver/nrfconnect/CMakeLists.txt @@ -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}) @@ -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_compile_definitions(app PUBLIC CHIP_HAVE_CONFIG_H)