mkrules.mk 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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)
  22. $(BUILD)/%.o: %.S
  23. $(ECHO) "CC $<"
  24. $(Q)$(CC) $(CFLAGS) -c -o $@ $<
  25. vpath %.s . $(TOP)
  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)
  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)
  46. $(BUILD)/%.pp: %.c
  47. $(ECHO) "PreProcess $<"
  48. $(Q)$(CC) $(CFLAGS) -E -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. $(HEADER_BUILD)/qstr.i.last: $(SRC_QSTR) $(QSTR_GLOBAL_DEPENDENCIES) | $(HEADER_BUILD)/mpversion.h
  64. $(ECHO) "GEN $@"
  65. $(Q)$(CPP) $(QSTR_GEN_EXTRA_CFLAGS) $(CFLAGS) $(if $(filter $?,$(QSTR_GLOBAL_DEPENDENCIES)),$^,$(if $?,$?,$^)) >$(HEADER_BUILD)/qstr.i.last;
  66. $(HEADER_BUILD)/qstr.split: $(HEADER_BUILD)/qstr.i.last
  67. $(ECHO) "GEN $@"
  68. $(Q)$(PYTHON) $(PY_SRC)/makeqstrdefs.py split $(HEADER_BUILD)/qstr.i.last $(HEADER_BUILD)/qstr $(QSTR_DEFS_COLLECTED)
  69. $(Q)touch $@
  70. $(QSTR_DEFS_COLLECTED): $(HEADER_BUILD)/qstr.split
  71. $(ECHO) "GEN $@"
  72. $(Q)$(PYTHON) $(PY_SRC)/makeqstrdefs.py cat $(HEADER_BUILD)/qstr.i.last $(HEADER_BUILD)/qstr $(QSTR_DEFS_COLLECTED)
  73. # $(sort $(var)) removes duplicates
  74. #
  75. # The net effect of this, is it causes the objects to depend on the
  76. # object directories (but only for existence), and the object directories
  77. # will be created if they don't exist.
  78. OBJ_DIRS = $(sort $(dir $(OBJ)))
  79. $(OBJ): | $(OBJ_DIRS)
  80. $(OBJ_DIRS):
  81. $(MKDIR) -p $@
  82. $(HEADER_BUILD):
  83. $(MKDIR) -p $@
  84. ifneq ($(FROZEN_DIR),)
  85. $(BUILD)/frozen.c: $(wildcard $(FROZEN_DIR)/*) $(HEADER_BUILD) $(FROZEN_EXTRA_DEPS)
  86. $(ECHO) "GEN $@"
  87. $(Q)$(MAKE_FROZEN) $(FROZEN_DIR) > $@
  88. endif
  89. ifneq ($(FROZEN_MPY_DIR),)
  90. # to build the MicroPython cross compiler
  91. $(TOP)/mpy-cross/mpy-cross: $(TOP)/py/*.[ch] $(TOP)/mpy-cross/*.[ch] $(TOP)/ports/windows/fmode.c
  92. $(Q)$(MAKE) -C $(TOP)/mpy-cross
  93. # make a list of all the .py files that need compiling and freezing
  94. FROZEN_MPY_PY_FILES := $(shell find -L $(FROZEN_MPY_DIR) -type f -name '*.py' | $(SED) -e 's=^$(FROZEN_MPY_DIR)/==')
  95. FROZEN_MPY_MPY_FILES := $(addprefix $(BUILD)/frozen_mpy/,$(FROZEN_MPY_PY_FILES:.py=.mpy))
  96. # to build .mpy files from .py files
  97. $(BUILD)/frozen_mpy/%.mpy: $(FROZEN_MPY_DIR)/%.py $(TOP)/mpy-cross/mpy-cross
  98. @$(ECHO) "MPY $<"
  99. $(Q)$(MKDIR) -p $(dir $@)
  100. $(Q)$(MPY_CROSS) -o $@ -s $(<:$(FROZEN_MPY_DIR)/%=%) $(MPY_CROSS_FLAGS) $<
  101. # to build frozen_mpy.c from all .mpy files
  102. $(BUILD)/frozen_mpy.c: $(FROZEN_MPY_MPY_FILES) $(BUILD)/genhdr/qstrdefs.generated.h
  103. @$(ECHO) "GEN $@"
  104. $(Q)$(MPY_TOOL) -f -q $(BUILD)/genhdr/qstrdefs.preprocessed.h $(FROZEN_MPY_MPY_FILES) > $@
  105. endif
  106. ifneq ($(PROG),)
  107. # Build a standalone executable (unix does this)
  108. all: $(PROG)
  109. $(PROG): $(OBJ)
  110. $(ECHO) "LINK $@"
  111. # Do not pass COPT here - it's *C* compiler optimizations. For example,
  112. # we may want to compile using Thumb, but link with non-Thumb libc.
  113. $(Q)$(CC) -o $@ $^ $(LIB) $(LDFLAGS)
  114. ifndef DEBUG
  115. $(Q)$(STRIP) $(STRIPFLAGS_EXTRA) $(PROG)
  116. endif
  117. $(Q)$(SIZE) $$(find $(BUILD) -path "$(BUILD)/build/frozen*.o") $(PROG)
  118. clean: clean-prog
  119. clean-prog:
  120. $(RM) -f $(PROG)
  121. $(RM) -f $(PROG).map
  122. .PHONY: clean-prog
  123. endif
  124. LIBMICROPYTHON = libmicropython.a
  125. # We can execute extra commands after library creation using
  126. # LIBMICROPYTHON_EXTRA_CMD. This may be needed e.g. to integrate
  127. # with 3rd-party projects which don't have proper dependency
  128. # tracking. Then LIBMICROPYTHON_EXTRA_CMD can e.g. touch some
  129. # other file to cause needed effect, e.g. relinking with new lib.
  130. lib $(LIBMICROPYTHON): $(OBJ)
  131. $(AR) rcs $(LIBMICROPYTHON) $^
  132. $(LIBMICROPYTHON_EXTRA_CMD)
  133. clean:
  134. $(RM) -rf $(BUILD) $(CLEAN_EXTRA)
  135. .PHONY: clean
  136. # Clean every non-git file from FROZEN_DIR/FROZEN_MPY_DIR, but making a backup.
  137. # We run rmdir below to avoid empty backup dir (it will silently fail if backup
  138. # is non-empty).
  139. clean-frozen:
  140. if [ -n "$(FROZEN_MPY_DIR)" ]; then \
  141. backup_dir=$(FROZEN_MPY_DIR).$$(date +%Y%m%dT%H%M%S); mkdir $$backup_dir; \
  142. cd $(FROZEN_MPY_DIR); git status --ignored -u all -s . | awk ' {print $$2}' \
  143. | xargs --no-run-if-empty cp --parents -t ../$$backup_dir; \
  144. rmdir ../$$backup_dir 2>/dev/null || true; \
  145. git clean -d -f .; \
  146. fi
  147. if [ -n "$(FROZEN_DIR)" ]; then \
  148. backup_dir=$(FROZEN_DIR).$$(date +%Y%m%dT%H%M%S); mkdir $$backup_dir; \
  149. cd $(FROZEN_DIR); git status --ignored -u all -s . | awk ' {print $$2}' \
  150. | xargs --no-run-if-empty cp --parents -t ../$$backup_dir; \
  151. rmdir ../$$backup_dir 2>/dev/null || true; \
  152. git clean -d -f .; \
  153. fi
  154. .PHONY: clean-frozen
  155. print-cfg:
  156. $(ECHO) "PY_SRC = $(PY_SRC)"
  157. $(ECHO) "BUILD = $(BUILD)"
  158. $(ECHO) "OBJ = $(OBJ)"
  159. .PHONY: print-cfg
  160. print-def:
  161. @$(ECHO) "The following defines are built into the $(CC) compiler"
  162. touch __empty__.c
  163. @$(CC) -E -Wp,-dM __empty__.c
  164. @$(RM) -f __empty__.c
  165. -include $(OBJ:.o=.P)