171578f5934fa5089e7fb59f54569a004dd7ac46
[senf.git] / senfscons / CompileCheck.py
1 import os, os.path, sys
2 from SCons.Script import *
3 import SCons.Scanner.C
4
5 def scanTests(f):
6     tests = {}
7     name = start = None
8     linenr= 0
9     for line in f:
10         linenr += 1
11         if line.startswith('COMPILE_FAIL(') and ')' in line:
12             name = line.split('(',1)[-1].split(')',1)[0]
13             start = linenr
14         elif line.startswith('}') and name and start:
15             tests[name] = (start, linenr)
16     return tests
17             
18 def CompileCheck(target, source, env):
19     tests = scanTests(file(source[0].abspath))
20     errf = os.popen(env.subst('$CXXCOM -DCOMPILE_CHECK 2>&1', source=source, target=target))
21     passedTests = {}
22     for error in errf:
23         elts = error.split(':',2)
24         if len(elts) != 3 : continue
25         filename, line, message = elts
26         if not os.path.exists(filename) : continue
27         try: line = int(line)
28         except ValueError : continue
29         filename = os.path.abspath(filename)
30         if filename != source[0].abspath : continue
31
32         for name,lines in tests.iteritems():
33             if line >= lines[0] and line <= lines[1]:
34                 passedTests[name] = 1
35                 print "Passed test '%s': %s" % (name, message.strip())
36     failedTests = set(tests.iterkeys()) - set(passedTests.iterkeys())
37     if failedTests:
38         for test in failedTests:
39             print "Test '%s' FAILED" % test
40         print
41         print "*** %d tests FAILED" % len(failedTests)
42         if os.path.exists(target[0].abspath):
43             os.unlink(target[0].abspath)
44         return 1
45     file(target[0].abspath,"w").close()
46     return 0
47
48 CompileCheck = Action(CompileCheck)
49
50 def generate(env):
51
52     builder = env.Builder(
53         action = CompileCheck,
54         suffix = '.tsto',
55         src_suffix = '.cc',
56         source_scanner = SCons.Scanner.C.CScanner(),
57         single_source=1
58     )
59
60     env.Append(BUILDERS = { 'CompileCheck': builder })
61
62 def exists(env):
63     return True