8000 py/mkrules: Automatically build mpy-cross if it doesn't exist. · micropython/micropython@1732a9f · GitHub
[go: up one dir, main page]

Skip to content

Commit 1732a9f

Browse files
10000
committed
py/mkrules: Automatically build mpy-cross if it doesn't exist.
Commit 4173950 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 <damien@micropython.org>
1 parent 23e2e00 commit 1732a9f

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

py/mkenv.mk

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ AR = $(CROSS_COMPILE)ar
5555

5656
MAKE_MANIFEST = $(PYTHON) $(TOP)/tools/makemanifest.py
5757
MAKE_FROZEN = $(PYTHON) $(TOP)/tools/make-frozen.py
58-
MPY_CROSS = $(TOP)/mpy-cross/mpy-cross
5958
MPY_TOOL = $(PYTHON) $(TOP)/tools/mpy-tool.py
6059

6160
MPY_LIB_DIR = $(TOP)/../micropython-lib
6261

62+
ifeq ($(MICROPY_MPYCROSS),)
63+
MICROPY_MPYCROSS = $(TOP)/mpy-cross/mpy-cross
64+
MICROPY_MPYCROSS_DEPENDENCY = $(MICROPY_MPYCROSS)
65+
endif
66+
6367
all:
6468
.PHONY: all
6569

py/mkrules.cmake

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,27 @@ if(MICROPY_FROZEN_MANIFEST)
129129
set(MICROPY_LIB_DIR ${MICROPY_DIR}/../micropython-lib)
130130
endif()
131131

132+
# If MICROPY_MPYCROSS is not explicitly defined in the environment (which
133+
# is what makemanifest.py will use) then create an mpy-cross dependency
134+
# to automatically build mpy-cross if needed.
135+
set(MICROPY_MPYCROSS $ENV{MICROPY_MPYCROSS})
136+
if(NOT MICROPY_MPYCROSS)
137+
set(MICROPY_MPYCROSS_DEPENDENCY ${MICROPY_DIR}/mpy-cross/mpy-cross)
138+
if(NOT MICROPY_MAKE_EXECUTABLE)
139+
set(MICROPY_MAKE_EXECUTABLE make)
140+
endif()
141+
add_custom_command(
142+
OUTPUT ${MICROPY_MPYCROSS_DEPENDENCY}
143+
COMMAND ${MICROPY_MAKE_EXECUTABLE} -C ${MICROPY_DIR}/mpy-cross
144+
)
145+
endif()
146+
132147
add_custom_command(
133148
OUTPUT ${MICROPY_FROZEN_CONTENT}
134149
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}
135150
DEPENDS MICROPY_FORCE_BUILD
136151
${MICROPY_QSTRDEFS_GENERATED}
152+
${MICROPY_MPYCROSS_DEPENDENCY}
137153
VERBATIM
138154
)
139155
endif()

py/mkrules.mk

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,15 @@ $(OBJ_DIRS):
136136
$(HEADER_BUILD):
137137
$(MKDIR) -p $@
138138

139+
ifneq ($(MICROPY_MPYCROSS_DEPENDENCY),)
140+
# to automatically build mpy-cross, if needed
141+
$(MICROPY_MPYCROSS_DEPENDENCY):
142+
$(MAKE) -C $(dir $@)
143+
endif
144+
139145
ifneq ($(FROZEN_MANIFEST),)
140146
# to build frozen_content.c from a manifest
141-
$(BUILD)/frozen_content.c: FORCE $(BUILD)/genhdr/qstrdefs.generated.h
147+
$(BUILD)/frozen_content.c: FORCE $(BUILD)/genhdr/qstrdefs.generated.h | $(MICROPY_MPYCROSS_DEPENDENCY)
142148
$(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)
143149

144150
ifneq ($(FROZEN_DIR),)
@@ -164,10 +170,10 @@ FROZEN_MPY_PY_FILES := $(shell find -L $(FROZEN_MPY_DIR) -type f -name '*.py' |
164170
FROZEN_MPY_MPY_FILES := $(addprefix $(BUILD)/frozen_mpy/,$(FROZEN_MPY_PY_FILES:.py=.mpy))
165171

166172
# to build .mpy files from .py files
167-
$(BUILD)/frozen_mpy/%.mpy: $(FROZEN_MPY_DIR)/%.py
173+
$(BUILD)/frozen_mpy/%.mpy: $(FROZEN_MPY_DIR)/%.py | $(MICROPY_MPYCROSS_DEPENDENCY)
168174
@$(ECHO) "MPY $<"
169175
$(Q)$(MKDIR) -p $(dir $@)
170-
$(Q)$(MPY_CROSS) -o $@ -s $(<:$(FROZEN_MPY_DIR)/%=%) $(MPY_CROSS_FLAGS) $<
176+
$(Q)$(MICROPY_MPYCROSS) -o $@ -s $(<:$(FROZEN_MPY_DIR)/%=%) $(MPY_CROSS_FLAGS) $<
171177

172178
# to build frozen_mpy.c from all .mpy files
173179
$(BUILD)/frozen_mpy.c: $(FROZEN_MPY_MPY_FILES) $(BUILD)/genhdr/qstrdefs.generated.h

0 commit comments

Comments
 (0)
0