rules.mk 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # ---------------------------------------
  2. # Common make rules for all examples
  3. # ---------------------------------------
  4. # Set all as default goal
  5. .DEFAULT_GOAL := all
  6. # ---------------------------------------
  7. # Compiler Flags
  8. # ---------------------------------------
  9. LIBS_GCC ?= -lm
  10. # libc
  11. LIBS += $(LIBS_GCC)
  12. ifneq ($(BOARD), spresense)
  13. LIBS += -lc -Wl,-Bstatic -lc++ -Wl,-Bdynamic
  14. endif
  15. # TinyUSB Stack source
  16. SRC_C += \
  17. src/tusb.c \
  18. src/common/tusb_fifo.c \
  19. src/device/usbd.c \
  20. src/device/usbd_control.c \
  21. src/class/audio/audio_device.c \
  22. src/class/cdc/cdc_device.c \
  23. src/class/dfu/dfu_device.c \
  24. src/class/dfu/dfu_rt_device.c \
  25. src/class/hid/hid_device.c \
  26. src/class/midi/midi_device.c \
  27. src/class/msc/msc_device.c \
  28. src/class/net/ecm_rndis_device.c \
  29. src/class/net/ncm_device.c \
  30. src/class/usbtmc/usbtmc_device.c \
  31. src/class/video/video_device.c \
  32. src/class/vendor/vendor_device.c
  33. # Fuzzers are c++
  34. SRC_CXX += \
  35. test/fuzz/dcd_fuzz.cc \
  36. test/fuzz/fuzz.cc \
  37. test/fuzz/msc_fuzz.cc \
  38. test/fuzz/net_fuzz.cc \
  39. test/fuzz/usbd_fuzz.cc
  40. # TinyUSB stack include
  41. INC += $(TOP)/src
  42. CFLAGS += $(addprefix -I,$(INC))
  43. CXXFLAGS += -std=c++17
  44. # LTO makes it difficult to analyze map file for optimizing size purpose
  45. # We will run this option in ci
  46. ifeq ($(NO_LTO),1)
  47. CFLAGS := $(filter-out -flto,$(CFLAGS))
  48. endif
  49. ifneq ($(LD_FILE),)
  50. LDFLAGS_LD_FILE ?= -Wl,-T,$(TOP)/$(LD_FILE)
  51. endif
  52. LDFLAGS += $(CFLAGS) $(LDFLAGS_LD_FILE) -fuse-ld=lld -Wl,-Map=$@.map -Wl,--cref -Wl,-gc-sections
  53. ifneq ($(SKIP_NANOLIB), 1)
  54. endif
  55. ASFLAGS += $(CFLAGS)
  56. # Assembly files can be name with upper case .S, convert it to .s
  57. SRC_S := $(SRC_S:.S=.s)
  58. # Due to GCC LTO bug https://bugs.launchpad.net/gcc-arm-embedded/+bug/1747966
  59. # assembly file should be placed first in linking order
  60. # '_asm' suffix is added to object of assembly file
  61. OBJ += $(addprefix $(BUILD)/obj/, $(SRC_S:.s=_asm.o))
  62. OBJ += $(addprefix $(BUILD)/obj/, $(SRC_C:.c=.o))
  63. OBJ += $(addprefix $(BUILD)/obj/, $(SRC_CXX:.cc=_cxx.o))
  64. # Verbose mode
  65. ifeq ("$(V)","1")
  66. $(info CFLAGS $(CFLAGS) ) $(info )
  67. $(info LDFLAGS $(LDFLAGS)) $(info )
  68. $(info ASFLAGS $(ASFLAGS)) $(info )
  69. endif
  70. # ---------------------------------------
  71. # Rules
  72. # ---------------------------------------
  73. all: $(BUILD)/$(PROJECT)
  74. OBJ_DIRS = $(sort $(dir $(OBJ)))
  75. $(OBJ): | $(OBJ_DIRS)
  76. $(OBJ_DIRS):
  77. ifeq ($(CMDEXE),1)
  78. @$(MKDIR) $(subst /,\,$@)
  79. else
  80. @$(MKDIR) -p $@
  81. endif
  82. $(BUILD)/$(PROJECT): $(OBJ)
  83. @echo LINK $@
  84. @ $(CXX) -o $@ $(LIB_FUZZING_ENGINE) $^ $(LIBS) $(LDFLAGS)
  85. # We set vpath to point to the top of the tree so that the source files
  86. # can be located. By following this scheme, it allows a single build rule
  87. # to be used to compile all .c files.
  88. vpath %.c . $(TOP)
  89. $(BUILD)/obj/%.o: %.c
  90. @echo CC $(notdir $@)
  91. @$(CC) $(CFLAGS) -c -MD -o $@ $<
  92. # All cpp srcs
  93. vpath %.cc . $(TOP)
  94. $(BUILD)/obj/%_cxx.o: %.cc
  95. @echo CXX $(notdir $@)
  96. @$(CXX) $(CFLAGS) $(CXXFLAGS) -c -MD -o $@ $<
  97. # ASM sources lower case .s
  98. vpath %.s . $(TOP)
  99. $(BUILD)/obj/%_asm.o: %.s
  100. @echo AS $(notdir $@)
  101. @$(CC) -x assembler-with-cpp $(ASFLAGS) -c -o $@ $<
  102. # ASM sources upper case .S
  103. vpath %.S . $(TOP)
  104. $(BUILD)/obj/%_asm.o: %.S
  105. @echo AS $(notdir $@)
  106. @$(CC) -x assembler-with-cpp $(ASFLAGS) -c -o $@ $<
  107. .PHONY: clean
  108. clean:
  109. ifeq ($(CMDEXE),1)
  110. rd /S /Q $(subst /,\,$(BUILD))
  111. else
  112. $(RM) -rf $(BUILD)
  113. endif
  114. # ---------------- GNU Make End -----------------------
  115. # get depenecies
  116. .PHONY: get-deps
  117. get-deps:
  118. ifdef DEPS_SUBMODULES
  119. git -C $(TOP) submodule update --init $(DEPS_SUBMODULES)
  120. endif
  121. size: $(BUILD)/$(PROJECT)
  122. -@echo ''
  123. @$(SIZE) $<
  124. -@echo ''
  125. # linkermap must be install previously at https://github.com/hathach/linkermap
  126. linkermap: $(BUILD)/$(PROJECT)
  127. @linkermap -v $<.map
  128. # Print out the value of a make variable.
  129. # https://stackoverflow.com/questions/16467718/how-to-print-out-a-variable-in-makefile
  130. print-%:
  131. @echo $* = $($*)