From ad123ed01393f34d31c7c9ac0f535a023f9385b7 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Mon, 31 Jul 2023 17:24:38 +1000 Subject: [PATCH] esp32/Makefile: Implement `make submodules` to match other ports. Signed-off-by: Jim Mussared --- extmod/extmod.cmake | 87 +++++++++++++++++++--------------- ports/esp32/Makefile | 25 ++++++---- ports/esp32/esp32_common.cmake | 3 ++ ports/rp2/Makefile | 7 ++- py/mkrules.cmake | 7 ++- 5 files changed, 79 insertions(+), 50 deletions(-) diff --git a/extmod/extmod.cmake b/extmod/extmod.cmake index 305cdee942..7a7b892c96 100644 --- a/extmod/extmod.cmake +++ b/extmod/extmod.cmake @@ -60,51 +60,62 @@ set(MICROPY_SOURCE_EXTMOD if(MICROPY_PY_BTREE) set(MICROPY_LIB_BERKELEY_DIR "${MICROPY_DIR}/lib/berkeley-db-1.xx") + string(CONCAT GIT_SUBMODULES "${GIT_SUBMODULES} " lib/berkeley-db-1.xx) - add_library(micropy_extmod_btree OBJECT - ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_close.c - ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_conv.c - ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_debug.c - ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_delete.c - ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_get.c - ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_open.c - ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_overflow.c - ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_page.c - ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_put.c - ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_search.c - ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_seq.c - ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_split.c - ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_utils.c - ${MICROPY_LIB_BERKELEY_DIR}/mpool/mpool.c - ) + if(ECHO_SUBMODULES OR ECHO_QUERY_VARIANTS) + # No-op, we're just doing submodule/variant discovery. + # Cannot run the add_library/target_include_directories rules (even though + # the build won't run) because IDF will attempt verify the files exist. + elseif(NOT EXISTS ${MICROPY_LIB_BERKELEY_DIR}/README) + # Regular build, submodule not initialised -- fail with a clear error. + message(FATAL_ERROR " MICROPY_PY_BTREE is enabled but the berkeley-db submodule is not initialised.\n Run 'make BOARD=${MICROPY_BOARD} submodules'") + else() + # Regular build, we have the submodule. + add_library(micropy_extmod_btree OBJECT + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_close.c + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_conv.c + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_debug.c + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_delete.c + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_get.c + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_open.c + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_overflow.c + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_page.c + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_put.c + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_search.c + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_seq.c + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_split.c + ${MICROPY_LIB_BERKELEY_DIR}/btree/bt_utils.c + ${MICROPY_LIB_BERKELEY_DIR}/mpool/mpool.c + ) - target_include_directories(micropy_extmod_btree PRIVATE - ${MICROPY_LIB_BERKELEY_DIR}/PORT/include - ) + target_include_directories(micropy_extmod_btree PRIVATE + ${MICROPY_LIB_BERKELEY_DIR}/PORT/include + ) - target_compile_definitions(micropy_extmod_btree PRIVATE - __DBINTERFACE_PRIVATE=1 - mpool_error=printf - abort=abort_ - "virt_fd_t=void*" - ) + target_compile_definitions(micropy_extmod_btree PRIVATE + __DBINTERFACE_PRIVATE=1 + mpool_error=printf + abort=abort_ + "virt_fd_t=void*" + ) - # The include directories and compile definitions below are needed to build - # modbtree.c and should be added to the main MicroPython target. + # The include directories and compile definitions below are needed to build + # modbtree.c and should be added to the main MicroPython target. - list(APPEND MICROPY_INC_CORE - "${MICROPY_LIB_BERKELEY_DIR}/PORT/include" - ) + list(APPEND MICROPY_INC_CORE + "${MICROPY_LIB_BERKELEY_DIR}/PORT/include" + ) - list(APPEND MICROPY_DEF_CORE - MICROPY_PY_BTREE=1 - __DBINTERFACE_PRIVATE=1 - "virt_fd_t=void*" - ) + list(APPEND MICROPY_DEF_CORE + MICROPY_PY_BTREE=1 + __DBINTERFACE_PRIVATE=1 + "virt_fd_t=void*" + ) - list(APPEND MICROPY_SOURCE_EXTMOD - ${MICROPY_EXTMOD_DIR}/modbtree.c - ) + list(APPEND MICROPY_SOURCE_EXTMOD + ${MICROPY_EXTMOD_DIR}/modbtree.c + ) + endif() endif() # Library for mbedtls diff --git a/ports/esp32/Makefile b/ports/esp32/Makefile index 7c8c225dfb..239618c6f7 100644 --- a/ports/esp32/Makefile +++ b/ports/esp32/Makefile @@ -26,12 +26,6 @@ BAUD ?= 460800 PYTHON ?= python3 -# Would be good to use cmake to discover submodules (see how rp2/Makefile does -# it), but on ESP32 the same trick doesn't work because "idf.py build" fails -# on berkeley-db dependency before printing out the submodule list. -# For now just force the submodule dependencies here. -GIT_SUBMODULES += lib/berkeley-db-1.xx lib/micropython-lib - .PHONY: all clean deploy erase submodules FORCE CMAKE_ARGS = @@ -40,20 +34,24 @@ ifdef USER_C_MODULES CMAKE_ARGS += -DUSER_C_MODULES=${USER_C_MODULES} endif -IDFPY_FLAGS += -D MICROPY_BOARD=$(BOARD) -D MICROPY_BOARD_DIR=$(abspath $(BOARD_DIR)) -B $(BUILD) $(CMAKE_ARGS) +IDFPY_FLAGS += -D MICROPY_BOARD=$(BOARD) -D MICROPY_BOARD_DIR=$(abspath $(BOARD_DIR)) $(CMAKE_ARGS) ifdef FROZEN_MANIFEST IDFPY_FLAGS += -D MICROPY_FROZEN_MANIFEST=$(FROZEN_MANIFEST) endif +ifdef BOARD_VARIANT + IDFPY_FLAGS += -D MICROPY_BOARD_VARIANT=$(BOARD_VARIANT) +endif + HELP_BUILD_ERROR ?= "See \033[1;31mhttps://github.com/micropython/micropython/wiki/Build-Troubleshooting\033[0m" define RUN_IDF_PY - idf.py $(IDFPY_FLAGS) -p $(PORT) -b $(BAUD) $(1) + idf.py $(IDFPY_FLAGS) -B $(BUILD) -p $(PORT) -b $(BAUD) $(1) endef all: - idf.py $(IDFPY_FLAGS) build || (echo -e $(HELP_BUILD_ERROR); false) + idf.py $(IDFPY_FLAGS) -B $(BUILD) build || (echo -e $(HELP_BUILD_ERROR); false) @$(PYTHON) makeimg.py \ $(BUILD)/sdkconfig \ $(BUILD)/bootloader/bootloader.bin \ @@ -85,5 +83,12 @@ size-components: size-files: $(call RUN_IDF_PY,size-files) +# Running the build with ECHO_SUBMODULES set will trigger py/mkrules.cmake to +# print out the value of the GIT_SUBMODULES variable, prefixed with +# "GIT_SUBMODULES", and then abort. This extracts out that line from the idf.py +# output and passes the list of submodules to py/mkrules.mk which does the +# `git submodule init` on each. submodules: - $(MAKE) -f ../../py/mkrules.mk GIT_SUBMODULES="$(GIT_SUBMODULES)" submodules + @GIT_SUBMODULES=$$(idf.py $(IDFPY_FLAGS) -B $(BUILD)/submodules -D ECHO_SUBMODULES=1 build 2>&1 | \ + grep '^GIT_SUBMODULES=' | cut -d= -f2); \ + $(MAKE) -f ../../py/mkrules.mk GIT_SUBMODULES="$${GIT_SUBMODULES}" submodules diff --git a/ports/esp32/esp32_common.cmake b/ports/esp32/esp32_common.cmake index 37a19316b6..098df1f1ae 100644 --- a/ports/esp32/esp32_common.cmake +++ b/ports/esp32/esp32_common.cmake @@ -11,6 +11,9 @@ endif() # Include core source components. include(${MICROPY_DIR}/py/py.cmake) +# CMAKE_BUILD_EARLY_EXPANSION is set during the component-discovery phase of +# `idf.py build`, so none of the extmod/usermod (and in reality, most of the +# micropython) rules need to happen. Specifically, you cannot invoke add_library. if(NOT CMAKE_BUILD_EARLY_EXPANSION) # Enable extmod components that will be configured by extmod.cmake. # A board may also have enabled additional components. diff --git a/ports/rp2/Makefile b/ports/rp2/Makefile index 4f5b4728ec..7c14415ad5 100644 --- a/ports/rp2/Makefile +++ b/ports/rp2/Makefile @@ -50,9 +50,14 @@ clean: # First ensure that pico-sdk is initialised, then use cmake to pick everything # else (including board-specific dependencies). +# Running the build with ECHO_SUBMODULES set will trigger py/mkrules.cmake to +# print out the value of the GIT_SUBMODULES variable, prefixed with +# "GIT_SUBMODULES", and then abort. This extracts out that line from the cmake +# output and passes the list of submodules to py/mkrules.mk which does the +# `git submodule init` on each. submodules: $(MAKE) -f ../../py/mkrules.mk GIT_SUBMODULES="lib/pico-sdk" submodules - GIT_SUBMODULES=$$(cmake -B $(BUILD)/submodules -DECHO_SUBMODULES=1 ${CMAKE_ARGS} -S . 2>&1 | \ + @GIT_SUBMODULES=$$(cmake -B $(BUILD)/submodules -DECHO_SUBMODULES=1 ${CMAKE_ARGS} -S . 2>&1 | \ grep '^GIT_SUBMODULES=' | cut -d= -f2); \ $(MAKE) -f ../../py/mkrules.mk GIT_SUBMODULES="$${GIT_SUBMODULES}" submodules diff --git a/py/mkrules.cmake b/py/mkrules.cmake index 7eb5fcf018..21d5c27334 100644 --- a/py/mkrules.cmake +++ b/py/mkrules.cmake @@ -178,7 +178,12 @@ if(MICROPY_FROZEN_MANIFEST) set(MICROPY_LIB_DIR ${MICROPY_DIR}/lib/micropython-lib) endif() - if(NOT (${ECHO_SUBMODULES}) AND NOT EXISTS ${MICROPY_LIB_DIR}/README.md) + if(ECHO_SUBMODULES OR ECHO_QUERY_VARIANTS) + # No-op, we're just doing submodule/variant discovery. + # Note: All the following rules are safe to run in discovery mode even + # though the submodule might not be available as they do not directly depend + # on anything from the submodule. + elseif(NOT EXISTS ${MICROPY_LIB_DIR}/README.md) message(FATAL_ERROR " micropython-lib not initialized.\n Run 'make BOARD=${MICROPY_BOARD} submodules'") endif()