• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# Nanoapp/CHRE 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# The NANOPB_OPTIONS variable can be used to supply an .options file to use when
11# generating code for all .proto files. Alternatively, if an .options file has
12# the same name as a .proto file in NANOPB_SRCS, it'll be automatically picked
13# up when generating code **only** for that .proto file.
14#
15# NANOPB_FLAGS can be used to supply additional command line arguments to the
16# nanopb compiler. Note that this is global and applies to all protobuf
17# generated source.
18#
19# NANOPB_INCLUDES may optionally be used to automatically add one or more
20# include path prefixes for C/C++ source and .proto files. For example, if the
21# file myprefix/proto/foo.proto is added to NANOPB_SRCS, but you'd like to use
22# #include "proto/foo.pb.h" in your source (rather than myprefix/proto/foo.pb.h)
23# and/or import "proto/foo.proto" in your .proto files, then set NANOPB_INCLUDES
24# to myprefix.
25
26# Environment Checks ###########################################################
27
28ifneq ($(NANOPB_SRCS),)
29ifeq ($(NANOPB_PREFIX),)
30$(error "NANOPB_SRCS is non-empty. You must supply a NANOPB_PREFIX environment \
31         variable containing a path to the nanopb project. Example: \
32         export NANOPB_PREFIX=$$HOME/path/to/nanopb/nanopb-c")
33endif
34endif
35
36ifeq ($(PROTOC),)
37PROTOC=protoc
38endif
39
40# Generated Source Files #######################################################
41
42NANOPB_GEN_PATH = $(OUT)/nanopb_gen
43
44ifeq ($(NANOPB_EXTENSION),)
45NANOPB_EXTENSION = pb
46else
47NANOPB_GENERATOR_FLAGS = --extension=.$(NANOPB_EXTENSION)
48endif
49
50NANOPB_GEN_SRCS += $(patsubst %.proto, \
51                              $(NANOPB_GEN_PATH)/%.$(NANOPB_EXTENSION).c, \
52                              $(NANOPB_SRCS))
53
54ifneq ($(NANOPB_GEN_SRCS),)
55COMMON_CFLAGS += -I$(NANOPB_PREFIX)
56COMMON_CFLAGS += -I$(NANOPB_GEN_PATH)
57COMMON_CFLAGS += $(addprefix -I$(NANOPB_GEN_PATH)/, $(NANOPB_INCLUDES))
58
59ifneq ($(NANOPB_INCLUDE_LIBRARY),false)
60COMMON_SRCS += $(NANOPB_PREFIX)/pb_common.c
61COMMON_SRCS += $(NANOPB_PREFIX)/pb_decode.c
62COMMON_SRCS += $(NANOPB_PREFIX)/pb_encode.c
63endif
64
65endif
66
67# NanoPB Compiler Flags ########################################################
68
69ifneq ($(NANOPB_GEN_SRCS),)
70ifneq ($(NANOPB_INCLUDE_LIBRARY),false)
71COMMON_CFLAGS += -DPB_NO_PACKED_STRUCTS=1
72endif
73endif
74
75# NanoPB Generator Setup #######################################################
76
77NANOPB_GENERATOR_SRCS = $(NANOPB_PREFIX)/generator/proto/nanopb_pb2.py
78NANOPB_GENERATOR_SRCS += $(NANOPB_PREFIX)/generator/proto/plugin_pb2.py
79
80$(NANOPB_GENERATOR_SRCS):
81	cd $(NANOPB_PREFIX)/generator/proto && make
82
83ifneq ($(NANOPB_OPTIONS),)
84NANOPB_OPTIONS_FLAG = --options-file=$(NANOPB_OPTIONS)
85else
86NANOPB_OPTIONS_FLAG =
87endif
88
89NANOPB_FLAGS += $(addprefix --proto_path=, $(abspath $(NANOPB_INCLUDES)))
90
91# Generate NanoPB Sources ######################################################
92
93COMMON_SRCS += $(NANOPB_GEN_SRCS)
94
95NANOPB_PROTOC = $(NANOPB_PREFIX)/generator/protoc-gen-nanopb
96
97$(NANOPB_GEN_PATH)/%.$(NANOPB_EXTENSION).c \
98        $(NANOPB_GEN_PATH)/%.$(NANOPB_EXTENSION).h: %.proto \
99                                                    %.options \
100                                                    $(NANOPB_GENERATOR_SRCS)
101	@echo " [NANOPB] $<"
102	$(V)mkdir -p $(dir $@)
103	$(V)$(PROTOC) --plugin=protoc-gen-nanopb=$(NANOPB_PROTOC) \
104	  --proto_path=$(abspath $(dir $<)) \
105	  $(NANOPB_FLAGS) \
106	  --nanopb_out="$(NANOPB_GENERATOR_FLAGS) --options-file=$(basename $<).options:$(dir $@)" \
107	  $(abspath $<)
108
109$(NANOPB_GEN_PATH)/%.$(NANOPB_EXTENSION).c \
110        $(NANOPB_GEN_PATH)/%.$(NANOPB_EXTENSION).h: %.proto \
111                                                    $(NANOPB_OPTIONS) \
112                                                    $(NANOPB_GENERATOR_SRCS)
113	@echo " [NANOPB] $<"
114	$(V)mkdir -p $(dir $@)
115	$(V)$(PROTOC) --plugin=protoc-gen-nanopb=$(NANOPB_PROTOC) \
116	  --proto_path=$(abspath $(dir $<)) \
117	  $(NANOPB_FLAGS) \
118	  --nanopb_out="$(NANOPB_GENERATOR_FLAGS) $(NANOPB_OPTIONS_FLAG):$(dir $@)" \
119	  $(abspath $<)
120