1cmake_minimum_required(VERSION 3.5)
2project(esp-idf C CXX ASM)
3
4if(CMAKE_CURRENT_LIST_DIR STREQUAL CMAKE_SOURCE_DIR)
5    message(FATAL_ERROR "Current directory '${CMAKE_CURRENT_LIST_DIR}' is not buildable. "
6    "Change directories to one of the example projects in '${CMAKE_CURRENT_LIST_DIR}/examples' and try "
7    "again.")
8endif()
9
10unset(compile_options)
11unset(c_compile_options)
12unset(cxx_compile_options)
13unset(compile_definitions)
14unset(link_options)
15
16# Add the following build specifications here, since these seem to be dependent
17# on config values on the root Kconfig.
18
19if(NOT BOOTLOADER_BUILD)
20
21    if(CONFIG_COMPILER_OPTIMIZATION_SIZE)
22        list(APPEND compile_options "-Os")
23        if(CMAKE_C_COMPILER_ID MATCHES "GNU")
24            list(APPEND compile_options "-freorder-blocks")
25        endif()
26    elseif(CONFIG_COMPILER_OPTIMIZATION_DEFAULT)
27        list(APPEND compile_options "-Og")
28    elseif(CONFIG_COMPILER_OPTIMIZATION_NONE)
29        list(APPEND compile_options "-O0")
30    elseif(CONFIG_COMPILER_OPTIMIZATION_PERF)
31        list(APPEND compile_options "-O2")
32    endif()
33
34else()  # BOOTLOADER_BUILD
35
36    if(CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE)
37        list(APPEND compile_options "-Os")
38        if(CMAKE_C_COMPILER_ID MATCHES "GNU")
39            list(APPEND compile_options "-freorder-blocks")
40        endif()
41    elseif(CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_DEBUG)
42        list(APPEND compile_options "-Og")
43    elseif(CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_NONE)
44        list(APPEND compile_options "-O0")
45    elseif(CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_PERF)
46        list(APPEND compile_options "-O2")
47    endif()
48
49endif()
50
51if(CONFIG_COMPILER_CXX_EXCEPTIONS)
52    list(APPEND cxx_compile_options "-fexceptions")
53else()
54    list(APPEND cxx_compile_options "-fno-exceptions")
55endif()
56
57if(CONFIG_COMPILER_CXX_RTTI)
58    list(APPEND cxx_compile_options "-frtti")
59else()
60    list(APPEND cxx_compile_options "-fno-rtti")
61    list(APPEND link_options "-fno-rtti")           # used to invoke correct multilib variant (no-rtti) during linking
62endif()
63
64if(CONFIG_COMPILER_SAVE_RESTORE_LIBCALLS)
65    list(APPEND compile_options "-msave-restore")
66endif()
67
68if(CONFIG_COMPILER_DISABLE_GCC8_WARNINGS)
69    list(APPEND compile_options "-Wno-parentheses"
70                                "-Wno-sizeof-pointer-memaccess"
71                                "-Wno-clobbered")
72
73    list(APPEND compile_options "-Wno-format-overflow"
74                                "-Wno-stringop-truncation"
75                                "-Wno-misleading-indentation"
76                                "-Wno-cast-function-type"
77                                "-Wno-implicit-fallthrough"
78                                "-Wno-unused-const-variable"
79                                "-Wno-switch-unreachable"
80                                "-Wno-format-truncation"
81                                "-Wno-memset-elt-size"
82                                "-Wno-int-in-bool-context")
83endif()
84
85if(CMAKE_C_COMPILER_ID MATCHES "GNU")
86    list(APPEND c_compile_options "-Wno-old-style-declaration")
87endif()
88
89# Clang finds some warnings in IDF code which GCC doesn't.
90# All these warnings should be fixed before Clang is presented
91# as a toolchain choice for users.
92if(CMAKE_C_COMPILER_ID MATCHES "Clang")
93    # Clang checks Doxygen comments for being in sync with function prototype.
94    # There are some inconsistencies, especially in ROM headers.
95    list(APPEND compile_options "-Wno-documentation")
96    # GCC allows repeated typedefs when the source and target types are the same.
97    # Clang doesn't allow this. This occurs in many components due to forward
98    # declarations.
99    list(APPEND compile_options "-Wno-typedef-redefinition")
100    # This issue is seemingly related to newlib's char type functions.
101    # Fix is not clear yet.
102    list(APPEND compile_options "-Wno-char-subscripts")
103    # Clang seems to notice format string issues which GCC doesn't.
104    list(APPEND compile_options "-Wno-format-security")
105    # Logic bug in essl component
106    list(APPEND compile_options "-Wno-tautological-overlap-compare")
107    # Some pointer checks in mDNS component check addresses which can't be NULL
108    list(APPEND compile_options "-Wno-tautological-pointer-compare")
109    # Similar to the above, in tcp_transport
110    list(APPEND compile_options "-Wno-pointer-bool-conversion")
111    # mbedTLS md5.c triggers this warning in md5_test_buf (false positive)
112    list(APPEND compile_options "-Wno-string-concatenation")
113    # multiple cases of implict convertions between unrelated enum types
114    list(APPEND compile_options "-Wno-enum-conversion")
115    # When IRAM_ATTR is specified both in function declaration and definition,
116    # it produces different section names, since section names include __COUNTER__.
117    # Occurs in multiple places.
118    list(APPEND compile_options "-Wno-section")
119    # Multiple cases of attributes unknown to clang, for example
120    # __attribute__((optimize("-O3")))
121    list(APPEND compile_options "-Wno-unknown-attributes")
122    # Clang also produces many -Wunused-function warnings which GCC doesn't.
123    # However these aren't treated as errors.
124endif()
125# More warnings may exist in unit tests and example projects.
126
127if(CONFIG_COMPILER_WARN_WRITE_STRINGS)
128    list(APPEND compile_options "-Wwrite-strings")
129endif()
130
131if(CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE)
132    list(APPEND compile_definitions "-DNDEBUG")
133endif()
134
135if(CONFIG_COMPILER_STACK_CHECK_MODE_NORM)
136    list(APPEND compile_options "-fstack-protector")
137elseif(CONFIG_COMPILER_STACK_CHECK_MODE_STRONG)
138    list(APPEND compile_options "-fstack-protector-strong")
139elseif(CONFIG_COMPILER_STACK_CHECK_MODE_ALL)
140    list(APPEND compile_options "-fstack-protector-all")
141endif()
142
143if(CONFIG_COMPILER_DUMP_RTL_FILES)
144    list(APPEND compile_options "-fdump-rtl-expand")
145endif()
146
147if(NOT ${CMAKE_C_COMPILER_VERSION} VERSION_LESS 8.0.0)
148    if(CONFIG_COMPILER_HIDE_PATHS_MACROS)
149        list(APPEND compile_options "-fmacro-prefix-map=${CMAKE_SOURCE_DIR}=.")
150        list(APPEND compile_options "-fmacro-prefix-map=${IDF_PATH}=IDF")
151    endif()
152endif()
153
154# GCC-specific options
155if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
156    list(APPEND compile_options "-fstrict-volatile-bitfields"
157                                "-Wno-error=unused-but-set-variable"
158                                )
159endif()
160
161if(CONFIG_ESP_SYSTEM_USE_EH_FRAME)
162  list(APPEND compile_options "-fasynchronous-unwind-tables")
163  list(APPEND link_options "-Wl,--eh-frame-hdr")
164endif()
165
166list(APPEND link_options "-fno-lto")
167
168# Placing jump tables in flash would cause issues with code that required
169# to be placed in IRAM
170list(APPEND compile_options "-fno-jump-tables")
171if(CMAKE_C_COMPILER_ID MATCHES "GNU")
172    # This flag is GCC-specific.
173    # Not clear yet if some other flag should be used for Clang.
174    list(APPEND compile_options "-fno-tree-switch-conversion")
175endif()
176
177if(CMAKE_C_COMPILER_ID MATCHES "LLVM")
178    list(APPEND compile_options "-fno-use-cxa-atexit")
179endif()
180
181idf_build_set_property(COMPILE_OPTIONS "${compile_options}" APPEND)
182idf_build_set_property(C_COMPILE_OPTIONS "${c_compile_options}" APPEND)
183idf_build_set_property(CXX_COMPILE_OPTIONS "${cxx_compile_options}" APPEND)
184idf_build_set_property(COMPILE_DEFINITIONS "${compile_definitions}" APPEND)
185idf_build_set_property(LINK_OPTIONS "${link_options}" APPEND)
186
187idf_build_get_property(build_component_targets __BUILD_COMPONENT_TARGETS)
188
189# Add each component as a subdirectory, processing each component's CMakeLists.txt
190foreach(component_target ${build_component_targets})
191    __component_get_property(dir ${component_target} COMPONENT_DIR)
192    __component_get_property(_name ${component_target} COMPONENT_NAME)
193    __component_get_property(prefix ${component_target} __PREFIX)
194    __component_get_property(alias ${component_target} COMPONENT_ALIAS)
195    set(COMPONENT_NAME ${_name})
196    set(COMPONENT_DIR ${dir})
197    set(COMPONENT_ALIAS ${alias})
198    set(COMPONENT_PATH ${dir}) # for backward compatibility only, COMPONENT_DIR is preferred
199    idf_build_get_property(build_prefix __PREFIX)
200    set(__idf_component_context 1)
201    if(NOT prefix STREQUAL build_prefix)
202        add_subdirectory(${dir} ${prefix}_${_name})
203    else()
204        add_subdirectory(${dir} ${_name})
205    endif()
206    set(__idf_component_context 0)
207endforeach()
208