1 # Some IDF-specific functions and functions
3 include(crosstool_version_check)
6 # Set some variables used by rest of the build
8 # Note at the time this macro is expanded, the config is not yet
9 # loaded and the toolchain and project are not yet set
11 macro(idf_set_global_variables)
12 # Note that CONFIG_xxx is not available when this function is called
14 set_default(EXTRA_COMPONENT_DIRS "")
16 # Commmon components, required by every component in the build
18 set_default(COMPONENT_REQUIRES_COMMON "cxx esp32 newlib freertos heap log soc")
20 # PROJECT_PATH has the path to the IDF project (top-level cmake directory)
22 # (cmake calls this CMAKE_SOURCE_DIR, keeping old name for compatibility.)
23 set(PROJECT_PATH "${CMAKE_SOURCE_DIR}")
26 message(WARNING "main is now a component, use of MAIN_SRCS is deprecated")
27 set_default(COMPONENT_DIRS "${PROJECT_PATH}/components ${EXTRA_COMPONENT_DIRS} \
28 ${IDF_PATH}/components")
30 set_default(COMPONENT_DIRS "${PROJECT_PATH}/components ${EXTRA_COMPONENT_DIRS} \
31 ${IDF_PATH}/components ${PROJECT_PATH}/main")
34 spaces2list(COMPONENT_DIRS)
36 spaces2list(COMPONENTS)
38 # Tell cmake to drop executables in the top-level build dir
39 set(EXECUTABLE_OUTPUT_PATH "${CMAKE_BINARY_DIR}")
42 set(IDFTOOL ${PYTHON} "${IDF_PATH}/tools/idf.py")
45 # Add all the IDF global compiler & preprocessor options
46 # (applied to all components). Some are config-dependent
48 # If you only want to set options for a particular component,
49 # don't call or edit this function. TODO DESCRIBE WHAT TO DO INSTEAD
51 function(idf_set_global_compiler_options)
52 add_definitions(-DESP_PLATFORM)
53 add_definitions(-DHAVE_CONFIG_H)
55 if(CONFIG_OPTIMIZATION_LEVEL_RELEASE)
56 add_compile_options(-Os)
58 add_compile_options(-Og)
61 add_c_compile_options(-std=gnu99)
63 add_cxx_compile_options(-std=gnu++11 -fno-rtti)
65 if(CONFIG_CXX_EXCEPTIONS)
66 add_cxx_compile_options(-fexceptions)
68 add_cxx_compile_options(-fno-exceptions)
71 # Default compiler configuration
72 add_compile_options(-ffunction-sections -fdata-sections -fstrict-volatile-bitfields -mlongcalls -nostdlib)
74 # Default warnings configuration
78 -Wno-error=unused-function
79 -Wno-error=unused-but-set-variable
80 -Wno-error=unused-variable
81 -Wno-error=deprecated-declarations
85 add_c_compile_options(
86 -Wno-old-style-declaration
90 if(NOT BOOTLOADER_BUILD)
91 if(CONFIG_STACK_CHECK_NORM)
92 add_compile_options(-fstack-protector)
93 elseif(CONFIG_STACK_CHECK_STRONG)
94 add_compile_options(-fstack-protector-strong)
95 elseif(CONFIG_STACK_CHECK_ALL)
96 add_compile_options(-fstack-protector-all)
100 if(CONFIG_OPTIMIZATION_ASSERTIONS_DISABLED)
101 add_definitions(-DNDEBUG)
104 # Always generate debug symbols (even in Release mode, these don't
105 # go into the final binary so have no impact on size)
106 add_compile_options(-ggdb)
108 # Enable ccache if it's on the path
109 if(NOT CCACHE_DISABLE)
110 find_program(CCACHE_FOUND ccache)
112 message(STATUS "ccache will be used for faster builds")
113 set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
120 # Verify the IDF environment is configured correctly (environment, toolchain, etc)
121 function(idf_verify_environment)
123 if(NOT CMAKE_PROJECT_NAME)
124 message(FATAL_ERROR "Internal error, IDF project.cmake should have set this variable already")
127 # Check toolchain is configured properly in cmake
128 if(NOT ( ${CMAKE_SYSTEM_NAME} STREQUAL "Generic" AND ${CMAKE_C_COMPILER} MATCHES xtensa))
129 message(FATAL_ERROR "Internal error, toolchain has not been set correctly by project "
130 "(or an invalid CMakeCache.txt file has been generated somehow)")
134 # Warn if the toolchain version doesn't match
136 # TODO: make these platform-specific for diff toolchains
137 gcc_version_check("5.2.0")
138 crosstool_version_check("1.22.0-80-g6c4433a")
144 # Calls add_executable to add the final project executable
145 # Adds .map & .bin file targets
146 # Sets up flash-related targets
147 function(idf_add_executable)
148 set(exe_target ${PROJECT_NAME}.elf)
151 spaces2list(MAIN_SRCS)
152 add_executable(${exe_target} ${MAIN_SRCS})
154 # Create a dummy file to work around CMake requirement of having a source
155 # file while adding an executable
156 add_executable(${exe_target} "${CMAKE_CURRENT_BINARY_DIR}/dummy_main_src.c")
157 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy_main_src.c)
159 add_custom_target(dummy_main_src DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/dummy_main_src.c)
161 add_dependencies(${exe_target} dummy_main_src)
164 add_map_file(${exe_target})
170 # Set linker args for 'exe_target' to generate a linker Map file
171 function(add_map_file exe_target)
172 get_filename_component(basename ${exe_target} NAME_WE)
173 set(mapfile "${basename}.map")
174 target_link_libraries(${exe_target} "-Wl,--gc-sections -Wl,--cref -Wl,--Map=${mapfile} -Wl,--start-group")
175 set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" APPEND PROPERTY
176 ADDITIONAL_MAKE_CLEAN_FILES
177 "${CMAKE_CURRENT_BINARY_DIR}/${mapfile}")
179 # add size targets, depend on map file, run idf_size.py
180 add_custom_target(size
181 DEPENDS ${exe_target}
182 COMMAND ${PYTHON} ${IDF_PATH}/tools/idf_size.py ${mapfile}
184 add_custom_target(size-files
185 DEPENDS ${exe_target}
186 COMMAND ${PYTHON} ${IDF_PATH}/tools/idf_size.py --files ${mapfile}
188 add_custom_target(size-components
189 DEPENDS ${exe_target}
190 COMMAND ${PYTHON} ${IDF_PATH}/tools/idf_size.py --archives ${mapfile}
195 # component_compile_options
197 # Wrapper around target_compile_options that passes the component name
198 function(component_compile_options)
199 target_compile_options(${COMPONENT_NAME} PRIVATE ${ARGV})
202 # component_compile_definitions
204 # Wrapper around target_compile_definitions that passes the component name
205 function(component_compile_definitions)
206 target_compile_definitions(${COMPONENT_NAME} PRIVATE ${ARGV})
209 # idf_get_git_revision
211 # Set global IDF_VER to the git revision of ESP-IDF.
213 # Running git_describe() here automatically triggers rebuilds
214 # if the ESP-IDF git version changes
215 function(idf_get_git_revision)
216 git_describe(IDF_VER "${IDF_PATH}")
217 add_definitions(-DIDF_VER=\"${IDF_VER}\")
218 git_submodule_check("${IDF_PATH}")
219 set(IDF_VER ${IDF_VER} PARENT_SCOPE)