From 78718fffb1f3010c7a40bb4c29c6ddf5b8dadaa3 Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 3 Aug 2021 22:30:15 +1000 Subject: [PATCH] py/mkrules: Automatically build mpy-cross if it doesn't exist. Commit 41739506589ec8397613c86d8f682fb7f86c0a9f removed automatic building of mpy-cross, which rebuilt it whenever any of its dependent source files changed. But needing to build mpy-cross, and not knowing how, is a frequent issue. This commit aims to help by automatically building mpy-cross only if it doesn't exist. For Makefiles it uses an order-only prerequisite, while for CMake it uses a custom command. If MICROPY_MPYCROSS (which is what makemanifest.py uses to locate the mpy-cross executable) is defined in the environment then automatic build will not be attempted, allowing a way to prevent this auto-build if needed. Thanks to Trammell Hudson aka @osresearch for the original idea; see #5760. Signed-off-by: Damien George --- py/mkenv.mk | 6 +++++- py/mkrules.cmake | 16 ++++++++++++++++ py/mkrules.mk | 12 +++++++++--- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/py/mkenv.mk b/py/mkenv.mk index d54f0a0d31..2b247974b6 100644 --- a/py/mkenv.mk +++ b/py/mkenv.mk @@ -55,11 +55,15 @@ AR = $(CROSS_COMPILE)ar MAKE_MANIFEST = $(PYTHON) $(TOP)/tools/makemanifest.py MAKE_FROZEN = $(PYTHON) $(TOP)/tools/make-frozen.py -MPY_CROSS = $(TOP)/mpy-cross/mpy-cross MPY_TOOL = $(PYTHON) $(TOP)/tools/mpy-tool.py MPY_LIB_DIR = $(TOP)/../micropython-lib +ifeq ($(MICROPY_MPYCROSS),) +MICROPY_MPYCROSS = $(TOP)/mpy-cross/mpy-cross +MICROPY_MPYCROSS_DEPENDENCY = $(MICROPY_MPYCROSS) +endif + all: .PHONY: all diff --git a/py/mkrules.cmake b/py/mkrules.cmake index 7589255b20..9d08017931 100644 --- a/py/mkrules.cmake +++ b/py/mkrules.cmake @@ -129,11 +129,27 @@ if(MICROPY_FROZEN_MANIFEST) set(MICROPY_LIB_DIR ${MICROPY_DIR}/../micropython-lib) endif() + # If MICROPY_MPYCROSS is not explicitly defined in the environment (which + # is what makemanifest.py will use) then create an mpy-cross dependency + # to automatically build mpy-cross if needed. + set(MICROPY_MPYCROSS $ENV{MICROPY_MPYCROSS}) + if(NOT MICROPY_MPYCROSS) + set(MICROPY_MPYCROSS_DEPENDENCY ${MICROPY_DIR}/mpy-cross/mpy-cross) + if(NOT MICROPY_MAKE_EXECUTABLE) + set(MICROPY_MAKE_EXECUTABLE make) + endif() + add_custom_command( + OUTPUT ${MICROPY_MPYCROSS_DEPENDENCY} + COMMAND ${MICROPY_MAKE_EXECUTABLE} -C ${MICROPY_DIR}/mpy-cross + ) + endif() + add_custom_command( OUTPUT ${MICROPY_FROZEN_CONTENT} COMMAND ${Python3_EXECUTABLE} ${MICROPY_DIR}/tools/makemanifest.py -o ${MICROPY_FROZEN_CONTENT} -v "MPY_DIR=${MICROPY_DIR}" -v "MPY_LIB_DIR=${MICROPY_LIB_DIR}" -v "PORT_DIR=${MICROPY_PORT_DIR}" -v "BOARD_DIR=${MICROPY_BOARD_DIR}" -b "${CMAKE_BINARY_DIR}" -f${MICROPY_CROSS_FLAGS} ${MICROPY_FROZEN_MANIFEST} DEPENDS MICROPY_FORCE_BUILD ${MICROPY_QSTRDEFS_GENERATED} + ${MICROPY_MPYCROSS_DEPENDENCY} VERBATIM ) endif() diff --git a/py/mkrules.mk b/py/mkrules.mk index 63f704967f..730ad45890 100644 --- a/py/mkrules.mk +++ b/py/mkrules.mk @@ -136,9 +136,15 @@ $(OBJ_DIRS): $(HEADER_BUILD): $(MKDIR) -p $@ +ifneq ($(MICROPY_MPYCROSS_DEPENDENCY),) +# to automatically build mpy-cross, if needed +$(MICROPY_MPYCROSS_DEPENDENCY): + $(MAKE) -C $(dir $@) +endif + ifneq ($(FROZEN_MANIFEST),) # to build frozen_content.c from a manifest -$(BUILD)/frozen_content.c: FORCE $(BUILD)/genhdr/qstrdefs.generated.h +$(BUILD)/frozen_content.c: FORCE $(BUILD)/genhdr/qstrdefs.generated.h | $(MICROPY_MPYCROSS_DEPENDENCY) $(Q)$(MAKE_MANIFEST) -o $@ -v "MPY_DIR=$(TOP)" -v "MPY_LIB_DIR=$(MPY_LIB_DIR)" -v "PORT_DIR=$(shell pwd)" -v "BOARD_DIR=$(BOARD_DIR)" -b "$(BUILD)" $(if $(MPY_CROSS_FLAGS),-f"$(MPY_CROSS_FLAGS)",) --mpy-tool-flags="$(MPY_TOOL_FLAGS)" $(FROZEN_MANIFEST) ifneq ($(FROZEN_DIR),) @@ -164,10 +170,10 @@ FROZEN_MPY_PY_FILES := $(shell find -L $(FROZEN_MPY_DIR) -type f -name '*.py' | FROZEN_MPY_MPY_FILES := $(addprefix $(BUILD)/frozen_mpy/,$(FROZEN_MPY_PY_FILES:.py=.mpy)) # to build .mpy files from .py files -$(BUILD)/frozen_mpy/%.mpy: $(FROZEN_MPY_DIR)/%.py +$(BUILD)/frozen_mpy/%.mpy: $(FROZEN_MPY_DIR)/%.py | $(MICROPY_MPYCROSS_DEPENDENCY) @$(ECHO) "MPY $<" $(Q)$(MKDIR) -p $(dir $@) - $(Q)$(MPY_CROSS) -o $@ -s $(<:$(FROZEN_MPY_DIR)/%=%) $(MPY_CROSS_FLAGS) $< + $(Q)$(MICROPY_MPYCROSS) -o $@ -s $(<:$(FROZEN_MPY_DIR)/%=%) $(MPY_CROSS_FLAGS) $< # to build frozen_mpy.c from all .mpy files $(BUILD)/frozen_mpy.c: $(FROZEN_MPY_MPY_FILES) $(BUILD)/genhdr/qstrdefs.generated.h