Change script for apply upstream code
[platform/upstream/connectedhomeip.git] / config / nrfconnect / nrfconnect-app.cmake
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 module for incorporating CHIP into an nRF Connect SDK
20 #         application for Nordic platforms.
21 #
22
23 #
24 #   The top-level CMakeLists.txt of an application should define CHIP_ROOT variable,
25 #   include this module and configure `app` build target properly. E.g.:
26 #
27 #   cmake_minimum_required(VERSION 3.13.1)
28 #
29 #   set(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/third_party/connectedhomeip)
30 #   get_filename_component(CHIP_ROOT ${CHIP_ROOT} REALPATH)
31 #   set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CHIP_ROOT}/config/nrfconnect/)
32 #
33 #   include(nrfconnect-app)
34 #
35 #   project(chip-nrf52840-lock-example)
36 #   target_sources(app PRIVATE main/main.cpp ...)
37 #
38
39 # Set DTC overlay before finding the Zephyr package.
40 if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/dts.overlay")
41     set(DTC_OVERLAY_FILE "${CMAKE_CURRENT_SOURCE_DIR}/dts.overlay")
42 endif()
43
44 # Set Kconfig root
45 if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/Kconfig)
46     set(KCONFIG_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/Kconfig)
47 else()
48     set(KCONFIG_ROOT ${CHIP_ROOT}/config/nrfconnect/Kconfig)
49 endif()
50
51 # ==================================================
52 # Load NCS/Zephyr build system
53 # ==================================================
54 find_package(Zephyr HINTS $ENV{ZEPHYR_BASE})
55
56 # TODO: temporary fix forcing time_t size to be equal long size - remove it after merging fix from Zephyr project.
57 zephyr_compile_definitions(_USE_LONG_TIME_T)
58
59 # ==================================================
60 # General settings
61 # ==================================================
62
63 include(chip-lib)
64
65 set(CHIP_COMMON_FLAGS
66     -D_SYS__PTHREADTYPES_H_
67     -DMBEDTLS_CONFIG_FILE=<nrf-config.h>
68     -DMBEDTLS_PK_WRITE_C
69     -DMBEDTLS_X509_CREATE_C
70     -DMBEDTLS_X509_CSR_WRITE_C
71     -isystem${ZEPHYR_BASE}/include/posix
72     -isystem${ZEPHYR_BASE}/../mbedtls/include
73     -I${CMAKE_CURRENT_SOURCE_DIR}
74     -I${CMAKE_CURRENT_SOURCE_DIR}/main/include
75 )
76
77 set(CHIP_LIBRARIES ${CHIP_OUTPUT_DIR}/lib/libCHIP.a)
78 if (CONFIG_CHIP_LIB_SHELL)
79     list(APPEND CHIP_LIBRARIES ${CHIP_OUTPUT_DIR}/obj/third_party/connectedhomeip/src/lib/shell/lib/libCHIPShell.a)
80 endif()
81 if (CONFIG_CHIP_PW_RPC)
82     list(APPEND CHIP_LIBRARIES ${CHIP_OUTPUT_DIR}/lib/libPwRpc.a)
83 endif()
84
85 find_file(CHIP_PROJECT_CONFIG 
86     CHIPProjectConfig.h
87     PATHS ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/main/include
88     NO_DEFAULT_PATH
89 )
90
91 # ==================================================
92 # Configure Zephyr
93 # ==================================================
94
95 function(set_openthread_config OT_DIR CONFIG_FILE)
96     get_property(DEFINES DIRECTORY ${OT_DIR} PROPERTY COMPILE_DEFINITIONS)
97     get_property(SUBDIRS DIRECTORY ${OT_DIR} PROPERTY SUBDIRECTORIES)
98
99     list(TRANSFORM DEFINES REPLACE 
100          OPENTHREAD_PROJECT_CORE_CONFIG_FILE=.*
101          OPENTHREAD_PROJECT_CORE_CONFIG_FILE="${CONFIG_FILE}")
102
103     set_property(DIRECTORY ${OT_DIR} PROPERTY COMPILE_DEFINITIONS ${DEFINES})
104
105     foreach(SUBDIR ${SUBDIRS})
106         set_openthread_config(${SUBDIR} ${CONFIG_FILE})
107     endforeach()
108 endfunction()
109
110 # Add dependency on nRF crypto library.
111 # This is temporary solution and should be removed after fixing it in nRF Connect SDK.
112 zephyr_include_directories(${ZEPHYR_BASE}/../nrfxlib/crypto/nrf_cc310_platform/include)
113
114 # Override Zephyr-supplied OpenThread configuration
115 if (CHIP_OPENTHREAD_CONFIG)
116     get_filename_component(CONFIG_DIR ${CHIP_OPENTHREAD_CONFIG} DIRECTORY)
117     get_filename_component(CONFIG_FILE ${CHIP_OPENTHREAD_CONFIG} NAME)
118     target_include_directories(ot-config INTERFACE ${CONFIG_DIR})
119     set_openthread_config(${ZEPHYR_BASE}/../modules/lib/openthread ${CONFIG_FILE})
120 endif()
121
122 # ==================================================
123 # Setup CHIP build
124 # ==================================================
125
126 chip_configure(ChipConfig
127     ARCH arm-none-eabi
128     CFLAGS ${CHIP_COMMON_FLAGS} --specs=nosys.specs
129     CXXFLAGS ${CHIP_COMMON_FLAGS}
130     PROJECT_CONFIG ${CHIP_PROJECT_CONFIG}
131 )
132
133 chip_build(ChipLib ChipConfig
134     BUILD_COMMAND ninja
135     BUILD_ARTIFACTS ${CHIP_LIBRARIES}
136 )
137
138 # ==================================================
139 # Configure application
140 # ==================================================
141
142 target_link_libraries(app PUBLIC ChipLib)
143 target_compile_definitions(app PRIVATE CHIP_HAVE_CONFIG_H)