Add a SCons tool for nanopb protos.
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>
Sat, 23 Nov 2013 09:04:12 +0000 (11:04 +0200)
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>
Sun, 29 Dec 2013 18:26:58 +0000 (20:26 +0200)
This tool script is useful both for building the tests and also for use in other applications.

tests/SConstruct
tests/common/SConscript
tests/site_scons/site_init.py
tests/site_scons/site_tools/nanopb.py [new file with mode: 0644]
tests/special_characters/SConscript

index 675989a..a44ee55 100644 (file)
@@ -14,7 +14,7 @@ scons CC=clang CXX=clang++
 ''')
 
 import os
-env = Environment(ENV = os.environ)
+env = Environment(ENV = os.environ, tools = ['default', 'nanopb'])
 
 # Allow overriding the compiler with scons CC=???
 if 'CC' in ARGUMENTS: env.Replace(CC = ARGUMENTS['CC'])
index 8130c85..144f149 100644 (file)
@@ -12,6 +12,6 @@ env.NanopbProto("person")
 # These are built using more strict warning flags.
 strict = env.Clone()
 strict.Append(CFLAGS = strict['CORECFLAGS'])
-strict.Object("pb_decode.o", "#../pb_decode.c")
-strict.Object("pb_encode.o", "#../pb_encode.c")
+strict.Object("pb_decode.o", "$NANOPB/pb_decode.c")
+strict.Object("pb_encode.o", "$NANOPB/pb_encode.c")
 
index 38aa1a4..2226144 100644 (file)
@@ -11,42 +11,6 @@ except ImportError:
 
 def add_nanopb_builders(env):
     '''Add the necessary builder commands for nanopb tests.'''
-    
-    # Build command for building .pb from .proto using protoc
-    def proto_actions(source, target, env, for_signature):
-        esc = env['ESCAPE']
-        dirs = ' '.join(['-I' + esc(env.GetBuildPath(d)) for d in env['PROTOCPATH']])
-        return '$PROTOC $PROTOCFLAGS %s -o%s %s' % (dirs, esc(str(target[0])), esc(str(source[0])))
-
-    proto_file_builder = Builder(generator = proto_actions,
-                                 suffix = '.pb',
-                                 src_suffix = '.proto')
-    env.Append(BUILDERS = {'Proto': proto_file_builder})
-    env.SetDefault(PROTOC = 'protoc')
-    env.SetDefault(PROTOCPATH = ['.'])
-
-    # Build command for running nanopb generator
-    import os.path
-    def nanopb_targets(target, source, env):
-        basename = os.path.splitext(str(source[0]))[0]
-        target.append(basename + '.pb.h')
-        return target, source
-
-    nanopb_file_builder = Builder(action = '$NANOPB_GENERATOR $NANOPB_FLAGS $SOURCE',
-                                  suffix = '.pb.c',
-                                  src_suffix = '.pb',
-                                  emitter = nanopb_targets)
-    env.Append(BUILDERS = {'Nanopb': nanopb_file_builder})
-    gen_path = env['ESCAPE'](env.GetBuildPath("#../generator/nanopb_generator.py"))
-    env.SetDefault(NANOPB_GENERATOR = 'python ' + gen_path)
-    env.SetDefault(NANOPB_FLAGS = '-q')
-
-    # Combined method to run both protoc and nanopb generator
-    def run_protoc_and_nanopb(env, source):
-        b1 = env.Proto(source)
-        b2 = env.Nanopb(source)
-        return b1 + b2
-    env.AddMethod(run_protoc_and_nanopb, "NanopbProto")
 
     # Build command that runs a test program and saves the output
     def run_test(target, source, env):
diff --git a/tests/site_scons/site_tools/nanopb.py b/tests/site_scons/site_tools/nanopb.py
new file mode 100644 (file)
index 0000000..0ed7046
--- /dev/null
@@ -0,0 +1,125 @@
+'''
+Scons Builder for nanopb .proto definitions.
+
+This tool will locate the nanopb generator and use it to generate .pb.c and
+.pb.h files from the .proto files.
+
+Basic example
+-------------
+# Build myproto.pb.c and myproto.pb.h from myproto.proto
+myproto = env.NanopbProto("myproto")
+
+# Link nanopb core to the program
+env.Append(CPPPATH = "$NANOB")
+myprog = env.Program(["myprog.c", myproto, "$NANOPB/pb_encode.c", "$NANOPB/pb_decode.c"])
+
+Configuration options
+---------------------
+Normally, this script is used in the test environment of nanopb and it locates
+the nanopb generator by a relative path. If this script is used in another
+application, the path to nanopb root directory has to be defined:
+
+env.SetDefault(NANOPB = "path/to/nanopb")
+
+Additionally, the path to protoc and the options to give to protoc can be
+defined manually:
+
+env.SetDefault(PROTOC = "path/to/protoc")
+env.SetDefault(PROTOCFLAGS = "--plugin=protoc-gen-nanopb=path/to/protoc-gen-nanopb")
+'''
+
+import SCons.Action
+import SCons.Builder
+import SCons.Util
+import os.path
+
+class NanopbWarning(SCons.Warnings.Warning):
+    pass
+SCons.Warnings.enableWarningClass(NanopbWarning)
+
+def _detect_nanopb(env):
+    '''Find the path to nanopb root directory.'''
+    if env.has_key('NANOPB'):
+        # Use nanopb dir given by user
+        return env['NANOPB']
+    
+    p = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..'))
+    if os.path.isdir(p) and os.path.isfile(os.path.join(p, 'pb.h')):
+        # Assume we are running under tests/site_scons/site_tools
+        return p
+    
+    raise SCons.Errors.StopError(NanopbWarning,
+        "Could not find the nanopb root directory")
+
+def _detect_protoc(env):
+    '''Find the path to the protoc compiler.'''
+    if env.has_key('PROTOC'):
+        # Use protoc defined by user
+        return env['PROTOC']
+    
+    p = _detect_nanopb(env)
+    p1 = os.path.join(p, 'generator-bin', 'protoc')
+    if os.path.exists(p1):
+        # Use protoc bundled with binary package
+        return p1
+    
+    p = env.WhereIs('protoc')
+    if p:
+        # Use protoc from path
+        return p
+    
+    raise SCons.Errors.StopError(NanopbWarning,
+        "Could not find the protoc compiler")
+
+def _detect_protocflags(env):
+    '''Find the options to use for protoc.'''
+    if env.has_key('PROTOCFLAGS'):
+        return env['PROTOCFLAGS']
+    
+    p = _detect_protoc(env)
+    n = _detect_nanopb(env)
+    if p == os.path.join(n, 'generator-bin', 'protoc'):
+        # Using the bundled protoc, no options needed
+        return ''
+    
+    e = env['ESCAPE']
+    if env['PLATFORM'] == 'win32':
+        return e('--plugin=protoc-gen-nanopb=' + os.path.join(n, 'generator', 'protoc-gen-nanopb.bat'))
+    else:
+        return e('--plugin=protoc-gen-nanopb=' + os.path.join(n, 'generator', 'protoc-gen-nanopb'))
+
+def _nanopb_proto_actions(source, target, env, for_signature):
+    esc = env['ESCAPE']
+    dirs = ' '.join(['-I' + esc(env.GetBuildPath(d)) for d in env['PROTOCPATH']])
+    return '$PROTOC $PROTOCFLAGS %s --nanopb_out=. %s' % (dirs, esc(str(source[0])))
+
+def _nanopb_proto_emitter(target, source, env):
+    basename = os.path.splitext(str(source[0]))[0]
+    target.append(basename + '.pb.h')
+    
+    if os.path.exists(basename + '.options'):
+        source.append(basename + '.options')
+    
+    return target, source
+
+_nanopb_proto_builder = SCons.Builder.Builder(
+    generator = _nanopb_proto_actions,
+    suffix = '.pb.c',
+    src_suffix = '.proto',
+    emitter = _nanopb_proto_emitter)
+       
+def generate(env):
+    '''Add Builder for nanopb protos.'''
+    
+    env['NANOPB'] = _detect_nanopb(env)
+    env['PROTOC'] = _detect_protoc(env)
+    env['PROTOCFLAGS'] = _detect_protocflags(env)
+    
+    env.SetDefault(PROTOCPATH = ['.', os.path.join(env['NANOPB'], 'generator', 'proto')])
+    
+    env.SetDefault(NANOPB_PROTO_CMD = '$PROTOC $PROTOC_OPTS --nanopb_out=. $SOURCE')
+    env['BUILDERS']['NanopbProto'] = _nanopb_proto_builder
+    
+def exists(env):
+    return _detect_protoc(env) and _detect_protoc_opts(env)
+
index 05dccae..2309cf2 100644 (file)
@@ -2,6 +2,5 @@
 
 Import('env')
 
-env.Proto("funny-proto+name has.characters.proto")
-env.Nanopb("funny-proto+name has.characters.pb.c", "funny-proto+name has.characters.pb")
+env.NanopbProto("funny-proto+name has.characters.proto")
 env.Object("funny-proto+name has.characters.pb.c")