blob: d026a64b0ce2180ac5d03446de91d219ce8b1372 [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 +110023 list(APPEND IDF_COMPONENTS riscv)
24endif()
25
26if(NOT DEFINED MICROPY_PY_TINYUSB)
27 if(CONFIG_IDF_TARGET_ESP32S2 OR CONFIG_IDF_TARGET_ESP32S3)
28 set(MICROPY_PY_TINYUSB ON)
29 endif()
30endif()
31
Damien George9b908822020-09-23 15:55:55 +100032# Include core source components.
33include(${MICROPY_DIR}/py/py.cmake)
Damien George9b908822020-09-23 15:55:55 +100034
Jim Mussaredad123ed2023-07-31 17:24:38 +100035# CMAKE_BUILD_EARLY_EXPANSION is set during the component-discovery phase of
36# `idf.py build`, so none of the extmod/usermod (and in reality, most of the
37# micropython) rules need to happen. Specifically, you cannot invoke add_library.
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -040038if(NOT CMAKE_BUILD_EARLY_EXPANSION)
Damien Georgebd375df2022-06-08 12:21:51 +100039 # Enable extmod components that will be configured by extmod.cmake.
40 # A board may also have enabled additional components.
41 set(MICROPY_PY_BTREE ON)
42
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -040043 include(${MICROPY_DIR}/py/usermod.cmake)
Damien George212fe7f2021-04-08 23:45:28 +100044 include(${MICROPY_DIR}/extmod/extmod.cmake)
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -040045endif()
46
Damien Georgee4650122023-05-09 09:52:54 +100047list(APPEND MICROPY_QSTRDEFS_PORT
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -070048 ${MICROPY_PORT_DIR}/qstrdefsport.h
Damien George75db0b92021-02-21 11:33:15 +110049)
50
Damien Georgee4650122023-05-09 09:52:54 +100051list(APPEND MICROPY_SOURCE_SHARED
Damien George136369d2021-07-09 14:19:15 +100052 ${MICROPY_DIR}/shared/readline/readline.c
53 ${MICROPY_DIR}/shared/netutils/netutils.c
54 ${MICROPY_DIR}/shared/timeutils/timeutils.c
55 ${MICROPY_DIR}/shared/runtime/interrupt_char.c
robert-hha04a1412024-03-10 15:14:20 +010056 ${MICROPY_DIR}/shared/runtime/mpirq.c
Damien George136369d2021-07-09 14:19:15 +100057 ${MICROPY_DIR}/shared/runtime/stdout_helpers.c
58 ${MICROPY_DIR}/shared/runtime/sys_stdio_mphal.c
59 ${MICROPY_DIR}/shared/runtime/pyexec.c
60)
61
Damien Georgee4650122023-05-09 09:52:54 +100062list(APPEND MICROPY_SOURCE_LIB
Damien George9b908822020-09-23 15:55:55 +100063 ${MICROPY_DIR}/lib/littlefs/lfs1.c
64 ${MICROPY_DIR}/lib/littlefs/lfs1_util.c
65 ${MICROPY_DIR}/lib/littlefs/lfs2.c
66 ${MICROPY_DIR}/lib/littlefs/lfs2_util.c
Damien Georgec9eb6bc2023-12-12 16:05:03 +110067 ${MICROPY_DIR}/lib/mbedtls_errors/esp32_mbedtls_errors.c
Damien George9b908822020-09-23 15:55:55 +100068 ${MICROPY_DIR}/lib/oofatfs/ff.c
69 ${MICROPY_DIR}/lib/oofatfs/ffunicode.c
Damien George9b908822020-09-23 15:55:55 +100070)
71
Damien Georgee4650122023-05-09 09:52:54 +100072list(APPEND MICROPY_SOURCE_DRIVERS
Damien George9b908822020-09-23 15:55:55 +100073 ${MICROPY_DIR}/drivers/bus/softspi.c
74 ${MICROPY_DIR}/drivers/dht/dht.c
75)
76
Angus Grattoncccac2c2025-03-11 17:17:39 +110077list(APPEND GIT_SUBMODULES lib/tinyusb)
78if(MICROPY_PY_TINYUSB)
Andrew Leech42479212024-05-23 17:23:41 +100079 set(TINYUSB_SRC "${MICROPY_DIR}/lib/tinyusb/src")
80 string(TOUPPER OPT_MCU_${IDF_TARGET} tusb_mcu)
81
82 list(APPEND MICROPY_DEF_TINYUSB
83 CFG_TUSB_MCU=${tusb_mcu}
84 )
85
86 list(APPEND MICROPY_SOURCE_TINYUSB
87 ${TINYUSB_SRC}/tusb.c
88 ${TINYUSB_SRC}/common/tusb_fifo.c
89 ${TINYUSB_SRC}/device/usbd.c
90 ${TINYUSB_SRC}/device/usbd_control.c
91 ${TINYUSB_SRC}/class/cdc/cdc_device.c
92 ${TINYUSB_SRC}/portable/synopsys/dwc2/dcd_dwc2.c
93 ${MICROPY_DIR}/shared/tinyusb/mp_usbd.c
94 ${MICROPY_DIR}/shared/tinyusb/mp_usbd_cdc.c
95 ${MICROPY_DIR}/shared/tinyusb/mp_usbd_descriptor.c
Sebastian Romero5f2d05d2024-12-04 16:30:56 +010096 ${MICROPY_DIR}/shared/tinyusb/mp_usbd_runtime.c
Andrew Leech42479212024-05-23 17:23:41 +100097 )
98
99 list(APPEND MICROPY_INC_TINYUSB
100 ${TINYUSB_SRC}
101 ${MICROPY_DIR}/shared/tinyusb/
102 )
103
104 list(APPEND MICROPY_LINK_TINYUSB
105 -Wl,--wrap=dcd_event_handler
106 )
107endif()
108
Damien Georgee4650122023-05-09 09:52:54 +1000109list(APPEND MICROPY_SOURCE_PORT
Daniƫl van de Giessenc10a74b2024-03-13 18:23:17 +0100110 panichandler.c
Damien George03eae482023-10-25 19:13:11 +1100111 adc.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700112 main.c
Damien George1db40ed2023-06-21 15:09:26 +1000113 ppp_set_auth.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700114 uart.c
115 usb.c
116 usb_serial_jtag.c
117 gccollect.c
118 mphalport.c
119 fatfs_port.c
120 help.c
121 machine_bitstream.c
122 machine_timer.c
123 machine_pin.c
124 machine_touchpad.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700125 machine_dac.c
126 machine_i2c.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700127 network_common.c
128 network_lan.c
129 network_ppp.c
130 network_wlan.c
131 mpnimbleport.c
132 modsocket.c
Angus Gratton82e69df2024-10-02 14:49:49 +1000133 lwip_patch.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700134 modesp.c
135 esp32_nvs.c
136 esp32_partition.c
137 esp32_rmt.c
138 esp32_ulp.c
139 modesp32.c
140 machine_hw_spi.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700141 mpthreadport.c
142 machine_rtc.c
143 machine_sdcard.c
144 modespnow.c
Damien George9b908822020-09-23 15:55:55 +1000145)
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700146list(TRANSFORM MICROPY_SOURCE_PORT PREPEND ${MICROPY_PORT_DIR}/)
Damien Georgecb31c0a2023-07-15 00:03:59 +1000147list(APPEND MICROPY_SOURCE_PORT ${CMAKE_BINARY_DIR}/pins.c)
Damien George9b908822020-09-23 15:55:55 +1000148
Damien Georgee4650122023-05-09 09:52:54 +1000149list(APPEND MICROPY_SOURCE_QSTR
Damien George9b908822020-09-23 15:55:55 +1000150 ${MICROPY_SOURCE_PY}
151 ${MICROPY_SOURCE_EXTMOD}
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -0400152 ${MICROPY_SOURCE_USERMOD}
Damien George136369d2021-07-09 14:19:15 +1000153 ${MICROPY_SOURCE_SHARED}
Damien George9b908822020-09-23 15:55:55 +1000154 ${MICROPY_SOURCE_LIB}
155 ${MICROPY_SOURCE_PORT}
Damien Georgef046b502021-09-22 00:00:26 +1000156 ${MICROPY_SOURCE_BOARD}
Andrew Leech42479212024-05-23 17:23:41 +1000157 ${MICROPY_SOURCE_TINYUSB}
Damien George9b908822020-09-23 15:55:55 +1000158)
159
Damien Georgee4650122023-05-09 09:52:54 +1000160list(APPEND IDF_COMPONENTS
Damien George9b908822020-09-23 15:55:55 +1000161 app_update
162 bootloader_support
Damien Georgeda2b5fa2021-02-14 01:36:30 +1100163 bt
Damien George9b908822020-09-23 15:55:55 +1000164 driver
Damien Georgee4650122023-05-09 09:52:54 +1000165 esp_adc
166 esp_app_format
Damien George9b908822020-09-23 15:55:55 +1000167 esp_common
168 esp_eth
169 esp_event
Damien Georgee4650122023-05-09 09:52:54 +1000170 esp_hw_support
171 esp_netif
172 esp_partition
173 esp_pm
174 esp_psram
Damien George9b908822020-09-23 15:55:55 +1000175 esp_ringbuf
176 esp_rom
Damien Georgee4650122023-05-09 09:52:54 +1000177 esp_system
178 esp_timer
Damien George9b908822020-09-23 15:55:55 +1000179 esp_wifi
180 freertos
Damien Georgee4650122023-05-09 09:52:54 +1000181 hal
Damien George9b908822020-09-23 15:55:55 +1000182 heap
183 log
184 lwip
185 mbedtls
Damien George9b908822020-09-23 15:55:55 +1000186 newlib
187 nvs_flash
188 sdmmc
189 soc
190 spi_flash
Damien George9b908822020-09-23 15:55:55 +1000191 ulp
Andrew Leech42479212024-05-23 17:23:41 +1000192 usb
Damien George9b908822020-09-23 15:55:55 +1000193 vfs
Damien George9b908822020-09-23 15:55:55 +1000194)
195
Karl Palssoncbd21b32025-01-27 16:05:07 +0000196# Provide the default LD fragment if not set
197if (MICROPY_USER_LDFRAGMENTS)
198 set(MICROPY_LDFRAGMENTS ${MICROPY_USER_LDFRAGMENTS})
Karl Palssoncbd21b32025-01-27 16:05:07 +0000199endif()
200
Angus Grattoncccac2c2025-03-11 17:17:39 +1100201if (UPDATE_SUBMODULES)
202 # ESP-IDF checks if some paths exist before CMake does. Some paths don't
203 # yet exist if this is an UPDATE_SUBMODULES pass on a brand new checkout, so remove
204 # any path which might not exist yet. A "real" build will not set UPDATE_SUBMODULES.
205 unset(MICROPY_SOURCE_TINYUSB)
206 unset(MICROPY_SOURCE_EXTMOD)
207 unset(MICROPY_SOURCE_LIB)
208 unset(MICROPY_INC_TINYUSB)
209 unset(MICROPY_INC_CORE)
210endif()
211
Damien George9b908822020-09-23 15:55:55 +1000212# Register the main IDF component.
213idf_component_register(
214 SRCS
215 ${MICROPY_SOURCE_PY}
216 ${MICROPY_SOURCE_EXTMOD}
Damien George136369d2021-07-09 14:19:15 +1000217 ${MICROPY_SOURCE_SHARED}
Damien George9b908822020-09-23 15:55:55 +1000218 ${MICROPY_SOURCE_LIB}
219 ${MICROPY_SOURCE_DRIVERS}
220 ${MICROPY_SOURCE_PORT}
Damien Georgef046b502021-09-22 00:00:26 +1000221 ${MICROPY_SOURCE_BOARD}
Andrew Leech42479212024-05-23 17:23:41 +1000222 ${MICROPY_SOURCE_TINYUSB}
Damien George9b908822020-09-23 15:55:55 +1000223 INCLUDE_DIRS
Damien George5dcc9b32021-04-09 00:59:16 +1000224 ${MICROPY_INC_CORE}
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -0400225 ${MICROPY_INC_USERMOD}
Andrew Leech42479212024-05-23 17:23:41 +1000226 ${MICROPY_INC_TINYUSB}
Damien George9b908822020-09-23 15:55:55 +1000227 ${MICROPY_PORT_DIR}
228 ${MICROPY_BOARD_DIR}
229 ${CMAKE_BINARY_DIR}
Jim Mussaredacbdbcd2024-03-21 15:46:41 +1100230 LDFRAGMENTS
Karl Palssoncbd21b32025-01-27 16:05:07 +0000231 ${MICROPY_LDFRAGMENTS}
Damien George9b908822020-09-23 15:55:55 +1000232 REQUIRES
233 ${IDF_COMPONENTS}
234)
235
236# Set the MicroPython target as the current (main) IDF component target.
237set(MICROPY_TARGET ${COMPONENT_TARGET})
238
239# Define mpy-cross flags, for use with frozen code.
Andrew Leech6d799372023-06-26 11:53:28 +1000240if(CONFIG_IDF_TARGET_ARCH STREQUAL "xtensa")
Damien George9b908822020-09-23 15:55:55 +1000241set(MICROPY_CROSS_FLAGS -march=xtensawin)
Alessandro Gatti95ce61d2023-10-31 13:28:25 +0100242endif()
Damien George9b908822020-09-23 15:55:55 +1000243
244# Set compile options for this port.
245target_compile_definitions(${MICROPY_TARGET} PUBLIC
Damien Georgeab9d47e2021-04-08 23:56:28 +1000246 ${MICROPY_DEF_CORE}
Jim Mussared2fbf42d2023-07-25 15:03:30 +1000247 ${MICROPY_DEF_BOARD}
Andrew Leech42479212024-05-23 17:23:41 +1000248 ${MICROPY_DEF_TINYUSB}
Damien George9b908822020-09-23 15:55:55 +1000249 MICROPY_ESP_IDF_4=1
250 MICROPY_VFS_FAT=1
251 MICROPY_VFS_LFS2=1
252 FFCONF_H=\"${MICROPY_OOFATFS_DIR}/ffconf.h\"
253 LFS1_NO_MALLOC LFS1_NO_DEBUG LFS1_NO_WARN LFS1_NO_ERROR LFS1_NO_ASSERT
254 LFS2_NO_MALLOC LFS2_NO_DEBUG LFS2_NO_WARN LFS2_NO_ERROR LFS2_NO_ASSERT
255)
256
257# Disable some warnings to keep the build output clean.
258target_compile_options(${MICROPY_TARGET} PUBLIC
259 -Wno-clobbered
260 -Wno-deprecated-declarations
261 -Wno-missing-field-initializers
262)
263
Andrew Leech42479212024-05-23 17:23:41 +1000264target_link_options(${MICROPY_TARGET} PUBLIC
265 ${MICROPY_LINK_TINYUSB}
266)
267
Damien George5dbb8222021-03-17 12:35:59 +1100268# Additional include directories needed for private NimBLE headers.
269target_include_directories(${MICROPY_TARGET} PUBLIC
270 ${IDF_PATH}/components/bt/host/nimble/nimble
271)
272
Damien Georgeab9d47e2021-04-08 23:56:28 +1000273# Add additional extmod and usermod components.
274target_link_libraries(${MICROPY_TARGET} micropy_extmod_btree)
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -0400275target_link_libraries(${MICROPY_TARGET} usermod)
276
Angus Grattonb20687d2024-12-03 10:02:46 +1100277# Extra linker options
278# (when wrap symbols are in standalone files, --undefined ensures
279# the linker doesn't skip that file.)
280target_link_options(${MICROPY_TARGET} PUBLIC
281 # Patch LWIP memory pool allocators (see lwip_patch.c)
282 -Wl,--undefined=memp_malloc
283 -Wl,--wrap=memp_malloc
284 -Wl,--wrap=memp_free
Angus Grattonff70a912024-10-22 15:02:32 +1100285
Angus Grattonb20687d2024-12-03 10:02:46 +1100286 # Enable the panic handler wrapper
287 -Wl,--undefined=esp_panic_handler
288 -Wl,--wrap=esp_panic_handler
289)
Angus Grattonff70a912024-10-22 15:02:32 +1100290
Luca Burelli31ef7c12023-07-05 18:03:11 +0200291# Collect all of the include directories and compile definitions for the IDF components,
292# including those added by the IDF Component Manager via idf_components.yaml.
293foreach(comp ${__COMPONENT_NAMES_RESOLVED})
Damien George0fabda32021-04-08 23:42:22 +1000294 micropy_gather_target_properties(__idf_${comp})
Damien Georgee4650122023-05-09 09:52:54 +1000295 micropy_gather_target_properties(${comp})
Damien George9b908822020-09-23 15:55:55 +1000296endforeach()
297
Damien George9b908822020-09-23 15:55:55 +1000298# Include the main MicroPython cmake rules.
299include(${MICROPY_DIR}/py/mkrules.cmake)
Damien Georgecb31c0a2023-07-15 00:03:59 +1000300
301# Generate source files for named pins (requires mkrules.cmake for MICROPY_GENHDR_DIR).
302
303set(GEN_PINS_PREFIX "${MICROPY_PORT_DIR}/boards/pins_prefix.c")
304set(GEN_PINS_MKPINS "${MICROPY_PORT_DIR}/boards/make-pins.py")
305set(GEN_PINS_SRC "${CMAKE_BINARY_DIR}/pins.c")
306set(GEN_PINS_HDR "${MICROPY_GENHDR_DIR}/pins.h")
307
308if(EXISTS "${MICROPY_BOARD_DIR}/pins.csv")
309 set(GEN_PINS_BOARD_CSV "${MICROPY_BOARD_DIR}/pins.csv")
310 set(GEN_PINS_BOARD_CSV_ARG --board-csv "${GEN_PINS_BOARD_CSV}")
311endif()
312
313target_sources(${MICROPY_TARGET} PRIVATE ${GEN_PINS_HDR})
314
315add_custom_command(
316 OUTPUT ${GEN_PINS_SRC} ${GEN_PINS_HDR}
317 COMMAND ${Python3_EXECUTABLE} ${GEN_PINS_MKPINS} ${GEN_PINS_BOARD_CSV_ARG}
318 --prefix ${GEN_PINS_PREFIX} --output-source ${GEN_PINS_SRC} --output-header ${GEN_PINS_HDR}
319 DEPENDS
320 ${MICROPY_MPVERSION}
321 ${GEN_PINS_MKPINS}
322 ${GEN_PINS_BOARD_CSV}
323 ${GEN_PINS_PREFIX}
324 VERBATIM
325 COMMAND_EXPAND_LISTS
326)