Build fixes for Windows/Visual C++
[apps/agl-service-can-low-level.git] / tests / site_scons / site_init.py
1 import subprocess
2 import sys
3
4 try:
5     # Make terminal colors work on windows
6     import colorama
7     colorama.init()
8 except ImportError:
9     pass
10
11 def add_nanopb_builders(env):
12     '''Add the necessary builder commands for nanopb tests.'''
13     
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])
18
19     proto_file_builder = Builder(generator = proto_actions,
20                                  suffix = '.pb',
21                                  src_suffix = '.proto')
22     env.Append(BUILDERS = {'Proto': proto_file_builder})
23     env.SetDefault(PROTOC = 'protoc')
24     env.SetDefault(PROTOCPATH = ['.'])
25
26     # Build command for running nanopb generator
27     import os.path
28     def nanopb_targets(target, source, env):
29         basename = os.path.splitext(str(source[0]))[0]
30         target.append(basename + '.pb.h')
31         return target, source
32
33     nanopb_file_builder = Builder(action = '$NANOPB_GENERATOR $NANOPB_FLAGS $SOURCE',
34                                   suffix = '.pb.c',
35                                   src_suffix = '.pb',
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')
40
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)
45         return b1 + b2
46     env.AddMethod(run_protoc_and_nanopb, "NanopbProto")
47
48     # Build command that runs a test program and saves the output
49     def run_test(target, source, env):
50         if len(source) > 1:
51             infile = open(str(source[1]))
52         else:
53             infile = None
54         
55         pipe = subprocess.Popen(str(source[0]),
56                                 stdin = infile,
57                                 stdout = open(str(target[0]), 'w'),
58                                 stderr = sys.stderr)
59         result = pipe.wait()
60         if result == 0:
61             print '\033[32m[ OK ]\033[0m   Ran ' + str(source[0])
62         else:
63             print '\033[31m[FAIL]\033[0m   Program ' + str(source[0]) + ' returned ' + str(result)
64         return result
65         
66     run_test_builder = Builder(action = run_test,
67                                suffix = '.output')
68     env.Append(BUILDERS = {'RunTest': run_test_builder})
69
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])
74
75     decode_builder = Builder(generator = decode_actions,
76                              suffix = '.decoded')
77     env.Append(BUILDERS = {'Decode': decode_builder})    
78
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()
83         if data1 == data2:
84             print '\033[32m[ OK ]\033[0m   Files equal: ' + str(source[0]) + ' and ' + str(source[1])
85             return 0
86         else:
87             print '\033[31m[FAIL]\033[0m   Files differ: ' + str(source[0]) + ' and ' + str(source[1])
88             return 1
89
90     compare_builder = Builder(action = compare_files,
91                               suffix = '.equal')
92     env.Append(BUILDERS = {'Compare': compare_builder})
93
94