Start moving the tests into subfolders. Transition to SCons for build system for...
[apps/agl-service-can-low-level.git] / tests / SConstruct
1 env = DefaultEnvironment()
2
3 env.Append(CPPPATH = ["#../", "#common"])
4
5 # Build command for building .pb from .proto using protoc
6 def proto_actions(source, target, env, for_signature):
7     dirs = ' '.join(['-I' + env.GetBuildPath(d) for d in env['PROTOCPATH']])
8     return '$PROTOC $PROTOCFLAGS %s -o%s %s' % (dirs, target[0], source[0])
9
10 proto_file_builder = Builder(generator = proto_actions,
11                              suffix = '.pb',
12                              src_suffix = '.proto')
13 env.Append(BUILDERS = {'Proto': proto_file_builder})
14 env.SetDefault(PROTOC = 'protoc')
15
16 # Define the include path to find nanopb.proto
17 env.Append(PROTOCPATH = ['#../generator', '/usr/include', '.'])
18
19 # Build command for running nanopb generator
20 import os.path
21 def nanopb_targets(target, source, env):
22     basename = os.path.splitext(str(source[0]))[0]
23     target.append(basename + '.pb.h')
24     return target, source
25
26 nanopb_file_builder = Builder(action = '$NANOPB_GENERATOR $NANOPB_FLAGS $SOURCE',
27                               suffix = '.pb.c',
28                               src_suffix = '.pb',
29                               emitter = nanopb_targets)
30 env.Append(BUILDERS = {'Nanopb': nanopb_file_builder})
31 env.SetDefault(NANOPB_GENERATOR = 'python ' + env.GetBuildPath("#../generator/nanopb_generator.py"))
32
33 # Combined method to run both protoc and nanopb generator
34 def run_protoc_and_nanopb(env, source):
35     b1 = env.Proto(source)
36     b2 = env.Nanopb(source)
37     return b1 + b2
38 env.AddMethod(run_protoc_and_nanopb, "NanopbProto")
39
40 # Build command that runs a test program and saves the output
41 def run_test_actions(source, target, env, for_signature):
42     cmd = str(source[0]) # Name of binary
43     if len(source) > 1:
44         cmd += ' <' + str(source[1]) # Input file
45     cmd += ' >' + str(target[0])
46     return cmd
47
48 run_test_builder = Builder(generator = run_test_actions,
49                            suffix = '.output')
50 env.Append(BUILDERS = {'RunTest': run_test_builder})
51
52 # Build command that decodes a message using protoc
53 def decode_actions(source, target, env, for_signature):
54     dirs = ' '.join(['-I' + env.GetBuildPath(d) for d in env['PROTOCPATH']])
55     return '$PROTOC $PROTOCFLAGS %s --decode=%s %s <%s >%s' % (dirs, env['MESSAGE'], source[1], source[0], target[0])
56
57 decode_builder = Builder(generator = decode_actions,
58                          suffix = '.decoded')
59 env.Append(BUILDERS = {'Decode': decode_builder})    
60
61 # Build command that asserts that two files be equal
62 def compare_files(target, source, env):
63     data1 = open(str(source[0]), 'rb').read()
64     data2 = open(str(source[1]), 'rb').read()
65     if data1 == data2:
66 #        open(str(target[0]), 'w').write('OK')
67         return 0
68     else:
69         return "Test failed: %s and %s differ!" % (source[0], source[1])
70
71 compare_builder = Builder(action = compare_files,
72                           suffix = '.equal')
73 env.Append(BUILDERS = {'Compare': compare_builder})
74
75 # Now include the SConscript files from all subdirectories
76 SConscript(Glob('*/SConscript'), exports = 'env')
77