mkrules.mk 7.6 KB

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