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        list(APPEND compile_options "-freorder-blocks")
24    elseif(CONFIG_COMPILER_OPTIMIZATION_DEFAULT)
25        list(APPEND compile_options "-Og")
26    elseif(CONFIG_COMPILER_OPTIMIZATION_NONE)
27        list(APPEND compile_options "-O0")
28    elseif(CONFIG_COMPILER_OPTIMIZATION_PERF)
29        list(APPEND compile_options "-O2")
30    endif()
31
32else()  # BOOTLOADER_BUILD
33
34    if(CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_SIZE)
35        list(APPEND compile_options "-Os")
36        list(APPEND compile_options "-freorder-blocks")
37    elseif(CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_DEBUG)
38        list(APPEND compile_options "-Og")
39    elseif(CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_NONE)
40        list(APPEND compile_options "-O0")
41    elseif(CONFIG_BOOTLOADER_COMPILER_OPTIMIZATION_PERF)
42        list(APPEND compile_options "-O2")
43    endif()
44
45endif()
46
47if(CONFIG_COMPILER_CXX_EXCEPTIONS)
48    list(APPEND cxx_compile_options "-fexceptions")
49else()
50    list(APPEND cxx_compile_options "-fno-exceptions")
51endif()
52
53if(CONFIG_COMPILER_CXX_RTTI)
54    list(APPEND cxx_compile_options "-frtti")
55else()
56    list(APPEND cxx_compile_options "-fno-rtti")
57    list(APPEND link_options "-fno-rtti")           # used to invoke correct multilib variant (no-rtti) during linking
58endif()
59
60if(CONFIG_COMPILER_DISABLE_GCC8_WARNINGS)
61    list(APPEND compile_options "-Wno-parentheses"
62                                "-Wno-sizeof-pointer-memaccess"
63                                "-Wno-clobbered")
64
65    list(APPEND compile_options "-Wno-format-overflow"
66                                "-Wno-stringop-truncation"
67                                "-Wno-misleading-indentation"
68                                "-Wno-cast-function-type"
69                                "-Wno-implicit-fallthrough"
70                                "-Wno-unused-const-variable"
71                                "-Wno-switch-unreachable"
72                                "-Wno-format-truncation"
73                                "-Wno-memset-elt-size"
74                                "-Wno-int-in-bool-context")
75endif()
76
77if(CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE)
78    list(APPEND compile_definitions "-DNDEBUG")
79endif()
80
81if(CONFIG_COMPILER_STACK_CHECK_MODE_NORM)
82    list(APPEND compile_options "-fstack-protector")
83elseif(CONFIG_COMPILER_STACK_CHECK_MODE_STRONG)
84    list(APPEND compile_options "-fstack-protector-strong")
85elseif(CONFIG_COMPILER_STACK_CHECK_MODE_ALL)
86    list(APPEND compile_options "-fstack-protector-all")
87endif()
88
89if(CONFIG_COMPILER_DUMP_RTL_FILES)
90    list(APPEND compile_options "-fdump-rtl-expand")
91endif()
92
93# GCC-specific options
94if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
95    list(APPEND compile_options "-fstrict-volatile-bitfields"
96                                "-Wno-error=unused-but-set-variable"
97                                )
98endif()
99
100list(APPEND link_options "-fno-lto")
101
102idf_build_set_property(COMPILE_OPTIONS "${compile_options}" APPEND)
103idf_build_set_property(C_COMPILE_OPTIONS "${c_compile_options}" APPEND)
104idf_build_set_property(CXX_COMPILE_OPTIONS "${cxx_compile_options}" APPEND)
105idf_build_set_property(COMPILE_DEFINITIONS "${compile_definitions}" APPEND)
106idf_build_set_property(LINK_OPTIONS "${link_options}" APPEND)
107
108idf_build_get_property(build_component_targets __BUILD_COMPONENT_TARGETS)
109
110# Add each component as a subdirectory, processing each component's CMakeLists.txt
111foreach(component_target ${build_component_targets})
112    __component_get_property(dir ${component_target} COMPONENT_DIR)
113    __component_get_property(_name ${component_target} COMPONENT_NAME)
114    __component_get_property(prefix ${component_target} __PREFIX)
115    __component_get_property(alias ${component_target} COMPONENT_ALIAS)
116    set(COMPONENT_NAME ${_name})
117    set(COMPONENT_DIR ${dir})
118    set(COMPONENT_ALIAS ${alias})
119    set(COMPONENT_PATH ${dir}) # for backward compatibility only, COMPONENT_DIR is preferred
120    idf_build_get_property(build_prefix __PREFIX)
121    set(__idf_component_context 1)
122    if(NOT prefix STREQUAL build_prefix)
123        add_subdirectory(${dir} ${prefix}_${_name})
124    else()
125        add_subdirectory(${dir} ${_name})
126    endif()
127    set(__idf_component_context 0)
128endforeach()
129