1#
2# Nanoapp NanoPB Makefile
3#
4# Include this file in your nanoapp Makefile to produce pb.c and pb.h (or
5# $NANOPB_EXTENSION.c and $NANOPB_EXTENSION.h if $NANOPB_EXTENSION is defined)
6# for .proto files specified in the NANOPB_SRCS variable. The produced pb.c or
7# $NANOPB_EXTENSION.c files are automatically added to the COMMON_SRCS variable
8# for the nanoapp build.
9#
10# NANOPB_FLAGS can be used to supply additional command line arguments to the
11# nanopb compiler. Note that this is global and applies to all protobuf
12# generated source.
13
14# Environment Checks ###########################################################
15
16ifneq ($(NANOPB_SRCS),)
17ifeq ($(NANOPB_PREFIX),)
18$(error "NANOPB_SRCS is non-empty. You must supply a NANOPB_PREFIX environment \
19         variable containing a path to the nanopb project. Example: \
20         export NANOPB_PREFIX=$$HOME/path/to/nanopb/nanopb-c")
21endif
22endif
23
24ifeq ($(PROTOC),)
25PROTOC=protoc
26endif
27
28# Generated Source Files #######################################################
29
30NANOPB_GEN_PATH = $(OUT)/nanopb_gen
31
32ifeq ($(NANOPB_EXTENSION),)
33NANOPB_EXTENSION = pb
34else
35NANOPB_GENERATOR_FLAGS = --extension=.$(NANOPB_EXTENSION)
36endif
37
38NANOPB_GEN_SRCS += $(patsubst %.proto, \
39                              $(NANOPB_GEN_PATH)/%.$(NANOPB_EXTENSION).c, \
40                              $(NANOPB_SRCS))
41
42ifneq ($(NANOPB_GEN_SRCS),)
43COMMON_CFLAGS += -I$(NANOPB_PREFIX)
44COMMON_CFLAGS += -I$(NANOPB_GEN_PATH)
45COMMON_CFLAGS += $(addprefix -I$(NANOPB_GEN_PATH)/, $(NANOPB_INCLUDES))
46
47ifneq ($(NANOPB_INCLUDE_LIBRARY),false)
48COMMON_SRCS += $(NANOPB_PREFIX)/pb_common.c
49COMMON_SRCS += $(NANOPB_PREFIX)/pb_decode.c
50COMMON_SRCS += $(NANOPB_PREFIX)/pb_encode.c
51endif
52
53endif
54
55# NanoPB Compiler Flags ########################################################
56
57ifneq ($(NANOPB_GEN_SRCS),)
58ifneq ($(NANOPB_INCLUDE_LIBRARY),false)
59COMMON_CFLAGS += -DPB_NO_PACKED_STRUCTS=1
60endif
61endif
62
63# NanoPB Generator Setup #######################################################
64
65NANOPB_GENERATOR_SRCS = $(NANOPB_PREFIX)/generator/proto/nanopb_pb2.py
66NANOPB_GENERATOR_SRCS += $(NANOPB_PREFIX)/generator/proto/plugin_pb2.py
67
68$(NANOPB_GENERATOR_SRCS):
69	cd $(NANOPB_PREFIX)/generator/proto && make
70
71# Generate NanoPB Sources ######################################################
72
73COMMON_SRCS += $(NANOPB_GEN_SRCS)
74
75NANOPB_PROTOC = $(NANOPB_PREFIX)/generator/protoc-gen-nanopb
76
77$(NANOPB_GEN_PATH)/%.$(NANOPB_EXTENSION).c \
78        $(NANOPB_GEN_PATH)/%.$(NANOPB_EXTENSION).h: %.proto \
79                                                    %.options \
80                                                    $(NANOPB_GENERATOR_SRCS)
81	@echo " [NANOPB] $<"
82	$(V)mkdir -p $(dir $@)
83	$(V)$(PROTOC) --plugin=protoc-gen-nanopb=$(NANOPB_PROTOC) $(NANOPB_FLAGS) \
84	  --nanopb_out="$(NANOPB_GENERATOR_FLAGS) --options-file=$(basename $<).options:$(NANOPB_GEN_PATH)/$(NANOPB_PROTO_PATH)" \
85	  $<
86
87$(NANOPB_GEN_PATH)/%.$(NANOPB_EXTENSION).c \
88        $(NANOPB_GEN_PATH)/%.$(NANOPB_EXTENSION).h: %.proto \
89                                                    $(NANOPB_OPTIONS) \
90                                                    $(NANOPB_GENERATOR_SRCS)
91	@echo " [NANOPB] $<"
92	$(V)mkdir -p $(dir $@)
93	$(V)$(PROTOC) --plugin=protoc-gen-nanopb=$(NANOPB_PROTOC) $(NANOPB_FLAGS) \
94	  --nanopb_out="$(NANOPB_GENERATOR_FLAGS) --options-file=$(NANOPB_OPTIONS):$(NANOPB_GEN_PATH)/$(NANOPB_PROTO_PATH)" \
95	  $<
96