blob: f6add156f212f3ef9fdf144319798f7f69a30b26 [file] [log] [blame]
Daniel Dunbar62b94392013-02-12 19:28:51 +00001# -*- Python -*- vim: set syntax=python tabstop=4 expandtab cc=80:
Daniel Dunbar42ea4632010-09-15 03:57:04 +00002
3# Configuration file for the 'lit' test runner.
4
5import os
Michael J. Spencerf5799be2010-12-10 19:47:54 +00006import sys
Daniel Dunbar42ea4632010-09-15 03:57:04 +00007import platform
8import tempfile
9import signal
10import subprocess
Howard Hinnant3778f272013-01-14 17:12:54 +000011import errno
12import time
Daniel Dunbar62b94392013-02-12 19:28:51 +000013import shlex
Daniel Dunbar42ea4632010-09-15 03:57:04 +000014
Daniel Dunbar4a381292013-08-09 14:44:11 +000015import lit.Test
16import lit.formats
17import lit.util
18
Daniel Dunbarf51f0312013-02-05 21:03:25 +000019# FIXME: For now, this is cribbed from lit.TestRunner, to avoid introducing a
20# dependency there. What we more ideally would like to do is lift the "xfail"
21# and "requires" handling to be a core lit framework feature.
22def isExpectedFail(test, xfails):
23 # Check if any of the xfails match an available feature or the target.
24 for item in xfails:
25 # If this is the wildcard, it always fails.
26 if item == '*':
27 return True
28
Daniel Dunbarba65d612013-02-06 00:04:52 +000029 # If this is a part of any of the features, it fails.
30 for feature in test.config.available_features:
31 if item in feature:
32 return True
Daniel Dunbarf51f0312013-02-05 21:03:25 +000033
34 # If this is a part of the target triple, it fails.
35 if item in test.suite.config.target_triple:
36 return True
37
38 return False
39
Daniel Dunbar42ea4632010-09-15 03:57:04 +000040class LibcxxTestFormat(lit.formats.FileBasedTest):
41 """
42 Custom test format handler for use with the test format use by libc++.
43
44 Tests fall into two categories:
45 FOO.pass.cpp - Executable test which should compile, run, and exit with
46 code 0.
47 FOO.fail.cpp - Negative test case which is expected to fail compilation.
48 """
49
Daniel Dunbar84958712013-02-05 18:03:49 +000050 def __init__(self, cxx_under_test, cpp_flags, ld_flags, exec_env):
Daniel Dunbarbc9a8482010-09-15 04:11:29 +000051 self.cxx_under_test = cxx_under_test
Daniel Dunbar5f09d9e02010-09-15 04:31:58 +000052 self.cpp_flags = list(cpp_flags)
53 self.ld_flags = list(ld_flags)
Daniel Dunbar84958712013-02-05 18:03:49 +000054 self.exec_env = dict(exec_env)
Daniel Dunbar42ea4632010-09-15 03:57:04 +000055
Howard Hinnant3778f272013-01-14 17:12:54 +000056 def execute_command(self, command, in_dir=None):
57 kwargs = {
58 'stdin' :subprocess.PIPE,
59 'stdout':subprocess.PIPE,
60 'stderr':subprocess.PIPE,
61 }
62 if in_dir:
63 kwargs['cwd'] = in_dir
64 p = subprocess.Popen(command, **kwargs)
Daniel Dunbar42ea4632010-09-15 03:57:04 +000065 out,err = p.communicate()
66 exitCode = p.wait()
67
68 # Detect Ctrl-C in subprocess.
69 if exitCode == -signal.SIGINT:
70 raise KeyboardInterrupt
71
72 return out, err, exitCode
73
74 def execute(self, test, lit_config):
Howard Hinnant3778f272013-01-14 17:12:54 +000075 while True:
76 try:
77 return self._execute(test, lit_config)
78 except OSError, oe:
79 if oe.errno != errno.ETXTBSY:
80 raise
81 time.sleep(0.1)
82
83 def _execute(self, test, lit_config):
Daniel Dunbarf51f0312013-02-05 21:03:25 +000084 # Extract test metadata from the test file.
85 xfails = []
86 requires = []
87 with open(test.getSourcePath()) as f:
88 for ln in f:
89 if 'XFAIL:' in ln:
90 items = ln[ln.index('XFAIL:') + 6:].split(',')
91 xfails.extend([s.strip() for s in items])
92 elif 'REQUIRES:' in ln:
93 items = ln[ln.index('REQUIRES:') + 9:].split(',')
94 requires.extend([s.strip() for s in items])
95 elif not ln.startswith("//") and ln.strip():
96 # Stop at the first non-empty line that is not a C++
97 # comment.
98 break
99
100 # Check that we have the required features.
101 #
102 # FIXME: For now, this is cribbed from lit.TestRunner, to avoid
103 # introducing a dependency there. What we more ideally would like to do
Daniel Dunbar62b94392013-02-12 19:28:51 +0000104 # is lift the "xfail" and "requires" handling to be a core lit
105 # framework feature.
Daniel Dunbarf51f0312013-02-05 21:03:25 +0000106 missing_required_features = [f for f in requires
107 if f not in test.config.available_features]
108 if missing_required_features:
109 return (lit.Test.UNSUPPORTED,
110 "Test requires the following features: %s" % (
111 ', '.join(missing_required_features),))
112
113 # Determine if this test is an expected failure.
114 isXFail = isExpectedFail(test, xfails)
115
116 # Evaluate the test.
117 result, report = self._evaluate_test(test, lit_config)
118
119 # Convert the test result based on whether this is an expected failure.
120 if isXFail:
121 if result != lit.Test.FAIL:
122 report += "\n\nTest was expected to FAIL, but did not.\n"
123 result = lit.Test.XPASS
124 else:
125 result = lit.Test.XFAIL
126
127 return result, report
128
129 def _evaluate_test(self, test, lit_config):
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000130 name = test.path_in_suite[-1]
131 source_path = test.getSourcePath()
Howard Hinnant3778f272013-01-14 17:12:54 +0000132 source_dir = os.path.dirname(source_path)
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000133
134 # Check what kind of test this is.
135 assert name.endswith('.pass.cpp') or name.endswith('.fail.cpp')
136 expected_compile_fail = name.endswith('.fail.cpp')
137
138 # If this is a compile (failure) test, build it and check for failure.
139 if expected_compile_fail:
140 cmd = [self.cxx_under_test, '-c',
Daniel Dunbar5f09d9e02010-09-15 04:31:58 +0000141 '-o', '/dev/null', source_path] + self.cpp_flags
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000142 out, err, exitCode = self.execute_command(cmd)
143 if exitCode == 1:
144 return lit.Test.PASS, ""
145 else:
146 report = """Command: %s\n""" % ' '.join(["'%s'" % a
147 for a in cmd])
148 report += """Exit Code: %d\n""" % exitCode
149 if out:
150 report += """Standard Output:\n--\n%s--""" % out
151 if err:
152 report += """Standard Error:\n--\n%s--""" % err
153 report += "\n\nExpected compilation to fail!"
Daniel Dunbar5f09d9e02010-09-15 04:31:58 +0000154 return lit.Test.FAIL, report
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000155 else:
156 exec_file = tempfile.NamedTemporaryFile(suffix="exe", delete=False)
157 exec_path = exec_file.name
158 exec_file.close()
159
160 try:
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000161 compile_cmd = [self.cxx_under_test, '-o', exec_path,
Daniel Dunbar5f09d9e02010-09-15 04:31:58 +0000162 source_path] + self.cpp_flags + self.ld_flags
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000163 cmd = compile_cmd
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000164 out, err, exitCode = self.execute_command(cmd)
165 if exitCode != 0:
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000166 report = """Command: %s\n""" % ' '.join(["'%s'" % a
167 for a in cmd])
168 report += """Exit Code: %d\n""" % exitCode
169 if out:
170 report += """Standard Output:\n--\n%s--""" % out
171 if err:
172 report += """Standard Error:\n--\n%s--""" % err
173 report += "\n\nCompilation failed unexpectedly!"
174 return lit.Test.FAIL, report
175
Daniel Dunbar84958712013-02-05 18:03:49 +0000176 cmd = []
177 if self.exec_env:
178 cmd.append('env')
179 cmd.extend('%s=%s' % (name, value)
180 for name,value in self.exec_env.items())
181 cmd.append(exec_path)
Howard Hinnantc1a45fb2012-08-02 18:36:47 +0000182 if lit_config.useValgrind:
183 cmd = lit_config.valgrindArgs + cmd
Howard Hinnant3778f272013-01-14 17:12:54 +0000184 out, err, exitCode = self.execute_command(cmd, source_dir)
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000185 if exitCode != 0:
Daniel Dunbar62b94392013-02-12 19:28:51 +0000186 report = """Compiled With: %s\n""" % \
187 ' '.join(["'%s'" % a for a in compile_cmd])
188 report += """Command: %s\n""" % \
189 ' '.join(["'%s'" % a for a in cmd])
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000190 report += """Exit Code: %d\n""" % exitCode
191 if out:
192 report += """Standard Output:\n--\n%s--""" % out
193 if err:
194 report += """Standard Error:\n--\n%s--""" % err
195 report += "\n\nCompiled test failed unexpectedly!"
196 return lit.Test.FAIL, report
197 finally:
198 try:
199 os.remove(exec_path)
200 except:
201 pass
202 return lit.Test.PASS, ""
203
204# name: The name of this test suite.
205config.name = 'libc++'
206
207# suffixes: A list of file extensions to treat as test files.
208config.suffixes = ['.cpp']
209
210# test_source_root: The root path where tests are located.
211config.test_source_root = os.path.dirname(__file__)
212
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000213# Gather various compiler parameters.
Daniel Dunbar4a381292013-08-09 14:44:11 +0000214cxx_under_test = lit_config.params.get('cxx_under_test', None)
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000215if cxx_under_test is None:
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000216 cxx_under_test = getattr(config, 'cxx_under_test', None)
Daniel Dunbar05abe932013-02-06 20:24:23 +0000217
218 # If no specific cxx_under_test was given, attempt to infer it as clang++.
219 clangxx = lit.util.which('clang++', config.environment['PATH'])
220 if clangxx is not None:
221 cxx_under_test = clangxx
Daniel Dunbar4a381292013-08-09 14:44:11 +0000222 lit_config.note("inferred cxx_under_test as: %r" % (cxx_under_test,))
Daniel Dunbar05abe932013-02-06 20:24:23 +0000223if cxx_under_test is None:
Daniel Dunbar4a381292013-08-09 14:44:11 +0000224 lit_config.fatal('must specify user parameter cxx_under_test '
225 '(e.g., --param=cxx_under_test=clang++)')
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000226
Daniel Dunbar4a381292013-08-09 14:44:11 +0000227libcxx_src_root = lit_config.params.get('libcxx_src_root', None)
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000228if libcxx_src_root is None:
229 libcxx_src_root = getattr(config, 'libcxx_src_root', None)
230 if libcxx_src_root is None:
231 libcxx_src_root = os.path.dirname(config.test_source_root)
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000232
Daniel Dunbar4a381292013-08-09 14:44:11 +0000233libcxx_obj_root = lit_config.params.get('libcxx_obj_root', None)
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000234if libcxx_obj_root is None:
235 libcxx_obj_root = getattr(config, 'libcxx_obj_root', None)
236 if libcxx_obj_root is None:
237 libcxx_obj_root = libcxx_src_root
238
Daniel Dunbar4a381292013-08-09 14:44:11 +0000239cxx_has_stdcxx0x_flag_str = lit_config.params.get('cxx_has_stdcxx0x_flag', None)
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000240if cxx_has_stdcxx0x_flag_str is not None:
Daniel Dunbar84958712013-02-05 18:03:49 +0000241 if cxx_has_stdcxx0x_flag_str.lower() in ('1', 'true'):
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000242 cxx_has_stdcxx0x_flag = True
Daniel Dunbar84958712013-02-05 18:03:49 +0000243 elif cxx_has_stdcxx0x_flag_str.lower() in ('', '0', 'false'):
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000244 cxx_has_stdcxx0x_flag = False
245 else:
Daniel Dunbar4a381292013-08-09 14:44:11 +0000246 lit_config.fatal(
247 'user parameter cxx_has_stdcxx0x_flag_str should be 0 or 1')
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000248else:
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000249 cxx_has_stdcxx0x_flag = getattr(config, 'cxx_has_stdcxx0x_flag', True)
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000250
Daniel Dunbar84958712013-02-05 18:03:49 +0000251# This test suite supports testing against either the system library or the
252# locally built one; the former mode is useful for testing ABI compatibility
Daniel Dunbar51789422013-02-06 17:47:08 +0000253# between the current headers and a shipping dynamic library.
Daniel Dunbar4a381292013-08-09 14:44:11 +0000254use_system_lib_str = lit_config.params.get('use_system_lib', None)
Daniel Dunbar84958712013-02-05 18:03:49 +0000255if use_system_lib_str is not None:
256 if use_system_lib_str.lower() in ('1', 'true'):
257 use_system_lib = True
258 elif use_system_lib_str.lower() in ('', '0', 'false'):
259 use_system_lib = False
260 else:
Daniel Dunbar4a381292013-08-09 14:44:11 +0000261 lit_config.fatal('user parameter use_system_lib should be 0 or 1')
Daniel Dunbar84958712013-02-05 18:03:49 +0000262else:
Daniel Dunbar51789422013-02-06 17:47:08 +0000263 # Default to testing against the locally built libc++ library.
264 use_system_lib = False
Daniel Dunbar4a381292013-08-09 14:44:11 +0000265 lit_config.note("inferred use_system_lib as: %r" % (use_system_lib,))
Daniel Dunbar84958712013-02-05 18:03:49 +0000266
Daniel Dunbar62b94392013-02-12 19:28:51 +0000267link_flags = []
Daniel Dunbar4a381292013-08-09 14:44:11 +0000268link_flags_str = lit_config.params.get('link_flags', None)
Daniel Dunbar62b94392013-02-12 19:28:51 +0000269if link_flags_str is None:
270 link_flags_str = getattr(config, 'link_flags', None)
271 if link_flags_str is None:
272 if sys.platform == 'darwin':
273 link_flags += ['-lSystem']
274 elif sys.platform == 'linux2':
275 link_flags += ['-lsupc++', '-lgcc_eh', '-lc', '-lm', '-lpthread',
276 '-lrt', '-lgcc_s']
277 else:
Daniel Dunbar4a381292013-08-09 14:44:11 +0000278 lit_config.fatal("unrecognized system")
279 lit_config.note("inferred link_flags as: %r" % (link_flags,))
Daniel Dunbar62b94392013-02-12 19:28:51 +0000280if not link_flags_str is None:
281 link_flags += shlex.split(link_flags_str)
282
Chandler Carruthce395a92011-01-23 01:05:20 +0000283# Configure extra compiler flags.
Daniel Dunbar62b94392013-02-12 19:28:51 +0000284include_paths = ['-I' + libcxx_src_root + '/include',
285 '-I' + libcxx_src_root + '/test/support']
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000286library_paths = ['-L' + libcxx_obj_root + '/lib']
Chandler Carruthce395a92011-01-23 01:05:20 +0000287compile_flags = []
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000288if cxx_has_stdcxx0x_flag:
Daniel Dunbar84958712013-02-05 18:03:49 +0000289 compile_flags += ['-std=c++0x']
Chandler Carruthce395a92011-01-23 01:05:20 +0000290
Daniel Dunbar62b94392013-02-12 19:28:51 +0000291# Configure extra linker parameters.
Daniel Dunbar84958712013-02-05 18:03:49 +0000292exec_env = {}
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000293if sys.platform == 'darwin':
Daniel Dunbar84958712013-02-05 18:03:49 +0000294 if not use_system_lib:
295 exec_env['DYLD_LIBRARY_PATH'] = os.path.join(libcxx_obj_root, 'lib')
296elif sys.platform == 'linux2':
Daniel Dunbard2d614c2013-02-06 17:45:53 +0000297 if not use_system_lib:
Daniel Dunbar62b94392013-02-12 19:28:51 +0000298 link_flags += ['-Wl,-R', libcxx_obj_root + '/lib']
299 compile_flags += ['-D__STDC_FORMAT_MACROS', '-D__STDC_LIMIT_MACROS',
300 '-D__STDC_CONSTANT_MACROS']
Daniel Dunbar84958712013-02-05 18:03:49 +0000301else:
Daniel Dunbar4a381292013-08-09 14:44:11 +0000302 lit_config.fatal("unrecognized system")
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000303
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000304config.test_format = LibcxxTestFormat(
305 cxx_under_test,
306 cpp_flags = ['-nostdinc++'] + compile_flags + include_paths,
Daniel Dunbar62b94392013-02-12 19:28:51 +0000307 ld_flags = ['-nodefaultlibs'] + library_paths + ['-lc++'] + link_flags,
Daniel Dunbar84958712013-02-05 18:03:49 +0000308 exec_env = exec_env)
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000309
Daniel Dunbarb6354a02013-02-05 22:28:03 +0000310# Get or infer the target triple.
Daniel Dunbar4a381292013-08-09 14:44:11 +0000311config.target_triple = lit_config.params.get('target_triple', None)
Daniel Dunbarb6354a02013-02-05 22:28:03 +0000312# If no target triple was given, try to infer it from the compiler under test.
313if config.target_triple is None:
314 config.target_triple = lit.util.capture(
315 [cxx_under_test, '-dumpmachine']).strip()
Daniel Dunbar4a381292013-08-09 14:44:11 +0000316 lit_config.note("inferred target_triple as: %r" % (config.target_triple,))
Daniel Dunbar582c97d2013-02-05 21:43:30 +0000317
318# Write an "available feature" that combines the triple when use_system_lib is
319# enabled. This is so that we can easily write XFAIL markers for tests that are
320# known to fail with versions of libc++ as were shipped with a particular
321# triple.
322if use_system_lib:
323 config.available_features.add('with_system_lib=%s' % (
324 config.target_triple,))