component_common.mk 4.8 KB

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