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 sub-project defining 'chip' target which represents CHIP library
20 # and other optional libraries like unit tests, built with 'nrfconnect'
22 # Since CHIP doesn't provide native CMake support, ExternalProject
23 # module is used to build the required artifacts with GN meta-build
24 # system. It is assumed that find_package(Zephyr) has been called before
25 # including this file.
30 include(ExternalProject)
31 include(zephyr-util.cmake)
33 # ==============================================================================
34 # Declare configuration variables and define constants
35 # ==============================================================================
37 # C/C++ compiler flags passed to CHIP build system
38 list(APPEND CHIP_CFLAGS)
40 # C compiler flags passed to CHIP build system
41 list(APPEND CHIP_CFLAGS_C)
43 # C++ compiler flags passed to CHIP build system
44 list(APPEND CHIP_CFLAGS_CC)
46 # CHIP libraries that the application should be linked with
47 list(APPEND CHIP_LIBRARIES)
49 # GN meta-build system arguments passed to the make_gn_args.py script
50 list(APPEND CHIP_GN_ARGS)
52 # ==============================================================================
54 # ==============================================================================
56 macro(chip_gn_arg_import FILE)
57 list(APPEND CHIP_GN_ARGS --module "${FILE}")
60 macro(chip_gn_arg_string ARG STRING)
61 list(APPEND CHIP_GN_ARGS --arg-string "${ARG}" "${STRING}")
64 macro(chip_gn_arg_bool ARG BOOLEAN)
66 list(APPEND CHIP_GN_ARGS --arg "${ARG}" "true")
68 list(APPEND CHIP_GN_ARGS --arg "${ARG}" "false")
72 macro(chip_gn_arg_cflags ARG CFLAGS)
73 list(APPEND CHIP_GN_ARGS --arg-cflags "${ARG}" "${CFLAGS}")
76 # ==============================================================================
77 # Prepare CHIP configuration based on the project Kconfig configuration
78 # ==============================================================================
81 get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../.. REALPATH)
84 set(GN_ROOT_TARGET ${CHIP_ROOT}/config/nrfconnect/chip-gn)
86 # Prepare compiler flags
89 list(APPEND CHIP_CFLAGS_C
95 list(APPEND CHIP_CFLAGS
96 -D_SYS__PTHREADTYPES_H_
97 -isystem${ZEPHYR_BASE}/include/posix
101 if (CONFIG_NORDIC_SECURITY_BACKEND)
102 # TODO: Remove when the missing dependency is fixed in nRF Connect SDK
103 zephyr_include_directories(${ZEPHYR_BASE}/../nrfxlib/crypto/nrf_cc310_platform/include)
105 list(APPEND CHIP_CFLAGS -DMBEDTLS_CONFIG_FILE=<nrf-config.h>)
108 zephyr_get_compile_flags(CHIP_CFLAGS_C C)
109 zephyr_get_compile_flags(CHIP_CFLAGS_CC CXX)
110 zephyr_get_gnu_cpp_standard(CHIP_CFLAGS_CC)
112 convert_list_of_flags_to_string_of_flags(CHIP_CFLAGS CHIP_CFLAGS)
113 convert_list_of_flags_to_string_of_flags(CHIP_CFLAGS_C CHIP_CFLAGS_C)
114 convert_list_of_flags_to_string_of_flags(CHIP_CFLAGS_CC CHIP_CFLAGS_CC)
116 # Prepare CHIP libraries that the application should be linked with
118 if (NOT CHIP_LIBRARIES)
119 set(CHIP_LIBRARIES -lCHIP)
122 if (CONFIG_CHIP_LIB_SHELL)
123 list(APPEND CHIP_LIBRARIES -lCHIPShell)
126 if (CONFIG_CHIP_PW_RPC)
127 list(APPEND CHIP_LIBRARIES -lPwRpc)
130 # Set up CHIP project configuration file
132 if (CONFIG_CHIP_PROJECT_CONFIG)
133 get_filename_component(CHIP_PROJECT_CONFIG
134 ${CONFIG_CHIP_PROJECT_CONFIG}
136 BASE_DIR ${CMAKE_SOURCE_DIR}
138 set(CHIP_PROJECT_CONFIG "<${CHIP_PROJECT_CONFIG}>")
140 set(CHIP_PROJECT_CONFIG "")
143 # Set up custom OpenThread configuration
145 if (CONFIG_CHIP_OPENTHREAD_CONFIG)
146 get_filename_component(CHIP_OPENTHREAD_CONFIG
147 ${CONFIG_CHIP_OPENTHREAD_CONFIG}
149 BASE_DIR ${CMAKE_SOURCE_DIR}
151 zephyr_set_openthread_config(${CHIP_OPENTHREAD_CONFIG})
154 # Find required programs
156 find_program(GN_EXECUTABLE gn)
157 if (${GN_EXECUTABLE} STREQUAL GN_EXECUTABLE-NOTFOUND)
158 message(FATAL_ERROR "The 'gn' command was not found. Make sure you have GN installed.")
160 # Parse the 'gn --version' output to find the installed version.
161 set(MIN_GN_VERSION 1851)
164 ${GN_EXECUTABLE} --version
165 OUTPUT_VARIABLE gn_version_output
166 ERROR_VARIABLE gn_error_output
167 RESULT_VARIABLE gn_status
170 if(${gn_status} EQUAL 0)
171 if(gn_version_output VERSION_LESS ${MIN_GN_VERSION})
172 message(FATAL_ERROR "Found unsuitable version of 'gn'. Required is at least ${MIN_GN_VERSION}")
175 message(FATAL_ERROR "Could NOT find working gn: Found gn (${GN_EXECUTABLE}), but failed to load with:\n ${gn_error_output}")
179 find_package(Python3 REQUIRED)
181 # ==============================================================================
182 # Generate configuration for CHIP GN build system
183 # ==============================================================================
185 if (CONFIG_CHIP_PW_RPC)
186 chip_gn_arg_import("${GN_ROOT_TARGET}/lib/pw_rpc/pw_rpc.gni")
189 chip_gn_arg_cflags("target_cflags" ${CHIP_CFLAGS})
190 chip_gn_arg_cflags("target_cflags_c" ${CHIP_CFLAGS_C})
191 chip_gn_arg_cflags("target_cflags_cc" ${CHIP_CFLAGS_CC})
192 chip_gn_arg_string("zephyr_ar" ${CMAKE_AR})
193 chip_gn_arg_string("zephyr_cc" ${CMAKE_C_COMPILER})
194 chip_gn_arg_string("zephyr_cxx" ${CMAKE_CXX_COMPILER})
195 chip_gn_arg_bool ("is_debug" CONFIG_DEBUG)
196 chip_gn_arg_bool ("chip_enable_openthread" CONFIG_NET_L2_OPENTHREAD)
197 chip_gn_arg_bool ("chip_inet_config_enable_ipv4" CONFIG_NET_IPV4)
198 chip_gn_arg_bool ("chip_build_tests" CONFIG_CHIP_BUILD_TESTS)
199 chip_gn_arg_bool ("chip_monolithic_tests" CONFIG_CHIP_BUILD_TESTS)
200 chip_gn_arg_bool ("chip_inet_config_enable_raw_endpoint" CONFIG_CHIP_BUILD_TESTS)
201 chip_gn_arg_bool ("chip_inet_config_enable_tcp_endpoint" CONFIG_CHIP_BUILD_TESTS)
202 chip_gn_arg_bool ("chip_inet_config_enable_dns_resolver" CONFIG_CHIP_BUILD_TESTS)
203 chip_gn_arg_bool ("chip_build_libshell" CONFIG_CHIP_LIB_SHELL)
204 chip_gn_arg_bool ("chip_build_pw_rpc_lib" CONFIG_CHIP_PW_RPC)
206 if (CONFIG_CHIP_ENABLE_DNSSD_SRP)
207 chip_gn_arg_string("chip_mdns" "platform")
210 if (CHIP_PROJECT_CONFIG)
211 chip_gn_arg_string("chip_project_config_include" ${CHIP_PROJECT_CONFIG})
212 chip_gn_arg_string("chip_system_project_config_include" ${CHIP_PROJECT_CONFIG})
215 if (BOARD STREQUAL "native_posix")
216 chip_gn_arg_string("target_cpu" "x86")
217 elseif (BOARD STREQUAL "native_posix_64")
218 chip_gn_arg_string("target_cpu" "x64")
221 # ==============================================================================
222 # Define 'chip-gn' target that builds CHIP library(ies) with GN build system
223 # ==============================================================================
226 PREFIX ${CMAKE_CURRENT_BINARY_DIR}
227 SOURCE_DIR ${CHIP_ROOT}
228 BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}
229 CONFIGURE_COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/make_gn_args.py
230 ${CHIP_GN_ARGS} > ${CMAKE_CURRENT_BINARY_DIR}/args.gn &&
233 --root-target=${GN_ROOT_TARGET}
234 --dotfile=${GN_ROOT_TARGET}/.gn
235 --script-executable=${Python3_EXECUTABLE}
236 --export-compile-commands
237 gen --check --fail-on-unused-args ${CMAKE_CURRENT_BINARY_DIR}
240 BUILD_BYPRODUCTS ${CHIP_LIBRARIES}
241 CONFIGURE_ALWAYS TRUE
243 USES_TERMINAL_CONFIGURE TRUE
244 USES_TERMINAL_BUILD TRUE
246 add_dependencies(chip-gn kernel)
248 # ==============================================================================
249 # Define 'chip' target that exposes CHIP headers & libraries to the application
250 # ==============================================================================
252 zephyr_interface_library_named(chip)
253 target_compile_definitions(chip INTERFACE CHIP_HAVE_CONFIG_H)
254 target_include_directories(chip INTERFACE
256 ${CHIP_ROOT}/src/app/server
257 ${CHIP_ROOT}/src/app/util
258 ${CHIP_ROOT}/src/include
260 ${CHIP_ROOT}/src/lib/core
261 ${CHIP_ROOT}/third_party/nlassert/repo/include
262 ${CMAKE_CURRENT_BINARY_DIR}/gen/include
263 ${CMAKE_CURRENT_BINARY_DIR}/gen/third_party/connectedhomeip/src/app/include
265 target_link_directories(chip INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/lib)
266 target_link_libraries(chip INTERFACE -Wl,--start-group ${CHIP_LIBRARIES} -Wl,--end-group)
267 add_dependencies(chip chip-gn)
269 endif() # CONFIG_CHIP