blob: 09b120391305fde792d024226cd745a0a05f40c7 [file] [log] [blame]
Angus Gratton043ba452024-10-22 16:01:01 +11001# This is the common ESP-IDF "main component" CMakeLists.txt contents for MicroPython.
2#
3# This file is included directly from a main_${IDF_TARGET}/CMakeLists.txt file
4# (or included from an out-of-tree main component CMakeLists.txt for out-of-tree
5# builds.)
6
Damien George9b908822020-09-23 15:55:55 +10007# Set location of base MicroPython directory.
Damien George0009a7d2021-06-24 16:03:25 +10008if(NOT MICROPY_DIR)
Damien Georgee4650122023-05-09 09:52:54 +10009 get_filename_component(MICROPY_DIR ${CMAKE_CURRENT_LIST_DIR}/../.. ABSOLUTE)
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -070010endif()
11
Damien Georgee4650122023-05-09 09:52:54 +100012# Set location of the ESP32 port directory.
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -070013if(NOT MICROPY_PORT_DIR)
14 get_filename_component(MICROPY_PORT_DIR ${MICROPY_DIR}/ports/esp32 ABSOLUTE)
Damien George0009a7d2021-06-24 16:03:25 +100015endif()
Damien George9b908822020-09-23 15:55:55 +100016
Angus Gratton4b1c6662025-03-04 12:20:02 +110017# RISC-V specific inclusions
18if(CONFIG_IDF_TARGET_ARCH_RISCV)
Alessandro Gatti9ab69062025-03-22 02:35:32 +010019 list(APPEND MICROPY_SOURCE_LIB
20 ${MICROPY_DIR}/shared/runtime/gchelper_native.c
21 ${MICROPY_DIR}/shared/runtime/gchelper_rv32i.s
22 )
Angus Gratton4b1c6662025-03-04 12:20:02 +110023endif()
24
25if(NOT DEFINED MICROPY_PY_TINYUSB)
26 if(CONFIG_IDF_TARGET_ESP32S2 OR CONFIG_IDF_TARGET_ESP32S3)
27 set(MICROPY_PY_TINYUSB ON)
28 endif()
29endif()
30
Damien Georgebfe16ef2025-03-13 15:19:08 +110031# Enable error text compression by default.
32if(NOT MICROPY_ROM_TEXT_COMPRESSION)
33 set(MICROPY_ROM_TEXT_COMPRESSION ON)
34endif()
35
Damien George9b908822020-09-23 15:55:55 +100036# Include core source components.
37include(${MICROPY_DIR}/py/py.cmake)
Damien George9b908822020-09-23 15:55:55 +100038
Jim Mussaredad123ed2023-07-31 17:24:38 +100039# CMAKE_BUILD_EARLY_EXPANSION is set during the component-discovery phase of
40# `idf.py build`, so none of the extmod/usermod (and in reality, most of the
41# micropython) rules need to happen. Specifically, you cannot invoke add_library.
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -040042if(NOT CMAKE_BUILD_EARLY_EXPANSION)
Damien Georgebd375df2022-06-08 12:21:51 +100043 # Enable extmod components that will be configured by extmod.cmake.
44 # A board may also have enabled additional components.
Alessandro Gattie3c2cf72025-03-21 02:12:31 +010045 if (NOT DEFINED MICROPY_PY_BTREE)
46 set(MICROPY_PY_BTREE ON)
47 endif()
Damien Georgebd375df2022-06-08 12:21:51 +100048
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -040049 include(${MICROPY_DIR}/py/usermod.cmake)
Damien George212fe7f2021-04-08 23:45:28 +100050 include(${MICROPY_DIR}/extmod/extmod.cmake)
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -040051endif()
52
Damien Georgee4650122023-05-09 09:52:54 +100053list(APPEND MICROPY_QSTRDEFS_PORT
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -070054 ${MICROPY_PORT_DIR}/qstrdefsport.h
Damien George75db0b92021-02-21 11:33:15 +110055)
56
Damien Georgee4650122023-05-09 09:52:54 +100057list(APPEND MICROPY_SOURCE_SHARED
Damien George136369d2021-07-09 14:19:15 +100058 ${MICROPY_DIR}/shared/readline/readline.c
59 ${MICROPY_DIR}/shared/netutils/netutils.c
60 ${MICROPY_DIR}/shared/timeutils/timeutils.c
61 ${MICROPY_DIR}/shared/runtime/interrupt_char.c
robert-hha04a1412024-03-10 15:14:20 +010062 ${MICROPY_DIR}/shared/runtime/mpirq.c
Damien George136369d2021-07-09 14:19:15 +100063 ${MICROPY_DIR}/shared/runtime/stdout_helpers.c
64 ${MICROPY_DIR}/shared/runtime/sys_stdio_mphal.c
65 ${MICROPY_DIR}/shared/runtime/pyexec.c
66)
67
Damien Georgee4650122023-05-09 09:52:54 +100068list(APPEND MICROPY_SOURCE_LIB
Damien George9b908822020-09-23 15:55:55 +100069 ${MICROPY_DIR}/lib/littlefs/lfs1.c
70 ${MICROPY_DIR}/lib/littlefs/lfs1_util.c
71 ${MICROPY_DIR}/lib/littlefs/lfs2.c
72 ${MICROPY_DIR}/lib/littlefs/lfs2_util.c
Damien Georgec9eb6bc2023-12-12 16:05:03 +110073 ${MICROPY_DIR}/lib/mbedtls_errors/esp32_mbedtls_errors.c
Damien George9b908822020-09-23 15:55:55 +100074 ${MICROPY_DIR}/lib/oofatfs/ff.c
75 ${MICROPY_DIR}/lib/oofatfs/ffunicode.c
Damien George9b908822020-09-23 15:55:55 +100076)
77
Damien Georgee4650122023-05-09 09:52:54 +100078list(APPEND MICROPY_SOURCE_DRIVERS
Damien George9b908822020-09-23 15:55:55 +100079 ${MICROPY_DIR}/drivers/bus/softspi.c
80 ${MICROPY_DIR}/drivers/dht/dht.c
81)
82
Angus Grattoncccac2c2025-03-11 17:17:39 +110083if(MICROPY_PY_TINYUSB)
Andrew Leech42479212024-05-23 17:23:41 +100084 string(TOUPPER OPT_MCU_${IDF_TARGET} tusb_mcu)
85
86 list(APPEND MICROPY_DEF_TINYUSB
87 CFG_TUSB_MCU=${tusb_mcu}
88 )
89
90 list(APPEND MICROPY_SOURCE_TINYUSB
Andrew Leech42479212024-05-23 17:23:41 +100091 ${MICROPY_DIR}/shared/tinyusb/mp_usbd.c
92 ${MICROPY_DIR}/shared/tinyusb/mp_usbd_cdc.c
93 ${MICROPY_DIR}/shared/tinyusb/mp_usbd_descriptor.c
Sebastian Romero5f2d05d2024-12-04 16:30:56 +010094 ${MICROPY_DIR}/shared/tinyusb/mp_usbd_runtime.c
Andrew Leech42479212024-05-23 17:23:41 +100095 )
96
97 list(APPEND MICROPY_INC_TINYUSB
Andrew Leech42479212024-05-23 17:23:41 +100098 ${MICROPY_DIR}/shared/tinyusb/
99 )
Andrew Leech42479212024-05-23 17:23:41 +1000100endif()
101
Damien Georgee4650122023-05-09 09:52:54 +1000102list(APPEND MICROPY_SOURCE_PORT
Daniƫl van de Giessenc10a74b2024-03-13 18:23:17 +0100103 panichandler.c
Damien George03eae482023-10-25 19:13:11 +1100104 adc.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700105 main.c
Damien George1db40ed2023-06-21 15:09:26 +1000106 ppp_set_auth.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700107 uart.c
108 usb.c
109 usb_serial_jtag.c
110 gccollect.c
111 mphalport.c
112 fatfs_port.c
113 help.c
114 machine_bitstream.c
115 machine_timer.c
116 machine_pin.c
117 machine_touchpad.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700118 machine_dac.c
119 machine_i2c.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700120 network_common.c
121 network_lan.c
122 network_ppp.c
123 network_wlan.c
124 mpnimbleport.c
125 modsocket.c
Angus Gratton82e69df2024-10-02 14:49:49 +1000126 lwip_patch.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700127 modesp.c
128 esp32_nvs.c
129 esp32_partition.c
130 esp32_rmt.c
131 esp32_ulp.c
132 modesp32.c
133 machine_hw_spi.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700134 mpthreadport.c
135 machine_rtc.c
136 machine_sdcard.c
137 modespnow.c
Damien George9b908822020-09-23 15:55:55 +1000138)
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700139list(TRANSFORM MICROPY_SOURCE_PORT PREPEND ${MICROPY_PORT_DIR}/)
Damien Georgecb31c0a2023-07-15 00:03:59 +1000140list(APPEND MICROPY_SOURCE_PORT ${CMAKE_BINARY_DIR}/pins.c)
Damien George9b908822020-09-23 15:55:55 +1000141
Damien Georgee4650122023-05-09 09:52:54 +1000142list(APPEND MICROPY_SOURCE_QSTR
Damien George9b908822020-09-23 15:55:55 +1000143 ${MICROPY_SOURCE_PY}
144 ${MICROPY_SOURCE_EXTMOD}
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -0400145 ${MICROPY_SOURCE_USERMOD}
Damien George136369d2021-07-09 14:19:15 +1000146 ${MICROPY_SOURCE_SHARED}
Damien George9b908822020-09-23 15:55:55 +1000147 ${MICROPY_SOURCE_LIB}
148 ${MICROPY_SOURCE_PORT}
Damien Georgef046b502021-09-22 00:00:26 +1000149 ${MICROPY_SOURCE_BOARD}
Andrew Leech42479212024-05-23 17:23:41 +1000150 ${MICROPY_SOURCE_TINYUSB}
Damien George9b908822020-09-23 15:55:55 +1000151)
152
Damien Georgee4650122023-05-09 09:52:54 +1000153list(APPEND IDF_COMPONENTS
Damien George9b908822020-09-23 15:55:55 +1000154 app_update
155 bootloader_support
Damien Georgeda2b5fa2021-02-14 01:36:30 +1100156 bt
Damien George9b908822020-09-23 15:55:55 +1000157 driver
Damien Georgee4650122023-05-09 09:52:54 +1000158 esp_adc
159 esp_app_format
Damien George9b908822020-09-23 15:55:55 +1000160 esp_common
161 esp_eth
162 esp_event
Damien Georgee4650122023-05-09 09:52:54 +1000163 esp_hw_support
164 esp_netif
165 esp_partition
166 esp_pm
167 esp_psram
Damien George9b908822020-09-23 15:55:55 +1000168 esp_ringbuf
169 esp_rom
Damien Georgee4650122023-05-09 09:52:54 +1000170 esp_system
171 esp_timer
Damien George9b908822020-09-23 15:55:55 +1000172 esp_wifi
173 freertos
Damien Georgee4650122023-05-09 09:52:54 +1000174 hal
Damien George9b908822020-09-23 15:55:55 +1000175 heap
176 log
177 lwip
178 mbedtls
Damien George9b908822020-09-23 15:55:55 +1000179 newlib
180 nvs_flash
181 sdmmc
182 soc
183 spi_flash
Damien George9b908822020-09-23 15:55:55 +1000184 ulp
Andrew Leech42479212024-05-23 17:23:41 +1000185 usb
Damien George9b908822020-09-23 15:55:55 +1000186 vfs
Damien George9b908822020-09-23 15:55:55 +1000187)
188
Karl Palssoncbd21b32025-01-27 16:05:07 +0000189# Provide the default LD fragment if not set
190if (MICROPY_USER_LDFRAGMENTS)
191 set(MICROPY_LDFRAGMENTS ${MICROPY_USER_LDFRAGMENTS})
Karl Palssoncbd21b32025-01-27 16:05:07 +0000192endif()
193
Angus Grattoncccac2c2025-03-11 17:17:39 +1100194if (UPDATE_SUBMODULES)
195 # ESP-IDF checks if some paths exist before CMake does. Some paths don't
196 # yet exist if this is an UPDATE_SUBMODULES pass on a brand new checkout, so remove
197 # any path which might not exist yet. A "real" build will not set UPDATE_SUBMODULES.
198 unset(MICROPY_SOURCE_TINYUSB)
199 unset(MICROPY_SOURCE_EXTMOD)
200 unset(MICROPY_SOURCE_LIB)
201 unset(MICROPY_INC_TINYUSB)
202 unset(MICROPY_INC_CORE)
203endif()
204
Damien George9b908822020-09-23 15:55:55 +1000205# Register the main IDF component.
206idf_component_register(
207 SRCS
208 ${MICROPY_SOURCE_PY}
209 ${MICROPY_SOURCE_EXTMOD}
Damien George136369d2021-07-09 14:19:15 +1000210 ${MICROPY_SOURCE_SHARED}
Damien George9b908822020-09-23 15:55:55 +1000211 ${MICROPY_SOURCE_LIB}
212 ${MICROPY_SOURCE_DRIVERS}
213 ${MICROPY_SOURCE_PORT}
Damien Georgef046b502021-09-22 00:00:26 +1000214 ${MICROPY_SOURCE_BOARD}
Andrew Leech42479212024-05-23 17:23:41 +1000215 ${MICROPY_SOURCE_TINYUSB}
Damien George9b908822020-09-23 15:55:55 +1000216 INCLUDE_DIRS
Damien George5dcc9b32021-04-09 00:59:16 +1000217 ${MICROPY_INC_CORE}
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -0400218 ${MICROPY_INC_USERMOD}
Andrew Leech42479212024-05-23 17:23:41 +1000219 ${MICROPY_INC_TINYUSB}
Damien George9b908822020-09-23 15:55:55 +1000220 ${MICROPY_PORT_DIR}
221 ${MICROPY_BOARD_DIR}
222 ${CMAKE_BINARY_DIR}
Jim Mussaredacbdbcd2024-03-21 15:46:41 +1100223 LDFRAGMENTS
Karl Palssoncbd21b32025-01-27 16:05:07 +0000224 ${MICROPY_LDFRAGMENTS}
Damien George9b908822020-09-23 15:55:55 +1000225 REQUIRES
226 ${IDF_COMPONENTS}
227)
228
229# Set the MicroPython target as the current (main) IDF component target.
230set(MICROPY_TARGET ${COMPONENT_TARGET})
231
232# Define mpy-cross flags, for use with frozen code.
Alessandro Gattifda9bf42025-03-22 02:40:59 +0100233if(CONFIG_IDF_TARGET_ARCH_XTENSA)
234 set(MICROPY_CROSS_FLAGS -march=xtensawin)
235elseif(CONFIG_IDF_TARGET_ARCH_RISCV)
236 set(MICROPY_CROSS_FLAGS -march=rv32imc)
Alessandro Gatti95ce61d2023-10-31 13:28:25 +0100237endif()
Damien George9b908822020-09-23 15:55:55 +1000238
239# Set compile options for this port.
240target_compile_definitions(${MICROPY_TARGET} PUBLIC
Alessandro Gatti116d0d42025-03-25 21:53:45 +0100241 ${MICROPY_DEF_COMPONENT}
Damien Georgeab9d47e2021-04-08 23:56:28 +1000242 ${MICROPY_DEF_CORE}
Jim Mussared2fbf42d2023-07-25 15:03:30 +1000243 ${MICROPY_DEF_BOARD}
Andrew Leech42479212024-05-23 17:23:41 +1000244 ${MICROPY_DEF_TINYUSB}
Damien George9b908822020-09-23 15:55:55 +1000245 MICROPY_VFS_FAT=1
246 MICROPY_VFS_LFS2=1
247 FFCONF_H=\"${MICROPY_OOFATFS_DIR}/ffconf.h\"
248 LFS1_NO_MALLOC LFS1_NO_DEBUG LFS1_NO_WARN LFS1_NO_ERROR LFS1_NO_ASSERT
249 LFS2_NO_MALLOC LFS2_NO_DEBUG LFS2_NO_WARN LFS2_NO_ERROR LFS2_NO_ASSERT
250)
251
252# Disable some warnings to keep the build output clean.
253target_compile_options(${MICROPY_TARGET} PUBLIC
Alessandro Gatti116d0d42025-03-25 21:53:45 +0100254 ${MICROPY_COMPILE_COMPONENT}
Damien George9b908822020-09-23 15:55:55 +1000255 -Wno-clobbered
256 -Wno-deprecated-declarations
257 -Wno-missing-field-initializers
258)
259
Damien George5dbb8222021-03-17 12:35:59 +1100260# Additional include directories needed for private NimBLE headers.
261target_include_directories(${MICROPY_TARGET} PUBLIC
262 ${IDF_PATH}/components/bt/host/nimble/nimble
263)
264
Damien Georgeab9d47e2021-04-08 23:56:28 +1000265# Add additional extmod and usermod components.
Alessandro Gattie3c2cf72025-03-21 02:12:31 +0100266if (MICROPY_PY_BTREE)
267 target_link_libraries(${MICROPY_TARGET} micropy_extmod_btree)
268endif()
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -0400269target_link_libraries(${MICROPY_TARGET} usermod)
270
Angus Grattonb20687d2024-12-03 10:02:46 +1100271# Extra linker options
272# (when wrap symbols are in standalone files, --undefined ensures
273# the linker doesn't skip that file.)
274target_link_options(${MICROPY_TARGET} PUBLIC
275 # Patch LWIP memory pool allocators (see lwip_patch.c)
276 -Wl,--undefined=memp_malloc
277 -Wl,--wrap=memp_malloc
278 -Wl,--wrap=memp_free
Angus Grattonff70a912024-10-22 15:02:32 +1100279
Angus Grattonb20687d2024-12-03 10:02:46 +1100280 # Enable the panic handler wrapper
281 -Wl,--undefined=esp_panic_handler
282 -Wl,--wrap=esp_panic_handler
283)
Angus Grattonff70a912024-10-22 15:02:32 +1100284
Luca Burelli31ef7c12023-07-05 18:03:11 +0200285# Collect all of the include directories and compile definitions for the IDF components,
286# including those added by the IDF Component Manager via idf_components.yaml.
287foreach(comp ${__COMPONENT_NAMES_RESOLVED})
Damien George0fabda32021-04-08 23:42:22 +1000288 micropy_gather_target_properties(__idf_${comp})
Damien Georgee4650122023-05-09 09:52:54 +1000289 micropy_gather_target_properties(${comp})
Damien George9b908822020-09-23 15:55:55 +1000290endforeach()
291
Damien George9b908822020-09-23 15:55:55 +1000292# Include the main MicroPython cmake rules.
293include(${MICROPY_DIR}/py/mkrules.cmake)
Damien Georgecb31c0a2023-07-15 00:03:59 +1000294
295# Generate source files for named pins (requires mkrules.cmake for MICROPY_GENHDR_DIR).
296
297set(GEN_PINS_PREFIX "${MICROPY_PORT_DIR}/boards/pins_prefix.c")
298set(GEN_PINS_MKPINS "${MICROPY_PORT_DIR}/boards/make-pins.py")
299set(GEN_PINS_SRC "${CMAKE_BINARY_DIR}/pins.c")
300set(GEN_PINS_HDR "${MICROPY_GENHDR_DIR}/pins.h")
301
302if(EXISTS "${MICROPY_BOARD_DIR}/pins.csv")
303 set(GEN_PINS_BOARD_CSV "${MICROPY_BOARD_DIR}/pins.csv")
304 set(GEN_PINS_BOARD_CSV_ARG --board-csv "${GEN_PINS_BOARD_CSV}")
305endif()
306
307target_sources(${MICROPY_TARGET} PRIVATE ${GEN_PINS_HDR})
308
309add_custom_command(
310 OUTPUT ${GEN_PINS_SRC} ${GEN_PINS_HDR}
311 COMMAND ${Python3_EXECUTABLE} ${GEN_PINS_MKPINS} ${GEN_PINS_BOARD_CSV_ARG}
312 --prefix ${GEN_PINS_PREFIX} --output-source ${GEN_PINS_SRC} --output-header ${GEN_PINS_HDR}
313 DEPENDS
314 ${MICROPY_MPVERSION}
315 ${GEN_PINS_MKPINS}
316 ${GEN_PINS_BOARD_CSV}
317 ${GEN_PINS_PREFIX}
318 VERBATIM
319 COMMAND_EXPAND_LISTS
320)