From 45c1a32e5046fe9323d4fd0213b474a554b2bbed Mon Sep 17 00:00:00 2001 From: Petteri Aimonen Date: Sun, 17 Nov 2013 20:04:29 +0200 Subject: [PATCH] Rework the Makefiles to be compatible with binary packages. --- examples/network_server/Makefile | 16 ++++++---------- examples/simple/Makefile | 9 ++++----- examples/using_double_on_avr/Makefile | 19 ++++++------------- examples/using_union_messages/Makefile | 16 ++++++---------- extra/nanopb.mk | 29 +++++++++++++++++++++++++++++ generator/nanopb_generator.py | 17 ++++++++++++----- generator/protoc-gen-nanopb | 12 ++++++++++++ generator/protoc-gen-nanopb.bat | 11 +++++++++++ 8 files changed, 86 insertions(+), 43 deletions(-) create mode 100644 extra/nanopb.mk create mode 100755 generator/protoc-gen-nanopb create mode 100644 generator/protoc-gen-nanopb.bat diff --git a/examples/network_server/Makefile b/examples/network_server/Makefile index 981f2cf9..3c014009 100644 --- a/examples/network_server/Makefile +++ b/examples/network_server/Makefile @@ -1,9 +1,8 @@ -CFLAGS = -ansi -Wall -Werror -g -O0 +# Include the nanopb provided Makefile rules +include ../../extra/nanopb.mk -# Path to the nanopb root folder -NANOPB_DIR = ../.. -DEPS = $(NANOPB_DIR)/pb_decode.c $(NANOPB_DIR)/pb_decode.h \ - $(NANOPB_DIR)/pb_encode.c $(NANOPB_DIR)/pb_encode.h $(NANOPB_DIR)/pb.h +# Compiler flags to enable all warnings & debug info +CFLAGS = -ansi -Wall -Werror -g -O0 CFLAGS += -I$(NANOPB_DIR) all: server client @@ -11,9 +10,6 @@ all: server client clean: rm -f server client fileproto.pb.c fileproto.pb.h -%: %.c $(DEPS) fileproto.pb.h fileproto.pb.c - $(CC) $(CFLAGS) -o $@ $< $(NANOPB_DIR)/pb_decode.c $(NANOPB_DIR)/pb_encode.c fileproto.pb.c common.c +%: %.c common.c fileproto.pb.c + $(CC) $(CFLAGS) -o $@ $^ $(NANOPB_CORE) -fileproto.pb.c fileproto.pb.h: fileproto.proto $(NANOPB_DIR)/generator/nanopb_generator.py - protoc -ofileproto.pb $< - python $(NANOPB_DIR)/generator/nanopb_generator.py fileproto.pb diff --git a/examples/simple/Makefile b/examples/simple/Makefile index 2be3a047..02a4c3ff 100644 --- a/examples/simple/Makefile +++ b/examples/simple/Makefile @@ -1,8 +1,8 @@ +# Include the nanopb provided Makefile rules +include ../../extra/nanopb.mk + # Compiler flags to enable all warnings & debug info CFLAGS = -Wall -Werror -g -O0 - -# Path to the nanopb root folder -NANOPB_DIR = ../.. CFLAGS += -I$(NANOPB_DIR) # C source code files that are required @@ -17,6 +17,5 @@ simple: $(CSRC) # Build rule for the protocol simple.pb.c: simple.proto - protoc -osimple.pb simple.proto - python $(NANOPB_DIR)/generator/nanopb_generator.py simple.pb + $(PROTOC) $(PROTOC_OPTS) --nanopb_out=. simple.proto diff --git a/examples/using_double_on_avr/Makefile b/examples/using_double_on_avr/Makefile index 0b5383fa..6f2203ae 100644 --- a/examples/using_double_on_avr/Makefile +++ b/examples/using_double_on_avr/Makefile @@ -1,10 +1,8 @@ -CFLAGS = -Wall -Werror -g -O0 +# Include the nanopb provided Makefile rules +include ../../extra/nanopb.mk -# Path to the nanopb root directory -NANOPB_DIR = ../.. -DEPS = double_conversion.c $(NANOPB_DIR)/pb.h \ - $(NANOPB_DIR)/pb_decode.c $(NANOPB_DIR)/pb_decode.h \ - $(NANOPB_DIR)/pb_encode.c $(NANOPB_DIR)/pb_encode.h +# Compiler flags to enable all warnings & debug info +CFLAGS = -Wall -Werror -g -O0 CFLAGS += -I$(NANOPB_DIR) all: run_tests @@ -15,13 +13,8 @@ clean: test_conversions: test_conversions.c double_conversion.c $(CC) $(CFLAGS) -o $@ $^ -%: %.c $(DEPS) doubleproto.pb.h doubleproto.pb.c - $(CC) $(CFLAGS) -o $@ $< double_conversion.c \ - $(NANOPB_DIR)/pb_decode.c $(NANOPB_DIR)/pb_encode.c doubleproto.pb.c - -doubleproto.pb.c doubleproto.pb.h: doubleproto.proto $(NANOPB_DIR)/generator/nanopb_generator.py - protoc -odoubleproto.pb $< - python $(NANOPB_DIR)/generator/nanopb_generator.py doubleproto.pb +%: %.c double_conversion.c doubleproto.pb.c + $(CC) $(CFLAGS) -o $@ $^ $(NANOPB_CORE) run_tests: test_conversions encode_double decode_double ./test_conversions diff --git a/examples/using_union_messages/Makefile b/examples/using_union_messages/Makefile index 0f7b5206..618bca50 100644 --- a/examples/using_union_messages/Makefile +++ b/examples/using_union_messages/Makefile @@ -1,9 +1,8 @@ -CFLAGS = -ansi -Wall -Werror -g -O0 +# Include the nanopb provided Makefile rules +include ../../extra/nanopb.mk -# Path to the nanopb root folder -NANOPB_DIR = ../.. -DEPS = $(NANOPB_DIR)/pb_decode.c $(NANOPB_DIR)/pb_decode.h \ - $(NANOPB_DIR)/pb_encode.c $(NANOPB_DIR)/pb_encode.h $(NANOPB_DIR)/pb.h +# Compiler flags to enable all warnings & debug info +CFLAGS = -ansi -Wall -Werror -g -O0 CFLAGS += -I$(NANOPB_DIR) all: encode decode @@ -14,9 +13,6 @@ all: encode decode clean: rm -f encode unionproto.pb.h unionproto.pb.c -%: %.c $(DEPS) unionproto.pb.h unionproto.pb.c - $(CC) $(CFLAGS) -o $@ $< $(NANOPB_DIR)/pb_decode.c $(NANOPB_DIR)/pb_encode.c unionproto.pb.c +%: %.c unionproto.pb.c + $(CC) $(CFLAGS) -o $@ $^ $(NANOPB_CORE) -unionproto.pb.h unionproto.pb.c: unionproto.proto $(NANOPB_DIR)/generator/nanopb_generator.py - protoc -ounionproto.pb $< - python $(NANOPB_DIR)/generator/nanopb_generator.py unionproto.pb diff --git a/extra/nanopb.mk b/extra/nanopb.mk new file mode 100644 index 00000000..b52c78c1 --- /dev/null +++ b/extra/nanopb.mk @@ -0,0 +1,29 @@ +# This is an include file for Makefiles. It provides rules for building +# .pb.c and .pb.h files out of .proto, as well the path to nanopb core. + +# Path to the nanopb root directory +NANOPB_DIR := $(abspath $(dir $(lastword $(MAKEFILE_LIST)))../) + +# Files for the nanopb core +NANOPB_CORE = $(NANOPB_DIR)/pb_encode.c $(NANOPB_DIR)/pb_decode.c + +# Check whether to use binary version of nanopb_generator or the +# system-supplied python interpreter. +ifneq "$(wildcard $(NANOPB_DIR)/generator-bin)" "" + # Binary package + PROTOC = $(NANOPB_DIR)/generator-bin/protoc + PROTOC_OPTS = +else + # Source only or git checkout + PROTOC = protoc + ifdef windir + PROTOC_OPTS = --plugin=nanopb=$(NANOPB_DIR)/generator/protoc-gen-nanopb.bat + else + PROTOC_OPTS = --plugin=nanopb=$(NANOPB_DIR)/generator/protoc-gen-nanopb + endif +endif + +# Rule for building .pb.c and .pb.h +%.pb.c %.pb.h: %.proto $(wildcard %.options) + $(PROTOC) $(PROTOC_OPTS) --nanopb_out=. $< + diff --git a/generator/nanopb_generator.py b/generator/nanopb_generator.py index 86c554a2..7b3c9f80 100755 --- a/generator/nanopb_generator.py +++ b/generator/nanopb_generator.py @@ -15,10 +15,17 @@ except: print raise -import proto.nanopb_pb2 as nanopb_pb2 -import proto.descriptor_pb2 as descriptor - - +try: + import proto.nanopb_pb2 as nanopb_pb2 + import proto.descriptor_pb2 as descriptor +except: + print + print "********************************************************************" + print "*** Failed to import the protocol definitions for generator. ***" + print "*** You have to run 'make' in the nanopb/generator/proto folder. ***" + print "********************************************************************" + print + raise # --------------------------------------------------------------------------- @@ -1054,7 +1061,7 @@ def main_plugin(): if __name__ == '__main__': # Check if we are running as a plugin under protoc - if 'protoc-gen-' in sys.argv[0]: + if 'protoc-gen-' in sys.argv[0] or '--protoc-plugin' in sys.argv: main_plugin() else: main_cli() diff --git a/generator/protoc-gen-nanopb b/generator/protoc-gen-nanopb new file mode 100755 index 00000000..90be6a58 --- /dev/null +++ b/generator/protoc-gen-nanopb @@ -0,0 +1,12 @@ +#!/bin/sh + +# This file is used to invoke nanopb_generator.py as a plugin +# to protoc on Linux and other *nix-style systems. +# Use it like this: +# protoc --plugin=nanopb=..../protoc-gen-nanopb --nanopb_out=dir foo.proto +# +# Note that if you use the binary package of nanopb, the protoc +# path is already set up properly and there is no need to give +# --plugin= on the command line. + +exec python $(dirname $0)/nanopb_generator.py --protoc-plugin diff --git a/generator/protoc-gen-nanopb.bat b/generator/protoc-gen-nanopb.bat new file mode 100644 index 00000000..f70ca600 --- /dev/null +++ b/generator/protoc-gen-nanopb.bat @@ -0,0 +1,11 @@ +:: This file is used to invoke nanopb_generator.py as a plugin +:: to protoc on Windows. +:: Use it like this: +:: protoc --plugin=nanopb=..../protoc-gen-nanopb.bat --nanopb_out=dir foo.proto +:: +:: Note that if you use the binary package of nanopb, the protoc +:: path is already set up properly and there is no need to give +:: --plugin= on the command line. + +set mydir=%~dp0 +python %mydir%\nanopb_generator.py --protoc-plugin -- 2.16.6