summaryrefslogtreecommitdiffstats
path: root/libs/nanopb/tests/site_scons
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-06-20 10:24:05 +0000
committerRomain Forlot <romain.forlot@iot.bzh>2017-06-20 10:24:05 +0000
commit32e25cbca210a359b09768537b6f443fe90a3070 (patch)
tree3309794c15d8a8f8e9c1c08cad072ee1378813ba /libs/nanopb/tests/site_scons
parent76c43dec62b2e21cd6446360c00d4fe6b437533f (diff)
Separation Generator to a dedicated repo
Change-Id: Id94831651c3266861435272a6e36c7884bef2c45 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'libs/nanopb/tests/site_scons')
-rw-r--r--libs/nanopb/tests/site_scons/site_init.py109
-rw-r--r--libs/nanopb/tests/site_scons/site_tools/nanopb.py126
2 files changed, 235 insertions, 0 deletions
diff --git a/libs/nanopb/tests/site_scons/site_init.py b/libs/nanopb/tests/site_scons/site_init.py
new file mode 100644
index 00000000..da5f6d65
--- /dev/null
+++ b/libs/nanopb/tests/site_scons/site_init.py
@@ -0,0 +1,109 @@
+import subprocess
+import sys
+import re
+
+try:
+ # Make terminal colors work on windows
+ import colorama
+ colorama.init()
+except ImportError:
+ pass
+
+def add_nanopb_builders(env):
+ '''Add the necessary builder commands for nanopb tests.'''
+
+ # Build command that runs a test program and saves the output
+ def run_test(target, source, env):
+ if len(source) > 1:
+ infile = open(str(source[1]))
+ else:
+ infile = None
+
+ if env.has_key("COMMAND"):
+ args = [env["COMMAND"]]
+ else:
+ args = [str(source[0])]
+
+ if env.has_key('ARGS'):
+ args.extend(env['ARGS'])
+
+ print 'Command line: ' + str(args)
+ pipe = subprocess.Popen(args,
+ stdin = infile,
+ stdout = open(str(target[0]), 'w'),
+ stderr = sys.stderr)
+ result = pipe.wait()
+ if result == 0:
+ print '\033[32m[ OK ]\033[0m Ran ' + args[0]
+ else:
+ print '\033[31m[FAIL]\033[0m Program ' + args[0] + ' returned ' + str(result)
+ return result
+
+ run_test_builder = Builder(action = run_test,
+ suffix = '.output')
+ env.Append(BUILDERS = {'RunTest': run_test_builder})
+
+ # Build command that decodes a message using protoc
+ def decode_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 --decode=%s %s <%s >%s' % (
+ dirs, env['MESSAGE'], esc(str(source[1])), esc(str(source[0])), esc(str(target[0])))
+
+ decode_builder = Builder(generator = decode_actions,
+ suffix = '.decoded')
+ env.Append(BUILDERS = {'Decode': decode_builder})
+
+ # Build command that encodes a message using protoc
+ def encode_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 --encode=%s %s <%s >%s' % (
+ dirs, env['MESSAGE'], esc(str(source[1])), esc(str(source[0])), esc(str(target[0])))
+
+ encode_builder = Builder(generator = encode_actions,
+ suffix = '.encoded')
+ env.Append(BUILDERS = {'Encode': encode_builder})
+
+ # Build command that asserts that two files be equal
+ def compare_files(target, source, env):
+ data1 = open(str(source[0]), 'rb').read()
+ data2 = open(str(source[1]), 'rb').read()
+ if data1 == data2:
+ print '\033[32m[ OK ]\033[0m Files equal: ' + str(source[0]) + ' and ' + str(source[1])
+ return 0
+ else:
+ print '\033[31m[FAIL]\033[0m Files differ: ' + str(source[0]) + ' and ' + str(source[1])
+ return 1
+
+ compare_builder = Builder(action = compare_files,
+ suffix = '.equal')
+ env.Append(BUILDERS = {'Compare': compare_builder})
+
+ # Build command that checks that each pattern in source2 is found in source1.
+ def match_files(target, source, env):
+ data = open(str(source[0]), 'rU').read()
+ patterns = open(str(source[1]))
+ for pattern in patterns:
+ if pattern.strip():
+ invert = False
+ if pattern.startswith('! '):
+ invert = True
+ pattern = pattern[2:]
+
+ status = re.search(pattern.strip(), data, re.MULTILINE)
+
+ if not status and not invert:
+ print '\033[31m[FAIL]\033[0m Pattern not found in ' + str(source[0]) + ': ' + pattern
+ return 1
+ elif status and invert:
+ print '\033[31m[FAIL]\033[0m Pattern should not exist, but does in ' + str(source[0]) + ': ' + pattern
+ return 1
+ else:
+ print '\033[32m[ OK ]\033[0m All patterns found in ' + str(source[0])
+ return 0
+
+ match_builder = Builder(action = match_files, suffix = '.matched')
+ env.Append(BUILDERS = {'Match': match_builder})
+
+
diff --git a/libs/nanopb/tests/site_scons/site_tools/nanopb.py b/libs/nanopb/tests/site_scons/site_tools/nanopb.py
new file mode 100644
index 00000000..c72a45d3
--- /dev/null
+++ b/libs/nanopb/tests/site_scons/site_tools/nanopb.py
@@ -0,0 +1,126 @@
+'''
+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']
+
+ n = _detect_nanopb(env)
+ p1 = os.path.join(n, 'generator-bin', 'protoc' + env['PROGSUFFIX'])
+ if os.path.exists(p1):
+ # Use protoc bundled with binary package
+ return env['ESCAPE'](p1)
+
+ p = env.WhereIs('protoc')
+ if p:
+ # Use protoc from path
+ return env['ESCAPE'](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)
+ p1 = os.path.join(n, 'generator-bin', 'protoc' + env['PROGSUFFIX'])
+ if p == env['ESCAPE'](p1):
+ # 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 $PROTOCFLAGS --nanopb_out=. $SOURCES')
+ env['BUILDERS']['NanopbProto'] = _nanopb_proto_builder
+
+def exists(env):
+ return _detect_protoc(env) and _detect_protoc_opts(env)
+