1ifneq ($(MKENV_INCLUDED),1) 2# We assume that mkenv is in the same directory as this file. 3THIS_MAKEFILE = $(lastword $(MAKEFILE_LIST)) 4include $(dir $(THIS_MAKEFILE))mkenv.mk 5endif 6 7# Extra deps that need to happen before object compilation. 8OBJ_EXTRA_ORDER_DEPS = 9 10ifeq ($(MICROPY_ROM_TEXT_COMPRESSION),1) 11# If compression is enabled, trigger the build of compressed.data.h... 12OBJ_EXTRA_ORDER_DEPS += $(HEADER_BUILD)/compressed.data.h 13# ...and enable the MP_COMPRESSED_ROM_TEXT macro (used by MP_ERROR_TEXT). 14CFLAGS += -DMICROPY_ROM_TEXT_COMPRESSION=1 15endif 16 17# QSTR generation uses the same CFLAGS, with these modifications. 18QSTR_GEN_FLAGS = -DNO_QSTR 19# Note: := to force evalulation immediately. 20QSTR_GEN_CFLAGS := $(CFLAGS) 21QSTR_GEN_CFLAGS += $(QSTR_GEN_FLAGS) 22QSTR_GEN_CXXFLAGS := $(CXXFLAGS) 23QSTR_GEN_CXXFLAGS += $(QSTR_GEN_FLAGS) 24 25# This file expects that OBJ contains a list of all of the object files. 26# The directory portion of each object file is used to locate the source 27# and should not contain any ..'s but rather be relative to the top of the 28# tree. 29# 30# So for example, py/map.c would have an object file name py/map.o 31# The object files will go into the build directory and mantain the same 32# directory structure as the source tree. So the final dependency will look 33# like this: 34# 35# build/py/map.o: py/map.c 36# 37# We set vpath to point to the top of the tree so that the source files 38# can be located. By following this scheme, it allows a single build rule 39# to be used to compile all .c files. 40 41vpath %.S . $(TOP) $(USER_C_MODULES) 42$(BUILD)/%.o: %.S 43 $(ECHO) "CC $<" 44 $(Q)$(CC) $(CFLAGS) -c -o $@ $< 45 46vpath %.s . $(TOP) $(USER_C_MODULES) 47$(BUILD)/%.o: %.s 48 $(ECHO) "AS $<" 49 $(Q)$(AS) -o $@ $< 50 51define compile_c 52$(ECHO) "CC $<" 53$(Q)$(CC) $(CFLAGS) -c -MD -o $@ $< 54@# The following fixes the dependency file. 55@# See http://make.paulandlesley.org/autodep.html for details. 56@# Regex adjusted from the above to play better with Windows paths, etc. 57@$(CP) $(@:.o=.d) $(@:.o=.P); \ 58 $(SED) -e 's/#.*//' -e 's/^.*: *//' -e 's/ *\\$$//' \ 59 -e '/^$$/ d' -e 's/$$/ :/' < $(@:.o=.d) >> $(@:.o=.P); \ 60 $(RM) -f $(@:.o=.d) 61endef 62 63define compile_cxx 64$(ECHO) "CXX $<" 65$(Q)$(CXX) $(CXXFLAGS) -c -MD -o $@ $< 66@# The following fixes the dependency file. 67@# See http://make.paulandlesley.org/autodep.html for details. 68@# Regex adjusted from the above to play better with Windows paths, etc. 69@$(CP) $(@:.o=.d) $(@:.o=.P); \ 70 $(SED) -e 's/#.*//' -e 's/^.*: *//' -e 's/ *\\$$//' \ 71 -e '/^$$/ d' -e 's/$$/ :/' < $(@:.o=.d) >> $(@:.o=.P); \ 72 $(RM) -f $(@:.o=.d) 73endef 74 75vpath %.c . $(TOP) $(USER_C_MODULES) 76$(BUILD)/%.o: %.c 77 $(call compile_c) 78 79vpath %.cpp . $(TOP) $(USER_C_MODULES) 80$(BUILD)/%.o: %.cpp 81 $(call compile_cxx) 82 83$(BUILD)/%.pp: %.c 84 $(ECHO) "PreProcess $<" 85 $(Q)$(CPP) $(CFLAGS) -Wp,-C,-dD,-dI -o $@ $< 86 87# The following rule uses | to create an order only prerequisite. Order only 88# prerequisites only get built if they don't exist. They don't cause timestamp 89# checking to be performed. 90# 91# We don't know which source files actually need the generated.h (since 92# it is #included from str.h). The compiler generated dependencies will cause 93# the right .o's to get recompiled if the generated.h file changes. Adding 94# an order-only dependency to all of the .o's will cause the generated .h 95# to get built before we try to compile any of them. 96$(OBJ): | $(HEADER_BUILD)/qstrdefs.generated.h $(HEADER_BUILD)/mpversion.h $(OBJ_EXTRA_ORDER_DEPS) 97 98# The logic for qstr regeneration (applied by makeqstrdefs.py) is: 99# - if anything in QSTR_GLOBAL_DEPENDENCIES is newer, then process all source files ($^) 100# - else, if list of newer prerequisites ($?) is not empty, then process just these ($?) 101# - else, process all source files ($^) [this covers "make -B" which can set $? to empty] 102# See more information about this process in docs/develop/qstr.rst. 103$(HEADER_BUILD)/qstr.i.last: $(SRC_QSTR) $(QSTR_GLOBAL_DEPENDENCIES) $(HEADER_BUILD)/moduledefs.h | $(QSTR_GLOBAL_REQUIREMENTS) 104 $(ECHO) "GEN $@" 105 $(Q)$(PYTHON) $(PY_SRC)/makeqstrdefs.py pp $(CPP) output $(HEADER_BUILD)/qstr.i.last cflags $(QSTR_GEN_CFLAGS) cxxflags $(QSTR_GEN_CXXFLAGS) sources $^ dependencies $(QSTR_GLOBAL_DEPENDENCIES) changed_sources $? 106 107$(HEADER_BUILD)/qstr.split: $(HEADER_BUILD)/qstr.i.last 108 $(ECHO) "GEN $@" 109 $(Q)$(PYTHON) $(PY_SRC)/makeqstrdefs.py split qstr $< $(HEADER_BUILD)/qstr _ 110 $(Q)$(TOUCH) $@ 111 112$(QSTR_DEFS_COLLECTED): $(HEADER_BUILD)/qstr.split 113 $(ECHO) "GEN $@" 114 $(Q)$(PYTHON) $(PY_SRC)/makeqstrdefs.py cat qstr _ $(HEADER_BUILD)/qstr $@ 115 116# Compressed error strings. 117$(HEADER_BUILD)/compressed.split: $(HEADER_BUILD)/qstr.i.last 118 $(ECHO) "GEN $@" 119 $(Q)$(PYTHON) $(PY_SRC)/makeqstrdefs.py split compress $< $(HEADER_BUILD)/compress _ 120 $(Q)$(TOUCH) $@ 121 122$(HEADER_BUILD)/compressed.collected: $(HEADER_BUILD)/compressed.split 123 $(ECHO) "GEN $@" 124 $(Q)$(PYTHON) $(PY_SRC)/makeqstrdefs.py cat compress _ $(HEADER_BUILD)/compress $@ 125 126# $(sort $(var)) removes duplicates 127# 128# The net effect of this, is it causes the objects to depend on the 129# object directories (but only for existence), and the object directories 130# will be created if they don't exist. 131OBJ_DIRS = $(sort $(dir $(OBJ))) 132$(OBJ): | $(OBJ_DIRS) 133$(OBJ_DIRS): 134 $(MKDIR) -p $@ 135 136$(HEADER_BUILD): 137 $(MKDIR) -p $@ 138 139ifneq ($(MICROPY_MPYCROSS_DEPENDENCY),) 140# to automatically build mpy-cross, if needed 141$(MICROPY_MPYCROSS_DEPENDENCY): 142 $(MAKE) -C $(dir $@) 143endif 144 145ifneq ($(FROZEN_MANIFEST),) 146# to build frozen_content.c from a manifest 147$(BUILD)/frozen_content.c: FORCE $(BUILD)/genhdr/qstrdefs.generated.h | $(MICROPY_MPYCROSS_DEPENDENCY) 148 $(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) 149 150ifneq ($(FROZEN_DIR),) 151$(error FROZEN_DIR cannot be used in conjunction with FROZEN_MANIFEST) 152endif 153 154ifneq ($(FROZEN_MPY_DIR),) 155$(error FROZEN_MPY_DIR cannot be used in conjunction with FROZEN_MANIFEST) 156endif 157endif 158 159ifneq ($(FROZEN_DIR),) 160$(info Warning: FROZEN_DIR is deprecated in favour of FROZEN_MANIFEST) 161$(BUILD)/frozen.c: $(wildcard $(FROZEN_DIR)/*) $(HEADER_BUILD) $(FROZEN_EXTRA_DEPS) 162 $(ECHO) "GEN $@" 163 $(Q)$(MAKE_FROZEN) $(FROZEN_DIR) > $@ 164endif 165 166ifneq ($(FROZEN_MPY_DIR),) 167$(info Warning: FROZEN_MPY_DIR is deprecated in favour of FROZEN_MANIFEST) 168# make a list of all the .py files that need compiling and freezing 169FROZEN_MPY_PY_FILES := $(shell find -L $(FROZEN_MPY_DIR) -type f -name '*.py' | $(SED) -e 's=^$(FROZEN_MPY_DIR)/==') 170FROZEN_MPY_MPY_FILES := $(addprefix $(BUILD)/frozen_mpy/,$(FROZEN_MPY_PY_FILES:.py=.mpy)) 171 172# to build .mpy files from .py files 173$(BUILD)/frozen_mpy/%.mpy: $(FROZEN_MPY_DIR)/%.py | $(MICROPY_MPYCROSS_DEPENDENCY) 174 @$(ECHO) "MPY $<" 175 $(Q)$(MKDIR) -p $(dir $@) 176 $(Q)$(MICROPY_MPYCROSS) -o $@ -s $(<:$(FROZEN_MPY_DIR)/%=%) $(MPY_CROSS_FLAGS) $< 177 178# to build frozen_mpy.c from all .mpy files 179$(BUILD)/frozen_mpy.c: $(FROZEN_MPY_MPY_FILES) $(BUILD)/genhdr/qstrdefs.generated.h 180 @$(ECHO) "GEN $@" 181 $(Q)$(MPY_TOOL) -f -q $(BUILD)/genhdr/qstrdefs.preprocessed.h $(FROZEN_MPY_MPY_FILES) > $@ 182endif 183 184ifneq ($(PROG),) 185# Build a standalone executable (unix does this) 186 187# The executable should have an .exe extension for builds targetting 'pure' 188# Windows, i.e. msvc or mingw builds, but not when using msys or cygwin's gcc. 189COMPILER_TARGET := $(shell $(CC) -dumpmachine) 190ifneq (,$(findstring mingw,$(COMPILER_TARGET))) 191PROG := $(PROG).exe 192endif 193 194all: $(PROG) 195 196$(PROG): $(OBJ) 197 $(ECHO) "LINK $@" 198# Do not pass COPT here - it's *C* compiler optimizations. For example, 199# we may want to compile using Thumb, but link with non-Thumb libc. 200 $(Q)$(CC) -o $@ $^ $(LIB) $(LDFLAGS) 201ifndef DEBUG 202 $(Q)$(STRIP) $(STRIPFLAGS_EXTRA) $@ 203endif 204 $(Q)$(SIZE) $$(find $(BUILD) -path "$(BUILD)/build/frozen*.o") $@ 205 206clean: clean-prog 207clean-prog: 208 $(RM) -f $(PROG) 209 $(RM) -f $(PROG).map 210 211.PHONY: clean-prog 212endif 213 214submodules: 215 $(ECHO) "Updating submodules: $(GIT_SUBMODULES)" 216ifneq ($(GIT_SUBMODULES),) 217 $(Q)git submodule sync $(addprefix $(TOP)/,$(GIT_SUBMODULES)) 218 $(Q)git submodule update --init $(addprefix $(TOP)/,$(GIT_SUBMODULES)) 219endif 220.PHONY: submodules 221 222LIBMICROPYTHON = libmicropython.a 223 224# We can execute extra commands after library creation using 225# LIBMICROPYTHON_EXTRA_CMD. This may be needed e.g. to integrate 226# with 3rd-party projects which don't have proper dependency 227# tracking. Then LIBMICROPYTHON_EXTRA_CMD can e.g. touch some 228# other file to cause needed effect, e.g. relinking with new lib. 229lib $(LIBMICROPYTHON): $(OBJ) 230 $(Q)$(AR) rcs $(LIBMICROPYTHON) $^ 231 $(LIBMICROPYTHON_EXTRA_CMD) 232 233clean: 234 $(RM) -rf $(BUILD) $(CLEAN_EXTRA) 235.PHONY: clean 236 237# Clean every non-git file from FROZEN_DIR/FROZEN_MPY_DIR, but making a backup. 238# We run rmdir below to avoid empty backup dir (it will silently fail if backup 239# is non-empty). 240clean-frozen: 241 if [ -n "$(FROZEN_MPY_DIR)" ]; then \ 242 backup_dir=$(FROZEN_MPY_DIR).$$(date +%Y%m%dT%H%M%S); mkdir $$backup_dir; \ 243 cd $(FROZEN_MPY_DIR); git status --ignored -u all -s . | awk ' {print $$2}' \ 244 | xargs --no-run-if-empty cp --parents -t ../$$backup_dir; \ 245 rmdir ../$$backup_dir 2>/dev/null || true; \ 246 git clean -d -f .; \ 247 fi 248 249 if [ -n "$(FROZEN_DIR)" ]; then \ 250 backup_dir=$(FROZEN_DIR).$$(date +%Y%m%dT%H%M%S); mkdir $$backup_dir; \ 251 cd $(FROZEN_DIR); git status --ignored -u all -s . | awk ' {print $$2}' \ 252 | xargs --no-run-if-empty cp --parents -t ../$$backup_dir; \ 253 rmdir ../$$backup_dir 2>/dev/null || true; \ 254 git clean -d -f .; \ 255 fi 256.PHONY: clean-frozen 257 258print-cfg: 259 $(ECHO) "PY_SRC = $(PY_SRC)" 260 $(ECHO) "BUILD = $(BUILD)" 261 $(ECHO) "OBJ = $(OBJ)" 262.PHONY: print-cfg 263 264print-def: 265 @$(ECHO) "The following defines are built into the $(CC) compiler" 266 $(TOUCH) __empty__.c 267 @$(CC) -E -Wp,-dM __empty__.c 268 @$(RM) -f __empty__.c 269 270-include $(OBJ:.o=.P) 271