component_common.mk 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 component's component.mk 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 project.mk
  50. # see project.mk GetVariable macro for details.
  51. get_variable:
  52. @echo "$(GET_VARIABLE)=$(call $(GET_VARIABLE)) "
  53. #Targets for build/clean. Use builtin recipe if component Makefile
  54. #hasn't defined its own.
  55. ifeq ("$(COMPONENT_OWNBUILDTARGET)", "")
  56. build: $(COMPONENT_LIBRARY)
  57. @mkdir -p $(COMPONENT_SRCDIRS)
  58. #Build the archive. We remove the archive first, otherwise ar will get confused if we update
  59. #an archive when multiple filenames have the same name (src1/test.o and src2/test.o)
  60. $(COMPONENT_LIBRARY): $(COMPONENT_OBJS)
  61. $(summary) AR $@
  62. $(Q) rm -f $@
  63. $(Q) $(AR) cru $@ $(COMPONENT_OBJS)
  64. endif
  65. ifeq ("$(COMPONENT_OWNCLEANTARGET)", "")
  66. clean:
  67. $(summary) RM $(COMPONENT_LIBRARY) $(COMPONENT_OBJS) $(COMPONENT_OBJS:.o=.d) $(COMPONENT_EXTRA_CLEAN)
  68. $(Q) rm -f $(COMPONENT_LIBRARY) $(COMPONENT_OBJS) $(COMPONENT_OBJS:.o=.d) $(COMPONENT_EXTRA_CLEAN)
  69. endif
  70. #Include all dependency files already generated
  71. -include $(COMPONENT_OBJS:.o=.d)
  72. #This pattern is generated for each COMPONENT_SRCDIR to compile the files in it.
  73. define GenerateCompileTargets
  74. # $(1) - directory containing source files, relative to $(COMPONENT_PATH)
  75. $(1)/%.o: $$(COMPONENT_PATH)/$(1)/%.c | $(1)
  76. $$(summary) CC $$@
  77. $$(Q) $$(CC) $$(CFLAGS) $(CPPFLAGS) $$(addprefix -I ,$$(COMPONENT_INCLUDES)) $$(addprefix -I ,$$(COMPONENT_EXTRA_INCLUDES)) -I$(1) -c $$< -o $$@
  78. $(1)/%.o: $$(COMPONENT_PATH)/$(1)/%.cpp | $(1)
  79. $$(summary) CXX $$@
  80. $$(Q) $$(CXX) $$(CXXFLAGS) $(CPPFLAGS) $$(addprefix -I,$$(COMPONENT_INCLUDES)) $$(addprefix -I,$$(COMPONENT_EXTRA_INCLUDES)) -I$(1) -c $$< -o $$@
  81. $(1)/%.o: $$(COMPONENT_PATH)/$(1)/%.S | $(1)
  82. $$(summary) AS $$@
  83. $$(Q) $$(CC) $$(CFLAGS) $(CPPFLAGS) $$(addprefix -I ,$$(COMPONENT_INCLUDES)) $$(addprefix -I ,$$(COMPONENT_EXTRA_INCLUDES)) -I$(1) -c $$< -o $$@
  84. # CWD is build dir, create the build subdirectory if it doesn't exist
  85. $(1):
  86. @mkdir -p $(1)
  87. endef
  88. #Generate all the compile target recipes
  89. $(foreach srcdir,$(COMPONENT_SRCDIRS), $(eval $(call GenerateCompileTargets,$(srcdir))))