Rules.mk 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. TOOL_PREFIX = riscv32-unknown-elf-
  2. #arm-none-eabi-
  3. CC = $(TOOL_PREFIX)gcc
  4. OBJCOPY = $(TOOL_PREFIX)objcopy
  5. LD = $(TOOL_PREFIX)ld
  6. PYTHON = python3
  7. ZIP = zip -r
  8. UNZIP = unzip
  9. ifneq ($(VERBOSE),1)
  10. TOOL_PREFIX := @$(TOOL_PREFIX)
  11. endif
  12. FLAGS = -g -Os -nostdlib -std=gnu99 -iquote $(ROOT)/inc
  13. FLAGS += -Wall -Werror -Wno-format -Wdeclaration-after-statement
  14. FLAGS += -Wstrict-prototypes -Wredundant-decls -Wnested-externs
  15. FLAGS += -fno-common -fno-exceptions -fno-strict-aliasing
  16. FLAGS += -mlittle-endian
  17. #-mthumb -mfloat-abi=soft
  18. FLAGS += -Wno-unused-value -ffunction-sections
  19. ifeq ($(mcu),stm32f1)
  20. FLAGS += -mcpu=cortex-m3 -DSTM32F1=1 -DMCU=1
  21. stm32f1=y
  22. arch=arm
  23. else ifeq ($(mcu),stm32f7)
  24. FLAGS += -mcpu=cortex-m7 -DSTM32F7=7 -DMCU=7
  25. stm32f7=y
  26. arch=arm
  27. else ifeq ($(mcu),at32f4)
  28. FLAGS += -mcpu=cortex-m4 -DAT32F4=4 -DMCU=4
  29. at32f4=y
  30. arch=arm
  31. else ifeq ($(mcu),gd32vf1)
  32. FLAGS += -march=rv32imac -DGD32VF1=2 -DMCU=2
  33. gd32vf1=y
  34. arch=rv32
  35. endif
  36. ifneq ($(debug),y)
  37. FLAGS += -DNDEBUG
  38. endif
  39. ifeq ($(bootloader),y)
  40. FLAGS += -DBOOTLOADER=1
  41. endif
  42. FLAGS += -MMD -MF .$(@F).d
  43. DEPS = .*.d
  44. FLAGS += $(FLAGS-y)
  45. CFLAGS += $(CFLAGS-y) $(FLAGS) -include decls.h
  46. AFLAGS += $(AFLAGS-y) $(FLAGS) -D__ASSEMBLY__
  47. LDFLAGS += $(LDFLAGS-y) $(FLAGS) -Wl,--gc-sections
  48. RULES_MK := y
  49. include Makefile
  50. SUBDIRS += $(SUBDIRS-y)
  51. OBJS += $(OBJS-y) $(patsubst %,%/build.o,$(SUBDIRS))
  52. # Force execution of pattern rules (for which PHONY cannot be directly used).
  53. .PHONY: FORCE
  54. FORCE:
  55. .PHONY: clean
  56. .SECONDARY:
  57. build.o: $(OBJS)
  58. $(LD) -r -o $@ $^
  59. %/build.o: FORCE
  60. $(MAKE) -f $(ROOT)/Rules.mk -C $* build.o
  61. %.o: %.c Makefile
  62. @echo CC $@
  63. $(CC) $(CFLAGS) -c $< -o $@
  64. %.o: %.S Makefile
  65. @echo AS $@
  66. $(CC) $(AFLAGS) -c $< -o $@
  67. %.ld: %.ld.S Makefile
  68. @echo CPP $@
  69. $(CC) -P -E $(AFLAGS) $< -o $@
  70. %.elf: $(OBJS) %.ld Makefile
  71. @echo LD $@
  72. $(CC) $(LDFLAGS) -T$(*F).ld $(OBJS) -o $@
  73. chmod a-x $@
  74. %.hex: %.elf
  75. @echo OBJCOPY $@
  76. $(OBJCOPY) -O ihex $< $@
  77. chmod a-x $@
  78. %.bin: %.elf
  79. @echo OBJCOPY $@
  80. $(OBJCOPY) -O binary $< $@
  81. chmod a-x $@
  82. %.o: $(RPATH)/%.c Makefile
  83. @echo CC $@
  84. $(CC) $(CFLAGS) -c $< -o $@
  85. %.o: $(RPATH)/%.S Makefile
  86. @echo AS $@
  87. $(CC) $(AFLAGS) -c $< -o $@
  88. clean:: $(addprefix _clean_,$(SUBDIRS) $(SUBDIRS-n) $(SUBDIRS-))
  89. rm -f *.orig *.rej *~ *.o *.elf *.hex *.bin *.ld $(DEPS)
  90. _clean_%: FORCE
  91. $(MAKE) -f $(ROOT)/Rules.mk -C $* clean
  92. -include $(DEPS)