Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / config / esp32 / components / chip / CMakeLists.txt
1 #
2 #   Copyright (c) 2021 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 # The compile flags written args.gn come from chip.c and chip.cpp.
18 # Any build specifications (include dirs, compile flags, definitions, etc)
19 # that is set in the component will be reflected in
20 # the output args.gn. By default, all components inherit build-level
21 # specifications stored as idf-build-properties as done in
22 # tools/cmake/build.cmake.
23 #
24 # Adding REQUIRES/PRIV_REQUIRES will also propagate the
25 # appropriate build specifications from the required component.
26 idf_build_get_property(idf_path IDF_PATH)
27
28 if(NOT CHIP_ROOT)
29     get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../.. REALPATH)
30 endif()
31
32 idf_component_register(SRCS chip.c chip.cpp
33                        PRIV_REQUIRES freertos lwip bt mdns mbedtls)
34
35 # Prepare initial args file (lacking compile flags)
36 # This will be saved as args.gn.in
37 set(chip_gn_args "import(\"//args.gni\")\n")
38
39 macro(chip_gn_arg_append arg val)
40     string(APPEND chip_gn_args "${arg} = ${val}\n")
41 endmacro()
42
43 chip_gn_arg_append("esp32_ar"              "\"${CMAKE_AR}\"")
44 chip_gn_arg_append("esp32_cc"              "\"${CMAKE_C_COMPILER}\"")
45 chip_gn_arg_append("esp32_cxx"             "\"${CMAKE_CXX_COMPILER}\"")
46 chip_gn_arg_append("esp32_cpu"             "\"esp32\"")
47
48 if(CONFIG_ENABLE_PW_RPC)
49     chip_gn_arg_append("chip_build_pw_rpc_lib"              "true")
50     chip_gn_arg_append("pw_log_BACKEND"                     "\"//third_party/connectedhomeip/third_party/pigweed/repo/pw_log_basic\"")
51     chip_gn_arg_append("pw_assert_BACKEND"                  "\"//third_party/connectedhomeip/third_party/pigweed/repo/pw_assert_log\"")
52     chip_gn_arg_append("pw_sys_io_BACKEND"                  "\"//third_party/connectedhomeip/examples/platform/esp32/pw_sys_io:pw_sys_io_esp32\"")
53     chip_gn_arg_append("dir_pw_third_party_nanopb"          "\"//third_party/connectedhomeip/third_party/nanopb/repo\"")
54 endif()
55
56 set(args_gn_input "${CMAKE_CURRENT_BINARY_DIR}/args.gn.in")
57 file(GENERATE OUTPUT "${args_gn_input}" CONTENT "${chip_gn_args}")
58
59 # This generates the final args.in file to be fed to
60 # CHIP build using GN build system. The necessary compile_commands.json
61 # and args.gn.in input file should exist by the time this is invoked,
62 # since these requirements ar generated at the parent project's
63 # GENERATE phase.
64 idf_build_get_property(idf_ver IDF_VER)
65 set(filter_out "-DHAVE_CONFIG_H" "-DIDF_VER=\\\"${idf_ver}\\\"" )
66
67 # CMake adds config include dir relative to build directory.
68 # Make sure full path is given to CHIP build.
69 list(APPEND filter_out "-Iconfig")
70 idf_build_get_property(config_dir CONFIG_DIR)
71 target_compile_options(${COMPONENT_LIB} PRIVATE "-I${config_dir}")
72
73 if(filter_out)
74     set(filter_arg "--filter-out=${filter_out}")
75 endif()
76
77 set(args_gn "${CMAKE_CURRENT_BINARY_DIR}/args.gn")
78 add_custom_command(OUTPUT "${args_gn}"
79                    COMMAND ${python} 
80                            ${CMAKE_CURRENT_LIST_DIR}/create_args_gn.py 
81                            "${CMAKE_BINARY_DIR}"
82                            "${idf_path}"
83                            "${CMAKE_CURRENT_LIST_DIR}/chip.c"
84                            "${CMAKE_CURRENT_LIST_DIR}/chip.cpp"
85                            "${args_gn_input}"
86                            "${args_gn}"
87                            "${filter_arg}"
88                    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
89                    DEPENDS "${CMAKE_BINARY_DIR}/compile_commands.json"
90                    VERBATIM)
91
92 add_custom_target(args_gn DEPENDS "${args_gn}")
93
94 # CHIP build as an external project.
95 find_program(GN_EXECUTABLE gn)
96 if (${GN_EXECUTABLE} STREQUAL GN_EXECUTABLE-NOTFOUND)
97     message(FATAL_ERROR "The 'gn' command was not found. Make sure you have GN installed."
98                         "Or have followed necessary build preparations stated in BUILDING.md.")
99 endif()
100
101 set(GN_ROOT_TARGET ${CHIP_ROOT}/config/esp32)
102
103 externalproject_add(
104     chip_gn
105     SOURCE_DIR              ${CHIP_ROOT}
106     BINARY_DIR              ${CMAKE_CURRENT_BINARY_DIR}
107     CONFIGURE_COMMAND       ${GN_EXECUTABLE} --root=${GN_ROOT_TARGET} gen --check --fail-on-unused-args ${CMAKE_CURRENT_BINARY_DIR}
108     BUILD_COMMAND           ninja
109     INSTALL_COMMAND         ""
110     BUILD_BYPRODUCTS        ${CHIP_LIBRARIES}
111     WORKING_DIRECTORY       ${CMAKE_CURRENT_LIST_DIR}
112     DEPENDS                 args_gn
113     BUILD_ALWAYS            1
114 )
115
116 idf_component_get_property(freertos_dir freertos COMPONENT_DIR)
117
118 # ESP-IDF components usually need 'freertos/<header.h>', while
119 # CHIP might include do 'header.h'.
120 target_include_directories(${COMPONENT_LIB} PRIVATE
121     "${freertos_dir}/include/freertos")
122
123 target_include_directories(${COMPONENT_LIB} INTERFACE
124     "${CHIP_ROOT}/src/platform/ESP32"
125     "${CHIP_ROOT}/src/include/"
126     "${CHIP_ROOT}/src/lib"
127     "${CHIP_ROOT}/src/"
128     "${CHIP_ROOT}/src/system"
129     "${CHIP_ROOT}/third_party/nlassert/repo/include"
130     "${CMAKE_CURRENT_BINARY_DIR}/src/include"
131     "${CMAKE_CURRENT_BINARY_DIR}/include"
132     "${CMAKE_CURRENT_BINARY_DIR}/gen/third_party/connectedhomeip/src/app/include"
133     "${CMAKE_CURRENT_BINARY_DIR}/gen/include"
134 )
135
136 idf_component_get_property(esp32_mbedtls_lib esp32_mbedtls COMPONENT_LIB)
137
138 set(chip_libraries "-lCHIP")
139
140 if(CONFIG_ENABLE_PW_RPC)
141     list(APPEND chip_libraries "-lPwRpc")
142 endif()
143
144 if(CONFIG_BT_ENABLED)
145     idf_component_get_property(bt_lib bt COMPONENT_LIB)
146     list(APPEND chip_libraries $<TARGET_FILE:${bt_lib}> -lbtdm_app)
147 endif()
148
149 target_link_libraries(${COMPONENT_LIB} INTERFACE "-L${CMAKE_CURRENT_BINARY_DIR}/lib")
150 target_link_libraries(${COMPONENT_LIB} INTERFACE -Wl,--start-group 
151                                                 ${chip_libraries}
152                                                 $<TARGET_FILE:mbedcrypto> $<TARGET_FILE:${esp32_mbedtls_lib}> 
153                                                 -Wl,--end-group)
154
155 # Make the component dependent on our CHIP build
156 add_dependencies(${COMPONENT_LIB} chip_gn)