Rework the Makefiles to be compatible with binary packages.
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>
Sun, 17 Nov 2013 18:04:29 +0000 (20:04 +0200)
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>
Sun, 29 Dec 2013 18:26:57 +0000 (20:26 +0200)
examples/network_server/Makefile
examples/simple/Makefile
examples/using_double_on_avr/Makefile
examples/using_union_messages/Makefile
extra/nanopb.mk [new file with mode: 0644]
generator/nanopb_generator.py
generator/protoc-gen-nanopb [new file with mode: 0755]
generator/protoc-gen-nanopb.bat [new file with mode: 0644]

index 981f2cf..3c01400 100644 (file)
@@ -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
index 2be3a04..02a4c3f 100644 (file)
@@ -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
 
index 0b5383f..6f2203a 100644 (file)
@@ -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
index 0f7b520..618bca5 100644 (file)
@@ -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 (file)
index 0000000..b52c78c
--- /dev/null
@@ -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=. $<
+
index 86c554a..7b3c9f8 100755 (executable)
@@ -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 (executable)
index 0000000..90be6a5
--- /dev/null
@@ -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 (file)
index 0000000..f70ca60
--- /dev/null
@@ -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