component.mk 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Main SDK Makefile
  2. # This Makefile gets included in the Makefile of all the components to set the correct include paths etc.
  3. # PWD is the build directory of the component and the top Makefile is the one in the
  4. # component source dir.
  5. #
  6. # The way the Makefile differentiates between those two is by looking at the environment
  7. # variable PROJECT_PATH. If this is set (to the basepath of the project), we're building a
  8. # component and its Makefile has included this makefile. If not, we're building the entire project.
  9. #
  10. #
  11. # This Makefile requires the environment variable SDK_PATH to be set
  12. # to the directory where the SDK (containing the make directory with
  13. # this file) is located
  14. #
  15. ifeq ("$(PROJECT_PATH)","")
  16. $(error Make was invoked from $(CURDIR). However please do not run make from the sdk or a component directory; invoke make from the project directory. See SDK README for details.)
  17. endif
  18. # Find the path to the component
  19. COMPONENT_PATH := $(abspath $(dir $(firstword $(MAKEFILE_LIST))))
  20. export COMPONENT_PATH
  21. include $(SDK_PATH)/make/common.mk
  22. #Some of these options are overridable by the components Makefile.
  23. #Name of the component
  24. COMPONENT_NAME ?= $(lastword $(subst /, ,$(realpath $(COMPONENT_PATH))))
  25. #Absolute path of the .a file
  26. COMPONENT_LIBRARY := lib$(COMPONENT_NAME).a
  27. #Source dirs a component has. Default to root directory of component.
  28. COMPONENT_SRCDIRS ?= .
  29. #Object files which need to be linked into the library
  30. #By default we take all .c/.S files in the component directory.
  31. ifeq ("$(COMPONENT_OBJS)", "")
  32. #Find all source files in all COMPONENT_SRCDIRS
  33. COMPONENT_OBJS := $(foreach compsrcdir,$(COMPONENT_SRCDIRS),$(patsubst %.c,%.o,$(wildcard $(COMPONENT_PATH)/$(compsrcdir)/*.c)))
  34. COMPONENT_OBJS += $(foreach compsrcdir,$(COMPONENT_SRCDIRS),$(patsubst %.cpp,%.o,$(wildcard $(COMPONENT_PATH)/$(compsrcdir)/*.cpp)))
  35. COMPONENT_OBJS += $(foreach compsrcdir,$(COMPONENT_SRCDIRS),$(patsubst %.S,%.o,$(wildcard $(COMPONENT_PATH)/$(compsrcdir)/*.S)))
  36. #Make relative by removing COMPONENT_PATH from all found object paths
  37. COMPONENT_OBJS := $(patsubst $(COMPONENT_PATH)/%,%,$(COMPONENT_OBJS))
  38. endif
  39. #By default, include only the include/ dir.
  40. COMPONENT_ADD_INCLUDEDIRS ?= include
  41. COMPONENT_ADD_LDFLAGS ?= -l$(COMPONENT_NAME)
  42. #If we're called to compile something, we'll get passed the COMPONENT_INCLUDES
  43. #variable with all the include dirs from all the components in random order. This
  44. #means we can accidentally grab a header from another component before grabbing our own.
  45. #To make sure that does not happen, re-order the includes so ours come first.
  46. OWN_INCLUDES:=$(abspath $(addprefix $(COMPONENT_PATH)/,$(COMPONENT_ADD_INCLUDEDIRS) $(COMPONENT_PRIV_INCLUDEDIRS)))
  47. COMPONENT_INCLUDES := $(OWN_INCLUDES) $(filter-out $(OWN_INCLUDES),$(COMPONENT_INCLUDES))
  48. #This target is used to collect variable values from inside the main makefile
  49. get_variable:
  50. @echo "$(GET_VARIABLE)=$(call $(GET_VARIABLE)) "
  51. #Targets for build/clean. Use builtin recipe if component Makefile
  52. #hasn't defined its own.
  53. ifeq ("$(COMPONENT_OWNBUILDTARGET)", "")
  54. build: $(COMPONENT_LIBRARY)
  55. @mkdir -p $(COMPONENT_SRCDIRS)
  56. #Build the archive. We remove the archive first, otherwise ar will get confused if we update
  57. #an archive when multiple filenames have the same name (src1/test.o and src2/test.o)
  58. $(COMPONENT_LIBRARY): $(COMPONENT_OBJS)
  59. $(vecho) AR $@
  60. $(Q) rm -f $@
  61. $(Q) $(AR) cru $@ $(COMPONENT_OBJS)
  62. endif
  63. ifeq ("$(COMPONENT_OWNCLEANTARGET)", "")
  64. clean:
  65. $(vecho) RM $(COMPONENT_LIBRARY) $(COMPONENT_OBJS) $(COMPONENT_OBJS:.o=.d) $(COMPONENT_EXTRA_CLEAN)
  66. $(Q) rm -f $(COMPONENT_LIBRARY) $(COMPONENT_OBJS) $(COMPONENT_OBJS:.o=.d) $(COMPONENT_EXTRA_CLEAN)
  67. endif
  68. #Also generate dependency files
  69. CFLAGS+=-MMD
  70. CXXFLAGS+=-MMD
  71. #Include all dependency files already generated
  72. -include $(COMPONENT_OBJS:.o=.d)
  73. #This pattern is generated for each COMPONENT_SRCDIR to compile the files in it.
  74. define GenerateCompileTargets
  75. # $(1) - directory containing source files, relative to $(COMPONENT_PATH)
  76. $(1)/%.o: $$(COMPONENT_PATH)/$(1)/%.c | $(1)
  77. $$(vecho) CC $$@
  78. $$(Q) $$(CC) $$(CFLAGS) $$(addprefix -I ,$$(COMPONENT_INCLUDES)) $$(addprefix -I ,$$(COMPONENT_EXTRA_INCLUDES)) -I$(1) -c $$< -o $$@
  79. $(1)/%.o: $$(COMPONENT_PATH)/$(1)/%.cpp | $(1)
  80. $$(vecho) CC $$@
  81. $$(Q) $$(CXX) $$(CXXFLAGS) $$(addprefix -I,$$(COMPONENT_INCLUDES)) $$(addprefix -I,$$(COMPONENT_EXTRA_INCLUDES)) -I$(1) -c $$< -o $$@
  82. $(1)/%.o: $$(COMPONENT_PATH)/$(1)/%.S | $(1)
  83. $$(vecho) CC $$@
  84. $$(Q) $$(CC) $$(CFLAGS) $$(addprefix -I ,$$(COMPONENT_INCLUDES)) $$(addprefix -I ,$$(COMPONENT_EXTRA_INCLUDES)) -I$(1) -c $$< -o $$@
  85. # CWD is build dir, create the build subdirectory if it doesn't exist
  86. $(1):
  87. @mkdir -p $(1)
  88. endef
  89. #Generate all the compile target recipes
  90. $(foreach srcdir,$(COMPONENT_SRCDIRS), $(eval $(call GenerateCompileTargets,$(srcdir))))