mkrules.mk 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. # List all native flags since the current build system doesn't have
  44. # the MicroPython configuration available. However, these flags are
  45. # needed to extract all qstrings
  46. QSTR_GEN_EXTRA_CFLAGS += -DNO_QSTR -DN_X64 -DN_X86 -DN_THUMB -DN_ARM -DN_XTENSA
  47. QSTR_GEN_EXTRA_CFLAGS += -I$(BUILD)/tmp
  48. vpath %.c . $(TOP)
  49. $(BUILD)/%.pp: %.c
  50. $(ECHO) "PreProcess $<"
  51. $(Q)$(CC) $(CFLAGS) -E -Wp,-C,-dD,-dI -o $@ $<
  52. # The following rule uses | to create an order only prerequisite. Order only
  53. # prerequisites only get built if they don't exist. They don't cause timestamp
  54. # checking to be performed.
  55. #
  56. # We don't know which source files actually need the generated.h (since
  57. # it is #included from str.h). The compiler generated dependencies will cause
  58. # the right .o's to get recompiled if the generated.h file changes. Adding
  59. # an order-only dependency to all of the .o's will cause the generated .h
  60. # to get built before we try to compile any of them.
  61. $(OBJ): | $(HEADER_BUILD)/qstrdefs.generated.h $(HEADER_BUILD)/mpversion.h
  62. $(HEADER_BUILD)/qstr.i.last: $(SRC_QSTR) | $(HEADER_BUILD)/mpversion.h
  63. $(ECHO) "GEN $@"
  64. $(Q)$(CPP) $(QSTR_GEN_EXTRA_CFLAGS) $(CFLAGS) $(if $?,$?,$^) >$(HEADER_BUILD)/qstr.i.last;
  65. $(HEADER_BUILD)/qstr.split: $(HEADER_BUILD)/qstr.i.last
  66. $(ECHO) "GEN $@"
  67. $(Q)$(PYTHON) $(PY_SRC)/makeqstrdefs.py split $(HEADER_BUILD)/qstr.i.last $(HEADER_BUILD)/qstr $(QSTR_DEFS_COLLECTED)
  68. $(Q)touch $@
  69. $(QSTR_DEFS_COLLECTED): $(HEADER_BUILD)/qstr.split
  70. $(ECHO) "GEN $@"
  71. $(Q)$(PYTHON) $(PY_SRC)/makeqstrdefs.py cat $(HEADER_BUILD)/qstr.i.last $(HEADER_BUILD)/qstr $(QSTR_DEFS_COLLECTED)
  72. # $(sort $(var)) removes duplicates
  73. #
  74. # The net effect of this, is it causes the objects to depend on the
  75. # object directories (but only for existence), and the object directories
  76. # will be created if they don't exist.
  77. OBJ_DIRS = $(sort $(dir $(OBJ)))
  78. $(OBJ): | $(OBJ_DIRS)
  79. $(OBJ_DIRS):
  80. $(MKDIR) -p $@
  81. $(HEADER_BUILD):
  82. $(MKDIR) -p $@
  83. ifneq ($(FROZEN_DIR),)
  84. $(BUILD)/frozen.c: $(wildcard $(FROZEN_DIR)/*) $(HEADER_BUILD) $(FROZEN_EXTRA_DEPS)
  85. $(ECHO) "Generating $@"
  86. $(Q)$(MAKE_FROZEN) $(FROZEN_DIR) > $@
  87. endif
  88. ifneq ($(FROZEN_MPY_DIR),)
  89. # to build the MicroPython cross compiler
  90. $(TOP)/mpy-cross/mpy-cross: $(TOP)/py/*.[ch] $(TOP)/mpy-cross/*.[ch] $(TOP)/ports/windows/fmode.c
  91. $(Q)$(MAKE) -C $(TOP)/mpy-cross
  92. # make a list of all the .py files that need compiling and freezing
  93. FROZEN_MPY_PY_FILES := $(shell find -L $(FROZEN_MPY_DIR) -type f -name '*.py' | $(SED) -e 's=^$(FROZEN_MPY_DIR)/==')
  94. FROZEN_MPY_MPY_FILES := $(addprefix $(BUILD)/frozen_mpy/,$(FROZEN_MPY_PY_FILES:.py=.mpy))
  95. # to build .mpy files from .py files
  96. $(BUILD)/frozen_mpy/%.mpy: $(FROZEN_MPY_DIR)/%.py $(TOP)/mpy-cross/mpy-cross
  97. @$(ECHO) "MPY $<"
  98. $(Q)$(MKDIR) -p $(dir $@)
  99. $(Q)$(MPY_CROSS) -o $@ -s $(<:$(FROZEN_MPY_DIR)/%=%) $(MPY_CROSS_FLAGS) $<
  100. # to build frozen_mpy.c from all .mpy files
  101. $(BUILD)/frozen_mpy.c: $(FROZEN_MPY_MPY_FILES) $(BUILD)/genhdr/qstrdefs.generated.h
  102. @$(ECHO) "Creating $@"
  103. $(Q)$(PYTHON) $(MPY_TOOL) -f -q $(BUILD)/genhdr/qstrdefs.preprocessed.h $(FROZEN_MPY_MPY_FILES) > $@
  104. endif
  105. ifneq ($(PROG),)
  106. # Build a standalone executable (unix does this)
  107. all: $(PROG)
  108. $(PROG): $(OBJ)
  109. $(ECHO) "LINK $@"
  110. # Do not pass COPT here - it's *C* compiler optimizations. For example,
  111. # we may want to compile using Thumb, but link with non-Thumb libc.
  112. $(Q)$(CC) -o $@ $^ $(LIB) $(LDFLAGS)
  113. ifndef DEBUG
  114. $(Q)$(STRIP) $(STRIPFLAGS_EXTRA) $(PROG)
  115. endif
  116. $(Q)$(SIZE) $$(find $(BUILD) -path "$(BUILD)/build/frozen*.o") $(PROG)
  117. clean: clean-prog
  118. clean-prog:
  119. $(RM) -f $(PROG)
  120. $(RM) -f $(PROG).map
  121. .PHONY: clean-prog
  122. endif
  123. LIBMICROPYTHON = libmicropython.a
  124. # We can execute extra commands after library creation using
  125. # LIBMICROPYTHON_EXTRA_CMD. This may be needed e.g. to integrate
  126. # with 3rd-party projects which don't have proper dependency
  127. # tracking. Then LIBMICROPYTHON_EXTRA_CMD can e.g. touch some
  128. # other file to cause needed effect, e.g. relinking with new lib.
  129. lib $(LIBMICROPYTHON): $(OBJ)
  130. $(AR) rcs $(LIBMICROPYTHON) $^
  131. $(LIBMICROPYTHON_EXTRA_CMD)
  132. clean:
  133. $(RM) -rf $(BUILD) $(CLEAN_EXTRA)
  134. .PHONY: clean
  135. print-cfg:
  136. $(ECHO) "PY_SRC = $(PY_SRC)"
  137. $(ECHO) "BUILD = $(BUILD)"
  138. $(ECHO) "OBJ = $(OBJ)"
  139. .PHONY: print-cfg
  140. print-def:
  141. @$(ECHO) "The following defines are built into the $(CC) compiler"
  142. touch __empty__.c
  143. @$(CC) -E -Wp,-dM __empty__.c
  144. @$(RM) -f __empty__.c
  145. -include $(OBJ:.o=.P)