Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / config / nrfconnect / chip-module / CMakeLists.txt
1 #
2 #   Copyright (c) 2020 Project CHIP Authors
3 #
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
7 #
8 #       http://www.apache.org/licenses/LICENSE-2.0
9 #
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.
15 #
16
17 #
18 #   @file
19 #     CMake sub-project defining 'chip' target which represents CHIP library
20 #     and other optional libraries like unit tests, built with 'nrfconnect'
21 #     platform.
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.
26 #
27
28 if (CONFIG_CHIP)
29
30 include(ExternalProject)
31 include(zephyr-util.cmake)
32
33 # ==============================================================================
34 # Declare configuration variables and define constants
35 # ==============================================================================
36
37 # C/C++ compiler flags passed to CHIP build system
38 list(APPEND CHIP_CFLAGS)
39
40 # C compiler flags passed to CHIP build system
41 list(APPEND CHIP_CFLAGS_C)
42
43 # C++ compiler flags passed to CHIP build system
44 list(APPEND CHIP_CFLAGS_CC)
45
46 # CHIP libraries that the application should be linked with
47 list(APPEND CHIP_LIBRARIES)
48
49 # GN meta-build system arguments passed to the make_gn_args.py script
50 list(APPEND CHIP_GN_ARGS)
51
52 # ==============================================================================
53 # Helper macros
54 # ==============================================================================
55
56 macro(chip_gn_arg_import FILE)
57     list(APPEND CHIP_GN_ARGS --module "${FILE}")
58 endmacro()
59
60 macro(chip_gn_arg_string ARG STRING)
61     list(APPEND CHIP_GN_ARGS --arg-string "${ARG}" "${STRING}")
62 endmacro()
63
64 macro(chip_gn_arg_bool ARG BOOLEAN)
65     if (${BOOLEAN})
66         list(APPEND CHIP_GN_ARGS --arg "${ARG}" "true")
67     else()
68         list(APPEND CHIP_GN_ARGS --arg "${ARG}" "false")
69     endif()
70 endmacro()
71
72 macro(chip_gn_arg_cflags ARG CFLAGS)
73     list(APPEND CHIP_GN_ARGS --arg-cflags "${ARG}" "${CFLAGS}")
74 endmacro()
75
76 # ==============================================================================
77 # Prepare CHIP configuration based on the project Kconfig configuration
78 # ==============================================================================
79
80 if (NOT CHIP_ROOT)
81     get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../.. REALPATH)
82 endif()
83
84 set(GN_ROOT_TARGET ${CHIP_ROOT}/config/nrfconnect/chip-gn)
85
86 # Prepare compiler flags
87
88 if (CONFIG_ARM)
89     list(APPEND CHIP_CFLAGS_C
90         --specs=nosys.specs
91     )
92 endif()
93
94 if (CONFIG_POSIX_API)
95     list(APPEND CHIP_CFLAGS
96         -D_SYS__PTHREADTYPES_H_
97         -isystem${ZEPHYR_BASE}/include/posix
98     )
99 endif()
100
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)
104
105     list(APPEND CHIP_CFLAGS -DMBEDTLS_CONFIG_FILE=<nrf-config.h>)
106 endif()
107
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)
111
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)
115
116 # Prepare CHIP libraries that the application should be linked with
117
118 if (NOT CHIP_LIBRARIES)
119     set(CHIP_LIBRARIES -lCHIP)
120 endif()
121
122 if (CONFIG_CHIP_LIB_SHELL)
123     list(APPEND CHIP_LIBRARIES -lCHIPShell)
124 endif()
125
126 if (CONFIG_CHIP_PW_RPC)
127     list(APPEND CHIP_LIBRARIES -lPwRpc)
128 endif()
129
130 # Set up CHIP project configuration file
131
132 if (CONFIG_CHIP_PROJECT_CONFIG)
133     get_filename_component(CHIP_PROJECT_CONFIG 
134         ${CONFIG_CHIP_PROJECT_CONFIG}
135         REALPATH
136         BASE_DIR ${CMAKE_SOURCE_DIR}
137     )
138     set(CHIP_PROJECT_CONFIG "<${CHIP_PROJECT_CONFIG}>")
139 else()
140     set(CHIP_PROJECT_CONFIG "")
141 endif()
142
143 # Set up custom OpenThread configuration
144
145 if (CONFIG_CHIP_OPENTHREAD_CONFIG)
146     get_filename_component(CHIP_OPENTHREAD_CONFIG 
147         ${CONFIG_CHIP_OPENTHREAD_CONFIG}
148         REALPATH
149         BASE_DIR ${CMAKE_SOURCE_DIR}
150     )
151     zephyr_set_openthread_config(${CHIP_OPENTHREAD_CONFIG})
152 endif()
153
154 # Find required programs
155
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.")
159 else()
160     # Parse the 'gn --version' output to find the installed version.
161     set(MIN_GN_VERSION 1851)
162     execute_process(
163         COMMAND
164         ${GN_EXECUTABLE} --version
165         OUTPUT_VARIABLE gn_version_output
166         ERROR_VARIABLE  gn_error_output
167         RESULT_VARIABLE gn_status
168     )
169
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}")
173         endif()
174     else()
175         message(FATAL_ERROR "Could NOT find working gn: Found gn (${GN_EXECUTABLE}), but failed to load with:\n ${gn_error_output}")
176     endif()
177 endif()
178
179 find_package(Python3 REQUIRED)
180
181 # ==============================================================================
182 # Generate configuration for CHIP GN build system
183 # ==============================================================================
184
185 if (CONFIG_CHIP_PW_RPC)
186     chip_gn_arg_import("${GN_ROOT_TARGET}/lib/pw_rpc/pw_rpc.gni")
187 endif()
188
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)
205
206 if (CONFIG_CHIP_ENABLE_DNSSD_SRP)
207     chip_gn_arg_string("chip_mdns" "platform")
208 endif()
209
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})
213 endif()
214
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")
219 endif()
220
221 # ==============================================================================
222 # Define 'chip-gn' target that builds CHIP library(ies) with GN build system
223 # ==============================================================================
224 ExternalProject_Add(
225     chip-gn
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 &&
231                             ${GN_EXECUTABLE}
232                                 --root=${CHIP_ROOT}
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}
238     BUILD_COMMAND           ninja
239     INSTALL_COMMAND         ""
240     BUILD_BYPRODUCTS        ${CHIP_LIBRARIES}
241     CONFIGURE_ALWAYS        TRUE
242     BUILD_ALWAYS            TRUE
243     USES_TERMINAL_CONFIGURE TRUE
244     USES_TERMINAL_BUILD     TRUE
245 )
246 add_dependencies(chip-gn kernel)
247
248 # ==============================================================================
249 # Define 'chip' target that exposes CHIP headers & libraries to the application
250 # ==============================================================================
251
252 zephyr_interface_library_named(chip)
253 target_compile_definitions(chip INTERFACE CHIP_HAVE_CONFIG_H)
254 target_include_directories(chip INTERFACE
255     ${CHIP_ROOT}/src
256     ${CHIP_ROOT}/src/app/server
257     ${CHIP_ROOT}/src/app/util
258     ${CHIP_ROOT}/src/include
259     ${CHIP_ROOT}/src/lib
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
264 )
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)
268
269 endif() # CONFIG_CHIP