5 # Make terminal colors work on windows
11 def add_nanopb_builders(env):
12 '''Add the necessary builder commands for nanopb tests.'''
14 # Build command for building .pb from .proto using protoc
15 def proto_actions(source, target, env, for_signature):
16 dirs = ' '.join(['-I' + env.GetBuildPath(d) for d in env['PROTOCPATH']])
17 return '$PROTOC $PROTOCFLAGS %s -o%s %s' % (dirs, target[0], source[0])
19 proto_file_builder = Builder(generator = proto_actions,
21 src_suffix = '.proto')
22 env.Append(BUILDERS = {'Proto': proto_file_builder})
23 env.SetDefault(PROTOC = 'protoc')
24 env.SetDefault(PROTOCPATH = ['.'])
26 # Build command for running nanopb generator
28 def nanopb_targets(target, source, env):
29 basename = os.path.splitext(str(source[0]))[0]
30 target.append(basename + '.pb.h')
33 nanopb_file_builder = Builder(action = '$NANOPB_GENERATOR $NANOPB_FLAGS $SOURCE',
36 emitter = nanopb_targets)
37 env.Append(BUILDERS = {'Nanopb': nanopb_file_builder})
38 env.SetDefault(NANOPB_GENERATOR = 'python ' + env.GetBuildPath("#../generator/nanopb_generator.py"))
39 env.SetDefault(NANOPB_FLAGS = '-q')
41 # Combined method to run both protoc and nanopb generator
42 def run_protoc_and_nanopb(env, source):
43 b1 = env.Proto(source)
44 b2 = env.Nanopb(source)
46 env.AddMethod(run_protoc_and_nanopb, "NanopbProto")
48 # Build command that runs a test program and saves the output
49 def run_test(target, source, env):
51 infile = open(str(source[1]))
55 pipe = subprocess.Popen(str(source[0]),
57 stdout = open(str(target[0]), 'w'),
61 print '\033[32m[ OK ]\033[0m Ran ' + str(source[0])
63 print '\033[31m[FAIL]\033[0m Program ' + str(source[0]) + ' returned ' + str(result)
66 run_test_builder = Builder(action = run_test,
68 env.Append(BUILDERS = {'RunTest': run_test_builder})
70 # Build command that decodes a message using protoc
71 def decode_actions(source, target, env, for_signature):
72 dirs = ' '.join(['-I' + env.GetBuildPath(d) for d in env['PROTOCPATH']])
73 return '$PROTOC $PROTOCFLAGS %s --decode=%s %s <%s >%s' % (dirs, env['MESSAGE'], source[1], source[0], target[0])
75 decode_builder = Builder(generator = decode_actions,
77 env.Append(BUILDERS = {'Decode': decode_builder})
79 # Build command that asserts that two files be equal
80 def compare_files(target, source, env):
81 data1 = open(str(source[0]), 'rb').read()
82 data2 = open(str(source[1]), 'rb').read()
84 print '\033[32m[ OK ]\033[0m Files equal: ' + str(source[0]) + ' and ' + str(source[1])
87 print '\033[31m[FAIL]\033[0m Files differ: ' + str(source[0]) + ' and ' + str(source[1])
90 compare_builder = Builder(action = compare_files,
92 env.Append(BUILDERS = {'Compare': compare_builder})