blob: 0a6b29983c2f583d0b5fd200adf935fd46e97204 [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
17# Include core source components.
18include(${MICROPY_DIR}/py/py.cmake)
Damien George9b908822020-09-23 15:55:55 +100019
Jim Mussaredad123ed2023-07-31 17:24:38 +100020# CMAKE_BUILD_EARLY_EXPANSION is set during the component-discovery phase of
21# `idf.py build`, so none of the extmod/usermod (and in reality, most of the
22# micropython) rules need to happen. Specifically, you cannot invoke add_library.
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -040023if(NOT CMAKE_BUILD_EARLY_EXPANSION)
Damien Georgebd375df2022-06-08 12:21:51 +100024 # Enable extmod components that will be configured by extmod.cmake.
25 # A board may also have enabled additional components.
26 set(MICROPY_PY_BTREE ON)
27
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -040028 include(${MICROPY_DIR}/py/usermod.cmake)
Damien George212fe7f2021-04-08 23:45:28 +100029 include(${MICROPY_DIR}/extmod/extmod.cmake)
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -040030endif()
31
Damien Georgee4650122023-05-09 09:52:54 +100032list(APPEND MICROPY_QSTRDEFS_PORT
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -070033 ${MICROPY_PORT_DIR}/qstrdefsport.h
Damien George75db0b92021-02-21 11:33:15 +110034)
35
Damien Georgee4650122023-05-09 09:52:54 +100036list(APPEND MICROPY_SOURCE_SHARED
Damien George136369d2021-07-09 14:19:15 +100037 ${MICROPY_DIR}/shared/readline/readline.c
38 ${MICROPY_DIR}/shared/netutils/netutils.c
39 ${MICROPY_DIR}/shared/timeutils/timeutils.c
40 ${MICROPY_DIR}/shared/runtime/interrupt_char.c
robert-hha04a1412024-03-10 15:14:20 +010041 ${MICROPY_DIR}/shared/runtime/mpirq.c
Damien George136369d2021-07-09 14:19:15 +100042 ${MICROPY_DIR}/shared/runtime/stdout_helpers.c
43 ${MICROPY_DIR}/shared/runtime/sys_stdio_mphal.c
44 ${MICROPY_DIR}/shared/runtime/pyexec.c
45)
46
Damien Georgee4650122023-05-09 09:52:54 +100047list(APPEND MICROPY_SOURCE_LIB
Damien George9b908822020-09-23 15:55:55 +100048 ${MICROPY_DIR}/lib/littlefs/lfs1.c
49 ${MICROPY_DIR}/lib/littlefs/lfs1_util.c
50 ${MICROPY_DIR}/lib/littlefs/lfs2.c
51 ${MICROPY_DIR}/lib/littlefs/lfs2_util.c
Damien Georgec9eb6bc2023-12-12 16:05:03 +110052 ${MICROPY_DIR}/lib/mbedtls_errors/esp32_mbedtls_errors.c
Damien George9b908822020-09-23 15:55:55 +100053 ${MICROPY_DIR}/lib/oofatfs/ff.c
54 ${MICROPY_DIR}/lib/oofatfs/ffunicode.c
Damien George9b908822020-09-23 15:55:55 +100055)
56
Damien Georgee4650122023-05-09 09:52:54 +100057list(APPEND MICROPY_SOURCE_DRIVERS
Damien George9b908822020-09-23 15:55:55 +100058 ${MICROPY_DIR}/drivers/bus/softspi.c
59 ${MICROPY_DIR}/drivers/dht/dht.c
60)
61
Andrew Leech42479212024-05-23 17:23:41 +100062string(CONCAT GIT_SUBMODULES "${GIT_SUBMODULES} " lib/tinyusb)
63if(MICROPY_PY_TINYUSB)
64 set(TINYUSB_SRC "${MICROPY_DIR}/lib/tinyusb/src")
65 string(TOUPPER OPT_MCU_${IDF_TARGET} tusb_mcu)
66
67 list(APPEND MICROPY_DEF_TINYUSB
68 CFG_TUSB_MCU=${tusb_mcu}
69 )
70
71 list(APPEND MICROPY_SOURCE_TINYUSB
72 ${TINYUSB_SRC}/tusb.c
73 ${TINYUSB_SRC}/common/tusb_fifo.c
74 ${TINYUSB_SRC}/device/usbd.c
75 ${TINYUSB_SRC}/device/usbd_control.c
76 ${TINYUSB_SRC}/class/cdc/cdc_device.c
77 ${TINYUSB_SRC}/portable/synopsys/dwc2/dcd_dwc2.c
78 ${MICROPY_DIR}/shared/tinyusb/mp_usbd.c
79 ${MICROPY_DIR}/shared/tinyusb/mp_usbd_cdc.c
80 ${MICROPY_DIR}/shared/tinyusb/mp_usbd_descriptor.c
81 )
82
83 list(APPEND MICROPY_INC_TINYUSB
84 ${TINYUSB_SRC}
85 ${MICROPY_DIR}/shared/tinyusb/
86 )
87
88 list(APPEND MICROPY_LINK_TINYUSB
89 -Wl,--wrap=dcd_event_handler
90 )
91endif()
92
Damien Georgee4650122023-05-09 09:52:54 +100093list(APPEND MICROPY_SOURCE_PORT
Daniƫl van de Giessenc10a74b2024-03-13 18:23:17 +010094 panichandler.c
Damien George03eae482023-10-25 19:13:11 +110095 adc.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -070096 main.c
Damien George1db40ed2023-06-21 15:09:26 +100097 ppp_set_auth.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -070098 uart.c
99 usb.c
100 usb_serial_jtag.c
101 gccollect.c
102 mphalport.c
103 fatfs_port.c
104 help.c
105 machine_bitstream.c
106 machine_timer.c
107 machine_pin.c
108 machine_touchpad.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700109 machine_dac.c
110 machine_i2c.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700111 network_common.c
112 network_lan.c
113 network_ppp.c
114 network_wlan.c
115 mpnimbleport.c
116 modsocket.c
Angus Gratton82e69df2024-10-02 14:49:49 +1000117 lwip_patch.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700118 modesp.c
119 esp32_nvs.c
120 esp32_partition.c
121 esp32_rmt.c
122 esp32_ulp.c
123 modesp32.c
124 machine_hw_spi.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700125 mpthreadport.c
126 machine_rtc.c
127 machine_sdcard.c
128 modespnow.c
Damien George9b908822020-09-23 15:55:55 +1000129)
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700130list(TRANSFORM MICROPY_SOURCE_PORT PREPEND ${MICROPY_PORT_DIR}/)
Damien Georgecb31c0a2023-07-15 00:03:59 +1000131list(APPEND MICROPY_SOURCE_PORT ${CMAKE_BINARY_DIR}/pins.c)
Damien George9b908822020-09-23 15:55:55 +1000132
Damien Georgee4650122023-05-09 09:52:54 +1000133list(APPEND MICROPY_SOURCE_QSTR
Damien George9b908822020-09-23 15:55:55 +1000134 ${MICROPY_SOURCE_PY}
135 ${MICROPY_SOURCE_EXTMOD}
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -0400136 ${MICROPY_SOURCE_USERMOD}
Damien George136369d2021-07-09 14:19:15 +1000137 ${MICROPY_SOURCE_SHARED}
Damien George9b908822020-09-23 15:55:55 +1000138 ${MICROPY_SOURCE_LIB}
139 ${MICROPY_SOURCE_PORT}
Damien Georgef046b502021-09-22 00:00:26 +1000140 ${MICROPY_SOURCE_BOARD}
Andrew Leech42479212024-05-23 17:23:41 +1000141 ${MICROPY_SOURCE_TINYUSB}
Damien George9b908822020-09-23 15:55:55 +1000142)
143
Damien Georgee4650122023-05-09 09:52:54 +1000144list(APPEND IDF_COMPONENTS
Damien George9b908822020-09-23 15:55:55 +1000145 app_update
146 bootloader_support
Damien Georgeda2b5fa2021-02-14 01:36:30 +1100147 bt
Damien George9b908822020-09-23 15:55:55 +1000148 driver
Damien Georgee4650122023-05-09 09:52:54 +1000149 esp_adc
150 esp_app_format
Damien George9b908822020-09-23 15:55:55 +1000151 esp_common
152 esp_eth
153 esp_event
Damien Georgee4650122023-05-09 09:52:54 +1000154 esp_hw_support
155 esp_netif
156 esp_partition
157 esp_pm
158 esp_psram
Damien George9b908822020-09-23 15:55:55 +1000159 esp_ringbuf
160 esp_rom
Damien Georgee4650122023-05-09 09:52:54 +1000161 esp_system
162 esp_timer
Damien George9b908822020-09-23 15:55:55 +1000163 esp_wifi
164 freertos
Damien Georgee4650122023-05-09 09:52:54 +1000165 hal
Damien George9b908822020-09-23 15:55:55 +1000166 heap
167 log
168 lwip
169 mbedtls
Damien George9b908822020-09-23 15:55:55 +1000170 newlib
171 nvs_flash
172 sdmmc
173 soc
174 spi_flash
Damien George9b908822020-09-23 15:55:55 +1000175 ulp
Andrew Leech42479212024-05-23 17:23:41 +1000176 usb
Damien George9b908822020-09-23 15:55:55 +1000177 vfs
Damien George9b908822020-09-23 15:55:55 +1000178)
179
Damien George9b908822020-09-23 15:55:55 +1000180# Register the main IDF component.
181idf_component_register(
182 SRCS
183 ${MICROPY_SOURCE_PY}
184 ${MICROPY_SOURCE_EXTMOD}
Damien George136369d2021-07-09 14:19:15 +1000185 ${MICROPY_SOURCE_SHARED}
Damien George9b908822020-09-23 15:55:55 +1000186 ${MICROPY_SOURCE_LIB}
187 ${MICROPY_SOURCE_DRIVERS}
188 ${MICROPY_SOURCE_PORT}
Damien Georgef046b502021-09-22 00:00:26 +1000189 ${MICROPY_SOURCE_BOARD}
Andrew Leech42479212024-05-23 17:23:41 +1000190 ${MICROPY_SOURCE_TINYUSB}
Damien George9b908822020-09-23 15:55:55 +1000191 INCLUDE_DIRS
Damien George5dcc9b32021-04-09 00:59:16 +1000192 ${MICROPY_INC_CORE}
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -0400193 ${MICROPY_INC_USERMOD}
Andrew Leech42479212024-05-23 17:23:41 +1000194 ${MICROPY_INC_TINYUSB}
Damien George9b908822020-09-23 15:55:55 +1000195 ${MICROPY_PORT_DIR}
196 ${MICROPY_BOARD_DIR}
197 ${CMAKE_BINARY_DIR}
Jim Mussaredacbdbcd2024-03-21 15:46:41 +1100198 LDFRAGMENTS
199 linker.lf
Damien George9b908822020-09-23 15:55:55 +1000200 REQUIRES
201 ${IDF_COMPONENTS}
202)
203
204# Set the MicroPython target as the current (main) IDF component target.
205set(MICROPY_TARGET ${COMPONENT_TARGET})
206
207# Define mpy-cross flags, for use with frozen code.
Andrew Leech6d799372023-06-26 11:53:28 +1000208if(CONFIG_IDF_TARGET_ARCH STREQUAL "xtensa")
Damien George9b908822020-09-23 15:55:55 +1000209set(MICROPY_CROSS_FLAGS -march=xtensawin)
Alessandro Gatti95ce61d2023-10-31 13:28:25 +0100210endif()
Damien George9b908822020-09-23 15:55:55 +1000211
212# Set compile options for this port.
213target_compile_definitions(${MICROPY_TARGET} PUBLIC
Damien Georgeab9d47e2021-04-08 23:56:28 +1000214 ${MICROPY_DEF_CORE}
Jim Mussared2fbf42d2023-07-25 15:03:30 +1000215 ${MICROPY_DEF_BOARD}
Andrew Leech42479212024-05-23 17:23:41 +1000216 ${MICROPY_DEF_TINYUSB}
Damien George9b908822020-09-23 15:55:55 +1000217 MICROPY_ESP_IDF_4=1
218 MICROPY_VFS_FAT=1
219 MICROPY_VFS_LFS2=1
220 FFCONF_H=\"${MICROPY_OOFATFS_DIR}/ffconf.h\"
221 LFS1_NO_MALLOC LFS1_NO_DEBUG LFS1_NO_WARN LFS1_NO_ERROR LFS1_NO_ASSERT
222 LFS2_NO_MALLOC LFS2_NO_DEBUG LFS2_NO_WARN LFS2_NO_ERROR LFS2_NO_ASSERT
223)
224
225# Disable some warnings to keep the build output clean.
226target_compile_options(${MICROPY_TARGET} PUBLIC
227 -Wno-clobbered
228 -Wno-deprecated-declarations
229 -Wno-missing-field-initializers
230)
231
Andrew Leech42479212024-05-23 17:23:41 +1000232target_link_options(${MICROPY_TARGET} PUBLIC
233 ${MICROPY_LINK_TINYUSB}
234)
235
Damien George5dbb8222021-03-17 12:35:59 +1100236# Additional include directories needed for private NimBLE headers.
237target_include_directories(${MICROPY_TARGET} PUBLIC
238 ${IDF_PATH}/components/bt/host/nimble/nimble
239)
240
Damien Georgeab9d47e2021-04-08 23:56:28 +1000241# Add additional extmod and usermod components.
242target_link_libraries(${MICROPY_TARGET} micropy_extmod_btree)
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -0400243target_link_libraries(${MICROPY_TARGET} usermod)
244
Angus Grattonb20687d2024-12-03 10:02:46 +1100245# Extra linker options
246# (when wrap symbols are in standalone files, --undefined ensures
247# the linker doesn't skip that file.)
248target_link_options(${MICROPY_TARGET} PUBLIC
249 # Patch LWIP memory pool allocators (see lwip_patch.c)
250 -Wl,--undefined=memp_malloc
251 -Wl,--wrap=memp_malloc
252 -Wl,--wrap=memp_free
Angus Grattonff70a912024-10-22 15:02:32 +1100253
Angus Grattonb20687d2024-12-03 10:02:46 +1100254 # Enable the panic handler wrapper
255 -Wl,--undefined=esp_panic_handler
256 -Wl,--wrap=esp_panic_handler
257)
Angus Grattonff70a912024-10-22 15:02:32 +1100258
Luca Burelli31ef7c12023-07-05 18:03:11 +0200259# Collect all of the include directories and compile definitions for the IDF components,
260# including those added by the IDF Component Manager via idf_components.yaml.
261foreach(comp ${__COMPONENT_NAMES_RESOLVED})
Damien George0fabda32021-04-08 23:42:22 +1000262 micropy_gather_target_properties(__idf_${comp})
Damien Georgee4650122023-05-09 09:52:54 +1000263 micropy_gather_target_properties(${comp})
Damien George9b908822020-09-23 15:55:55 +1000264endforeach()
265
Damien George9b908822020-09-23 15:55:55 +1000266# Include the main MicroPython cmake rules.
267include(${MICROPY_DIR}/py/mkrules.cmake)
Damien Georgecb31c0a2023-07-15 00:03:59 +1000268
269# Generate source files for named pins (requires mkrules.cmake for MICROPY_GENHDR_DIR).
270
271set(GEN_PINS_PREFIX "${MICROPY_PORT_DIR}/boards/pins_prefix.c")
272set(GEN_PINS_MKPINS "${MICROPY_PORT_DIR}/boards/make-pins.py")
273set(GEN_PINS_SRC "${CMAKE_BINARY_DIR}/pins.c")
274set(GEN_PINS_HDR "${MICROPY_GENHDR_DIR}/pins.h")
275
276if(EXISTS "${MICROPY_BOARD_DIR}/pins.csv")
277 set(GEN_PINS_BOARD_CSV "${MICROPY_BOARD_DIR}/pins.csv")
278 set(GEN_PINS_BOARD_CSV_ARG --board-csv "${GEN_PINS_BOARD_CSV}")
279endif()
280
281target_sources(${MICROPY_TARGET} PRIVATE ${GEN_PINS_HDR})
282
283add_custom_command(
284 OUTPUT ${GEN_PINS_SRC} ${GEN_PINS_HDR}
285 COMMAND ${Python3_EXECUTABLE} ${GEN_PINS_MKPINS} ${GEN_PINS_BOARD_CSV_ARG}
286 --prefix ${GEN_PINS_PREFIX} --output-source ${GEN_PINS_SRC} --output-header ${GEN_PINS_HDR}
287 DEPENDS
288 ${MICROPY_MPVERSION}
289 ${GEN_PINS_MKPINS}
290 ${GEN_PINS_BOARD_CSV}
291 ${GEN_PINS_PREFIX}
292 VERBATIM
293 COMMAND_EXPAND_LISTS
294)