blob: 87f6c8956af8472c88281568a7fe7fee389219a6 [file] [log] [blame]
Daniel Dunbar42ea4632010-09-15 03:57:04 +00001# -*- Python -*-
2
3# Configuration file for the 'lit' test runner.
4
5import os
6import platform
7import tempfile
8import signal
9import subprocess
10
11class LibcxxTestFormat(lit.formats.FileBasedTest):
12 """
13 Custom test format handler for use with the test format use by libc++.
14
15 Tests fall into two categories:
16 FOO.pass.cpp - Executable test which should compile, run, and exit with
17 code 0.
18 FOO.fail.cpp - Negative test case which is expected to fail compilation.
19 """
20
Daniel Dunbarbc9a8482010-09-15 04:11:29 +000021 def __init__(self, cxx_under_test, options):
22 self.cxx_under_test = cxx_under_test
23 self.options = list(options)
Daniel Dunbar42ea4632010-09-15 03:57:04 +000024
25 def execute_command(self, command):
26 p = subprocess.Popen(command, stdin=subprocess.PIPE,
27 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
28 out,err = p.communicate()
29 exitCode = p.wait()
30
31 # Detect Ctrl-C in subprocess.
32 if exitCode == -signal.SIGINT:
33 raise KeyboardInterrupt
34
35 return out, err, exitCode
36
37 def execute(self, test, lit_config):
38 name = test.path_in_suite[-1]
39 source_path = test.getSourcePath()
40
41 # Check what kind of test this is.
42 assert name.endswith('.pass.cpp') or name.endswith('.fail.cpp')
43 expected_compile_fail = name.endswith('.fail.cpp')
44
45 # If this is a compile (failure) test, build it and check for failure.
46 if expected_compile_fail:
47 cmd = [self.cxx_under_test, '-c',
48 '-o', '/dev/null', source_path] + self.options
49 out, err, exitCode = self.execute_command(cmd)
50 if exitCode == 1:
51 return lit.Test.PASS, ""
52 else:
53 report = """Command: %s\n""" % ' '.join(["'%s'" % a
54 for a in cmd])
55 report += """Exit Code: %d\n""" % exitCode
56 if out:
57 report += """Standard Output:\n--\n%s--""" % out
58 if err:
59 report += """Standard Error:\n--\n%s--""" % err
60 report += "\n\nExpected compilation to fail!"
61 return Test.FAIL, report
62 else:
63 exec_file = tempfile.NamedTemporaryFile(suffix="exe", delete=False)
64 exec_path = exec_file.name
65 exec_file.close()
66
67 try:
68 cmd = [self.cxx_under_test, '-o', exec_path,
69 source_path] + self.options
70 out, err, exitCode = self.execute_command(cmd)
71 if exitCode != 0:
Daniel Dunbar42ea4632010-09-15 03:57:04 +000072 report = """Command: %s\n""" % ' '.join(["'%s'" % a
73 for a in cmd])
74 report += """Exit Code: %d\n""" % exitCode
75 if out:
76 report += """Standard Output:\n--\n%s--""" % out
77 if err:
78 report += """Standard Error:\n--\n%s--""" % err
79 report += "\n\nCompilation failed unexpectedly!"
80 return lit.Test.FAIL, report
81
82 cmd = [exec_path]
83 out, err, exitCode = self.execute_command(cmd)
84 if exitCode != 0:
Daniel Dunbar42ea4632010-09-15 03:57:04 +000085 report = """Command: %s\n""" % ' '.join(["'%s'" % a
86 for a in cmd])
87 report += """Exit Code: %d\n""" % exitCode
88 if out:
89 report += """Standard Output:\n--\n%s--""" % out
90 if err:
91 report += """Standard Error:\n--\n%s--""" % err
92 report += "\n\nCompiled test failed unexpectedly!"
93 return lit.Test.FAIL, report
94 finally:
95 try:
96 os.remove(exec_path)
97 except:
98 pass
99 return lit.Test.PASS, ""
100
101# name: The name of this test suite.
102config.name = 'libc++'
103
104# suffixes: A list of file extensions to treat as test files.
105config.suffixes = ['.cpp']
106
107# test_source_root: The root path where tests are located.
108config.test_source_root = os.path.dirname(__file__)
109
110# FIXME: Would be nice to Use -stdlib=libc++ option with Clang's that accept it.
111cxx_under_test = lit.params.get('cxx_under_test', None)
112if cxx_under_test is None:
113 lit.fatal('must specify user parameter cxx_under_test '
114 '(e.g., --param=cxx_under_test=clang++)')
115config.test_format = LibcxxTestFormat(cxx_under_test,
116 ['-nostdinc++',
117 '-I/usr/include/c++/v1',
118 '-nodefaultlibs', '-lc++',
119 '-lSystem'])
120
121config.target_triple = None