blob: b6bfcdacc3cb4eacddf9c030ec54f693fa1bbcc6 [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
Sebastian Romero5f2d05d2024-12-04 16:30:56 +010081 ${MICROPY_DIR}/shared/tinyusb/mp_usbd_runtime.c
Andrew Leech42479212024-05-23 17:23:41 +100082 )
83
84 list(APPEND MICROPY_INC_TINYUSB
85 ${TINYUSB_SRC}
86 ${MICROPY_DIR}/shared/tinyusb/
87 )
88
89 list(APPEND MICROPY_LINK_TINYUSB
90 -Wl,--wrap=dcd_event_handler
91 )
92endif()
93
Damien Georgee4650122023-05-09 09:52:54 +100094list(APPEND MICROPY_SOURCE_PORT
Daniƫl van de Giessenc10a74b2024-03-13 18:23:17 +010095 panichandler.c
Damien George03eae482023-10-25 19:13:11 +110096 adc.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -070097 main.c
Damien George1db40ed2023-06-21 15:09:26 +100098 ppp_set_auth.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -070099 uart.c
100 usb.c
101 usb_serial_jtag.c
102 gccollect.c
103 mphalport.c
104 fatfs_port.c
105 help.c
106 machine_bitstream.c
107 machine_timer.c
108 machine_pin.c
109 machine_touchpad.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700110 machine_dac.c
111 machine_i2c.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700112 network_common.c
113 network_lan.c
114 network_ppp.c
115 network_wlan.c
116 mpnimbleport.c
117 modsocket.c
Angus Gratton82e69df2024-10-02 14:49:49 +1000118 lwip_patch.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700119 modesp.c
120 esp32_nvs.c
121 esp32_partition.c
122 esp32_rmt.c
123 esp32_ulp.c
124 modesp32.c
125 machine_hw_spi.c
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700126 mpthreadport.c
127 machine_rtc.c
128 machine_sdcard.c
129 modespnow.c
Damien George9b908822020-09-23 15:55:55 +1000130)
Brian 'redbeard' Harrington5fe2a3f2023-06-12 13:02:10 -0700131list(TRANSFORM MICROPY_SOURCE_PORT PREPEND ${MICROPY_PORT_DIR}/)
Damien Georgecb31c0a2023-07-15 00:03:59 +1000132list(APPEND MICROPY_SOURCE_PORT ${CMAKE_BINARY_DIR}/pins.c)
Damien George9b908822020-09-23 15:55:55 +1000133
Damien Georgee4650122023-05-09 09:52:54 +1000134list(APPEND MICROPY_SOURCE_QSTR
Damien George9b908822020-09-23 15:55:55 +1000135 ${MICROPY_SOURCE_PY}
136 ${MICROPY_SOURCE_EXTMOD}
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -0400137 ${MICROPY_SOURCE_USERMOD}
Damien George136369d2021-07-09 14:19:15 +1000138 ${MICROPY_SOURCE_SHARED}
Damien George9b908822020-09-23 15:55:55 +1000139 ${MICROPY_SOURCE_LIB}
140 ${MICROPY_SOURCE_PORT}
Damien Georgef046b502021-09-22 00:00:26 +1000141 ${MICROPY_SOURCE_BOARD}
Andrew Leech42479212024-05-23 17:23:41 +1000142 ${MICROPY_SOURCE_TINYUSB}
Damien George9b908822020-09-23 15:55:55 +1000143)
144
Damien Georgee4650122023-05-09 09:52:54 +1000145list(APPEND IDF_COMPONENTS
Damien George9b908822020-09-23 15:55:55 +1000146 app_update
147 bootloader_support
Damien Georgeda2b5fa2021-02-14 01:36:30 +1100148 bt
Damien George9b908822020-09-23 15:55:55 +1000149 driver
Damien Georgee4650122023-05-09 09:52:54 +1000150 esp_adc
151 esp_app_format
Damien George9b908822020-09-23 15:55:55 +1000152 esp_common
153 esp_eth
154 esp_event
Damien Georgee4650122023-05-09 09:52:54 +1000155 esp_hw_support
156 esp_netif
157 esp_partition
158 esp_pm
159 esp_psram
Damien George9b908822020-09-23 15:55:55 +1000160 esp_ringbuf
161 esp_rom
Damien Georgee4650122023-05-09 09:52:54 +1000162 esp_system
163 esp_timer
Damien George9b908822020-09-23 15:55:55 +1000164 esp_wifi
165 freertos
Damien Georgee4650122023-05-09 09:52:54 +1000166 hal
Damien George9b908822020-09-23 15:55:55 +1000167 heap
168 log
169 lwip
170 mbedtls
Damien George9b908822020-09-23 15:55:55 +1000171 newlib
172 nvs_flash
173 sdmmc
174 soc
175 spi_flash
Damien George9b908822020-09-23 15:55:55 +1000176 ulp
Andrew Leech42479212024-05-23 17:23:41 +1000177 usb
Damien George9b908822020-09-23 15:55:55 +1000178 vfs
Damien George9b908822020-09-23 15:55:55 +1000179)
180
Damien George9b908822020-09-23 15:55:55 +1000181# Register the main IDF component.
182idf_component_register(
183 SRCS
184 ${MICROPY_SOURCE_PY}
185 ${MICROPY_SOURCE_EXTMOD}
Damien George136369d2021-07-09 14:19:15 +1000186 ${MICROPY_SOURCE_SHARED}
Damien George9b908822020-09-23 15:55:55 +1000187 ${MICROPY_SOURCE_LIB}
188 ${MICROPY_SOURCE_DRIVERS}
189 ${MICROPY_SOURCE_PORT}
Damien Georgef046b502021-09-22 00:00:26 +1000190 ${MICROPY_SOURCE_BOARD}
Andrew Leech42479212024-05-23 17:23:41 +1000191 ${MICROPY_SOURCE_TINYUSB}
Damien George9b908822020-09-23 15:55:55 +1000192 INCLUDE_DIRS
Damien George5dcc9b32021-04-09 00:59:16 +1000193 ${MICROPY_INC_CORE}
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -0400194 ${MICROPY_INC_USERMOD}
Andrew Leech42479212024-05-23 17:23:41 +1000195 ${MICROPY_INC_TINYUSB}
Damien George9b908822020-09-23 15:55:55 +1000196 ${MICROPY_PORT_DIR}
197 ${MICROPY_BOARD_DIR}
198 ${CMAKE_BINARY_DIR}
Jim Mussaredacbdbcd2024-03-21 15:46:41 +1100199 LDFRAGMENTS
200 linker.lf
Damien George9b908822020-09-23 15:55:55 +1000201 REQUIRES
202 ${IDF_COMPONENTS}
203)
204
205# Set the MicroPython target as the current (main) IDF component target.
206set(MICROPY_TARGET ${COMPONENT_TARGET})
207
208# Define mpy-cross flags, for use with frozen code.
Andrew Leech6d799372023-06-26 11:53:28 +1000209if(CONFIG_IDF_TARGET_ARCH STREQUAL "xtensa")
Damien George9b908822020-09-23 15:55:55 +1000210set(MICROPY_CROSS_FLAGS -march=xtensawin)
Alessandro Gatti95ce61d2023-10-31 13:28:25 +0100211endif()
Damien George9b908822020-09-23 15:55:55 +1000212
213# Set compile options for this port.
214target_compile_definitions(${MICROPY_TARGET} PUBLIC
Damien Georgeab9d47e2021-04-08 23:56:28 +1000215 ${MICROPY_DEF_CORE}
Jim Mussared2fbf42d2023-07-25 15:03:30 +1000216 ${MICROPY_DEF_BOARD}
Andrew Leech42479212024-05-23 17:23:41 +1000217 ${MICROPY_DEF_TINYUSB}
Damien George9b908822020-09-23 15:55:55 +1000218 MICROPY_ESP_IDF_4=1
219 MICROPY_VFS_FAT=1
220 MICROPY_VFS_LFS2=1
221 FFCONF_H=\"${MICROPY_OOFATFS_DIR}/ffconf.h\"
222 LFS1_NO_MALLOC LFS1_NO_DEBUG LFS1_NO_WARN LFS1_NO_ERROR LFS1_NO_ASSERT
223 LFS2_NO_MALLOC LFS2_NO_DEBUG LFS2_NO_WARN LFS2_NO_ERROR LFS2_NO_ASSERT
224)
225
226# Disable some warnings to keep the build output clean.
227target_compile_options(${MICROPY_TARGET} PUBLIC
228 -Wno-clobbered
229 -Wno-deprecated-declarations
230 -Wno-missing-field-initializers
231)
232
Andrew Leech42479212024-05-23 17:23:41 +1000233target_link_options(${MICROPY_TARGET} PUBLIC
234 ${MICROPY_LINK_TINYUSB}
235)
236
Damien George5dbb8222021-03-17 12:35:59 +1100237# Additional include directories needed for private NimBLE headers.
238target_include_directories(${MICROPY_TARGET} PUBLIC
239 ${IDF_PATH}/components/bt/host/nimble/nimble
240)
241
Damien Georgeab9d47e2021-04-08 23:56:28 +1000242# Add additional extmod and usermod components.
243target_link_libraries(${MICROPY_TARGET} micropy_extmod_btree)
Michael O'Cleirigh0ccd9e02021-03-27 17:10:39 -0400244target_link_libraries(${MICROPY_TARGET} usermod)
245
Angus Grattonb20687d2024-12-03 10:02:46 +1100246# Extra linker options
247# (when wrap symbols are in standalone files, --undefined ensures
248# the linker doesn't skip that file.)
249target_link_options(${MICROPY_TARGET} PUBLIC
250 # Patch LWIP memory pool allocators (see lwip_patch.c)
251 -Wl,--undefined=memp_malloc
252 -Wl,--wrap=memp_malloc
253 -Wl,--wrap=memp_free
Angus Grattonff70a912024-10-22 15:02:32 +1100254
Angus Grattonb20687d2024-12-03 10:02:46 +1100255 # Enable the panic handler wrapper
256 -Wl,--undefined=esp_panic_handler
257 -Wl,--wrap=esp_panic_handler
258)
Angus Grattonff70a912024-10-22 15:02:32 +1100259
Luca Burelli31ef7c12023-07-05 18:03:11 +0200260# Collect all of the include directories and compile definitions for the IDF components,
261# including those added by the IDF Component Manager via idf_components.yaml.
262foreach(comp ${__COMPONENT_NAMES_RESOLVED})
Damien George0fabda32021-04-08 23:42:22 +1000263 micropy_gather_target_properties(__idf_${comp})
Damien Georgee4650122023-05-09 09:52:54 +1000264 micropy_gather_target_properties(${comp})
Damien George9b908822020-09-23 15:55:55 +1000265endforeach()
266
Damien George9b908822020-09-23 15:55:55 +1000267# Include the main MicroPython cmake rules.
268include(${MICROPY_DIR}/py/mkrules.cmake)
Damien Georgecb31c0a2023-07-15 00:03:59 +1000269
270# Generate source files for named pins (requires mkrules.cmake for MICROPY_GENHDR_DIR).
271
272set(GEN_PINS_PREFIX "${MICROPY_PORT_DIR}/boards/pins_prefix.c")
273set(GEN_PINS_MKPINS "${MICROPY_PORT_DIR}/boards/make-pins.py")
274set(GEN_PINS_SRC "${CMAKE_BINARY_DIR}/pins.c")
275set(GEN_PINS_HDR "${MICROPY_GENHDR_DIR}/pins.h")
276
277if(EXISTS "${MICROPY_BOARD_DIR}/pins.csv")
278 set(GEN_PINS_BOARD_CSV "${MICROPY_BOARD_DIR}/pins.csv")
279 set(GEN_PINS_BOARD_CSV_ARG --board-csv "${GEN_PINS_BOARD_CSV}")
280endif()
281
282target_sources(${MICROPY_TARGET} PRIVATE ${GEN_PINS_HDR})
283
284add_custom_command(
285 OUTPUT ${GEN_PINS_SRC} ${GEN_PINS_HDR}
286 COMMAND ${Python3_EXECUTABLE} ${GEN_PINS_MKPINS} ${GEN_PINS_BOARD_CSV_ARG}
287 --prefix ${GEN_PINS_PREFIX} --output-source ${GEN_PINS_SRC} --output-header ${GEN_PINS_HDR}
288 DEPENDS
289 ${MICROPY_MPVERSION}
290 ${GEN_PINS_MKPINS}
291 ${GEN_PINS_BOARD_CSV}
292 ${GEN_PINS_PREFIX}
293 VERBATIM
294 COMMAND_EXPAND_LISTS
295)