nanopb.mk 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. # Environment Checks ###########################################################
  14. ifneq ($(NANOPB_SRCS),)
  15. ifeq ($(NANOPB_PREFIX),)
  16. $(error "NANOPB_SRCS is non-empty. You must supply a NANOPB_PREFIX environment \
  17. variable containing a path to the nanopb project. Example: \
  18. export NANOPB_PREFIX=$$HOME/path/to/nanopb/nanopb-c")
  19. endif
  20. endif
  21. ifeq ($(PROTOC),)
  22. PROTOC=protoc
  23. endif
  24. # Generated Source Files #######################################################
  25. NANOPB_GEN_PATH = $(OUT)/nanopb_gen
  26. ifeq ($(NANOPB_EXTENSION),)
  27. NANOPB_EXTENSION = pb
  28. else
  29. NANOPB_GENERATOR_FLAGS = --extension=.$(NANOPB_EXTENSION)
  30. endif
  31. NANOPB_GEN_SRCS += $(patsubst %.proto, \
  32. $(NANOPB_GEN_PATH)/%.$(NANOPB_EXTENSION).c, \
  33. $(NANOPB_SRCS))
  34. ifneq ($(NANOPB_GEN_SRCS),)
  35. COMMON_CFLAGS += -I$(NANOPB_PREFIX)
  36. COMMON_CFLAGS += -I$(NANOPB_GEN_PATH)
  37. COMMON_CFLAGS += $(addprefix -I$(NANOPB_GEN_PATH)/, $(NANOPB_INCLUDES))
  38. ifneq ($(NANOPB_INCLUDE_LIBRARY),false)
  39. COMMON_SRCS += $(NANOPB_PREFIX)/pb_common.c
  40. COMMON_SRCS += $(NANOPB_PREFIX)/pb_decode.c
  41. COMMON_SRCS += $(NANOPB_PREFIX)/pb_encode.c
  42. endif
  43. endif
  44. # NanoPB Compiler Flags ########################################################
  45. ifneq ($(NANOPB_GEN_SRCS),)
  46. ifneq ($(NANOPB_INCLUDE_LIBRARY),false)
  47. COMMON_CFLAGS += -DPB_NO_PACKED_STRUCTS=1
  48. endif
  49. endif
  50. # NanoPB Generator Setup #######################################################
  51. NANOPB_GENERATOR_SRCS = $(NANOPB_PREFIX)/generator/proto/nanopb_pb2.py
  52. NANOPB_GENERATOR_SRCS += $(NANOPB_PREFIX)/generator/proto/plugin_pb2.py
  53. $(NANOPB_GENERATOR_SRCS):
  54. cd $(NANOPB_PREFIX)/generator/proto && make
  55. # Generate NanoPB Sources ######################################################
  56. COMMON_SRCS += $(NANOPB_GEN_SRCS)
  57. NANOPB_PROTOC = $(NANOPB_PREFIX)/generator/protoc-gen-nanopb
  58. $(NANOPB_GEN_PATH)/%.$(NANOPB_EXTENSION).c \
  59. $(NANOPB_GEN_PATH)/%.$(NANOPB_EXTENSION).h: %.proto \
  60. %.options \
  61. $(NANOPB_GENERATOR_SRCS)
  62. mkdir -p $(dir $@)
  63. $(PROTOC) --plugin=protoc-gen-nanopb=$(NANOPB_PROTOC) $(NANOPB_FLAGS) \
  64. --nanopb_out="$(NANOPB_GENERATOR_FLAGS) --options-file=$(basename $<).options:$(NANOPB_GEN_PATH)/$(NANOPB_PROTO_PATH)" \
  65. $<
  66. $(NANOPB_GEN_PATH)/%.$(NANOPB_EXTENSION).c \
  67. $(NANOPB_GEN_PATH)/%.$(NANOPB_EXTENSION).h: %.proto \
  68. $(NANOPB_OPTIONS) \
  69. $(NANOPB_GENERATOR_SRCS)
  70. mkdir -p $(dir $@)
  71. $(PROTOC) --plugin=protoc-gen-nanopb=$(NANOPB_PROTOC) $(NANOPB_FLAGS) \
  72. --nanopb_out="$(NANOPB_GENERATOR_FLAGS) --options-file=$(NANOPB_OPTIONS):$(NANOPB_GEN_PATH)/$(NANOPB_PROTO_PATH)" \
  73. $<