From 9c9d739634913e5b3f85fa07b23ab9878f6b9b8a Mon Sep 17 00:00:00 2001 From: Daniel Lockau Date: Tue, 6 Feb 2024 07:46:38 +0100 Subject: [PATCH] Makefile.{include,base},pkg/nanopb: fix target dependencies & code generation --- Makefile.base | 3 --- Makefile.include | 14 +++++++++--- pkg/nanopb/Makefile.gensrc | 47 +++++++++++++++++++++++--------------- 3 files changed, 40 insertions(+), 24 deletions(-) diff --git a/Makefile.base b/Makefile.base index cfc060c97348..912eff9ff4b1 100644 --- a/Makefile.base +++ b/Makefile.base @@ -97,9 +97,6 @@ compile-commands: | $(DIRS:%=COMPILE-COMMANDS--%) $(file >>$(BINDIR)/$(MODULE)/compile_cmds.txt,TARGET_ARCH: $(TARGET_ARCH)) $(file >>$(BINDIR)/$(MODULE)/compile_cmds.txt,TARGET_ARCH_LLVM: $(TARGET_ARCH_LLVM)) -# include makefile snippets for packages in $(PKG_PATHS) that modify GENSRC: --include $(PKG_PATHS:%=%Makefile.gensrc) - GENOBJC := $(GENSRC:%.c=%.o) OBJC_LTO := $(SRC:%.c=$(BINDIR)/$(MODULE)/%.o) OBJC_NOLTO := $(SRC_NOLTO:%.c=$(BINDIR)/$(MODULE)/%.o) diff --git a/Makefile.include b/Makefile.include index 993edd8e3413..6835e53f03f2 100644 --- a/Makefile.include +++ b/Makefile.include @@ -709,6 +709,12 @@ BUILDDEPS += $(BUILD_DIR)/CACHEDIR.TAG # clean removing dependencies that make previously considered as up to date. $(BUILDDEPS): $(CLEAN) +# include makefile snippets for packages in $(PKG_PATHS) that modify GENSRC: +-include $(PKG_PATHS:%=%Makefile.gensrc) +# remove duplicates & make accessible to subprocesses +GENSRC := $(sort $(GENSRC)) +export GENSRC + # Save value to verify it is not modified later _BASELIBS_VALUE_BEFORE_USAGE := $(BASELIBS) @@ -757,7 +763,7 @@ $(APPLICATION_MODULE).module: FORCE # Other modules are built by application.inc.mk and packages building _SUBMAKE_LIBS = $(filter-out $(APPLICATION_MODULE).module $(APPDEPS), $(BASELIBS) $(ARCHIVES)) -$(_SUBMAKE_LIBS): $(APPLICATION_MODULE).module pkg-build +$(_SUBMAKE_LIBS): $(APPLICATION_MODULE).module pkg-build $(GENSRC) # 'print-size' triggers a rebuild. Use 'info-buildsize' if you do not need to rebuild. print-size: $(ELFFILE) @@ -810,13 +816,15 @@ endif @$(COLOR_ECHO) # The `clean` needs to be serialized before everything else. -all $(BASELIBS) $(ARCHIVES) $(BUILDDEPS) ..in-docker-container: | $(CLEAN) +all $(BASELIBS) $(ARCHIVES) $(BUILDDEPS) $(GENSRC) ..in-docker-container: | $(CLEAN) .PHONY: pkg-prepare pkg-build pkg-prepare: -@$(foreach dir,$(PKG_PATHS),"$(MAKE)" -C $(dir) prepare $(NEWLINE)) -pkg-build: $(BUILDDEPS) +$(GENSRC): pkg-prepare + +pkg-build: $(BUILDDEPS) | $(GENSRC) $(foreach dir,$(PKG_PATHS),$(QQ)"$(MAKE)" -C $(dir) $(NEWLINE)) clean: diff --git a/pkg/nanopb/Makefile.gensrc b/pkg/nanopb/Makefile.gensrc index 1c86af590c66..76b541f60533 100644 --- a/pkg/nanopb/Makefile.gensrc +++ b/pkg/nanopb/Makefile.gensrc @@ -2,29 +2,40 @@ PROTOC ?= protoc PROTOC_GEN_NANOPB ?= $(PKGDIRBASE)/nanopb/generator/protoc-gen-nanopb PROTOBUF_FILES ?= $(wildcard *.proto) -PROTOBUF_PATH ?= $(CURDIR) -GENSRC += $(PROTOBUF_FILES:%.proto=$(BINDIR)/$(MODULE)/%.pb.c) -GENOBJC := $(GENSRC:%.c=%.o) + +# remove duplicates +PROTOBUF_FILES := $(sort $(PROTOBUF_FILES)) + +NANOPB_OUT_DIR := $(BINDIR)/nanopb ifneq (,$(PROTOBUF_FILES)) - INCLUDES += -I$(BINDIR)/$(MODULE) + INCLUDES += -I$(NANOPB_OUT_DIR) endif +PROTOBUF_FILES_BASENAMES = $(notdir $(PROTOBUF_FILES)) + +GENSRC += $(PROTOBUF_FILES_BASENAMES:%.proto=$(NANOPB_OUT_DIR)/%.pb.c) + # workaround for old protoc PROTO_INCLUDES += -I. # add nanopb specific includes PROTO_INCLUDES += -I$(PKGDIRBASE)/nanopb/generator/proto -PROTO_INCLUDES += -I$(PROTOBUF_PATH) - -$(SRC) $(SRCXX): $(GENSRC) - -$(GENSRC): $(PROTOBUF_FILES) - $(Q)D=$(BINDIR)/$(MODULE) && \ - mkdir -p "$$D" && \ - cd $(CURDIR) && \ - $(MAKE) -C $(PKGDIRBASE)/nanopb/generator/proto && \ - for protofile in $(PROTOBUF_FILES); do \ - protoc --plugin=protoc-gen-nanopb=$(PROTOC_GEN_NANOPB) \ - --nanopb_out="$$D" $(PROTO_INCLUDES) \ - $^ \ - ; done + +# We need to filter all protobuf files for source generation as pattern +# matching won't work due to the potentially different directory +# prefixes. + +nanopb_select_proto_from_target = $(firstword $(filter %$(notdir $(basename $(basename $(1)))).proto,$(PROTOBUF_FILES))) + +$(NANOPB_OUT_DIR)/%.pb.c: $(PROTOBUF_FILES) +# We have to create the output directory here because properly chaining +# with the clean target is currently not possible. + -$(Q)mkdir -p $(NANOPB_OUT_DIR) +# Change of directory is required here because of protoc shortcomings. +# Setting --proto_path to the same value will fail under certain +# conditions. + $(Q)cd "$(dir $(call nanopb_select_proto_from_target,$@))" \ + && protoc --plugin=protoc-gen-nanopb=$(PROTOC_GEN_NANOPB) \ + --proto_path=. \ + --nanopb_out="$(NANOPB_OUT_DIR)" $(PROTO_INCLUDES) \ + $(notdir $(call nanopb_select_proto_from_target,$@))