esp32/Makefile: Implement `make submodules` to match other ports.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
diff --git a/ports/esp32/Makefile b/ports/esp32/Makefile
index 7c8c225..239618c 100644
--- a/ports/esp32/Makefile
+++ b/ports/esp32/Makefile
@@ -26,12 +26,6 @@
 
 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 @@
 	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-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 37a1931..098df1f 100644
--- a/ports/esp32/esp32_common.cmake
+++ b/ports/esp32/esp32_common.cmake
@@ -11,6 +11,9 @@
 # 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.