Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makefile.{include,base},pkg/nanopb: fix target deps & code generation #20400

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Makefile.base
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 11 additions & 3 deletions Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
47 changes: 29 additions & 18 deletions pkg/nanopb/Makefile.gensrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,40 @@ PROTOC ?= protoc
PROTOC_GEN_NANOPB ?= $(PKGDIRBASE)/nanopb/generator/protoc-gen-nanopb

PROTOBUF_FILES ?= $(wildcard *.proto)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In our branch we have

Suggested change
PROTOBUF_FILES ?= $(wildcard *.proto)
PROTOBUF_FILES += $(wildcard *.proto)

and apps that don't build with the upstream version.

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,$@))
Loading