mkrules.mk 9.1 KB

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