2 # Copyright (c) 2020 Project CHIP Authors
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
19 # CMake module for configuring and building CHIP libraries to be used
20 # in Zephyr applications.
22 # It is assumed that at this point:
23 # - CHIP_ROOT is defined
24 # - find_package(Zephyr) has been called
27 # ==================================================
29 # ==================================================
31 include(ExternalProject)
33 # Directory for CHIP build artifacts
34 set(CHIP_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/chip")
36 # Define Zephyr C/C++ compiler flags which should not be forwarded to CHIP
37 # build system (e.g. because CHIP configures them on its own).
38 set(CHIP_CFLAG_EXCLUDES
39 "-fno-asynchronous-unwind-tables"
42 "-fno-reorder-functions"
50 macro(chip_gn_arg_bool_if CONDITION ARGSTRING GN_VARNAME)
52 string(APPEND ${ARGSTRING} "${GN_VARNAME} = true\n")
54 string(APPEND ${ARGSTRING} "${GN_VARNAME} = false\n")
58 macro(chip_gn_arg_string ARGSTRING GN_STRING)
59 string(APPEND ${ARGSTRING} "${GN_STRING}\n")
62 macro(chip_gn_arg_cflags ARGSTRING ARG CFLAGS)
63 set(CFLAG_EXCLUDES "[")
64 foreach(cflag ${CHIP_CFLAG_EXCLUDES})
65 string(APPEND CFLAG_EXCLUDES "\"${cflag}\", ")
67 string(APPEND CFLAG_EXCLUDES "]")
68 string(APPEND ${ARGSTRING} "${ARG} = filter_exclude(string_split(\"${CFLAGS}\"), ${CFLAG_EXCLUDES})\n")
71 # Function to retrieve Zephyr compilation flags for the given language (C or CXX)
72 function(zephyr_get_compile_flags VAR LANG)
73 # We want to treat all zephyr-provided headers as system headers, so
74 # warnings in them don't trigger -Werror. That means that for the headers
75 # zephyr returns as "non-system" headers (the ones from
76 # zephyr_get_include_directories_for_lang) we need to manually replace "-I"
77 # with "-isystem" before adding them to INCLUDES.
79 zephyr_get_include_directories_for_lang(${LANG} temp_includes)
80 foreach(include ${temp_includes})
81 string(REPLACE "-I" "-isystem" include ${include})
82 list(APPEND INCLUDES ${include})
85 zephyr_get_system_include_directories_for_lang(${LANG} SYSTEM_INCLUDES)
86 zephyr_get_compile_definitions_for_lang(${LANG} DEFINES)
87 zephyr_get_compile_options_for_lang(${LANG} FLAGS)
88 set(${VAR} ${INCLUDES} ${SYSTEM_INCLUDES} ${DEFINES} ${FLAGS} ${${VAR}} PARENT_SCOPE)
91 function(chip_select_cpp_standard VAR)
93 set(${VAR} -std=gnu++11 PARENT_SCOPE)
94 elseif (CONFIG_STD_CPP14)
95 set(${VAR} -std=gnu++14 PARENT_SCOPE)
96 elseif (CONFIG_STD_CPP17)
97 set(${VAR} -std=gnu++17 PARENT_SCOPE)
98 elseif (CONFIG_STD_CPP2A)
99 set(${VAR} -std=gnu++20 PARENT_SCOPE)
101 message(FATAL_ERROR "Building with unsupported C++ standard")
105 # ==================================================
106 # Define chip configuration target
107 # ==================================================
109 function(chip_configure TARGET_NAME)
110 cmake_parse_arguments(CHIP
116 # Prepare CFLAGS & CXXFLAGS
117 zephyr_get_compile_flags(CHIP_CFLAGS C)
118 convert_list_of_flags_to_string_of_flags(CHIP_CFLAGS CHIP_CFLAGS)
120 zephyr_get_compile_flags(CHIP_CXXFLAGS CXX)
121 list(FILTER CHIP_CXXFLAGS EXCLUDE REGEX -std.*)
122 chip_select_cpp_standard(CHIP_CXX_STANDARD)
123 list(APPEND CHIP_CXXFLAGS ${CHIP_CXX_STANDARD})
124 convert_list_of_flags_to_string_of_flags(CHIP_CXXFLAGS CHIP_CXXFLAGS)
127 chip_gn_arg_cflags(GN_ARGS "target_cflags_c" ${CHIP_CFLAGS})
128 chip_gn_arg_cflags(GN_ARGS "target_cflags_cc" ${CHIP_CXXFLAGS})
129 chip_gn_arg_string(GN_ARGS "zephyr_ar = \"${CMAKE_AR}\"")
130 chip_gn_arg_string(GN_ARGS "zephyr_cc = \"${CMAKE_C_COMPILER}\"")
131 chip_gn_arg_string(GN_ARGS "zephyr_cxx = \"${CMAKE_CXX_COMPILER}\"")
133 if (CHIP_PROJECT_CONFIG)
134 chip_gn_arg_string(GN_ARGS "chip_project_config_include = \"<${CHIP_PROJECT_CONFIG}>\"")
135 chip_gn_arg_string(GN_ARGS "chip_system_project_config_include = \"<${CHIP_PROJECT_CONFIG}>\"")
138 if (BOARD STREQUAL "native_posix")
139 chip_gn_arg_string(GN_ARGS "target_cpu = \"x86\"")
140 elseif (BOARD STREQUAL "native_posix_64")
141 chip_gn_arg_string(GN_ARGS "target_cpu = \"x64\"")
144 chip_gn_arg_bool_if(CONFIG_NET_L2_OPENTHREAD GN_ARGS "chip_enable_openthread")
145 chip_gn_arg_bool_if(CONFIG_NET_IPV4 GN_ARGS "chip_inet_config_enable_ipv4")
146 chip_gn_arg_bool_if(CHIP_BUILD_TESTS GN_ARGS "chip_build_tests")
147 chip_gn_arg_bool_if(CONFIG_DEBUG GN_ARGS "is_debug")
148 chip_gn_arg_bool_if(CHIP_BUILD_TESTS GN_ARGS "chip_inet_config_enable_raw_endpoint")
149 chip_gn_arg_bool_if(CHIP_BUILD_TESTS GN_ARGS "chip_inet_config_enable_tcp_endpoint")
150 chip_gn_arg_bool_if(CHIP_BUILD_TESTS GN_ARGS "chip_inet_config_enable_dns_resolver")
151 chip_gn_arg_bool_if(CONFIG_CHIP_LIB_SHELL GN_ARGS "chip_build_libshell")
152 chip_gn_arg_bool_if(CONFIG_CHIP_PW_RPC GN_ARGS "chip_build_pw_rpc_lib")
154 file(GENERATE OUTPUT ${CHIP_OUTPUT_DIR}/args.gn CONTENT "${GN_ARGS}")
159 PREFIX ${CHIP_OUTPUT_DIR}
160 SOURCE_DIR ${CHIP_ROOT}
161 BINARY_DIR ${CHIP_OUTPUT_DIR}
162 CONFIGURE_COMMAND gn --root=${CHIP_ROOT}/config/nrfconnect gen --check --fail-on-unused-args ${CHIP_OUTPUT_DIR}
166 USES_TERMINAL_CONFIGURE TRUE
170 # ==================================================
171 # Define chip build target
172 # ==================================================
174 function(chip_build TARGET_NAME BASE_TARGET_NAME)
175 cmake_parse_arguments(CHIP "" "" "BUILD_COMMAND;BUILD_ARTIFACTS" ${ARGN})
177 # Define build target
180 PREFIX ${CHIP_OUTPUT_DIR}
181 SOURCE_DIR ${CHIP_ROOT}
182 BINARY_DIR ${CHIP_OUTPUT_DIR}
184 BUILD_COMMAND ${CHIP_BUILD_COMMAND}
186 BUILD_BYPRODUCTS ${CHIP_BUILD_ARTIFACTS}
188 USES_TERMINAL_BUILD TRUE
191 # Define interface library containing desired CHIP byproducts
192 add_library(${TARGET_NAME} INTERFACE)
193 target_include_directories(${TARGET_NAME} INTERFACE
195 ${CHIP_ROOT}/src/app/util
197 ${CHIP_ROOT}/src/lib/core
198 ${CHIP_ROOT}/src/include
199 ${CHIP_ROOT}/third_party/nlassert/repo/include
200 ${CHIP_OUTPUT_DIR}/gen/include
201 ${CHIP_OUTPUT_DIR}/gen/third_party/connectedhomeip/src/lib/support/include
202 ${CHIP_OUTPUT_DIR}/gen/third_party/connectedhomeip/src/app/include
204 target_link_directories(${TARGET_NAME} INTERFACE ${CHIP_OUTPUT_DIR}/lib)
205 target_link_libraries(${TARGET_NAME} INTERFACE -Wl,--start-group ${CHIP_BUILD_ARTIFACTS} -Wl,--end-group)
207 add_dependencies(${TARGET_NAME}Build ${BASE_TARGET_NAME})
208 add_dependencies(${TARGET_NAME} ${TARGET_NAME}Build)
209 add_dependencies(${TARGET_NAME}Build kernel)