rules.mk 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # e200z common makefile scripts and rules.
  2. ##############################################################################
  3. # Processing options coming from the upper Makefile.
  4. #
  5. # Compiler options
  6. OPT = $(USE_OPT)
  7. COPT = $(USE_COPT)
  8. CPPOPT = $(USE_CPPOPT)
  9. # Garbage collection
  10. ifeq ($(USE_LINK_GC),yes)
  11. OPT += -ffunction-sections -fdata-sections -fno-common
  12. LDOPT := --gc-sections
  13. else
  14. LDOPT := --no-gc-sections
  15. endif
  16. # Linker extra options
  17. ifneq ($(USE_LDOPT),)
  18. LDOPT := $(LDOPT),$(USE_LDOPT)
  19. endif
  20. # Link time optimizations
  21. ifeq ($(USE_LTO),yes)
  22. OPT += -flto
  23. endif
  24. # Output directory and files
  25. ifeq ($(BUILDDIR),)
  26. BUILDDIR = build
  27. endif
  28. ifeq ($(BUILDDIR),.)
  29. BUILDDIR = build
  30. endif
  31. # Dependencies directory
  32. ifeq ($(DEPDIR),)
  33. DEPDIR = .dep
  34. endif
  35. ifeq ($(DEPDIR),.)
  36. DEPDIR = .dep
  37. endif
  38. OUTFILES = $(BUILDDIR)/$(PROJECT)
  39. # Source files groups and paths
  40. SRC = $(CSRC)$(CPPSRC)
  41. SRCPATHS = $(sort $(dir $(ASMXSRC)) $(dir $(ASMSRC)) $(dir $(SRC)))
  42. # Various directories
  43. OBJDIR = $(BUILDDIR)/obj
  44. LSTDIR = $(BUILDDIR)/lst
  45. # Object files groups
  46. COBJS = $(addprefix $(OBJDIR)/, $(notdir $(CSRC:.c=.o)))
  47. CPPOBJS = $(addprefix $(OBJDIR)/, $(notdir $(CPPSRC:.cpp=.o)))
  48. ASMOBJS = $(addprefix $(OBJDIR)/, $(notdir $(ASMSRC:.s=.o)))
  49. ASMXOBJS = $(addprefix $(OBJDIR)/, $(notdir $(ASMXSRC:.S=.o)))
  50. OBJS = $(ASMXOBJS) $(ASMOBJS) $(COBJS) $(CPPOBJS)
  51. # Paths
  52. IINCDIR = $(patsubst %,-I%,$(INCDIR) $(DINCDIR) $(UINCDIR))
  53. LLIBDIR = $(patsubst %,-L%,$(DLIBDIR) $(ULIBDIR))
  54. # Macros
  55. DEFS = $(DDEFS) $(UDEFS)
  56. ADEFS = $(DADEFS) $(UADEFS)
  57. # Libs
  58. LIBS = $(DLIBS) $(ULIBS)
  59. # Various settings
  60. MCFLAGS =
  61. ODFLAGS = -x --syms
  62. ASFLAGS = $(MCFLAGS) $(OPT) -Wa,-amhls=$(LSTDIR)/$(notdir $(<:.s=.lst)) $(ADEFS)
  63. ASXFLAGS = $(MCFLAGS) $(OPT) -Wa,-amhls=$(LSTDIR)/$(notdir $(<:.S=.lst)) $(ADEFS)
  64. CFLAGS = $(MCFLAGS) $(OPT) $(COPT) $(CWARN) -Wa,-alms=$(LSTDIR)/$(notdir $(<:.c=.lst)) $(DEFS)
  65. CPPFLAGS = $(MCFLAGS) $(OPT) $(CPPOPT) $(CPPWARN) -Wa,-alms=$(LSTDIR)/$(notdir $(<:.cpp=.lst)) $(DEFS)
  66. LDFLAGS = $(MCFLAGS) $(OPT) $(LLIBDIR) -Wl,-Map=$(BUILDDIR)/$(PROJECT).map,--cref,--no-warn-mismatch,$(LDOPT)
  67. # Generate dependency information
  68. ASFLAGS += -MD -MP -MF $(DEPDIR)/$(@F).d
  69. ASXFLAGS += -MD -MP -MF $(DEPDIR)/$(@F).d
  70. CFLAGS += -MD -MP -MF $(DEPDIR)/$(@F).d
  71. CPPFLAGS += -MD -MP -MF $(DEPDIR)/$(@F).d
  72. # Paths where to search for sources
  73. VPATH = $(SRCPATHS)
  74. #
  75. # Makefile rules
  76. #
  77. all: PRE_MAKE_ALL_RULE_HOOK $(OBJS) $(OUTFILES) POST_MAKE_ALL_RULE_HOOK
  78. PRE_MAKE_ALL_RULE_HOOK:
  79. POST_MAKE_ALL_RULE_HOOK:
  80. $(OBJS): | PRE_MAKE_ALL_RULE_HOOK $(BUILDDIR) $(OBJDIR) $(LSTDIR) $(DEPDIR)
  81. $(BUILDDIR):
  82. ifneq ($(USE_VERBOSE_COMPILE),yes)
  83. @echo Compiler Options
  84. @echo $(CC) -c $(CFLAGS) -I. $(IINCDIR) main.c -o main.o
  85. @echo
  86. endif
  87. @mkdir -p $(BUILDDIR)
  88. $(OBJDIR):
  89. @mkdir -p $(OBJDIR)
  90. $(LSTDIR):
  91. @mkdir -p $(LSTDIR)
  92. $(DEPDIR):
  93. @mkdir -p $(DEPDIR)
  94. $(CPPOBJS) : $(OBJDIR)/%.o : %.cpp $(MAKEFILE_LIST)
  95. ifeq ($(USE_VERBOSE_COMPILE),yes)
  96. @echo
  97. $(CPPC) -c $(CPPFLAGS) -I. $(IINCDIR) $< -o $@
  98. else
  99. @echo Compiling $(<F)
  100. @$(CPPC) -c $(CPPFLAGS) -I. $(IINCDIR) $< -o $@
  101. endif
  102. $(COBJS) : $(OBJDIR)/%.o : %.c $(MAKEFILE_LIST)
  103. ifeq ($(USE_VERBOSE_COMPILE),yes)
  104. @echo
  105. $(CC) -c $(CFLAGS) -I. $(IINCDIR) $< -o $@
  106. else
  107. @echo Compiling $(<F)
  108. @$(CC) -c $(CFLAGS) -I. $(IINCDIR) $< -o $@
  109. endif
  110. $(ASMOBJS) : $(OBJDIR)/%.o : %.s $(MAKEFILE_LIST)
  111. ifeq ($(USE_VERBOSE_COMPILE),yes)
  112. @echo
  113. $(AS) -c $(ASFLAGS) -I. $(IINCDIR) $< -o $@
  114. else
  115. @echo Compiling $(<F)
  116. @$(AS) -c $(ASFLAGS) -I. $(IINCDIR) $< -o $@
  117. endif
  118. $(ASMXOBJS) : $(OBJDIR)/%.o : %.S $(MAKEFILE_LIST)
  119. ifeq ($(USE_VERBOSE_COMPILE),yes)
  120. @echo
  121. $(CC) -c $(ASXFLAGS) -I. $(IINCDIR) $< -o $@
  122. else
  123. @echo Compiling $(<F)
  124. @$(CC) -c $(ASXFLAGS) -I. $(IINCDIR) $< -o $@
  125. endif
  126. $(BUILDDIR)/$(PROJECT): $(OBJS)
  127. ifeq ($(USE_VERBOSE_COMPILE),yes)
  128. @echo
  129. $(LD) $(OBJS) $(LDFLAGS) $(LIBS) -o $@
  130. else
  131. @echo Linking $@
  132. @$(LD) $(OBJS) $(LDFLAGS) $(LIBS) -o $@
  133. endif
  134. lib: $(OBJS) $(BUILDDIR)/lib$(PROJECT).a
  135. $(BUILDDIR)/lib$(PROJECT).a: $(OBJS)
  136. @$(AR) -r $@ $^
  137. @echo
  138. @echo Done
  139. clean: CLEAN_RULE_HOOK
  140. @echo Cleaning
  141. @echo - $(DEPDIR)
  142. @-rm -fR $(DEPDIR)/* $(BUILDDIR)/* 2>/dev/null
  143. @-if [ -d "$(DEPDIR)" ]; then rmdir -p --ignore-fail-on-non-empty $(subst ./,,$(DEPDIR)) 2>/dev/null; fi
  144. @echo - $(BUILDDIR)
  145. @-if [ -d "$(BUILDDIR)" ]; then rmdir -p --ignore-fail-on-non-empty $(subst ./,,$(BUILDDIR)) 2>/dev/null; fi
  146. @echo
  147. @echo Done
  148. CLEAN_RULE_HOOK:
  149. .PHONY: gcov
  150. gcov:
  151. $(COV) -u -b -o $(BUILDDIR)/obj $(GCOVSRC)
  152. #
  153. # Include the dependency files, should be the last of the makefile
  154. #
  155. -include $(wildcard $(DEPDIR)/*)
  156. # *** EOF ***