diff --git a/config/common/cmake/chip_gn.cmake b/config/common/cmake/chip_gn.cmake new file mode 100644 index 00000000000000..bfaa91ab28200b --- /dev/null +++ b/config/common/cmake/chip_gn.cmake @@ -0,0 +1,176 @@ +# +# Copyright (c) 2023 Project CHIP Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# +# @file +# CMake file defining to setup and build the Matter library +# and other optional libraries like unit tests. +# Matter headers and libraries are exposed to the application +# as a specific interface target. +# Since Matter doesn't provide native CMake support, ExternalProject +# module is used to build the required artifacts with GN meta-build +# system. +# + +# ============================================================================== +# Validate paths +# ============================================================================== +if (NOT GN_ROOT_TARGET) + message(FATAL_ERROR "GN_ROOT_TARGET not defined. Please provide the path to your Matter GN project.") +endif() + +if (NOT CHIP_ROOT) + get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_LIST_DIR}/../../.. REALPATH) +endif() + +# ============================================================================== +# Find required programs +# ============================================================================== +find_package(Python3 REQUIRED) +find_program(GN_EXECUTABLE gn REQUIRED) + +# 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 + 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() + +# ============================================================================== +# Macros +# ============================================================================== +# Setup and build the Matter library and other optional libraries like unit tests. +# Expose Matter headers & libraries to the application as specific +# interface target. +# [Args]: +# target - interface target name +# Available options are: +# LIB_SHELL Build and add Matter shell library +# LIB_PW_RPC Build and add Matter PW RPC library +# LIB_TESTS Build and add Matter unit tests library +# DEVICE_INFO_EXAMPLE_PROVIDER Add example device info provider support +# +# GN_DEPENDENCIES List of targets that should be built before Matter GN project +macro(matter_build target) + set(options) + set(oneValueArgs + LIB_TESTS + LIB_SHELL + LIB_PW_RPC + DEVICE_INFO_EXAMPLE_PROVIDER + ) + set(multiValueArgs GN_DEPENDENCIES) + + cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + set(MATTER_LIB_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib) + + # Prepare Matter libraries that the application should be linked with + set(MATTER_LIBRARIES -lCHIP) + + if (ARG_LIB_SHELL) + list(APPEND MATTER_LIBRARIES -lCHIPShell) + endif() + + if (ARG_LIB_PW_RPC) + list(APPEND MATTER_LIBRARIES -lPwRpc) + endif() + + if (ARG_DEVICE_INFO_EXAMPLE_PROVIDER) + list(APPEND MATTER_LIBRARIES -lMatterDeviceInfoProviderExample) + endif() + + list(TRANSFORM MATTER_LIBRARIES REPLACE + "-l(.*)" + "${MATTER_LIB_DIR}/lib\\1.a" + ) + + # ============================================================================== + # Define 'chip-gn' target that builds CHIP library(ies) with GN build system + # ============================================================================== + ExternalProject_Add( + chip-gn + PREFIX ${CMAKE_CURRENT_BINARY_DIR} + SOURCE_DIR ${CHIP_ROOT} + BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} + CONFIGURE_COMMAND "" + CONFIGURE_HANDLED_BY_BUILD TRUE + BUILD_COMMAND ${CMAKE_COMMAND} -E echo "Starting Matter library build in ${CMAKE_CURRENT_BINARY_DIR}" + COMMAND ${Python3_EXECUTABLE} ${CHIP_ROOT}/config/common/cmake/make_gn_args.py @args.tmp > args.gn.tmp + # Replace the config only if it has changed to avoid triggering unnecessary rebuilds + COMMAND bash -c "(! diff -q args.gn.tmp args.gn && mv args.gn.tmp args.gn) || true" + # Regenerate the ninja build system + COMMAND ${GN_EXECUTABLE} + --root=${CHIP_ROOT} + --root-target=${GN_ROOT_TARGET} + --dotfile=${GN_ROOT_TARGET}/.gn + --script-executable=${Python3_EXECUTABLE} + gen --check --fail-on-unused-args ${CMAKE_CURRENT_BINARY_DIR} + COMMAND ninja + COMMAND ${CMAKE_COMMAND} -E echo "Matter library build complete" + INSTALL_COMMAND "" + # Byproducts are removed by the clean target removing config and .ninja_deps + # allows a rebuild of the external project after the clean target has been run. + BUILD_BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/args.gn + ${CMAKE_CURRENT_BINARY_DIR}/build.ninja + ${CMAKE_CURRENT_BINARY_DIR}/.ninja_deps + ${CMAKE_CURRENT_BINARY_DIR}/build.ninja.stamp + ${MATTER_LIBRARIES} + BUILD_ALWAYS TRUE + USES_TERMINAL_CONFIGURE TRUE + USES_TERMINAL_BUILD TRUE + ) + + if(ARG_GN_DEPENDENCIES) + add_dependencies(chip-gn ${ARG_GN_DEPENDENCIES}) + endif() + + # ============================================================================== + # Define target that exposes Matter headers & libraries to the application + # ============================================================================== + add_library(${target} INTERFACE) + target_compile_definitions(${target} INTERFACE CHIP_HAVE_CONFIG_H) + target_include_directories(${target} INTERFACE + ${CHIP_ROOT}/src + ${CHIP_ROOT}/src/include + ${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 + ) + + # ============================================================================== + # Link required libraries + # ============================================================================== + target_link_libraries(${target} INTERFACE -Wl,--start-group ${MATTER_LIBRARIES} -Wl,--end-group) + add_dependencies(${target} chip-gn) + + if (ARG_LIB_SHELL) + target_link_options(${target} INTERFACE -Wl,--whole-archive ${MATTER_LIB_DIR}/libCHIPShell.a -Wl,--no-whole-archive) + endif() + + if (ARG_LIB_TESTS) + target_link_options(${target} INTERFACE -Wl,--whole-archive ${MATTER_LIB_DIR}/libCHIP_tests.a -Wl,--no-whole-archive) + endif() + + if (ARG_DEVICE_INFO_EXAMPLE_PROVIDER) + target_include_directories(${target} INTERFACE ${CHIP_ROOT}/examples/providers) + endif() +endmacro() diff --git a/config/common/cmake/chip_gn_args.cmake b/config/common/cmake/chip_gn_args.cmake new file mode 100644 index 00000000000000..5c52d1f8439eb0 --- /dev/null +++ b/config/common/cmake/chip_gn_args.cmake @@ -0,0 +1,204 @@ +# +# Copyright (c) 2023 Project CHIP Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# +# @file +# CMake file that allows collecting C/C++ compiler flags passed to +# the Matter build system. +# + +include(${CMAKE_CURRENT_LIST_DIR}/util.cmake) + +# ============================================================================== +# Configuration variables and define constants +# ============================================================================== + +# C/C++ compiler flags passed to the Matter build system +if (NOT MATTER_CFLAGS) + set(MATTER_CFLAGS PARENT_SCOPE) +endif() + +# C compiler flags passed to the Matter build system +if (NOT MATTER_CFLAGS_C) + set(MATTER_CFLAGS_C PARENT_SCOPE) +endif() + +# C++ compiler flags passed to the Matter build system +if (NOT MATTER_CFLAGS_CC) + set(MATTER_CFLAGS_CC PARENT_SCOPE) +endif() + +# GN meta-build system arguments in the form of 'key1 = value1\nkey2 = value2...' string +if (NOT MATTER_GN_ARGS) + set(MATTER_GN_ARGS PARENT_SCOPE) +endif() + +# ============================================================================== +# Macros +# ============================================================================== +# Add import GN argument +# [Args]: +# file - path to file that should be imported +macro(matter_add_gn_arg_import file) + string(APPEND MATTER_GN_ARGS "--module\n${file}\n") +endmacro() + +# Add string GN argument +# [Args]: +# key - key name +# value - string value +macro(matter_add_gn_arg_string key value) + string(APPEND MATTER_GN_ARGS "--arg-string\n${key}\n${value}\n") +endmacro() + +# Add bool GN argument +# [Args]: +# key - bool variable +macro(matter_add_gn_arg_bool key) + if (${ARGN}) + string(APPEND MATTER_GN_ARGS "--arg\n${key}\ntrue\n") + else() + string(APPEND MATTER_GN_ARGS "--arg\n${key}\nfalse\n") + endif() +endmacro() + +# Add compiler flag GN argument +# [Args]: +# key - key name +# value - compiler flag value +macro(matter_add_gn_arg_cflags key value) + string(APPEND MATTER_GN_ARGS "--arg-cflags\n${key}\n${value}\n") +endmacro() + +# Add simple variable GN argument +# [Args]: +# key - variable name +# value - variable value +macro(matter_add_gn_arg key value) + string(APPEND MATTER_GN_ARGS "--arg\n${key}\n${value}\n") +endmacro() + +# Add items to Matter common compiler flags +# [Args]: +# flags - flags to add +macro(matter_add_flags flags) + list(APPEND MATTER_CFLAGS ${flags}) +endmacro() + +# Add items to Matter C compiler flags +# [Args]: +# flags - flags to add +macro(matter_add_cflags flags) + list(APPEND MATTER_CFLAGS_C ${flags}) +endmacro() + +# Add items to Matter CXX compiler flags +# [Args]: +# flags - flags to add +macro(matter_add_cxxflags flags) + list(APPEND MATTER_CFLAGS_CC ${flags}) +endmacro() + +# Add GNU CPP standard flag to Matter CXX compiler flags +# [Args]: +# std_version - standard version number e.g. 17 for C++17 +macro(matter_add_gnu_cpp_standard std_version) + list(APPEND MATTER_CFLAGS_CC -std=gnu++${std_version}) +endmacro() + +# Get compiler flags from listed targets. +# Collect common compile flags and save them in MATTER_CFLAGS +# Collect C/CXX compile flags and save them in MATTER_CFLAGS_C/MATTER_CFLAGS_CXX +# [Args]: +# targets - list of targets +macro(matter_get_compiler_flags_from_targets targets) + foreach(target ${targets}) + get_target_common_compile_flags(EXTERNAL_TARGET_CFLAGS ${target}) + get_lang_compile_flags(EXTERNAL_TARGET_CFLAGS_C ${target} C) + get_lang_compile_flags(EXTERNAL_TARGET_CFLAGS_CXX ${target} CXX) + list(APPEND MATTER_CFLAGS ${EXTERNAL_TARGET_CFLAGS}) + list(APPEND MATTER_CFLAGS_C ${EXTERNAL_TARGET_CFLAGS_C}) + list(APPEND MATTER_CFLAGS_CC ${EXTERNAL_TARGET_CFLAGS_CXX}) + # Reset between targets + set(EXTERNAL_TARGET_CFLAGS "") + set(EXTERNAL_TARGET_CFLAGS_C "") + set(EXTERNAL_TARGET_CFLAGS_CXX "") + endforeach() +endmacro() + +# Generate the common Matter GN arguments. +# Pass all compilation flags to GN build. +# Available options are: +# DEBUG Debug build +# LIB_SHELL Add Matter shell library +# LIB_PW_RPC Add Matter PW RPC library +# LIB_TESTS Add Matter unit tests library +# DEVICE_INFO_EXAMPLE_PROVIDER Add example device info provider support +# PROJECT_CONFIG Path to the project-specific configuration file +# +macro(matter_common_gn_args) + set(options) + set(oneValueArgs + DEBUG + LIB_TESTS + LIB_SHELL + LIB_PW_RPC + DEVICE_INFO_EXAMPLE_PROVIDER + PROJECT_CONFIG + ) + set(multiValueArgs) + + cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + convert_list_of_flags_to_string_of_flags(MATTER_CFLAGS MATTER_CFLAGS) + convert_list_of_flags_to_string_of_flags(MATTER_CFLAGS_C MATTER_CFLAGS_C) + convert_list_of_flags_to_string_of_flags(MATTER_CFLAGS_CC MATTER_CFLAGS_CC) + + if (MATTER_CFLAGS) + matter_add_gn_arg_cflags ("target_cflags" ${MATTER_CFLAGS}) + endif() # MATTER_CFLAGS + if (MATTER_CFLAGS_C) + matter_add_gn_arg_cflags ("target_cflags_c" ${MATTER_CFLAGS_C}) + endif() # MATTER_CFLAGS_C + if (MATTER_CFLAGS_CC) + matter_add_gn_arg_cflags ("target_cflags_cc" ${MATTER_CFLAGS_CC}) + endif() # MATTER_CFLAGS_CC + + matter_add_gn_arg_bool ("is_debug" ${ARG_DEBUG}) + matter_add_gn_arg_bool ("chip_build_tests" ${ARG_LIB_TESTS}) + matter_add_gn_arg_bool ("chip_build_libshell" ${ARG_LIB_SHELL}) + + if (ARG_LIB_PW_RPC) + matter_add_gn_arg_bool ("chip_build_pw_rpc_lib" ${ARG_LIB_PW_RPC}) + endif() # ARG_LIB_PW_RPC + if (ARG_DEVICE_INFO_EXAMPLE_PROVIDER) + matter_add_gn_arg_bool ("chip_build_example_providers" ${ARG_DEVICE_INFO_EXAMPLE_PROVIDER}) + endif() # ARG_DEVICE_INFO_EXAMPLE_PROVIDER + if (ARG_PROJECT_CONFIG) + get_filename_component(PROJECT_CONFIG + ${ARG_PROJECT_CONFIG} + REALPATH + BASE_DIR ${CMAKE_SOURCE_DIR} + ) + matter_add_gn_arg_string("chip_project_config_include" "<${PROJECT_CONFIG}>") + matter_add_gn_arg_string("chip_system_project_config_include" "<${PROJECT_CONFIG}>") + endif() # CHIP_PROJECT_CONFIG +endmacro() + +# Generate the temporary GN arguments file from the settings +macro(matter_generate_args_tmp_file) + file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/args.tmp" CONTENT ${MATTER_GN_ARGS}) +endmacro() \ No newline at end of file diff --git a/config/telink/chip-module/make_gn_args.py b/config/common/cmake/make_gn_args.py similarity index 56% rename from config/telink/chip-module/make_gn_args.py rename to config/common/cmake/make_gn_args.py index f30cd459a6e7d4..c6282bc0f8b451 100755 --- a/config/telink/chip-module/make_gn_args.py +++ b/config/common/cmake/make_gn_args.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -# Copyright (c) 2021 Project CHIP Authors +# Copyright (c) 2023 Project CHIP Authors # All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,10 +18,9 @@ # import argparse -import re import sys -GN_SPECIAL_CHARACTERS = r'(["$\\])' +GN_SPECIAL_SEPARATOR = "+|+" GN_CFLAG_EXCLUDES = [ '-fno-asynchronous-unwind-tables', '-fno-common', @@ -35,10 +34,6 @@ ] -def escape_strings(gn_args): - return [[key, re.sub(GN_SPECIAL_CHARACTERS, r'\\\1', value)] for key, value in gn_args] - - def write_gn_args(args): if args.module: sys.stdout.write('import("{}")\n'.format(args.module)) @@ -47,14 +42,31 @@ def write_gn_args(args): sys.stdout.write('{} = {}\n'.format(key, value)) for key, value in args.arg_string: - sys.stdout.write('{} = "{}"\n'.format(key, value)) + # Escaped quote and dollar sign characters + filtered_value = value.replace('"', '\\"') + filtered_value = filtered_value.replace('$', '\\$') + sys.stdout.write('{} = "{}"\n'.format(key, filtered_value)) cflag_excludes = ', '.join(['"{}"'.format(exclude) for exclude in GN_CFLAG_EXCLUDES]) for key, value in args.arg_cflags: - sys.stdout.write('{} = filter_exclude(string_split("{}"), [{}])\n'.format( - key, value, cflag_excludes)) + filtered_value = value.split(" -") + # Remove empty include paths and defines caused by Cmake generator expressions + filtered_value = filter(lambda v: v != "D", filtered_value) + filtered_value = filter(lambda v: v != "isystem", filtered_value) + # Escaped quote and dollar sign characters + filtered_value = map(lambda v: v.replace('"', '\\"'), filtered_value) + filtered_value = map(lambda v: v.replace('$', '\\$'), filtered_value) + # Remove white spaces around the argument and remove internal whitespace + # for correct splitting in string_split() function + filtered_value = map(lambda v: v.strip(), filtered_value) + filtered_value = map(lambda v: v.replace(' ', ''), filtered_value) + # Remove duplicates + filtered_value = list(dict.fromkeys(filtered_value)) + + sys.stdout.write('{} = filter_exclude(string_split("{}", "{}"), [{}])\n'.format( + key, "{}-".format(GN_SPECIAL_SEPARATOR).join(filtered_value), GN_SPECIAL_SEPARATOR, cflag_excludes)) def main(): @@ -64,8 +76,6 @@ def main(): parser.add_argument('--arg-string', action='append', nargs=2, default=[]) parser.add_argument('--arg-cflags', action='append', nargs=2, default=[]) args = parser.parse_args() - args.arg_string = escape_strings(args.arg_string) - args.arg_cflags = escape_strings(args.arg_cflags) write_gn_args(args) diff --git a/config/openiotsdk/util.cmake b/config/common/cmake/util.cmake similarity index 84% rename from config/openiotsdk/util.cmake rename to config/common/cmake/util.cmake index cfa34725f781fa..91988e946fc64b 100644 --- a/config/openiotsdk/util.cmake +++ b/config/common/cmake/util.cmake @@ -1,5 +1,5 @@ # -# Copyright (c) 2022 Project CHIP Authors +# Copyright (c) 2023 Project CHIP Authors # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -19,6 +19,10 @@ # CMake utilities for managing and retrieving build configuration # +# ============================================================================== +# Functions +# ============================================================================== + # Get compilation flags for specific lang # The flags might contains compile language generator expressions that # look like this: @@ -55,39 +59,35 @@ function(get_flags_for_lang lang input output) set(${output} ${tmp_list} PARENT_SCOPE) endfunction() + # Get include directory of target build # Get target property of includes directories # For each flag add -I prefix and put it in quotation marks # [Args]: # target - target name # output - output variable name +# Note: Includes are returned as a generator expression which must be evalued +# at build time. function(get_include_directories target output) get_property(flags TARGET ${target} PROPERTY INTERFACE_INCLUDE_DIRECTORIES) - list(APPEND CFLAG_LIST) - foreach(flag ${flags}) - list(APPEND CFLAG_LIST "\"-isystem${flag}\"") - endforeach() - set(${output} ${CFLAG_LIST} PARENT_SCOPE) + set(${output} "-isystem$, -isystem>" PARENT_SCOPE) endfunction() + # Get compile definitions of target build # Get target property of compile definitions # For each flag change format, add -D prefix and put it in quotation marks # [Args]: # target - target name # output - output variable name +# Note: Definitions are returned as a generator expression which must be evalued +# at build time. function(get_compile_definitions target output) get_property(flags TARGET ${target} PROPERTY INTERFACE_COMPILE_DEFINITIONS) - - list(APPEND CFLAG_LIST) - foreach(flag ${flags}) - # Replace each quote with a '\"' - format required for the GN arguments - string(REPLACE "\"" "\\\\\"" output_flag ${flag}) - list(APPEND CFLAG_LIST "\"-D${output_flag}\"") - endforeach() - set(${output} ${CFLAG_LIST} PARENT_SCOPE) + set(${output} "-D$, -D>" PARENT_SCOPE) endfunction() + # Get compile options of build for specific language # Get property of compile options # [Args]: @@ -111,6 +111,7 @@ function(get_target_common_compile_flags VAR TARGET) set(${VAR} ${INCLUDES} ${DEFINES} ${${VAR}} PARENT_SCOPE) endfunction() + # Retrieve target compiler flags for the specific language (C or CXX) # [Args]: # VAR - flags variable name @@ -121,16 +122,16 @@ function(get_lang_compile_flags VAR TARGET LANG) set(${VAR} ${FLAGS} ${${VAR}} PARENT_SCOPE) endfunction() + # Convert list of flags to string format # [Args]: # ptr_list_of_flags - list fo flags # string_of_flags - output string -# separator - flags separator -function(convert_list_of_flags_to_string_of_flags ptr_list_of_flags string_of_flags separator) +function(convert_list_of_flags_to_string_of_flags ptr_list_of_flags string_of_flags) # Convert the list to a string so we can do string replace # operations on it and replace the ";" list separators with a - # desirable one so the flags are spaced out - string(REPLACE ";" ${separator} locally_scoped_string_of_flags "${${ptr_list_of_flags}}") + # whitespace so the flags are spaced out + string(REPLACE ";" " " locally_scoped_string_of_flags "${${ptr_list_of_flags}}") # Removing excess spaces string(REPLACE " " " " locally_scoped_string_of_flags "${locally_scoped_string_of_flags}") diff --git a/config/mbed/CHIPProjectConfig.h b/config/mbed/CHIPProjectConfig.h deleted file mode 100644 index fea8f05c9672c5..00000000000000 --- a/config/mbed/CHIPProjectConfig.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * - * Copyright (c) 2021 Project CHIP Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * @file - * Project-specific default configuration overrides - */ - -#pragma once - -// ====== Project-specific Configuration Overrides Defaults ===== - -#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION 0 -#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP 0 diff --git a/config/mbed/CMakeLists.txt b/config/mbed/CMakeLists.txt index b50f46e0acc2f3..2a0eb84dda52b3 100644 --- a/config/mbed/CMakeLists.txt +++ b/config/mbed/CMakeLists.txt @@ -1,5 +1,5 @@ # -# Copyright (c) 2020 Project CHIP Authors +# Copyright (c) 2020-2023 Project CHIP Authors # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -16,105 +16,29 @@ # # @file -# CMake sub-project defining 'chip' target which represents CHIP library -# and other optional libraries like unit tests, built with 'mbed' -# platform. -# Since CHIP doesn't provide native CMake support, ExternalProject -# module is used to build the required artifacts with GN meta-build -# system. +# CMake sub-project to configure and build the chip library. # include(ExternalProject) -include(mbed-util.cmake) # ============================================================================== -# Declare configuration variables and define constants -# ============================================================================== -# C/C++ compiler flags passed to CHIP build system -list(APPEND CHIP_CFLAGS) - -# C compiler flags passed to CHIP build system -list(APPEND CHIP_CFLAGS_C) - -# C++ compiler flags passed to CHIP build system -list(APPEND CHIP_CFLAGS_CC) - -# CHIP libraries that the application should be linked with -list(APPEND CHIP_LIBRARIES) - -# GN meta-build system arguments in the form of 'key1 = value1\nkey2 = value2...' string -string(APPEND CHIP_GN_ARGS) - -# C/C++ compiler flags which should not be forwarded to CHIP -# build system (e.g. because CHIP configures them on its own) -set(CHIP_CFLAG_EXCLUDES - "-fno-asynchronous-unwind-tables" - "-fno-common" - "-fno-defer-pop" - "-fno-reorder-functions" - "-ffunction-sections" - "-fdata-sections" - "-g*" - "-O*" - "-W*" -) - -# ============================================================================== -# Helper macros +# Prepare CHIP configuration based on the project configuration # ============================================================================== +# Set paths +if (NOT CHIP_ROOT) + get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../.. REALPATH) +endif() +get_filename_component(GN_ROOT_TARGET ${CHIP_ROOT}/config/mbed/chip-gn REALPATH) +get_filename_component(COMMON_CMAKE_SOURCE_DIR ${CHIP_ROOT}/config/common/cmake REALPATH) -macro(chip_gn_arg_import FILE) - string(APPEND CHIP_GN_ARGS "import(\"${FILE}\")\n") -endmacro() - -macro(chip_gn_arg_string ARG STRING) - string(APPEND CHIP_GN_ARGS "${ARG} = \"${STRING}\"\n") -endmacro() +# Get common Cmake sources +include(${COMMON_CMAKE_SOURCE_DIR}/util.cmake) +include(${COMMON_CMAKE_SOURCE_DIR}/chip_gn_args.cmake) +include(${COMMON_CMAKE_SOURCE_DIR}/chip_gn.cmake) -macro(chip_gn_arg_bool ARG BOOLEAN) - if (${BOOLEAN}) - string(APPEND CHIP_GN_ARGS "${ARG} = true\n") - else() - string(APPEND CHIP_GN_ARGS "${ARG} = false\n") - endif() -endmacro() - -macro(chip_gn_arg_flags ARG CFLAGS) - list(SORT CFLAGS) - string(APPEND CHIP_GN_ARGS "${ARG} = [${CFLAGS}]\n") -endmacro() - -macro(chip_gn_arg_lang_flags ARG CFLAGS) - list(SORT CFLAGS) - list(SORT CHIP_CFLAG_EXCLUDES) - set(CFLAG_EXCLUDES "[") - foreach(cflag ${CHIP_CFLAG_EXCLUDES}) - string(APPEND CFLAG_EXCLUDES "\"${cflag}\", ") - endforeach() - string(APPEND CFLAG_EXCLUDES "]") - string(APPEND CHIP_GN_ARGS "${ARG} = filter_exclude(string_split(\"${CFLAGS}\"), ${CFLAG_EXCLUDES})\n") -endmacro() - -macro(mbed_interface_library_named name) - add_library(${name} INTERFACE) -endmacro() - -# Select gnu++ standard based on project configuration -macro(mbed_get_gnu_cpp_standard VAR) - if (CONFIG_STD_CPP11) - list(APPEND ${VAR} -std=gnu++11) - elseif (CONFIG_STD_CPP14) - list(APPEND ${VAR} -std=gnu++14) - elseif (CONFIG_STD_CPP17) - list(APPEND ${VAR} -std=gnu++17) - elseif (CONFIG_STD_CPP2A) - list(APPEND ${VAR} -std=gnu++20) - endif() -endmacro() +# Mbed targets passed to CHIP build +set(CONFIG_CHIP_EXTERNAL_TARGETS) -# ============================================================================== -# Prepare CHIP configuration based on the project configuration -# ============================================================================== # Read configuration file and parse it content to create cmake variable file(STRINGS ${CMAKE_CURRENT_BINARY_DIR}/config ConfigContents) foreach(NameAndValue ${ConfigContents}) @@ -127,202 +51,108 @@ foreach(NameAndValue ${ConfigContents}) # Set the variable set(${Name} "${Value}") endforeach() - if (CONFIG_CHIP_PW_RPC) - set(CONFIG_STD_CPP17 y) -endif() + if (${APP_TARGET} MATCHES "pigweed-app") + set(CONFIG_CHIP_PW_RPC_ECHO_PROTO "y") + elseif (${APP_TARGET} MATCHES "lighting-app") + set(CONFIG_CHIP_PW_RPC_COMMON_PROTO "y") + set(CONFIG_CHIP_PW_RPC_LIGHTING_PROTO "y") + elseif (${APP_TARGET} MATCHES "lock-app") + set(CONFIG_CHIP_PW_RPC_COMMON_PROTO "y") + set(CONFIG_CHIP_PW_RPC_LOCKING_PROTO "y") + endif() +endif(CONFIG_CHIP_PW_RPC) -if (NOT CHIP_ROOT) - get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../.. REALPATH) +if (${CMAKE_BUILD_TYPE} STREQUAL "Debug") + set(CONFIG_CHIP_DEBUG YES) +else() + set(CONFIG_CHIP_DEBUG NO) endif() -set(GN_ROOT_TARGET ${CHIP_ROOT}/config/mbed/chip-gn) - # Prepare compiler flags +matter_add_cflags(${CMAKE_C_FLAGS_INIT}) +matter_add_cxxflags(${CMAKE_CXX_FLAGS_INIT}) -mbed_get_target_common_compile_flags(CHIP_CFLAGS mbed-core) -mbed_get_lang_compile_flags(CHIP_CFLAGS_C mbed-core C) -list(APPEND CHIP_CFLAGS_C ${CMAKE_C_FLAGS_INIT}) -mbed_get_lang_compile_flags(CHIP_CFLAGS_CC mbed-core CXX) -list(APPEND CHIP_CFLAGS_CC ${CMAKE_CXX_FLAGS_INIT}) - -mbed_get_target_common_compile_flags(CHIP_MBEDCMSISCM_CFLAGS mbed-cmsis-cortex-m) -list(APPEND CHIP_CFLAGS ${CHIP_MBEDCMSISCM_CFLAGS}) -if(MBED_TARGET STREQUAL "CY8CPROTO_062_4343W") - mbed_get_target_common_compile_flags(CHIP_MBEDCMSISCY8_CFLAGS mbed-cy8cproto-062-4343w) - list(APPEND CHIP_CFLAGS ${CHIP_MBEDCMSISCY8_CFLAGS}) - mbed_get_target_common_compile_flags(CHIP_MBEDPSOC6_CFLAGS mbed-psoc6) - list(APPEND CHIP_CFLAGS ${CHIP_MBEDPSOC6_CFLAGS}) - mbed_get_target_common_compile_flags(CHIP_MBEDCAT1_CFLAGS mbed-cat1a) - list(APPEND CHIP_CFLAGS ${CHIP_MBEDCAT1_CFLAGS}) - mbed_get_target_common_compile_flags(CHIP_MBEDCMSISCY8MODUS_CFLAGS mbed-cy8cproto-062-4343w-bsp-design-modus) - list(APPEND CHIP_CFLAGS ${CHIP_MBEDCMSISCY8MODUS_CFLAGS}) -endif() - -# Add support for Mbed Posix Socket -mbed_get_target_common_compile_flags(CHIP_MBEDPOSIXSOCKET_CFLAGS mbed-os-posix-socket) -list(APPEND CHIP_CFLAGS ${CHIP_MBEDPOSIXSOCKET_CFLAGS}) - -# Add support for Mbed BLE -mbed_get_target_common_compile_flags(CHIP_MBEDBLE_CFLAGS mbed-ble) -list(APPEND CHIP_CFLAGS ${CHIP_MBEDBLE_CFLAGS}) +# Create a list of external targets passed to CHIP build +list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS + mbed-core + mbed-cmsis-cortex-m -# Add support for Mbed event -mbed_get_target_common_compile_flags(CHIP_MBEDEVENTS_CFLAGS mbed-events) -list(APPEND CHIP_CFLAGS ${CHIP_MBEDEVENTS_CFLAGS}) + mbed-rtos + mbed-events -# Add support for Mbed rtos -mbed_get_target_common_compile_flags(CHIP_MBEDRTOS_CFLAGS mbed-rtos) -list(APPEND CHIP_CFLAGS ${CHIP_MBEDRTOS_CFLAGS}) + mbed-os-posix-socket + mbed-netsocket + mbed-ble -# Add support for Mbed storage -mbed_get_target_common_compile_flags(CHIP_MBEDSTORAGE_CFLAGS mbed-storage-kv-global-api) -list(APPEND CHIP_CFLAGS ${CHIP_MBEDSTORAGE_CFLAGS}) + mbed-mbedtls -# Add support for Mbed Socket -mbed_get_target_common_compile_flags(CHIP_MBEDNETSOCKET_CFLAGS mbed-netsocket) -list(APPEND CHIP_CFLAGS ${CHIP_MBEDNETSOCKET_CFLAGS}) - -if (CONFIG_CHIP_WITH_EXTERNAL_MBEDTLS) - mbed_get_target_common_compile_flags(CHIP_MBEDTLS_CFLAGS mbed-mbedtls) - list(APPEND CHIP_CFLAGS ${CHIP_MBEDTLS_CFLAGS}) -endif() - -mbed_get_gnu_cpp_standard(CHIP_CFLAGS_CC) - -list(APPEND CHIP_CFLAGS - \"-D__LINUX_ERRNO_EXTENSIONS__=1\" + mbed-storage-kv-global-api ) -list(APPEND CHIP_CFLAGS - \"-DCHIP_ADDRESS_RESOLVE_IMPL_INCLUDE_HEADER=\" -) - -if (CONFIG_MBED_BSD_SOCKET_TRACE) - list(APPEND CHIP_CFLAGS - \"-DMBED_BSD_SOCKET_TRACE=1\" +if(MBED_TARGET STREQUAL "CY8CPROTO_062_4343W") + list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS + mbed-cy8cproto-062-4343w + mbed-psoc6 + mbed-cat1a + mbed-cy8cproto-062-4343w-bsp-design-modus ) endif() -# CFLAGS are put in random order, sort them before converting them to a string -list(SORT CHIP_CFLAGS) -list(SORT CHIP_CFLAGS_C) -list(SORT CHIP_CFLAGS_CC) - -set(SEPARATOR ",") -convert_list_of_flags_to_string_of_flags(CHIP_CFLAGS CHIP_CFLAGS ${SEPARATOR}) -set(SEPARATOR " ") -convert_list_of_flags_to_string_of_flags(CHIP_CFLAGS_C CHIP_CFLAGS_C ${SEPARATOR}) -convert_list_of_flags_to_string_of_flags(CHIP_CFLAGS_CC CHIP_CFLAGS_CC ${SEPARATOR}) +# Get compiler flags from external targets +matter_get_compiler_flags_from_targets("${CONFIG_CHIP_EXTERNAL_TARGETS}") -# Prepare CHIP libraries that the application should be linked with - -if (NOT CHIP_LIBRARIES) - set(CHIP_LIBRARIES -lCHIP) -endif() - -if (CONFIG_CHIP_LIB_SHELL) - list(APPEND CHIP_LIBRARIES -lCHIPShell) -endif() +# Additional settings +matter_add_flags(-D__LINUX_ERRNO_EXTENSIONS__=1) +matter_add_flags(-DCHIP_ADDRESS_RESOLVE_IMPL_INCLUDE_HEADER=) if (CONFIG_CHIP_PW_RPC) - list(APPEND CHIP_LIBRARIES -lPwRpc) - if (${APP_TARGET} MATCHES "pigweed-app") - set(CONFIG_CHIP_PW_RPC_ECHO_PROTO "y") - elseif (${APP_TARGET} MATCHES "lighting-app") - set(CONFIG_CHIP_PW_RPC_COMMON_PROTO "y") - set(CONFIG_CHIP_PW_RPC_LIGHTING_PROTO "y") - elseif (${APP_TARGET} MATCHES "lock-app") - set(CONFIG_CHIP_PW_RPC_COMMON_PROTO "y") - set(CONFIG_CHIP_PW_RPC_LOCKING_PROTO "y") - endif() -endif(CONFIG_CHIP_PW_RPC) - -# Set up CHIP project configuration file - -set(CHIP_DEFAULT_CONFIG_FILE "<${CHIP_ROOT}/config/mbed/CHIPProjectConfig.h>") -set(CHIP_PROJECT_CONFIG ${CHIP_DEFAULT_CONFIG_FILE}) - -if (CONFIG_CHIP_PROJECT_CONFIG) - get_filename_component(CHIP_PROJECT_CONFIG - ${CONFIG_CHIP_PROJECT_CONFIG} - REALPATH - BASE_DIR ${CMAKE_SOURCE_DIR} - ) - set(CHIP_PROJECT_CONFIG "<${CHIP_PROJECT_CONFIG}>") + matter_add_gnu_cpp_standard("17") endif() -if (${CMAKE_BUILD_TYPE} STREQUAL "debug") - set(CONFIG_DEBUG "y") +if (CONFIG_MBED_BSD_SOCKET_TRACE) + matter_add_flags(-DMBED_BSD_SOCKET_TRACE=1) endif() # ============================================================================== # Generate configuration for CHIP GN build system # ============================================================================== +matter_common_gn_args( + DEBUG CONFIG_CHIP_DEBUG + LIB_SHELL CONFIG_CHIP_LIB_SHELL + LIB_TESTS CONFIG_CHIP_BUILD_TESTS + LIB_PW_RPC CONFIG_CHIP_PW_RPC + PROJECT_CONFIG ${CONFIG_CHIP_PROJECT_CONFIG} +) if (CONFIG_CHIP_PW_RPC) - chip_gn_arg_import("${GN_ROOT_TARGET}/lib/pw_rpc/pw_rpc.gni") + matter_add_gn_arg_import("${GN_ROOT_TARGET}/lib/pw_rpc/pw_rpc.gni") endif() - -chip_gn_arg_flags("target_cflags" ${CHIP_CFLAGS}) -chip_gn_arg_lang_flags("target_cflags_c" ${CHIP_CFLAGS_C}) -chip_gn_arg_lang_flags("target_cflags_cc" ${CHIP_CFLAGS_CC}) -chip_gn_arg_string("mbed_ar" ${CMAKE_AR}) -chip_gn_arg_string("mbed_cc" ${CMAKE_C_COMPILER}) -chip_gn_arg_string("mbed_cxx" ${CMAKE_CXX_COMPILER}) -chip_gn_arg_string("chip_project_config_include" "${CHIP_PROJECT_CONFIG}") -chip_gn_arg_bool ("is_debug" CONFIG_DEBUG) -chip_gn_arg_bool ("chip_build_tests" CONFIG_CHIP_BUILD_TESTS) -chip_gn_arg_bool ("chip_monolithic_tests" CONFIG_CHIP_BUILD_TESTS) -chip_gn_arg_bool ("chip_build_libshell" CONFIG_CHIP_LIB_SHELL) -chip_gn_arg_bool ("chip_with_platform_mbedtls" CONFIG_CHIP_WITH_EXTERNAL_MBEDTLS) -chip_gn_arg_bool ("chip_build_pw_rpc_lib" CONFIG_CHIP_PW_RPC) -chip_gn_arg_bool ("chip_enable_data_model" CONFIG_CHIP_DATA_MODEL) +matter_add_gn_arg_string("mbed_ar" ${CMAKE_AR}) +matter_add_gn_arg_string("mbed_cc" ${CMAKE_C_COMPILER}) +matter_add_gn_arg_string("mbed_cxx" ${CMAKE_CXX_COMPILER}) +matter_add_gn_arg_bool ("chip_monolithic_tests" CONFIG_CHIP_BUILD_TESTS) +matter_add_gn_arg_bool ("chip_with_platform_mbedtls" CONFIG_CHIP_WITH_EXTERNAL_MBEDTLS) +matter_add_gn_arg_bool ("chip_enable_data_model" CONFIG_CHIP_DATA_MODEL) if (CONFIG_CHIP_PW_RPC) - chip_gn_arg_bool ("chip_build_pw_rpc_echo_proto" CONFIG_CHIP_PW_RPC_ECHO_PROTO) - chip_gn_arg_bool ("chip_build_pw_rpc_common_proto" CONFIG_CHIP_PW_RPC_COMMON_PROTO) - chip_gn_arg_bool ("chip_build_pw_rpc_lighting_proto" CONFIG_CHIP_PW_RPC_LIGHTING_PROTO) - chip_gn_arg_bool ("chip_build_pw_rpc_locking_proto" CONFIG_CHIP_PW_RPC_LOCKING_PROTO) + matter_add_gn_arg_bool ("chip_build_pw_rpc_echo_proto" CONFIG_CHIP_PW_RPC_ECHO_PROTO) + matter_add_gn_arg_bool ("chip_build_pw_rpc_common_proto" CONFIG_CHIP_PW_RPC_COMMON_PROTO) + matter_add_gn_arg_bool ("chip_build_pw_rpc_lighting_proto" CONFIG_CHIP_PW_RPC_LIGHTING_PROTO) + matter_add_gn_arg_bool ("chip_build_pw_rpc_locking_proto" CONFIG_CHIP_PW_RPC_LOCKING_PROTO) endif(CONFIG_CHIP_PW_RPC) if (CONFIG_CHIP_OTA_REQUESTOR) - chip_gn_arg_bool ("chip_enable_ota_requestor" CONFIG_CHIP_OTA_REQUESTOR) + matter_add_gn_arg_bool ("chip_enable_ota_requestor" CONFIG_CHIP_OTA_REQUESTOR) endif(CONFIG_CHIP_OTA_REQUESTOR) - -file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/args.gn CONTENT ${CHIP_GN_ARGS}) - -# ============================================================================== -# Define 'chip-gn' target that builds CHIP library(ies) with GN build system -# ============================================================================== -ExternalProject_Add( - chip-gn - PREFIX ${CMAKE_CURRENT_BINARY_DIR} - SOURCE_DIR ${CHIP_ROOT} - BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} - CONFIGURE_COMMAND gn --root=${GN_ROOT_TARGET} gen --export-compile-commands --check --fail-on-unused-args ${CMAKE_CURRENT_BINARY_DIR} - BUILD_COMMAND ninja - INSTALL_COMMAND "" - BUILD_BYPRODUCTS ${CHIP_LIBRARIES} - CONFIGURE_ALWAYS TRUE - BUILD_ALWAYS TRUE - USES_TERMINAL_CONFIGURE TRUE - USES_TERMINAL_BUILD TRUE -) +matter_generate_args_tmp_file() # ============================================================================== -# Define 'chip' target that exposes CHIP headers & libraries to the application +# Build chip library # ============================================================================== -mbed_interface_library_named(chip) -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 - ${CMAKE_CURRENT_BINARY_DIR}/gen/include +matter_build(chip + LIB_SHELL ${CONFIG_CHIP_LIB_SHELL} + LIB_TESTS ${CONFIG_CHIP_BUILD_TESTS} + LIB_PW_RPC ${CONFIG_CHIP_PW_RPC} ) -target_link_directories(chip INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/lib) -target_link_libraries(chip INTERFACE -Wl,--start-group ${CHIP_LIBRARIES} -Wl,--end-group) -add_dependencies(chip chip-gn) # ============================================================================== # Define mbed target configuration according to CHIP component usage @@ -420,14 +250,14 @@ target_include_directories(${APP_TARGET} PRIVATE ${CHIP_ROOT}/examples/common/pigweed/mbed ${MBED_COMMON}/pw_sys_io/public - ${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/third_party/pigweed/repo/pw_rpc/protos.proto_library/pwpb - ${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/third_party/pigweed/repo/pw_protobuf/common_protos.proto_library/nanopb + ${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/pigweed/repo/pw_rpc/protos.proto_library/pwpb + ${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/pigweed/repo/pw_protobuf/common_protos.proto_library/nanopb ) if (CONFIG_CHIP_PW_RPC_ECHO_PROTO) target_include_directories(${APP_TARGET} PRIVATE - ${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/third_party/pigweed/repo/pw_rpc/protos.proto_library/nanopb_rpc - ${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/third_party/pigweed/repo/pw_rpc/protos.proto_library/nanopb + ${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/pigweed/repo/pw_rpc/protos.proto_library/nanopb_rpc + ${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/pigweed/repo/pw_rpc/protos.proto_library/nanopb ) list(APPEND CHIP_DEFINES CHIP_PW_RPC_ECHO_PROTO=1 @@ -436,11 +266,11 @@ endif(CONFIG_CHIP_PW_RPC_ECHO_PROTO) if (CONFIG_CHIP_PW_RPC_COMMON_PROTO) target_include_directories(${APP_TARGET} PRIVATE - ${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/examples/common/pigweed/button_service.proto_library/nanopb - ${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/examples/common/pigweed/button_service.proto_library/nanopb_rpc + ${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/examples/common/pigweed/button_service.proto_library/nanopb + ${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/examples/common/pigweed/button_service.proto_library/nanopb_rpc - ${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/examples/common/pigweed/device_service.proto_library/nanopb - ${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/examples/common/pigweed/device_service.proto_library/nanopb_rpc + ${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/examples/common/pigweed/device_service.proto_library/nanopb + ${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/examples/common/pigweed/device_service.proto_library/nanopb_rpc ) list(APPEND CHIP_DEFINES CHIP_PW_RPC_COMMON_PROTO=1 @@ -449,8 +279,8 @@ endif(CONFIG_CHIP_PW_RPC_COMMON_PROTO) if (CONFIG_CHIP_PW_RPC_LIGHTING_PROTO) target_include_directories(${APP_TARGET} PRIVATE - ${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/examples/common/pigweed/lighting_service.proto_library/nanopb - ${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/examples/common/pigweed/lighting_service.proto_library/nanopb_rpc + ${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/examples/common/pigweed/lighting_service.proto_library/nanopb + ${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/examples/common/pigweed/lighting_service.proto_library/nanopb_rpc ) list(APPEND CHIP_DEFINES CHIP_PW_RPC_LIGHTING_PROTO=1 @@ -459,8 +289,8 @@ endif(CONFIG_CHIP_PW_RPC_LIGHTING_PROTO) if (CONFIG_CHIP_PW_RPC_LOCKING_PROTO) target_include_directories(${APP_TARGET} PRIVATE - ${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/examples/common/pigweed/locking_service.proto_library/nanopb - ${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/third_party/connectedhomeip/examples/common/pigweed/locking_service.proto_library/nanopb_rpc + ${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/examples/common/pigweed/locking_service.proto_library/nanopb + ${CMAKE_CURRENT_BINARY_DIR}/protocol_buffer/gen/examples/common/pigweed/locking_service.proto_library/nanopb_rpc ) list(APPEND CHIP_DEFINES CHIP_PW_RPC_LOCKING_PROTO=1 diff --git a/config/mbed/chip-gn/.gn b/config/mbed/chip-gn/.gn index 751e7454035e18..47eb417b94b8f7 100644 --- a/config/mbed/chip-gn/.gn +++ b/config/mbed/chip-gn/.gn @@ -13,6 +13,7 @@ # limitations under the License. import("//build_overrides/build.gni") +import("//build_overrides/chip.gni") # The location of the build configuration file. buildconfig = "//build/config/BUILDCONFIG.gn" @@ -24,10 +25,8 @@ default_args = { target_cpu = "arm" target_os = "mbed" - default_configs_warnings = [ - "${build_root}/config/compiler:warnings_default", - "//:chip_custom_cflags_config", - ] + default_configs_warnings = + [ "${build_root}/config/compiler:warnings_default" ] - import("//args.gni") + import("${chip_root}/config/mbed/chip-gn/args.gni") } diff --git a/config/mbed/chip-gn/BUILD.gn b/config/mbed/chip-gn/BUILD.gn index 28f56889a1bf46..76d017b86039e3 100644 --- a/config/mbed/chip-gn/BUILD.gn +++ b/config/mbed/chip-gn/BUILD.gn @@ -20,11 +20,6 @@ assert(current_os == "mbed") declare_args() { chip_build_pw_rpc_lib = false - chip_custom_build_cflags = [] -} - -config("chip_custom_cflags_config") { - cflags = chip_custom_build_cflags } group("mbed") { @@ -35,7 +30,7 @@ group("mbed") { } if (chip_build_pw_rpc_lib) { - deps += [ "//lib/pw_rpc" ] + deps += [ "${chip_root}/config/mbed/chip-gn/lib/pw_rpc" ] } } diff --git a/config/mbed/chip-gn/args.gni b/config/mbed/chip-gn/args.gni index 5fd323b58eefd9..83753bc7ca8012 100644 --- a/config/mbed/chip-gn/args.gni +++ b/config/mbed/chip-gn/args.gni @@ -23,9 +23,7 @@ chip_device_project_config_include = "" chip_inet_config_enable_udp_endpoint = true chip_inet_config_enable_tcp_endpoint = true -chip_custom_build_cflags = [] - -custom_toolchain = "//toolchain:mbed" -mbedtls_target = "//mbedtls:mbedtls" +custom_toolchain = "${chip_root}/config/mbed/chip-gn/toolchain:mbed" +mbedtls_target = "${chip_root}/config/mbed/chip-gn/mbedtls:mbedtls" pw_build_PIP_CONSTRAINTS = [ "${chip_root}/scripts/setup/constraints.txt" ] diff --git a/config/mbed/mbed-util.cmake b/config/mbed/mbed-util.cmake deleted file mode 100644 index adc1a2281dc107..00000000000000 --- a/config/mbed/mbed-util.cmake +++ /dev/null @@ -1,139 +0,0 @@ -# -# Copyright (c) 2020 Project CHIP Authors -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -# -# @file -# CMake utilities for managing and retrieving mbed build configuration -# - -# Get compilation flags for specific lang -# The flags might contains compile language generator expressions that -# look like this: -# $<$:-fno-exceptions> -# Applying a regex to extract the flag and also to find out if the language matches. -# [Args]: -# input - list of flags to parse -# output - list of flags set to specific language -function(get_flags_for_lang lang input output) - set(tmp_list "") - - list(LENGTH ${input} nb_elem) - set(index 0) - set(is_compile_lang_expression 0) - - while(${index} LESS ${nb_elem}) - list(GET ${input} ${index} value) - if(value MATCHES ":([^>]+)") - string(REGEX REPLACE "^[^:]*:" "" updated_flag ${value}) - list(APPEND tmp_list ${updated_flag}) - if(NOT value MATCHES ">$") - set(is_compile_lang_expression 1) - endif() - elseif(is_compile_lang_expression) - if(value MATCHES ">$") - set(is_compile_lang_expression 0) - endif() - string(REPLACE ">" "" updated_flag ${value}) - list(APPEND tmp_list ${updated_flag}) - endif() - math(EXPR index "${index}+1") - endwhile() - - set(${output} ${tmp_list} PARENT_SCOPE) -endfunction() - -# Get include directory of target build -# Get target property of includes directories -# For each flag add -I prefix and put it in quotation marks -# [Args]: -# target - target name -# output - output variable name -function(mbed_get_include_directories target output) - get_property(flags TARGET ${target} PROPERTY INTERFACE_INCLUDE_DIRECTORIES) - list(APPEND CFLAG_LIST) - foreach(flag ${flags}) - list(APPEND CFLAG_LIST "\"-isystem${flag}\"") - endforeach() - set(${output} ${CFLAG_LIST} PARENT_SCOPE) -endfunction() - -# Get compile definitions of target build -# Get target property of compile definitions -# For each flag change format, add -D prefix and put it in quotation marks -# [Args]: -# target - target name -# output - output variable name -function(mbed_get_compile_definitions target output) - get_property(flags TARGET ${target} PROPERTY INTERFACE_COMPILE_DEFINITIONS) - - list(APPEND CFLAG_LIST) - foreach(flag ${flags}) - # Replace each quote with a '\"' - format required for the GN arguments - string(REPLACE "\"" "\\\\\"" output_flag ${flag}) - list(APPEND CFLAG_LIST "\"-D${output_flag}\"") - endforeach() - set(${output} ${CFLAG_LIST} PARENT_SCOPE) -endfunction() - -# Get compile options of mbed build for specific language -# Get mbed-core property of compile options -# [Args]: -# lang - compilation languge (C, C++ or ASM) -# target - target name -# output - output variable name -function(mbed_get_compile_options_for_lang lang target output) - get_property(flags TARGET ${target} PROPERTY INTERFACE_COMPILE_OPTIONS) - get_flags_for_lang(${lang} flags output_list) - set(${output} ${output_list} PARENT_SCOPE) -endfunction() - - -# Retrieve common compilation flags specific for target -# [Args]: -# VAR - flags variable name -# TARGET - target name -function(mbed_get_target_common_compile_flags VAR TARGET) - mbed_get_include_directories(${TARGET} INCLUDES) - mbed_get_compile_definitions(${TARGET} DEFINES) - set(${VAR} ${INCLUDES} ${DEFINES} ${${VAR}} PARENT_SCOPE) -endfunction() - -# Retrieve target compiler flags for the specific language (C or CXX) -# [Args]: -# VAR - flags variable name -# TARGET - target name -# LANG - compilation languge (C, C++ or ASM) -function(mbed_get_lang_compile_flags VAR TARGET LANG) - mbed_get_compile_options_for_lang(${LANG} ${TARGET} FLAGS) - set(${VAR} ${FLAGS} ${${VAR}} PARENT_SCOPE) -endfunction() - -# Convert list of flags to string format -# [Args]: -# ptr_list_of_flags - list fo flags -# string_of_flags - output string -# separator - flags separator -function(convert_list_of_flags_to_string_of_flags ptr_list_of_flags string_of_flags separator) - # Convert the list to a string so we can do string replace - # operations on it and replace the ";" list separators with a - # desirable one so the flags are spaced out - string(REPLACE ";" ${separator} locally_scoped_string_of_flags "${${ptr_list_of_flags}}") - # Removing excess spaces - string(REPLACE " " " " locally_scoped_string_of_flags "${locally_scoped_string_of_flags}") - - # Set the output variable in the parent scope - set(${string_of_flags} ${locally_scoped_string_of_flags} PARENT_SCOPE) -endfunction() diff --git a/config/nrfconnect/chip-module/CMakeLists.txt b/config/nrfconnect/chip-module/CMakeLists.txt index 4ef9feb7de4c26..2511fa3094cff6 100644 --- a/config/nrfconnect/chip-module/CMakeLists.txt +++ b/config/nrfconnect/chip-module/CMakeLists.txt @@ -1,5 +1,5 @@ # -# Copyright (c) 2020 Project CHIP Authors +# Copyright (c) 2020-2023 Project CHIP Authors # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -32,79 +32,37 @@ include(../../zephyr/ota-image.cmake) include(../../zephyr/zephyr-util.cmake) include(generate_factory_data.cmake) -# ============================================================================== -# Declare configuration variables and define constants -# ============================================================================== - -# C/C++ compiler flags passed to CHIP build system -list(APPEND CHIP_CFLAGS) - -# C compiler flags passed to CHIP build system -list(APPEND CHIP_CFLAGS_C) - -# C++ compiler flags passed to CHIP build system -list(APPEND CHIP_CFLAGS_CC) - -# CHIP libraries that the application should be linked with -list(APPEND CHIP_LIBRARIES) - -# GN meta-build system arguments passed to the make_gn_args.py script -string(APPEND CHIP_GN_ARGS) - -# ============================================================================== -# Helper macros -# ============================================================================== - -macro(chip_gn_arg_import FILE) - string(APPEND CHIP_GN_ARGS "--module\n${FILE}\n") -endmacro() - -macro(chip_gn_arg_string ARG STRING) - string(APPEND CHIP_GN_ARGS "--arg-string\n${ARG}\n${STRING}\n") -endmacro() - -macro(chip_gn_arg_bool ARG) - if (${ARGN}) - string(APPEND CHIP_GN_ARGS "--arg\n${ARG}\ntrue\n") - else() - string(APPEND CHIP_GN_ARGS "--arg\n${ARG}\nfalse\n") - endif() -endmacro() - -macro(chip_gn_arg_cflags ARG CFLAGS) - string(APPEND CHIP_GN_ARGS "--arg-cflags\n${ARG}\n${CFLAGS}\n") -endmacro() - -macro(chip_gn_arg ARG VALUE) - string(APPEND CHIP_GN_ARGS "--arg\n${ARG}\n${VALUE}\n") -endmacro() - # ============================================================================== # Prepare CHIP configuration based on the project Kconfig configuration # ============================================================================== +# Set paths + if (NOT CHIP_ROOT) get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../.. REALPATH) endif() +get_filename_component(GN_ROOT_TARGET ${CHIP_ROOT}/config/nrfconnect/chip-gn REALPATH) +get_filename_component(COMMON_CMAKE_SOURCE_DIR ${CHIP_ROOT}/config/common/cmake REALPATH) -set(CHIP_LIB_DIR ${CMAKE_CURRENT_BINARY_DIR}/lib) -set(CHIP_GN_ROOT_TARGET ${CHIP_ROOT}/config/nrfconnect/chip-gn) +# Get common Cmake sources + +include(${COMMON_CMAKE_SOURCE_DIR}/chip_gn_args.cmake) +include(${COMMON_CMAKE_SOURCE_DIR}/chip_gn.cmake) # Prepare compiler flags +matter_add_flags(-D_DEFAULT_SOURCE) -list(APPEND CHIP_CFLAGS -D_DEFAULT_SOURCE) +if (CHIP_CFLAGS) + matter_add_flags("${CHIP_CFLAGS}") +endif() if (CONFIG_ARM) - list(APPEND CHIP_CFLAGS_C - --specs=nosys.specs - ) + matter_add_cflags(--specs=nosys.specs) endif() if (CONFIG_POSIX_API) - list(APPEND CHIP_CFLAGS - -D_SYS__PTHREADTYPES_H_ - -isystem${ZEPHYR_BASE}/include/zephyr/posix - ) + matter_add_flags(-D_SYS__PTHREADTYPES_H_) + matter_add_flags(-isystem${ZEPHYR_BASE}/include/zephyr/posix) endif() if (CONFIG_NORDIC_SECURITY_BACKEND) @@ -113,7 +71,7 @@ if (CONFIG_NORDIC_SECURITY_BACKEND) if(TARGET platform_cc3xx) zephyr_include_directories($) endif() - list(APPEND CHIP_CFLAGS -DMBEDTLS_CONFIG_FILE=) + matter_add_flags(-DMBEDTLS_CONFIG_FILE=) elseif(CONFIG_MBEDTLS) zephyr_include_directories($) zephyr_compile_definitions($) @@ -123,41 +81,12 @@ if (CONFIG_NRF_802154_RADIO_DRIVER) zephyr_include_directories($) endif() -zephyr_get_compile_flags(CHIP_CFLAGS_C C) -zephyr_get_compile_flags(CHIP_CFLAGS_CC CXX) -zephyr_get_gnu_cpp_standard(CHIP_CFLAGS_CC) - -convert_list_of_flags_to_string_of_flags(CHIP_CFLAGS CHIP_CFLAGS) -convert_list_of_flags_to_string_of_flags(CHIP_CFLAGS_C CHIP_CFLAGS_C) -convert_list_of_flags_to_string_of_flags(CHIP_CFLAGS_CC CHIP_CFLAGS_CC) - -# Prepare CHIP libraries that the application should be linked with - -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) - get_filename_component(CHIP_PROJECT_CONFIG - ${CONFIG_CHIP_PROJECT_CONFIG} - REALPATH - BASE_DIR ${CMAKE_SOURCE_DIR} - ) - set(CHIP_PROJECT_CONFIG "<${CHIP_PROJECT_CONFIG}>") -else() - set(CHIP_PROJECT_CONFIG "") -endif() +zephyr_get_compile_flags(ZEPHYR_CFLAGS_C C) +matter_add_cflags("${ZEPHYR_CFLAGS_C}") +zephyr_get_compile_flags(ZEPHYR_CFLAGS_CC CXX) +matter_add_cxxflags("${ZEPHYR_CFLAGS_CC}") +zephyr_get_gnu_cpp_standard(ZEPHYR_GNU_CPP_STD) +matter_add_cxxflags(${ZEPHYR_GNU_CPP_STD}) # Set up custom OpenThread configuration @@ -180,162 +109,96 @@ endif() get_property(CHIP_COMPILER_LAUNCHER GLOBAL PROPERTY RULE_LAUNCH_COMPILE) -# Find required programs - -find_package(Python3 REQUIRED) -find_program(GN_EXECUTABLE gn REQUIRED) - -# 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 - 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 # ============================================================================== -chip_gn_arg_cflags("target_cflags" ${CHIP_CFLAGS}) -chip_gn_arg_cflags("target_cflags_c" ${CHIP_CFLAGS_C}) -chip_gn_arg_cflags("target_cflags_cc" ${CHIP_CFLAGS_CC}) -chip_gn_arg_string("zephyr_ar" ${CMAKE_AR}) -chip_gn_arg_string("zephyr_cc" ${CMAKE_C_COMPILER}) -chip_gn_arg_string("zephyr_cxx" ${CMAKE_CXX_COMPILER}) -chip_gn_arg_bool ("is_debug" CONFIG_DEBUG) -chip_gn_arg_bool ("chip_logging" CONFIG_LOG) -chip_gn_arg_bool ("chip_enable_openthread" CONFIG_NET_L2_OPENTHREAD) -chip_gn_arg_bool ("chip_openthread_ftd" CONFIG_OPENTHREAD_FTD) -chip_gn_arg_bool ("chip_config_network_layer_ble" CONFIG_BT) -chip_gn_arg_bool ("chip_inet_config_enable_ipv4" CONFIG_NET_IPV4) -chip_gn_arg_bool ("chip_enable_nfc" CONFIG_CHIP_NFC_COMMISSIONING) -chip_gn_arg_bool ("chip_enable_ota_requestor" CONFIG_CHIP_OTA_REQUESTOR) -chip_gn_arg_bool ("chip_persist_subscriptions" CONFIG_CHIP_PERSISTENT_SUBSCRIPTIONS) -chip_gn_arg_bool ("chip_build_tests" CONFIG_CHIP_BUILD_TESTS) -chip_gn_arg_bool ("chip_monolithic_tests" CONFIG_CHIP_BUILD_TESTS) -chip_gn_arg_bool ("chip_inet_config_enable_tcp_endpoint" CONFIG_CHIP_BUILD_TESTS) -chip_gn_arg_bool ("chip_build_libshell" CONFIG_CHIP_LIB_SHELL) -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_malloc_sys_heap" CONFIG_CHIP_MALLOC_SYS_HEAP) -chip_gn_arg_bool ("chip_enable_wifi" CONFIG_WIFI_NRF700X) +matter_common_gn_args( + DEBUG CONFIG_DEBUG + LIB_SHELL CONFIG_CHIP_LIB_SHELL + LIB_TESTS CONFIG_CHIP_BUILD_TESTS + PROJECT_CONFIG ${CONFIG_CHIP_PROJECT_CONFIG} + DEVICE_INFO_EXAMPLE_PROVIDER CONFIG_CHIP_EXAMPLE_DEVICE_INFO_PROVIDER +) +matter_add_gn_arg_string("zephyr_ar" ${CMAKE_AR}) +matter_add_gn_arg_string("zephyr_cc" ${CMAKE_C_COMPILER}) +matter_add_gn_arg_string("zephyr_cxx" ${CMAKE_CXX_COMPILER}) +matter_add_gn_arg_bool ("chip_logging" CONFIG_LOG) +matter_add_gn_arg_bool ("chip_enable_openthread" CONFIG_NET_L2_OPENTHREAD) +matter_add_gn_arg_bool ("chip_openthread_ftd" CONFIG_OPENTHREAD_FTD) +matter_add_gn_arg_bool ("chip_config_network_layer_ble" CONFIG_BT) +matter_add_gn_arg_bool ("chip_inet_config_enable_ipv4" CONFIG_NET_IPV4) +matter_add_gn_arg_bool ("chip_enable_nfc" CONFIG_CHIP_NFC_COMMISSIONING) +matter_add_gn_arg_bool ("chip_enable_ota_requestor" CONFIG_CHIP_OTA_REQUESTOR) +matter_add_gn_arg_bool ("chip_persist_subscriptions" CONFIG_CHIP_PERSISTENT_SUBSCRIPTIONS) +matter_add_gn_arg_bool ("chip_monolithic_tests" CONFIG_CHIP_BUILD_TESTS) +matter_add_gn_arg_bool ("chip_inet_config_enable_tcp_endpoint" CONFIG_CHIP_BUILD_TESTS) +matter_add_gn_arg_bool ("chip_error_logging" CONFIG_MATTER_LOG_LEVEL GREATER_EQUAL 1) +matter_add_gn_arg_bool ("chip_progress_logging" CONFIG_MATTER_LOG_LEVEL GREATER_EQUAL 3) +matter_add_gn_arg_bool ("chip_detail_logging" CONFIG_MATTER_LOG_LEVEL GREATER_EQUAL 4) +matter_add_gn_arg_bool ("chip_automation_logging" FALSE) +matter_add_gn_arg_bool ("chip_malloc_sys_heap" CONFIG_CHIP_MALLOC_SYS_HEAP) +matter_add_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) + matter_add_gn_arg_bool("chip_use_transitional_commissionable_data_provider" FALSE) + matter_add_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) + matter_add_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) + matter_add_gn_arg_bool("chip_enable_rotating_device_id" TRUE) + matter_add_gn_arg_bool("chip_enable_additional_data_advertising" TRUE) endif() if (CONFIG_NET_L2_OPENTHREAD) - chip_gn_arg_string("chip_mdns" "platform") + matter_add_gn_arg_string("chip_mdns" "platform") elseif(CONFIG_WIFI_NRF700X) - chip_gn_arg_string("chip_mdns" "minimal") + matter_add_gn_arg_string("chip_mdns" "minimal") else() - chip_gn_arg_string("chip_mdns" "none") + matter_add_gn_arg_string("chip_mdns" "none") endif() if (CONFIG_CHIP_CRYPTO_PSA) - chip_gn_arg_string("chip_crypto" "psa") -endif() - -if (CHIP_PROJECT_CONFIG) - chip_gn_arg_string("chip_project_config_include" ${CHIP_PROJECT_CONFIG}) - chip_gn_arg_string("chip_system_project_config_include" ${CHIP_PROJECT_CONFIG}) + matter_add_gn_arg_string("chip_crypto" "psa") endif() if (BOARD STREQUAL "native_posix") - chip_gn_arg_string("target_cpu" "x86") + matter_add_gn_arg_string("target_cpu" "x86") elseif (BOARD STREQUAL "native_posix_64") - chip_gn_arg_string("target_cpu" "x64") + matter_add_gn_arg_string("target_cpu" "x64") endif() if (NOT CONFIG_CHIP_DEBUG_SYMBOLS) - chip_gn_arg_string("symbol_level" "0") + matter_add_gn_arg_string("symbol_level" "0") endif() if (CHIP_COMPILER_LAUNCHER) - chip_gn_arg_string("pw_command_launcher" ${CHIP_COMPILER_LAUNCHER}) + matter_add_gn_arg_string("pw_command_launcher" ${CHIP_COMPILER_LAUNCHER}) endif() if (CONFIG_CHIP_PW_RPC) set(PIGWEED_DIR "//third_party/pigweed/repo") - chip_gn_arg_string("pw_assert_BACKEND" ${PIGWEED_DIR}/pw_assert_log:check_backend) - chip_gn_arg_string("pw_log_BACKEND" ${PIGWEED_DIR}/pw_log_basic) - chip_gn_arg("pw_build_LINK_DEPS" [\"${PIGWEED_DIR}/pw_assert:impl\",\ \"${PIGWEED_DIR}/pw_log:impl\"]) + matter_add_gn_arg_string("pw_assert_BACKEND" ${PIGWEED_DIR}/pw_assert_log:check_backend) + matter_add_gn_arg_string("pw_log_BACKEND" ${PIGWEED_DIR}/pw_log_basic) + matter_add_gn_arg("pw_build_LINK_DEPS" [\"${PIGWEED_DIR}/pw_assert:impl\",\ \"${PIGWEED_DIR}/pw_log:impl\"]) endif() -if (CONFIG_CHIP_EXAMPLE_DEVICE_INFO_PROVIDER) - chip_gn_arg_bool("chip_build_example_providers" TRUE) -endif() - -file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/args.tmp" CONTENT ${CHIP_GN_ARGS}) - -# ============================================================================== -# Define 'chip-gn' target that builds CHIP library(ies) with GN build system -# ============================================================================== -ExternalProject_Add( - chip-gn - PREFIX ${CMAKE_CURRENT_BINARY_DIR} - SOURCE_DIR ${CHIP_ROOT} - BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} - CONFIGURE_COMMAND "" - BUILD_COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/make_gn_args.py - @args.tmp > args.gn && - ${GN_EXECUTABLE} - --root=${CHIP_ROOT} - --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_BUILD TRUE -) -add_dependencies(chip-gn kernel) +matter_generate_args_tmp_file() # ============================================================================== -# Define 'chip' target that exposes CHIP headers & libraries to the application +# Build chip library # ============================================================================== -zephyr_interface_library_named(chip) -target_compile_definitions(chip INTERFACE CHIP_HAVE_CONFIG_H) -target_compile_definitions(chip INTERFACE _DEFAULT_SOURCE) -target_include_directories(chip INTERFACE - ${CHIP_ROOT}/src - ${CHIP_ROOT}/src/include - ${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 +matter_build(chip + LIB_SHELL ${CONFIG_CHIP_LIB_SHELL} + LIB_TESTS ${CONFIG_CHIP_BUILD_TESTS} + DEVICE_INFO_EXAMPLE_PROVIDER ${CONFIG_CHIP_EXAMPLE_DEVICE_INFO_PROVIDER} + GN_DEPENDENCIES kernel ) - -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() +set_property(GLOBAL APPEND PROPERTY ZEPHYR_INTERFACE_LIBS chip) +target_compile_definitions(chip INTERFACE _DEFAULT_SOURCE) if (CONFIG_CHIP_MALLOC_SYS_HEAP_OVERRIDE) target_link_options(chip INTERFACE @@ -350,10 +213,6 @@ if (CONFIG_CHIP_MALLOC_SYS_HEAP_OVERRIDE) ) endif() -target_link_libraries(chip INTERFACE -Wl,--start-group ${CHIP_LIBRARIES} -Wl,--end-group) - -add_dependencies(chip chip-gn) - # ============================================================================== # Define 'chip-ota-image' target for building CHIP OTA image # ============================================================================== diff --git a/config/nrfconnect/chip-module/make_gn_args.py b/config/nrfconnect/chip-module/make_gn_args.py deleted file mode 100755 index 3c3f611c00b437..00000000000000 --- a/config/nrfconnect/chip-module/make_gn_args.py +++ /dev/null @@ -1,77 +0,0 @@ -#!/usr/bin/env python3 - -# -# Copyright (c) 2021 Project CHIP Authors -# All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -import argparse -import re -import sys - -GN_SPECIAL_CHARACTERS = r'(["$\\])' -GN_CFLAG_EXCLUDES = [ - '-fno-asynchronous-unwind-tables', - '-fno-common', - '-fno-defer-pop', - '-fno-reorder-functions', - '-ffunction-sections', - '-fdata-sections', - '-g', - '-g0', - '-g1', - '-g2', - '-g3', - '-O*', - '-W*', -] - - -def escape_strings(gn_args): - return [[key, re.sub(GN_SPECIAL_CHARACTERS, r'\\\1', value)] for key, value in gn_args] - - -def write_gn_args(args): - if args.module: - sys.stdout.write('import("{}")\n'.format(args.module)) - - for key, value in args.arg: - sys.stdout.write('{} = {}\n'.format(key, value)) - - for key, value in args.arg_string: - sys.stdout.write('{} = "{}"\n'.format(key, value)) - - cflag_excludes = ', '.join(['"{}"'.format(exclude) - for exclude in GN_CFLAG_EXCLUDES]) - - for key, value in args.arg_cflags: - sys.stdout.write('{} = filter_exclude(string_split("{}"), [{}])\n'.format( - key, value, cflag_excludes)) - - -def main(): - parser = argparse.ArgumentParser(fromfile_prefix_chars='@') - parser.add_argument('--module', action='store') - parser.add_argument('--arg', action='append', nargs=2, default=[]) - parser.add_argument('--arg-string', action='append', nargs=2, default=[]) - parser.add_argument('--arg-cflags', action='append', nargs=2, default=[]) - args = parser.parse_args() - args.arg_string = escape_strings(args.arg_string) - args.arg_cflags = escape_strings(args.arg_cflags) - write_gn_args(args) - - -if __name__ == "__main__": - main() diff --git a/config/openiotsdk/CMakeLists.txt b/config/openiotsdk/CMakeLists.txt index 1e7a77a0f21615..7f559587a88a66 100644 --- a/config/openiotsdk/CMakeLists.txt +++ b/config/openiotsdk/CMakeLists.txt @@ -16,75 +16,10 @@ # # @file -# CMake sub-project defining 'chip' target which represents CHIP library -# and other optional libraries like unit tests, built with 'open-iot-sdk' -# platform. -# Since CHIP doesn't provide native CMake support, ExternalProject -# module is used to build the required artifacts with GN meta-build -# system. +# CMake sub-project to configure and build the chip library. # include(ExternalProject) -include(util.cmake) - -# ============================================================================== -# Declare configuration variables and define constants -# ============================================================================== -# C/C++ compiler flags passed to CHIP build system -list(APPEND CHIP_CFLAGS \"-Wno-unused-function\") - -# C compiler flags passed to CHIP build system -list(APPEND CHIP_CFLAGS_C ${CMAKE_C_FLAGS}) - -# C++ compiler flags passed to CHIP build system -list(APPEND CHIP_CFLAGS_CC ${CMAKE_CXX_FLAGS}) - -# GN meta-build system arguments in the form of 'key1 = value1\nkey2 = value2...' string -string(APPEND CHIP_GN_ARGS) - -# ============================================================================== -# Helper macros -# ============================================================================== -macro(chip_gn_arg_import FILE) - string(APPEND CHIP_GN_ARGS "--module\n${FILE}\n") -endmacro() - -macro(chip_gn_arg_string ARG STRING) - string(APPEND CHIP_GN_ARGS "--arg-string\n${ARG}\n${STRING}\n") -endmacro() - -macro(chip_gn_arg_bool ARG) - if (${ARGN}) - string(APPEND CHIP_GN_ARGS "--arg\n${ARG}\ntrue\n") - else() - string(APPEND CHIP_GN_ARGS "--arg\n${ARG}\nfalse\n") - endif() -endmacro() - -macro(chip_gn_arg_cflags ARG CFLAGS) - string(APPEND CHIP_GN_ARGS "--arg-cflags\n${ARG}\n${CFLAGS}\n") -endmacro() - -macro(chip_gn_arg_cflags_lang ARG CFLAGS) - string(APPEND CHIP_GN_ARGS "--arg-cflags-lang\n${ARG}\n${CFLAGS}\n") -endmacro() - -macro(chip_gn_arg ARG VALUE) - string(APPEND CHIP_GN_ARGS "--arg\n${ARG}\n${VALUE}\n") -endmacro() - -# Select gnu++ standard based on project configuration -macro(get_gnu_cpp_standard VAR) - if (CONFIG_STD_CPP11) - list(APPEND ${VAR} -std=gnu++11) - elseif (CONFIG_STD_CPP14) - list(APPEND ${VAR} -std=gnu++14) - elseif (CONFIG_STD_CPP17) - list(APPEND ${VAR} -std=gnu++17) - elseif (CONFIG_STD_CPP2A) - list(APPEND ${VAR} -std=gnu++20) - endif() -endmacro() # ============================================================================== # Prepare CHIP configuration based on the project configuration @@ -93,160 +28,55 @@ endmacro() if (NOT CHIP_ROOT) get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../.. REALPATH) endif() +get_filename_component(GN_ROOT_TARGET ${CHIP_ROOT}/config/openiotsdk/chip-gn REALPATH) +get_filename_component(COMMON_CMAKE_SOURCE_DIR ${CHIP_ROOT}/config/common/cmake REALPATH) -set(GN_ROOT_TARGET ${CHIP_ROOT}/config/openiotsdk/chip-gn) +# Get common Cmake sources +include(${COMMON_CMAKE_SOURCE_DIR}/util.cmake) +include(${COMMON_CMAKE_SOURCE_DIR}/chip_gn_args.cmake) +include(${COMMON_CMAKE_SOURCE_DIR}/chip_gn.cmake) # Prepare compiler flags -# Get compiler flags from external targets -foreach(target ${EXTERNAL_TARGETS}) - get_target_common_compile_flags(EXTERNAL_TARGET_CFLAGS ${target}) - list(APPEND CHIP_CFLAGS ${EXTERNAL_TARGET_CFLAGS}) -endforeach() - -# Remove duplicated flags -list(REMOVE_DUPLICATES CHIP_CFLAGS) - -get_gnu_cpp_standard(CHIP_CFLAGS_CC) - -# CFLAGS are put in random order, sort them before converting them to a string -list(SORT CHIP_CFLAGS) -list(SORT CHIP_CFLAGS_C) -list(SORT CHIP_CFLAGS_CC) - -set(SEPARATOR ",") -convert_list_of_flags_to_string_of_flags(CHIP_CFLAGS CHIP_CFLAGS ${SEPARATOR}) -set(SEPARATOR " ") -convert_list_of_flags_to_string_of_flags(CHIP_CFLAGS_C CHIP_CFLAGS_C ${SEPARATOR}) -convert_list_of_flags_to_string_of_flags(CHIP_CFLAGS_CC CHIP_CFLAGS_CC ${SEPARATOR}) +matter_add_cflags(${CMAKE_C_FLAGS}) +matter_add_cxxflags(${CMAKE_CXX_FLAGS}) -# Prepare CHIP libraries that the application should be linked with -set(CHIP_LIBRARIES "${CMAKE_CURRENT_BINARY_DIR}/lib/libCHIP.a") - -if (CONFIG_CHIP_LIB_SHELL) - list(APPEND CHIP_LIBRARIES "${CMAKE_CURRENT_BINARY_DIR}/lib/libCHIPShell.a") -endif() - -# Set up CHIP project configuration file - -if (CONFIG_CHIP_PROJECT_CONFIG) - get_filename_component(CHIP_PROJECT_CONFIG - ${CONFIG_CHIP_PROJECT_CONFIG} - REALPATH - BASE_DIR ${CMAKE_SOURCE_DIR} - ) - set(CHIP_PROJECT_CONFIG "<${CHIP_PROJECT_CONFIG}>") -else() - set(CHIP_PROJECT_CONFIG "") -endif() - -if (${CMAKE_BUILD_TYPE} STREQUAL "Debug") - set(CONFIG_DEBUG YES) -endif() - -# 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 - ) - - 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() - -find_package(Python3 REQUIRED) +matter_get_compiler_flags_from_targets("${CONFIG_CHIP_EXTERNAL_TARGETS}") # ============================================================================== # Generate configuration for CHIP GN build system # ============================================================================== -chip_gn_arg_string("target_cpu" "${CMAKE_SYSTEM_PROCESSOR}") -chip_gn_arg_cflags("target_cflags" ${CHIP_CFLAGS}) -chip_gn_arg_cflags_lang("target_cflags_c" ${CHIP_CFLAGS_C}) -chip_gn_arg_cflags_lang("target_cflags_cc" ${CHIP_CFLAGS_CC}) -chip_gn_arg_string("openiotsdk_ar" ${CMAKE_AR}) -chip_gn_arg_string("openiotsdk_cc" ${CMAKE_C_COMPILER}) -chip_gn_arg_string("openiotsdk_cxx" ${CMAKE_CXX_COMPILER}) -chip_gn_arg_string("chip_project_config_include" "${CHIP_PROJECT_CONFIG}") -chip_gn_arg_string("chip_system_project_config_include" "${CHIP_PROJECT_CONFIG}") -chip_gn_arg_bool ("is_debug" CONFIG_DEBUG) -chip_gn_arg_bool ("chip_build_tests" CONFIG_CHIP_LIB_TESTS) -chip_gn_arg_bool ("chip_monolithic_tests" CONFIG_CHIP_LIB_TESTS) -chip_gn_arg_bool ("chip_build_libshell" CONFIG_CHIP_LIB_SHELL) -chip_gn_arg_bool ("chip_detail_logging" CONFIG_CHIP_DETAIL_LOGGING) -chip_gn_arg_bool ("chip_progress_logging" CONFIG_CHIP_PROGRESS_LOGGING) -chip_gn_arg_bool ("chip_automation_logging" CONFIG_CHIP_AUTOMATION_LOGGING) -chip_gn_arg_bool ("chip_error_logging" CONFIG_CHIP_ERROR_LOGGING) -chip_gn_arg_bool ("chip_openiotsdk_use_tfm" TFM_SUPPORT) -chip_gn_arg_bool ("chip_openiotsdk_use_psa_ps" CONFIG_CHIP_OPEN_IOT_SDK_USE_PSA_PS) +matter_common_gn_args( + DEBUG CONFIG_CHIP_DEBUG + LIB_SHELL CONFIG_CHIP_LIB_SHELL + LIB_TESTS CONFIG_CHIP_LIB_TESTS + PROJECT_CONFIG ${CONFIG_CHIP_PROJECT_CONFIG} +) +matter_add_gn_arg_string("target_cpu" "${CMAKE_SYSTEM_PROCESSOR}") +matter_add_gn_arg_string("openiotsdk_ar" ${CMAKE_AR}) +matter_add_gn_arg_string("openiotsdk_cc" ${CMAKE_C_COMPILER}) +matter_add_gn_arg_string("openiotsdk_cxx" ${CMAKE_CXX_COMPILER}) +matter_add_gn_arg_bool ("chip_monolithic_tests" CONFIG_CHIP_LIB_TESTS) +matter_add_gn_arg_bool ("chip_detail_logging" CONFIG_CHIP_DETAIL_LOGGING) +matter_add_gn_arg_bool ("chip_progress_logging" CONFIG_CHIP_PROGRESS_LOGGING) +matter_add_gn_arg_bool ("chip_automation_logging" CONFIG_CHIP_AUTOMATION_LOGGING) +matter_add_gn_arg_bool ("chip_error_logging" CONFIG_CHIP_ERROR_LOGGING) +matter_add_gn_arg_bool ("chip_openiotsdk_use_tfm" TFM_SUPPORT) +matter_add_gn_arg_bool ("chip_openiotsdk_use_psa_ps" CONFIG_CHIP_OPEN_IOT_SDK_USE_PSA_PS) if (TARGET cmsis-rtos-api) - chip_gn_arg_string("target_os" "cmsis-rtos") + matter_add_gn_arg_string("target_os" "cmsis-rtos") endif() -chip_gn_arg_string("optimize_debug_level" "s") +matter_add_gn_arg_string("optimize_debug_level" "s") -file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/args.tmp" CONTENT ${CHIP_GN_ARGS}) +matter_generate_args_tmp_file() # ============================================================================== -# Define 'chip-gn' target that builds CHIP library(ies) with GN build system +# Build chip library # ============================================================================== -ExternalProject_Add( - chip-gn - PREFIX ${CMAKE_CURRENT_BINARY_DIR} - SOURCE_DIR ${CHIP_ROOT} - BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} - CONFIGURE_COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/make_gn_args.py - @args.tmp > args.gn && - ${GN_EXECUTABLE} - --root=${CHIP_ROOT} - --root-target=${GN_ROOT_TARGET} - --dotfile=${GN_ROOT_TARGET}/.gn - --script-executable=${Python3_EXECUTABLE} - gen --check --fail-on-unused-args ${CMAKE_CURRENT_BINARY_DIR} - BUILD_COMMAND ninja - INSTALL_COMMAND "" - BUILD_BYPRODUCTS ${CHIP_LIBRARIES} - BUILD_ALWAYS TRUE - USES_TERMINAL_CONFIGURE TRUE - USES_TERMINAL_BUILD TRUE +matter_build(chip + LIB_SHELL ${CONFIG_CHIP_LIB_SHELL} + LIB_TESTS ${CONFIG_CHIP_LIB_TESTS} ) -# ============================================================================== -# Define 'openiotsdk-chip' target that exposes CHIP and Open IoT SDK -# headers & libraries to the application -# ============================================================================== -add_library(openiotsdk-chip INTERFACE) -target_compile_definitions(openiotsdk-chip INTERFACE CHIP_HAVE_CONFIG_H) -target_include_directories(openiotsdk-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(openiotsdk-chip INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/lib) -target_link_libraries(openiotsdk-chip INTERFACE -Wl,--start-group ${CHIP_LIBRARIES} -Wl,--end-group) -target_link_libraries(openiotsdk-chip INTERFACE - ${EXTERNAL_TARGETS} +target_link_libraries(chip INTERFACE + ${CONFIG_CHIP_EXTERNAL_TARGETS} ) -add_dependencies(openiotsdk-chip chip-gn) - -if (NOT ${CMAKE_BUILD_TYPE} STREQUAL "Debug") - target_compile_definitions(openiotsdk-chip INTERFACE - NDEBUG - ) -endif() diff --git a/config/openiotsdk/chip-gn/args.gni b/config/openiotsdk/chip-gn/args.gni index a468c14510aad9..fd9edf986f8ee7 100644 --- a/config/openiotsdk/chip-gn/args.gni +++ b/config/openiotsdk/chip-gn/args.gni @@ -19,6 +19,7 @@ import("${chip_root}/src/crypto/crypto.gni") chip_device_platform = "openiotsdk" chip_build_tests = false +chip_build_libshell = false chip_project_config_include = "" chip_system_project_config_include = "" diff --git a/config/openiotsdk/cmake/chip.cmake b/config/openiotsdk/cmake/chip.cmake index 9a7dcf629e3a06..bdef5870433d23 100644 --- a/config/openiotsdk/cmake/chip.cmake +++ b/config/openiotsdk/cmake/chip.cmake @@ -37,10 +37,16 @@ if(CONFIG_CHIP_OPEN_IOT_SDK_USE_PSA_PS AND NOT TFM_SUPPORT) message( FATAL_ERROR "You can not use PSA Protected Storage without TF-M support" ) endif() +if (${CMAKE_BUILD_TYPE} STREQUAL "Debug") + set(CONFIG_CHIP_DEBUG YES) +else() + set(CONFIG_CHIP_DEBUG NO) +endif() + # Add CHIP sources add_subdirectory(${OPEN_IOT_SDK_CONFIG} ./chip_build) -# Additional openiotsdk-chip target configuration +# Additional chip target configuration # TF-M support requires the right order of generating targets if(TFM_SUPPORT) diff --git a/config/openiotsdk/cmake/sdk.cmake b/config/openiotsdk/cmake/sdk.cmake index 52055cacdfc49a..25851c2d7ded2f 100644 --- a/config/openiotsdk/cmake/sdk.cmake +++ b/config/openiotsdk/cmake/sdk.cmake @@ -25,7 +25,7 @@ get_filename_component(OPEN_IOT_SDK_SOURCE ${CHIP_ROOT}/third_party/open-iot-sdk get_filename_component(OPEN_IOT_SDK_STORAGE_SOURCE ${CHIP_ROOT}/third_party/open-iot-sdk/storage REALPATH) # Open IoT SDK targets passed to CHIP build -list(APPEND EXTERNAL_TARGETS) +list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS) # Additional Open IoT SDK build configuration set(TFM_SUPPORT NO CACHE BOOL "Add Trusted Firmware-M (TF-M) support to application") @@ -193,7 +193,7 @@ if(TARGET mbedtls-config) endif() if("mcu-driver-reference-platforms-for-arm" IN_LIST IOTSDK_FETCH_LIST) - list(APPEND EXTERNAL_TARGETS + list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS mcu-driver-bootstrap mcu-driver-hal mdh-arm-corstone-300-common @@ -202,7 +202,7 @@ if("mcu-driver-reference-platforms-for-arm" IN_LIST IOTSDK_FETCH_LIST) endif() if("cmsis-5" IN_LIST IOTSDK_FETCH_LIST) - list(APPEND EXTERNAL_TARGETS + list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS cmsis-core cmsis-rtos-api iotsdk-ip-network-api @@ -210,13 +210,13 @@ if("cmsis-5" IN_LIST IOTSDK_FETCH_LIST) endif() if("cmsis-freertos" IN_LIST IOTSDK_FETCH_LIST) - list(APPEND EXTERNAL_TARGETS + list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS freertos-cmsis-rtos ) endif() if("mbedtls" IN_LIST IOTSDK_FETCH_LIST) - list(APPEND EXTERNAL_TARGETS + list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS mbedtls mbedtls-config mbedtls-threading-cmsis-rtos @@ -224,7 +224,7 @@ if("mbedtls" IN_LIST IOTSDK_FETCH_LIST) endif() if("lwip" IN_LIST IOTSDK_FETCH_LIST) - list(APPEND EXTERNAL_TARGETS + list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS lwipcore lwip-cmsis-port lwip-cmsis-sys @@ -234,14 +234,14 @@ if("lwip" IN_LIST IOTSDK_FETCH_LIST) endif() if("cmsis-sockets-api" IN_LIST IOTSDK_FETCH_LIST) - list(APPEND EXTERNAL_TARGETS + list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS cmsis-sockets-api lwip-sockets ) endif() if("trusted-firmware-m" IN_LIST IOTSDK_FETCH_LIST) - list(APPEND EXTERNAL_TARGETS + list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS tfm-ns-interface tfm-ns-interface-cmsis-rtos ) @@ -251,14 +251,14 @@ endif() # Add Open IoT SDK storage source add_subdirectory(${OPEN_IOT_SDK_STORAGE_SOURCE} ./sdk_storage_build) -list(APPEND EXTERNAL_TARGETS +list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS iotsdk-blockdevice iotsdk-tdbstore ) # Add custom storage library add_subdirectory(${OPEN_IOT_SDK_CONFIG}/storage storage_build) -list(APPEND EXTERNAL_TARGETS +list(APPEND CONFIG_CHIP_EXTERNAL_TARGETS openiotsdk-storage ) diff --git a/config/openiotsdk/make_gn_args.py b/config/openiotsdk/make_gn_args.py deleted file mode 100755 index 074645140deeb0..00000000000000 --- a/config/openiotsdk/make_gn_args.py +++ /dev/null @@ -1,78 +0,0 @@ -#!/usr/bin/env python3 - -# -# Copyright (c) 2022 Project CHIP Authors -# All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -import argparse -import re -import sys - -GN_SPECIAL_CHARACTERS = r'(["$\\])' -GN_CFLAG_EXCLUDES = [ - '-fno-asynchronous-unwind-tables', - '-fno-common', - '-fno-defer-pop', - '-fno-reorder-functions', - '-ffunction-sections', - '-fdata-sections', - '-g*', - '-O*', - '-W*', -] - - -def escape_strings(gn_args): - return [[key, re.sub(GN_SPECIAL_CHARACTERS, r'\\\1', value)] for key, value in gn_args] - - -def write_gn_args(args): - if args.module: - sys.stdout.write('import("{}")\n'.format(args.module)) - - for key, value in args.arg: - sys.stdout.write('{} = {}\n'.format(key, value)) - - for key, value in args.arg_string: - sys.stdout.write('{} = "{}"\n'.format(key, value)) - - for key, value in args.arg_cflags: - # Remove empty include paths - fix generator expressions issue - filtered_value = [x for x in value.split(",") if x != '"-isystem"'] - sys.stdout.write('{} = [{}]\n'.format( - key, ",".join(filtered_value))) - - cflag_excludes = ', '.join(['"{}"'.format(exclude) - for exclude in GN_CFLAG_EXCLUDES]) - - for key, value in args.arg_cflags_lang: - sys.stdout.write('{} = filter_exclude(string_split("{}"), [{}])\n'.format( - key, value, cflag_excludes)) - - -def main(): - parser = argparse.ArgumentParser(fromfile_prefix_chars='@') - parser.add_argument('--module', action='store') - parser.add_argument('--arg', action='append', nargs=2, default=[]) - parser.add_argument('--arg-string', action='append', nargs=2, default=[]) - parser.add_argument('--arg-cflags', action='append', nargs=2, default=[]) - parser.add_argument('--arg-cflags-lang', action='append', nargs=2, default=[]) - args = parser.parse_args() - write_gn_args(args) - - -if __name__ == "__main__": - main() diff --git a/config/telink/chip-module/CMakeLists.txt b/config/telink/chip-module/CMakeLists.txt index 91d108153bc68f..e1953b324d3c07 100644 --- a/config/telink/chip-module/CMakeLists.txt +++ b/config/telink/chip-module/CMakeLists.txt @@ -30,68 +30,6 @@ include(../../zephyr/ota-image.cmake) include(../../zephyr/zephyr-util.cmake) include(generate_factory_data.cmake) -# ============================================================================== -# Declare configuration variables and define constants -# ============================================================================== - -# C/C++ compiler flags passed to CHIP build system -list(APPEND CHIP_CFLAGS) - -# C compiler flags passed to CHIP build system -list(APPEND CHIP_CFLAGS_C) - -# C++ compiler flags passed to CHIP build system -list(APPEND CHIP_CFLAGS_CC) - -# CHIP libraries that the application should be linked with -list(APPEND CHIP_LIBRARIES) - -# GN meta-build system arguments passed to the make_gn_args.py script -string(APPEND CHIP_GN_ARGS) - -# C/C++ compiler flags which should not be forwarded to CHIP -# build system (e.g. because CHIP configures them on its own) -set(CHIP_CFLAG_EXCLUDES - "-fno-asynchronous-unwind-tables" - "-fno-common" - "-fno-defer-pop" - "-fno-reorder-functions" - "-ffunction-sections" - "-fdata-sections" - "-g*" - "-O*" - "-W*" -) - -# ============================================================================== -# Helper macros -# ============================================================================== - -macro(chip_gn_arg_import FILE) - string(APPEND CHIP_GN_ARGS "--module\n${FILE}\n") -endmacro() - -macro(chip_gn_arg_string ARG STRING) - string(APPEND CHIP_GN_ARGS "--arg-string\n${ARG}\n${STRING}\n") -endmacro() - -macro(chip_gn_arg_bool ARG) - if (${ARGN}) - string(APPEND CHIP_GN_ARGS "--arg\n${ARG}\ntrue\n") - else() - string(APPEND CHIP_GN_ARGS "--arg\n${ARG}\nfalse\n") - endif() -endmacro() - -macro(chip_gn_arg_cflags ARG CFLAGS) - string(APPEND CHIP_GN_ARGS "--arg-cflags\n${ARG}\n${CFLAGS}\n") -endmacro() - -macro(chip_gn_arg ARG VALUE) - string(APPEND CHIP_GN_ARGS "--arg\n${ARG}\n${VALUE}\n") -endmacro() - - # ============================================================================== # Prepare CHIP configuration based on the project Kconfig configuration # ============================================================================== @@ -99,48 +37,29 @@ endmacro() if (NOT CHIP_ROOT) get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../.. REALPATH) endif() +get_filename_component(GN_ROOT_TARGET ${CHIP_ROOT}/config/telink/chip-gn REALPATH) +get_filename_component(COMMON_CMAKE_SOURCE_DIR ${CHIP_ROOT}/config/common/cmake REALPATH) -set(GN_ROOT_TARGET ${CHIP_ROOT}/config/telink/chip-gn) - -if (CONFIG_POSIX_API) - list(APPEND CHIP_CFLAGS - -D_SYS__PTHREADTYPES_H_ - -isystem${ZEPHYR_BASE}/include/zephyr/posix - ) -endif() - -zephyr_include_directories(${CHIP_ROOT}/src/platform/telink/) - -zephyr_get_compile_flags(CHIP_CFLAGS_C C) -zephyr_get_compile_flags(CHIP_CFLAGS_CC CXX) -zephyr_get_gnu_cpp_standard(CHIP_CFLAGS_CC) +# Get common Cmake sources -convert_list_of_flags_to_string_of_flags(CHIP_CFLAGS CHIP_CFLAGS) -convert_list_of_flags_to_string_of_flags(CHIP_CFLAGS_C CHIP_CFLAGS_C) -convert_list_of_flags_to_string_of_flags(CHIP_CFLAGS_CC CHIP_CFLAGS_CC) +include(${COMMON_CMAKE_SOURCE_DIR}/chip_gn_args.cmake) +include(${COMMON_CMAKE_SOURCE_DIR}/chip_gn.cmake) -# Prepare CHIP libraries that the application should be linked with +# Prepare compiler flags -if (NOT CHIP_LIBRARIES) - set(CHIP_LIBRARIES -lCHIP) -endif() - -if (CONFIG_CHIP_LIB_SHELL) - list(APPEND CHIP_LIBRARIES -lCHIPShell) +if (CONFIG_POSIX_API) + matter_add_flags(-D_SYS__PTHREADTYPES_H_) + matter_add_flags(-isystem${ZEPHYR_BASE}/include/zephyr/posix) endif() -# Set up CHIP project configuration file +zephyr_include_directories(${CHIP_ROOT}/src/platform/telink/) -if (CONFIG_CHIP_PROJECT_CONFIG) - get_filename_component(CHIP_PROJECT_CONFIG - ${CONFIG_CHIP_PROJECT_CONFIG} - REALPATH - BASE_DIR ${CMAKE_SOURCE_DIR} - ) - set(CHIP_PROJECT_CONFIG "<${CHIP_PROJECT_CONFIG}>") -else() - set(CHIP_PROJECT_CONFIG "") -endif() +zephyr_get_compile_flags(ZEPHYR_CFLAGS_C C) +matter_add_cflags("${ZEPHYR_CFLAGS_C}") +zephyr_get_compile_flags(ZEPHYR_CFLAGS_CC CXX) +matter_add_cxxflags("${ZEPHYR_CFLAGS_CC}") +zephyr_get_gnu_cpp_standard(ZEPHYR_GNU_CPP_STD) +matter_add_cxxflags(${ZEPHYR_GNU_CPP_STD}) # Set up custom OpenThread configuration @@ -153,147 +72,66 @@ if (CONFIG_CHIP_OPENTHREAD_CONFIG) zephyr_set_openthread_config(${CHIP_OPENTHREAD_CONFIG}) endif() -# 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 - ) - - 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() - -find_package(Python3 REQUIRED) - # ============================================================================== # Generate configuration for CHIP GN build system # ============================================================================== -chip_gn_arg_cflags("target_cflags" ${CHIP_CFLAGS}) -chip_gn_arg_cflags("target_cflags_c" ${CHIP_CFLAGS_C}) -chip_gn_arg_cflags("target_cflags_cc" ${CHIP_CFLAGS_CC}) -chip_gn_arg_string("zephyr_ar" ${CMAKE_AR}) -chip_gn_arg_string("zephyr_cc" ${CMAKE_C_COMPILER}) -chip_gn_arg_string("zephyr_cxx" ${CMAKE_CXX_COMPILER}) -chip_gn_arg_bool ("is_debug" CONFIG_DEBUG) -chip_gn_arg_bool ("chip_logging" CONFIG_LOG) -chip_gn_arg_bool ("chip_enable_openthread" CONFIG_NET_L2_OPENTHREAD) -chip_gn_arg_bool ("chip_openthread_ftd" CONFIG_OPENTHREAD_FTD) -chip_gn_arg_bool ("chip_config_network_layer_ble" CONFIG_BT) -chip_gn_arg_bool ("chip_inet_config_enable_ipv4" CONFIG_NET_IPV4) -chip_gn_arg_bool ("chip_enable_ota_requestor" CONFIG_CHIP_OTA_REQUESTOR) -chip_gn_arg_bool ("chip_build_tests" CONFIG_CHIP_BUILD_TESTS) -chip_gn_arg_bool ("chip_inet_config_enable_tcp_endpoint" CONFIG_CHIP_BUILD_TESTS) -chip_gn_arg_bool ("chip_build_libshell" CONFIG_CHIP_LIB_SHELL) -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) +matter_common_gn_args( + DEBUG CONFIG_DEBUG + LIB_SHELL CONFIG_CHIP_LIB_SHELL + LIB_TESTS CONFIG_CHIP_BUILD_TESTS + PROJECT_CONFIG ${CONFIG_CHIP_PROJECT_CONFIG} + DEVICE_INFO_EXAMPLE_PROVIDER CONFIG_CHIP_EXAMPLE_DEVICE_INFO_PROVIDER +) +matter_add_gn_arg_string("zephyr_ar" ${CMAKE_AR}) +matter_add_gn_arg_string("zephyr_cc" ${CMAKE_C_COMPILER}) +matter_add_gn_arg_string("zephyr_cxx" ${CMAKE_CXX_COMPILER}) +matter_add_gn_arg_bool ("chip_logging" CONFIG_LOG) +matter_add_gn_arg_bool ("chip_enable_openthread" CONFIG_NET_L2_OPENTHREAD) +matter_add_gn_arg_bool ("chip_openthread_ftd" CONFIG_OPENTHREAD_FTD) +matter_add_gn_arg_bool ("chip_config_network_layer_ble" CONFIG_BT) +matter_add_gn_arg_bool ("chip_inet_config_enable_ipv4" CONFIG_NET_IPV4) +matter_add_gn_arg_bool ("chip_enable_ota_requestor" CONFIG_CHIP_OTA_REQUESTOR) +matter_add_gn_arg_bool ("chip_inet_config_enable_tcp_endpoint" CONFIG_CHIP_BUILD_TESTS) +matter_add_gn_arg_bool ("chip_error_logging" CONFIG_MATTER_LOG_LEVEL GREATER_EQUAL 1) +matter_add_gn_arg_bool ("chip_progress_logging" CONFIG_MATTER_LOG_LEVEL GREATER_EQUAL 3) +matter_add_gn_arg_bool ("chip_detail_logging" CONFIG_MATTER_LOG_LEVEL GREATER_EQUAL 4) +matter_add_gn_arg_bool ("chip_automation_logging" FALSE) 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") + matter_add_gn_arg_bool ("chip_use_transitional_commissionable_data_provider" "false") + matter_add_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") + matter_add_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") + matter_add_gn_arg_bool("chip_enable_rotating_device_id" "true") + matter_add_gn_arg_bool("chip_enable_additional_data_advertising" "true") endif() if (CONFIG_CHIP_ENABLE_DNSSD_SRP) - chip_gn_arg_string("chip_mdns" "platform") -endif() - -if (CHIP_PROJECT_CONFIG) - chip_gn_arg_string("chip_project_config_include" ${CHIP_PROJECT_CONFIG}) - chip_gn_arg_string("chip_system_project_config_include" ${CHIP_PROJECT_CONFIG}) + matter_add_gn_arg_string("chip_mdns" "platform") endif() if (CONFIG_CHIP_PW_RPC) set(PIGWEED_DIR "//third_party/pigweed/repo") - chip_gn_arg_string("pw_assert_BACKEND" ${PIGWEED_DIR}/pw_assert_log:check_backend) - chip_gn_arg_string("pw_log_BACKEND" ${PIGWEED_DIR}/pw_log_basic) - chip_gn_arg("pw_build_LINK_DEPS" [\"${PIGWEED_DIR}/pw_assert:impl\",\ \"${PIGWEED_DIR}/pw_log:impl\"]) -endif() - -if (CONFIG_CHIP_EXAMPLE_DEVICE_INFO_PROVIDER) - chip_gn_arg_bool("chip_build_example_providers" "true") - list(APPEND CHIP_LIBRARIES -lMatterDeviceInfoProviderExample) + matter_add_gn_arg_string("pw_assert_BACKEND" ${PIGWEED_DIR}/pw_assert_log:check_backend) + matter_add_gn_arg_string("pw_log_BACKEND" ${PIGWEED_DIR}/pw_log_basic) + matter_add_gn_arg("pw_build_LINK_DEPS" [\"${PIGWEED_DIR}/pw_assert:impl\",\ \"${PIGWEED_DIR}/pw_log:impl\"]) endif() -file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/args.tmp" CONTENT ${CHIP_GN_ARGS}) - -# ============================================================================== -# Define 'chip-gn' target that builds CHIP library(ies) with GN build system -# ============================================================================== -ExternalProject_Add( - chip-gn - PREFIX ${CMAKE_CURRENT_BINARY_DIR} - SOURCE_DIR ${CHIP_ROOT} - BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} - CONFIGURE_COMMAND "" - BUILD_COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/make_gn_args.py - @args.tmp > args.gn && - ${GN_EXECUTABLE} - --root=${CHIP_ROOT} - --root-target=${GN_ROOT_TARGET} - --dotfile=${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) +matter_generate_args_tmp_file() # ============================================================================== -# Define 'chip' target that exposes CHIP headers & libraries to the application +# Build chip library # ============================================================================== - -zephyr_interface_library_named(chip) -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 +matter_build(chip + LIB_SHELL ${CONFIG_CHIP_LIB_SHELL} + DEVICE_INFO_EXAMPLE_PROVIDER ${CONFIG_CHIP_EXAMPLE_DEVICE_INFO_PROVIDER} + GN_DEPENDENCIES kernel ) -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() - -target_link_libraries(chip INTERFACE -Wl,--start-group ${CHIP_LIBRARIES} -Wl,--end-group) - -add_dependencies(chip chip-gn) +set_property(GLOBAL APPEND PROPERTY ZEPHYR_INTERFACE_LIBS chip) # ============================================================================== # Define 'chip-ota-image' target for building CHIP OTA image diff --git a/config/zephyr/chip-module/CMakeLists.txt b/config/zephyr/chip-module/CMakeLists.txt index ba25a316f57a2e..6d84a962a69cc0 100644 --- a/config/zephyr/chip-module/CMakeLists.txt +++ b/config/zephyr/chip-module/CMakeLists.txt @@ -1,5 +1,5 @@ # -# Copyright (c) 2022 Project CHIP Authors +# Copyright (c) 2022-2023 Project CHIP Authors # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -28,123 +28,41 @@ if (CONFIG_CHIP) include(ExternalProject) include(../zephyr-util.cmake) -# ============================================================================== -# Declare configuration variables and define constants -# ============================================================================== - -# C/C++ compiler flags passed to CHIP build system -list(APPEND CHIP_CFLAGS) - -# C compiler flags passed to CHIP build system -list(APPEND CHIP_CFLAGS_C) - -# C++ compiler flags passed to CHIP build system -list(APPEND CHIP_CFLAGS_CC) - -# CHIP libraries that the application should be linked with -list(APPEND CHIP_LIBRARIES) - -# GN meta-build system arguments in the form of 'key1 = value1\nkey2 = value2...' string -string(APPEND CHIP_GN_ARGS) - -# C/C++ compiler flags which should not be forwarded to CHIP -# build system (e.g. because CHIP configures them on its own) -set(CHIP_CFLAG_EXCLUDES - "-fno-asynchronous-unwind-tables" - "-fno-common" - "-fno-defer-pop" - "-fno-reorder-functions" - "-ffunction-sections" - "-fdata-sections" - "-g*" - "-O*" - "-W*" -) - -# ============================================================================== -# Helper macros -# ============================================================================== - -macro(chip_gn_arg_import FILE) - string(APPEND CHIP_GN_ARGS "import(\"${FILE}\")\n") -endmacro() - -macro(chip_gn_arg_string ARG STRING) - string(APPEND CHIP_GN_ARGS "${ARG} = \"${STRING}\"\n") -endmacro() - -macro(chip_gn_arg_bool ARG BOOLEAN) - if (${BOOLEAN}) - string(APPEND CHIP_GN_ARGS "${ARG} = true\n") - else() - string(APPEND CHIP_GN_ARGS "${ARG} = false\n") - endif() -endmacro() - -macro(chip_gn_arg_cflags ARG CFLAGS) - set(CFLAG_EXCLUDES "[") - foreach(cflag ${CHIP_CFLAG_EXCLUDES}) - string(APPEND CFLAG_EXCLUDES "\"${cflag}\", ") - endforeach() - string(APPEND CFLAG_EXCLUDES "]") - string(APPEND CHIP_GN_ARGS "${ARG} = filter_exclude(string_split(\"${CFLAGS}\"), ${CFLAG_EXCLUDES})\n") -endmacro() - +# # ============================================================================== # Prepare CHIP configuration based on the project Kconfig configuration # ============================================================================== - +# Set paths if (NOT CHIP_ROOT) get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../.. REALPATH) endif() +get_filename_component(GN_ROOT_TARGET ${CHIP_ROOT}/config/zephyr/chip-gn REALPATH) +get_filename_component(COMMON_CMAKE_SOURCE_DIR ${CHIP_ROOT}/config/common/cmake REALPATH) -set(GN_ROOT_TARGET ${CHIP_ROOT}/config/zephyr/chip-gn) - -if (CONFIG_POSIX_API) - list(APPEND CHIP_CFLAGS - -D_SYS__PTHREADTYPES_H_ - -isystem${ZEPHYR_BASE}/include/zephyr/posix - ) -endif() - -list(APPEND CHIP_CFLAGS -isystem${ZEPHYR_BASE}/../modules/crypto/mbedtls/include/) - -zephyr_get_compile_flags(CHIP_CFLAGS_C C) -zephyr_get_compile_flags(CHIP_CFLAGS_CC CXX) -zephyr_get_gnu_cpp_standard(CHIP_CFLAGS_CC) - -convert_list_of_flags_to_string_of_flags(CHIP_CFLAGS CHIP_CFLAGS) -convert_list_of_flags_to_string_of_flags(CHIP_CFLAGS_C CHIP_CFLAGS_C) -convert_list_of_flags_to_string_of_flags(CHIP_CFLAGS_CC CHIP_CFLAGS_CC) - -# Prepare CHIP libraries that the application should be linked with - -if (NOT CHIP_LIBRARIES) - set(CHIP_LIBRARIES -lCHIP) -endif() - -if (CONFIG_CHIP_LIB_SHELL) - list(APPEND CHIP_LIBRARIES -lCHIPShell) -endif() - +# Additional configuration if (CONFIG_CHIP_PW_RPC) - list(APPEND CHIP_LIBRARIES -lPwRpc) + set(CONFIG_CHIP_LIB_PW_RPC YES) endif() +# Get common Cmake sources +include(${COMMON_CMAKE_SOURCE_DIR}/chip_gn_args.cmake) +include(${COMMON_CMAKE_SOURCE_DIR}/chip_gn.cmake) -# Set up CHIP project configuration file +# Prepare compiler flags +matter_add_flags(-isystem${ZEPHYR_BASE}/../modules/crypto/mbedtls/include/) -if (CONFIG_CHIP_PROJECT_CONFIG) - get_filename_component(CHIP_PROJECT_CONFIG - ${CONFIG_CHIP_PROJECT_CONFIG} - REALPATH - BASE_DIR ${CMAKE_SOURCE_DIR} - ) - set(CHIP_PROJECT_CONFIG "<${CHIP_PROJECT_CONFIG}>") -else() - set(CHIP_PROJECT_CONFIG "") +if (CONFIG_POSIX_API) + matter_add_flags(-D_SYS__PTHREADTYPES_H_) + matter_add_flags(-isystem${ZEPHYR_BASE}/include/zephyr/posix) endif() +zephyr_get_compile_flags(ZEPHYR_CFLAGS_C C) +matter_add_cflags(${ZEPHYR_CFLAGS_C}) +zephyr_get_compile_flags(ZEPHYR_CFLAGS_CC CXX) +matter_add_cxxflags(${ZEPHYR_CFLAGS_CC}) +zephyr_get_gnu_cpp_standard(ZEPHYR_GNU_CPP_STD) +matter_add_cxxflags(${ZEPHYR_GNU_CPP_STD}) + # Set up custom OpenThread configuration if (CONFIG_CHIP_OPENTHREAD_CONFIG) @@ -156,102 +74,39 @@ if (CONFIG_CHIP_OPENTHREAD_CONFIG) zephyr_set_openthread_config(${CHIP_OPENTHREAD_CONFIG}) endif() -# 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 1000) - execute_process( - COMMAND - ${GN_EXECUTABLE} --version - OUTPUT_VARIABLE gn_version_output - ERROR_VARIABLE gn_error_output - RESULT_VARIABLE gn_status - ) - - 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() - -find_package(Python3 REQUIRED) - # ============================================================================== # Generate configuration for CHIP GN build system # ============================================================================== -chip_gn_arg_cflags("target_cflags" ${CHIP_CFLAGS}) -chip_gn_arg_cflags("target_cflags_c" ${CHIP_CFLAGS_C}) -chip_gn_arg_cflags("target_cflags_cc" ${CHIP_CFLAGS_CC}) -chip_gn_arg_string("zephyr_ar" ${CMAKE_AR}) -chip_gn_arg_string("zephyr_cc" ${CMAKE_C_COMPILER}) -chip_gn_arg_string("zephyr_cxx" ${CMAKE_CXX_COMPILER}) -chip_gn_arg_string("chip_project_config_include" "${CHIP_PROJECT_CONFIG}") -chip_gn_arg_string("chip_system_project_config_include" "${CHIP_PROJECT_CONFIG}") -chip_gn_arg_bool ("is_debug" CONFIG_DEBUG) -chip_gn_arg_bool ("chip_enable_openthread" CONFIG_NET_L2_OPENTHREAD) -chip_gn_arg_bool ("chip_openthread_ftd" CONFIG_OPENTHREAD_FTD) -chip_gn_arg_bool ("chip_inet_config_enable_ipv4" CONFIG_NET_IPV4) -chip_gn_arg_bool ("chip_build_tests" CONFIG_CHIP_BUILD_TESTS) -chip_gn_arg_bool ("chip_inet_config_enable_tcp_endpoint" CONFIG_CHIP_BUILD_TESTS) -chip_gn_arg_bool ("chip_build_libshell" CONFIG_CHIP_LIB_SHELL) +matter_common_gn_args( + DEBUG CONFIG_DEBUG + LIB_SHELL CONFIG_CHIP_LIB_SHELL + LIB_TESTS CONFIG_CHIP_BUILD_TESTS + LIB_PW_RPC CONFIG_CHIP_PW_RPC + PROJECT_CONFIG ${CONFIG_CHIP_PROJECT_CONFIG} +) +matter_add_gn_arg_string("zephyr_ar" ${CMAKE_AR}) +matter_add_gn_arg_string("zephyr_cc" ${CMAKE_C_COMPILER}) +matter_add_gn_arg_string("zephyr_cxx" ${CMAKE_CXX_COMPILER}) +matter_add_gn_arg_bool ("chip_enable_openthread" CONFIG_NET_L2_OPENTHREAD) +matter_add_gn_arg_bool ("chip_openthread_ftd" CONFIG_OPENTHREAD_FTD) +matter_add_gn_arg_bool ("chip_inet_config_enable_ipv4" CONFIG_NET_IPV4) +matter_add_gn_arg_bool ("chip_inet_config_enable_tcp_endpoint" CONFIG_CHIP_BUILD_TESTS) if (CONFIG_CHIP_ENABLE_DNSSD_SRP) - chip_gn_arg_string("chip_mdns" "platform") + matter_add_gn_arg_string("chip_mdns" "platform") endif() -file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/args.gn CONTENT ${CHIP_GN_ARGS}) +matter_generate_args_tmp_file() # ============================================================================== -# Define 'chip-gn' target that builds CHIP library(ies) with GN build system +# Build chip library # ============================================================================== - -ExternalProject_Add( - chip-gn - PREFIX ${CMAKE_CURRENT_BINARY_DIR} - SOURCE_DIR ${CHIP_ROOT} - BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR} - CONFIGURE_COMMAND ${GN_EXECUTABLE} - --root=${CHIP_ROOT} - --root-target=${GN_ROOT_TARGET} - --dotfile=${GN_ROOT_TARGET}/.gn - --script-executable=${Python3_EXECUTABLE} - gen --check --fail-on-unused-args ${CMAKE_CURRENT_BINARY_DIR} - BUILD_COMMAND ninja - INSTALL_COMMAND "" - BUILD_BYPRODUCTS ${CHIP_LIBRARIES} - BUILD_ALWAYS TRUE - USES_TERMINAL_CONFIGURE TRUE - USES_TERMINAL_BUILD TRUE +matter_build(chip + LIB_SHELL ${CONFIG_CHIP_LIB_SHELL} + LIB_PW_RPC ${CONFIG_CHIP_PW_RPC} + LIB_TESTS ${CONFIG_CHIP_BUILD_TESTS} + GN_DEPENDENCIES kernel ) -add_dependencies(chip-gn kernel) - -# ============================================================================== -# Define 'chip' target that exposes CHIP headers & libraries to the application -# ============================================================================== - -zephyr_interface_library_named(chip) -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 - ${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() - -target_link_libraries(chip INTERFACE -Wl,--start-group ${CHIP_LIBRARIES} -Wl,--end-group) -add_dependencies(chip chip-gn) +set_property(GLOBAL APPEND PROPERTY ZEPHYR_INTERFACE_LIBS chip) endif() # CONFIG_CHIP \ No newline at end of file diff --git a/config/zephyr/chip-module/make_gn_args.py b/config/zephyr/chip-module/make_gn_args.py deleted file mode 100644 index f30cd459a6e7d4..00000000000000 --- a/config/zephyr/chip-module/make_gn_args.py +++ /dev/null @@ -1,73 +0,0 @@ -#!/usr/bin/env python3 - -# -# Copyright (c) 2021 Project CHIP Authors -# All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -import argparse -import re -import sys - -GN_SPECIAL_CHARACTERS = r'(["$\\])' -GN_CFLAG_EXCLUDES = [ - '-fno-asynchronous-unwind-tables', - '-fno-common', - '-fno-defer-pop', - '-fno-reorder-functions', - '-ffunction-sections', - '-fdata-sections', - '-g*', - '-O*', - '-W*', -] - - -def escape_strings(gn_args): - return [[key, re.sub(GN_SPECIAL_CHARACTERS, r'\\\1', value)] for key, value in gn_args] - - -def write_gn_args(args): - if args.module: - sys.stdout.write('import("{}")\n'.format(args.module)) - - for key, value in args.arg: - sys.stdout.write('{} = {}\n'.format(key, value)) - - for key, value in args.arg_string: - sys.stdout.write('{} = "{}"\n'.format(key, value)) - - cflag_excludes = ', '.join(['"{}"'.format(exclude) - for exclude in GN_CFLAG_EXCLUDES]) - - for key, value in args.arg_cflags: - sys.stdout.write('{} = filter_exclude(string_split("{}"), [{}])\n'.format( - key, value, cflag_excludes)) - - -def main(): - parser = argparse.ArgumentParser(fromfile_prefix_chars='@') - parser.add_argument('--module', action='store') - parser.add_argument('--arg', action='append', nargs=2, default=[]) - parser.add_argument('--arg-string', action='append', nargs=2, default=[]) - parser.add_argument('--arg-cflags', action='append', nargs=2, default=[]) - args = parser.parse_args() - args.arg_string = escape_strings(args.arg_string) - args.arg_cflags = escape_strings(args.arg_cflags) - write_gn_args(args) - - -if __name__ == "__main__": - main() diff --git a/examples/all-clusters-app/mbed/main/AppTask.cpp b/examples/all-clusters-app/mbed/main/AppTask.cpp index d011f5f5ae3cdb..53628a0c636fca 100644 --- a/examples/all-clusters-app/mbed/main/AppTask.cpp +++ b/examples/all-clusters-app/mbed/main/AppTask.cpp @@ -26,7 +26,7 @@ #include #include -#include +#include static LEDWidget sStatusLED(MBED_CONF_APP_SYSTEM_STATE_LED); diff --git a/examples/all-clusters-minimal-app/mbed/main/AppTask.cpp b/examples/all-clusters-minimal-app/mbed/main/AppTask.cpp index 9343fc33899700..62317774c1520c 100644 --- a/examples/all-clusters-minimal-app/mbed/main/AppTask.cpp +++ b/examples/all-clusters-minimal-app/mbed/main/AppTask.cpp @@ -26,7 +26,7 @@ #include #include -#include +#include static LEDWidget sStatusLED(MBED_CONF_APP_SYSTEM_STATE_LED); diff --git a/examples/common/pigweed/mbed/Rpc.cpp b/examples/common/pigweed/mbed/Rpc.cpp index 65e9d48824160a..79940b005eba06 100644 --- a/examples/common/pigweed/mbed/Rpc.cpp +++ b/examples/common/pigweed/mbed/Rpc.cpp @@ -28,7 +28,7 @@ #include "pw_sys_io/sys_io.h" #include "pw_sys_io_mbed/init.h" -#include +#include #ifdef CHIP_PW_RPC_ECHO_PROTO #include "pw_rpc/echo_service_nanopb.h" diff --git a/examples/platform/openiotsdk/app/CMakeLists.txt b/examples/platform/openiotsdk/app/CMakeLists.txt index 0a37f4d2b495a5..4d566fa2e37cd6 100644 --- a/examples/platform/openiotsdk/app/CMakeLists.txt +++ b/examples/platform/openiotsdk/app/CMakeLists.txt @@ -30,7 +30,7 @@ target_include_directories(openiotsdk-app target_link_libraries(openiotsdk-app PUBLIC - openiotsdk-chip + chip $ ) diff --git a/src/inet/tests/TestInetCommonPosix.cpp b/src/inet/tests/TestInetCommonPosix.cpp index 8a187600375e07..32554adfe0cbd5 100644 --- a/src/inet/tests/TestInetCommonPosix.cpp +++ b/src/inet/tests/TestInetCommonPosix.cpp @@ -83,7 +83,7 @@ System::LayerImpl gSystemLayer; Inet::UDPEndPointManagerImpl gUDP; Inet::TCPEndPointManagerImpl gTCP; -#if CHIP_SYSTEM_CONFIG_USE_LWIP +#if CHIP_SYSTEM_CONFIG_USE_LWIP && !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) static sys_mbox_t * sLwIPEventQueue = NULL; static unsigned int sLwIPAcquireCount = 0; @@ -122,7 +122,7 @@ static std::vector sNetIFs; // interface to filter static bool NetworkIsReady(); static void OnLwIPInitComplete(void * arg); -#endif // CHIP_SYSTEM_CONFIG_USE_LWIP +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP && !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) char gDefaultTapDeviceName[32]; bool gDone = false; @@ -161,9 +161,9 @@ void ShutdownTestInetCommon() void InitSystemLayer() { -#if CHIP_SYSTEM_CONFIG_USE_LWIP +#if CHIP_SYSTEM_CONFIG_USE_LWIP && !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) AcquireLwIP(); -#endif // !CHIP_SYSTEM_CONFIG_USE_LWIP +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP && !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) gSystemLayer.Init(); } @@ -172,12 +172,12 @@ void ShutdownSystemLayer() { gSystemLayer.Shutdown(); -#if CHIP_SYSTEM_CONFIG_USE_LWIP +#if CHIP_SYSTEM_CONFIG_USE_LWIP && !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) ReleaseLwIP(); -#endif // CHIP_SYSTEM_CONFIG_USE_LWIP +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP && !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) } -#if CHIP_SYSTEM_CONFIG_USE_LWIP +#if CHIP_SYSTEM_CONFIG_USE_LWIP && !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) static void PrintNetworkState() { char intfName[chip::Inet::InterfaceId::kMaxIfNameLength]; @@ -222,11 +222,11 @@ static void PrintNetworkState() } } } -#endif // CHIP_SYSTEM_CONFIG_USE_LWIP +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP && !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) void InitNetwork() { -#if CHIP_SYSTEM_CONFIG_USE_LWIP +#if CHIP_SYSTEM_CONFIG_USE_LWIP && !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) // If an tap device name hasn't been specified, derive one from the IPv6 interface id. @@ -287,9 +287,8 @@ void InitNetwork() } } #endif // CHIP_TARGET_STYLE_UNIX -#if !defined(CHIP_DEVICE_LAYER_TARGET_OPEN_IOT_SDK) + tcpip_init(OnLwIPInitComplete, NULL); -#endif // !defined(CHIP_DEVICE_LAYER_TARGET_OPEN_IOT_SDK) // Lock LwIP stack LOCK_TCPIP_CORE(); @@ -420,7 +419,7 @@ void InitNetwork() AcquireLwIP(); -#endif // CHIP_SYSTEM_CONFIG_USE_LWIP +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP && !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) gTCP.Init(gSystemLayer); gUDP.Init(gSystemLayer); @@ -432,7 +431,7 @@ void ServiceEvents(uint32_t aSleepTimeMilliseconds) if (!printed) { -#if CHIP_SYSTEM_CONFIG_USE_LWIP +#if CHIP_SYSTEM_CONFIG_USE_LWIP && !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) if (NetworkIsReady()) #endif { @@ -485,7 +484,7 @@ void ServiceEvents(uint32_t aSleepTimeMilliseconds) #endif // CHIP_SYSTEM_CONFIG_USE_LWIP } -#if CHIP_SYSTEM_CONFIG_USE_LWIP +#if CHIP_SYSTEM_CONFIG_USE_LWIP && !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) static bool NetworkIsReady() { bool ready = true; @@ -509,7 +508,7 @@ static void OnLwIPInitComplete(void * arg) printf("Waiting for addresses assignment...\n"); } -#endif // CHIP_SYSTEM_CONFIG_USE_LWIP +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP && !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) void ShutdownNetwork() { @@ -524,7 +523,7 @@ void ShutdownNetwork() return Loop::Continue; }); gUDP.Shutdown(); -#if CHIP_SYSTEM_CONFIG_USE_LWIP +#if CHIP_SYSTEM_CONFIG_USE_LWIP && !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) ReleaseLwIP(); #endif } diff --git a/src/lib/support/tests/TestCHIPArgParser.cpp b/src/lib/support/tests/TestCHIPArgParser.cpp index 76d079c7bab33b..c54e0223ca7e07 100644 --- a/src/lib/support/tests/TestCHIPArgParser.cpp +++ b/src/lib/support/tests/TestCHIPArgParser.cpp @@ -546,6 +546,7 @@ static void UnknownOptionTest_UnknownLongOptionAfterArgs() VerifyArgErrorContains(0, "--barf"); } +#ifndef CHIP_CONFIG_NON_POSIX_LONG_OPT static void UnknownOptionTest_IgnoreUnknownLongOption() { bool res; @@ -578,6 +579,7 @@ static void UnknownOptionTest_IgnoreUnknownLongOption() VerifyNonOptionArg(2, "non-opt-arg-1"); VerifyNonOptionArg(3, "non-opt-arg-2"); } +#endif // !CHIP_CONFIG_NON_POSIX_LONG_OPT static void UnknownOptionTest_IgnoreUnknownShortOption() { diff --git a/src/platform/mbed/Logging.h b/src/platform/mbed/Logging.h index ce1ce9c8c2a2c5..df327141945fba 100644 --- a/src/platform/mbed/Logging.h +++ b/src/platform/mbed/Logging.h @@ -23,7 +23,7 @@ #pragma once -#include +#include namespace chip { namespace Logging { diff --git a/src/platform/openiotsdk/SystemPlatformConfig.h b/src/platform/openiotsdk/SystemPlatformConfig.h index c17393375bbe4c..bd2e9b39c86786 100644 --- a/src/platform/openiotsdk/SystemPlatformConfig.h +++ b/src/platform/openiotsdk/SystemPlatformConfig.h @@ -39,4 +39,6 @@ #define CHIP_SYSTEM_CONFIG_USE_LWIP_MONOTONIC_TIME 0 #endif // CHIP_SYSTEM_CONFIG_USE_LWIP_MONOTONIC_TIME +#define CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT 1 + // ========== Platform-specific Configuration Overrides ========= diff --git a/src/platform/tests/TestKeyValueStoreMgr.cpp b/src/platform/tests/TestKeyValueStoreMgr.cpp index ec2c35723a043c..fad7e490078069 100644 --- a/src/platform/tests/TestKeyValueStoreMgr.cpp +++ b/src/platform/tests/TestKeyValueStoreMgr.cpp @@ -260,6 +260,7 @@ static void TestKeyValueStoreMgr_NonExistentDelete(nlTestSuite * inSuite, void * NL_TEST_ASSERT(inSuite, err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND); } +#if !defined(__ZEPHYR__) && !defined(__MBED__) static void TestKeyValueStoreMgr_MultiRead(nlTestSuite * inSuite, void * inContext) { constexpr const char * kTestKey = "multi_key"; @@ -283,6 +284,7 @@ static void TestKeyValueStoreMgr_MultiRead(nlTestSuite * inSuite, void * inConte err = KeyValueStoreMgr().Delete(kTestKey); NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR); } +#endif #ifdef __ZEPHYR__ static void TestKeyValueStoreMgr_DoFactoryReset(nlTestSuite * inSuite, void * inContext) diff --git a/src/system/SystemConfig.h b/src/system/SystemConfig.h index 4b8680b8f6b782..17fbe26101fe32 100644 --- a/src/system/SystemConfig.h +++ b/src/system/SystemConfig.h @@ -595,6 +595,23 @@ struct LwIPEvent; #endif #endif // CHIP_SYSTEM_CONFIG_USE_LWIP_MONOTONIC_TIME +/** + * @def CHIP_SYSTEM_CONFIG_USE_LWIP_SKIP_INIT + * + * @brief + * Skip LwIP initalization during network setup. + * + * The platform can provide its own LwIP initialization. + * This option allows skipping initialization steps in network setup e.g. in unit tests. + * + * This configuration is overridden if CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT is set. + */ +#if CHIP_SYSTEM_CONFIG_USE_LWIP +#ifndef CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT +#define CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT 0 +#endif // CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP + /** * @def CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD * diff --git a/src/system/tests/TestSystemTimer.cpp b/src/system/tests/TestSystemTimer.cpp index 9188d63656a06a..bc5d3bc6206870 100644 --- a/src/system/tests/TestSystemTimer.cpp +++ b/src/system/tests/TestSystemTimer.cpp @@ -625,14 +625,13 @@ static int TestSetup(void * aContext) return FAILURE; } -#if !defined(CHIP_DEVICE_LAYER_TARGET_OPEN_IOT_SDK) -#if CHIP_SYSTEM_CONFIG_USE_LWIP && LWIP_VERSION_MAJOR == 2 && LWIP_VERSION_MINOR == 0 +#if CHIP_SYSTEM_CONFIG_USE_LWIP && (LWIP_VERSION_MAJOR == 2) && (LWIP_VERSION_MINOR == 0) && !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) static sys_mbox_t * sLwIPEventQueue = NULL; sys_mbox_new(sLwIPEventQueue, 100); tcpip_init(NULL, NULL); -#endif // CHIP_SYSTEM_CONFIG_USE_LWIP && LWIP_VERSION_MAJOR == 2 && LWIP_VERSION_MINOR == 0 -#endif // !defined(CHIP_DEVICE_LAYER_TARGET_OPEN_IOT_SDK) +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP && (LWIP_VERSION_MAJOR == 2) && (LWIP_VERSION_MINOR == 0) && + // !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) sLayer.Init(); @@ -652,11 +651,10 @@ static int TestTeardown(void * aContext) lContext.mLayer->Shutdown(); -#if !defined(CHIP_DEVICE_LAYER_TARGET_OPEN_IOT_SDK) -#if CHIP_SYSTEM_CONFIG_USE_LWIP && (LWIP_VERSION_MAJOR == 2) && (LWIP_VERSION_MINOR == 0) +#if CHIP_SYSTEM_CONFIG_USE_LWIP && (LWIP_VERSION_MAJOR == 2) && (LWIP_VERSION_MINOR == 0) && !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) tcpip_finish(NULL, NULL); -#endif // CHIP_SYSTEM_CONFIG_USE_LWIP && (LWIP_VERSION_MAJOR == 2) && (LWIP_VERSION_MINOR == 0) -#endif // !defined(CHIP_DEVICE_LAYER_TARGET_OPEN_IOT_SDK) +#endif // CHIP_SYSTEM_CONFIG_USE_LWIP && (LWIP_VERSION_MAJOR == 2) && (LWIP_VERSION_MINOR == 0) && + // !(CHIP_SYSTEM_CONFIG_LWIP_SKIP_INIT) ::chip::Platform::MemoryShutdown(); return (SUCCESS); diff --git a/src/test_driver/mbed/unit_tests/main/main.cpp b/src/test_driver/mbed/unit_tests/main/main.cpp index 40e4bf39ff0023..f3fbc445eb4773 100644 --- a/src/test_driver/mbed/unit_tests/main/main.cpp +++ b/src/test_driver/mbed/unit_tests/main/main.cpp @@ -17,9 +17,9 @@ */ #include "netsocket/WiFiInterface.h" +#include +#include #include -#include -#include using namespace ::chip; using namespace ::chip::Logging::Platform; diff --git a/src/test_driver/openiotsdk/unit-tests/CMakeLists.txt b/src/test_driver/openiotsdk/unit-tests/CMakeLists.txt index 15470ba12589f8..e9c0f48953cfde 100644 --- a/src/test_driver/openiotsdk/unit-tests/CMakeLists.txt +++ b/src/test_driver/openiotsdk/unit-tests/CMakeLists.txt @@ -22,6 +22,13 @@ get_filename_component(OPEN_IOT_SDK_EXAMPLE_COMMON ${CHIP_ROOT}/examples/platfor list(APPEND CMAKE_MODULE_PATH ${OPEN_IOT_SDK_CONFIG}/cmake) +# Application CHIP build configuration +set(CONFIG_CHIP_LIB_TESTS YES) +set(CONFIG_CHIP_DETAIL_LOGGING NO) +set(CONFIG_CHIP_PROGRESS_LOGGING NO) +set(CONFIG_CHIP_AUTOMATION_LOGGING YES) +set(CONFIG_CHIP_ERROR_LOGGING NO) + # Toolchain files need to exist before first call to project include(toolchain) @@ -39,13 +46,6 @@ if(TARGET lwip-cmsis-port) ) endif() -# Application CHIP build configuration -set(CONFIG_CHIP_LIB_TESTS YES) -set(CONFIG_CHIP_DETAIL_LOGGING NO) -set(CONFIG_CHIP_PROGRESS_LOGGING NO) -set(CONFIG_CHIP_AUTOMATION_LOGGING YES) -set(CONFIG_CHIP_ERROR_LOGGING NO) - include(chip) add_subdirectory(${OPEN_IOT_SDK_EXAMPLE_COMMON}/app ./app_build) @@ -74,7 +74,6 @@ foreach(TEST_NAME IN LISTS TEST_NAMES_FROM_FILE) target_link_options(${TEST_NAME} PUBLIC -Wl,--whole-archive "${CMAKE_CURRENT_BINARY_DIR}/chip_build/lib/lib${TEST_NAME}.a" - -Wl,--no-whole-archive "${CMAKE_CURRENT_BINARY_DIR}/chip_build/lib/libCHIP_tests.a" -Wl,--no-whole-archive) # set_target_link requires APP_TARGET to be defined diff --git a/src/test_driver/openiotsdk/unit-tests/main/main.cpp b/src/test_driver/openiotsdk/unit-tests/main/main.cpp index 8c41fd04d68593..947e2b9f55137a 100644 --- a/src/test_driver/openiotsdk/unit-tests/main/main.cpp +++ b/src/test_driver/openiotsdk/unit-tests/main/main.cpp @@ -19,8 +19,8 @@ #include #include +#include #include -#include #include