blob: 010e20b75c9d0c618cf4c3a713f6c8e961a252da [file] [log] [blame]
Daniel Dunbar42ea4632010-09-15 03:57:04 +00001# -*- Python -*-
2
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 Dunbar42ea4632010-09-15 03:57:04 +000013
Daniel Dunbarf51f0312013-02-05 21:03:25 +000014# FIXME: For now, this is cribbed from lit.TestRunner, to avoid introducing a
15# dependency there. What we more ideally would like to do is lift the "xfail"
16# and "requires" handling to be a core lit framework feature.
17def isExpectedFail(test, xfails):
18 # Check if any of the xfails match an available feature or the target.
19 for item in xfails:
20 # If this is the wildcard, it always fails.
21 if item == '*':
22 return True
23
Daniel Dunbarba65d612013-02-06 00:04:52 +000024 # If this is a part of any of the features, it fails.
25 for feature in test.config.available_features:
26 if item in feature:
27 return True
Daniel Dunbarf51f0312013-02-05 21:03:25 +000028
29 # If this is a part of the target triple, it fails.
30 if item in test.suite.config.target_triple:
31 return True
32
33 return False
34
Daniel Dunbar42ea4632010-09-15 03:57:04 +000035class LibcxxTestFormat(lit.formats.FileBasedTest):
36 """
37 Custom test format handler for use with the test format use by libc++.
38
39 Tests fall into two categories:
40 FOO.pass.cpp - Executable test which should compile, run, and exit with
41 code 0.
42 FOO.fail.cpp - Negative test case which is expected to fail compilation.
43 """
44
Daniel Dunbar84958712013-02-05 18:03:49 +000045 def __init__(self, cxx_under_test, cpp_flags, ld_flags, exec_env):
Daniel Dunbarbc9a8482010-09-15 04:11:29 +000046 self.cxx_under_test = cxx_under_test
Daniel Dunbar5f09d9e02010-09-15 04:31:58 +000047 self.cpp_flags = list(cpp_flags)
48 self.ld_flags = list(ld_flags)
Daniel Dunbar84958712013-02-05 18:03:49 +000049 self.exec_env = dict(exec_env)
Daniel Dunbar42ea4632010-09-15 03:57:04 +000050
Howard Hinnant3778f272013-01-14 17:12:54 +000051 def execute_command(self, command, in_dir=None):
52 kwargs = {
53 'stdin' :subprocess.PIPE,
54 'stdout':subprocess.PIPE,
55 'stderr':subprocess.PIPE,
56 }
57 if in_dir:
58 kwargs['cwd'] = in_dir
59 p = subprocess.Popen(command, **kwargs)
Daniel Dunbar42ea4632010-09-15 03:57:04 +000060 out,err = p.communicate()
61 exitCode = p.wait()
62
63 # Detect Ctrl-C in subprocess.
64 if exitCode == -signal.SIGINT:
65 raise KeyboardInterrupt
66
67 return out, err, exitCode
68
69 def execute(self, test, lit_config):
Howard Hinnant3778f272013-01-14 17:12:54 +000070 while True:
71 try:
72 return self._execute(test, lit_config)
73 except OSError, oe:
74 if oe.errno != errno.ETXTBSY:
75 raise
76 time.sleep(0.1)
77
78 def _execute(self, test, lit_config):
Daniel Dunbarf51f0312013-02-05 21:03:25 +000079 # Extract test metadata from the test file.
80 xfails = []
81 requires = []
82 with open(test.getSourcePath()) as f:
83 for ln in f:
84 if 'XFAIL:' in ln:
85 items = ln[ln.index('XFAIL:') + 6:].split(',')
86 xfails.extend([s.strip() for s in items])
87 elif 'REQUIRES:' in ln:
88 items = ln[ln.index('REQUIRES:') + 9:].split(',')
89 requires.extend([s.strip() for s in items])
90 elif not ln.startswith("//") and ln.strip():
91 # Stop at the first non-empty line that is not a C++
92 # comment.
93 break
94
95 # Check that we have the required features.
96 #
97 # FIXME: For now, this is cribbed from lit.TestRunner, to avoid
98 # introducing a dependency there. What we more ideally would like to do
99 # is lift the "xfail" and "requires" handling to be a core lit framework
100 # feature.
101 missing_required_features = [f for f in requires
102 if f not in test.config.available_features]
103 if missing_required_features:
104 return (lit.Test.UNSUPPORTED,
105 "Test requires the following features: %s" % (
106 ', '.join(missing_required_features),))
107
108 # Determine if this test is an expected failure.
109 isXFail = isExpectedFail(test, xfails)
110
111 # Evaluate the test.
112 result, report = self._evaluate_test(test, lit_config)
113
114 # Convert the test result based on whether this is an expected failure.
115 if isXFail:
116 if result != lit.Test.FAIL:
117 report += "\n\nTest was expected to FAIL, but did not.\n"
118 result = lit.Test.XPASS
119 else:
120 result = lit.Test.XFAIL
121
122 return result, report
123
124 def _evaluate_test(self, test, lit_config):
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000125 name = test.path_in_suite[-1]
126 source_path = test.getSourcePath()
Howard Hinnant3778f272013-01-14 17:12:54 +0000127 source_dir = os.path.dirname(source_path)
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000128
129 # Check what kind of test this is.
130 assert name.endswith('.pass.cpp') or name.endswith('.fail.cpp')
131 expected_compile_fail = name.endswith('.fail.cpp')
132
133 # If this is a compile (failure) test, build it and check for failure.
134 if expected_compile_fail:
135 cmd = [self.cxx_under_test, '-c',
Daniel Dunbar5f09d9e02010-09-15 04:31:58 +0000136 '-o', '/dev/null', source_path] + self.cpp_flags
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000137 out, err, exitCode = self.execute_command(cmd)
138 if exitCode == 1:
139 return lit.Test.PASS, ""
140 else:
141 report = """Command: %s\n""" % ' '.join(["'%s'" % a
142 for a in cmd])
143 report += """Exit Code: %d\n""" % exitCode
144 if out:
145 report += """Standard Output:\n--\n%s--""" % out
146 if err:
147 report += """Standard Error:\n--\n%s--""" % err
148 report += "\n\nExpected compilation to fail!"
Daniel Dunbar5f09d9e02010-09-15 04:31:58 +0000149 return lit.Test.FAIL, report
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000150 else:
151 exec_file = tempfile.NamedTemporaryFile(suffix="exe", delete=False)
152 exec_path = exec_file.name
153 exec_file.close()
154
155 try:
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000156 compile_cmd = [self.cxx_under_test, '-o', exec_path,
Daniel Dunbar5f09d9e02010-09-15 04:31:58 +0000157 source_path] + self.cpp_flags + self.ld_flags
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000158 cmd = compile_cmd
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000159 out, err, exitCode = self.execute_command(cmd)
160 if exitCode != 0:
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000161 report = """Command: %s\n""" % ' '.join(["'%s'" % a
162 for a in cmd])
163 report += """Exit Code: %d\n""" % exitCode
164 if out:
165 report += """Standard Output:\n--\n%s--""" % out
166 if err:
167 report += """Standard Error:\n--\n%s--""" % err
168 report += "\n\nCompilation failed unexpectedly!"
169 return lit.Test.FAIL, report
170
Daniel Dunbar84958712013-02-05 18:03:49 +0000171 cmd = []
172 if self.exec_env:
173 cmd.append('env')
174 cmd.extend('%s=%s' % (name, value)
175 for name,value in self.exec_env.items())
176 cmd.append(exec_path)
Howard Hinnantc1a45fb2012-08-02 18:36:47 +0000177 if lit_config.useValgrind:
178 cmd = lit_config.valgrindArgs + cmd
Howard Hinnant3778f272013-01-14 17:12:54 +0000179 out, err, exitCode = self.execute_command(cmd, source_dir)
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000180 if exitCode != 0:
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000181 report = """Compiled With: %s\n""" % ' '.join(["'%s'" % a
182 for a in compile_cmd])
183 report += """Command: %s\n""" % ' '.join(["'%s'" % a
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000184 for a in cmd])
185 report += """Exit Code: %d\n""" % exitCode
186 if out:
187 report += """Standard Output:\n--\n%s--""" % out
188 if err:
189 report += """Standard Error:\n--\n%s--""" % err
190 report += "\n\nCompiled test failed unexpectedly!"
191 return lit.Test.FAIL, report
192 finally:
193 try:
194 os.remove(exec_path)
195 except:
196 pass
197 return lit.Test.PASS, ""
198
199# name: The name of this test suite.
200config.name = 'libc++'
201
202# suffixes: A list of file extensions to treat as test files.
203config.suffixes = ['.cpp']
204
205# test_source_root: The root path where tests are located.
206config.test_source_root = os.path.dirname(__file__)
207
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000208# Gather various compiler parameters.
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000209cxx_under_test = lit.params.get('cxx_under_test', None)
210if cxx_under_test is None:
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000211 cxx_under_test = getattr(config, 'cxx_under_test', None)
Daniel Dunbar05abe932013-02-06 20:24:23 +0000212
213 # If no specific cxx_under_test was given, attempt to infer it as clang++.
214 clangxx = lit.util.which('clang++', config.environment['PATH'])
215 if clangxx is not None:
216 cxx_under_test = clangxx
217 lit.note("inferred cxx_under_test as: %r" % (cxx_under_test,))
218if cxx_under_test is None:
219 lit.fatal('must specify user parameter cxx_under_test '
220 '(e.g., --param=cxx_under_test=clang++)')
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000221
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000222libcxx_src_root = lit.params.get('libcxx_src_root', None)
223if libcxx_src_root is None:
224 libcxx_src_root = getattr(config, 'libcxx_src_root', None)
225 if libcxx_src_root is None:
226 libcxx_src_root = os.path.dirname(config.test_source_root)
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000227
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000228libcxx_obj_root = lit.params.get('libcxx_obj_root', None)
229if libcxx_obj_root is None:
230 libcxx_obj_root = getattr(config, 'libcxx_obj_root', None)
231 if libcxx_obj_root is None:
232 libcxx_obj_root = libcxx_src_root
233
234cxx_has_stdcxx0x_flag_str = lit.params.get('cxx_has_stdcxx0x_flag', None)
235if cxx_has_stdcxx0x_flag_str is not None:
Daniel Dunbar84958712013-02-05 18:03:49 +0000236 if cxx_has_stdcxx0x_flag_str.lower() in ('1', 'true'):
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000237 cxx_has_stdcxx0x_flag = True
Daniel Dunbar84958712013-02-05 18:03:49 +0000238 elif cxx_has_stdcxx0x_flag_str.lower() in ('', '0', 'false'):
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000239 cxx_has_stdcxx0x_flag = False
240 else:
241 lit.fatal('user parameter cxx_has_stdcxx0x_flag_str should be 0 or 1')
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000242else:
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000243 cxx_has_stdcxx0x_flag = getattr(config, 'cxx_has_stdcxx0x_flag', True)
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000244
Daniel Dunbar84958712013-02-05 18:03:49 +0000245# This test suite supports testing against either the system library or the
246# locally built one; the former mode is useful for testing ABI compatibility
Daniel Dunbar51789422013-02-06 17:47:08 +0000247# between the current headers and a shipping dynamic library.
Daniel Dunbar84958712013-02-05 18:03:49 +0000248use_system_lib_str = lit.params.get('use_system_lib', None)
249if use_system_lib_str is not None:
250 if use_system_lib_str.lower() in ('1', 'true'):
251 use_system_lib = True
252 elif use_system_lib_str.lower() in ('', '0', 'false'):
253 use_system_lib = False
254 else:
255 lit.fatal('user parameter use_system_lib should be 0 or 1')
256else:
Daniel Dunbar51789422013-02-06 17:47:08 +0000257 # Default to testing against the locally built libc++ library.
258 use_system_lib = False
Daniel Dunbar05abe932013-02-06 20:24:23 +0000259 lit.note("inferred use_system_lib as: %r" % (use_system_lib,))
Daniel Dunbar84958712013-02-05 18:03:49 +0000260
Chandler Carruthce395a92011-01-23 01:05:20 +0000261# Configure extra compiler flags.
Howard Hinnantf1e633c2013-01-14 17:07:27 +0000262include_paths = ['-I' + libcxx_src_root + '/include', '-I' + libcxx_src_root + '/test/support']
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000263library_paths = ['-L' + libcxx_obj_root + '/lib']
Chandler Carruthce395a92011-01-23 01:05:20 +0000264compile_flags = []
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000265if cxx_has_stdcxx0x_flag:
Daniel Dunbar84958712013-02-05 18:03:49 +0000266 compile_flags += ['-std=c++0x']
Chandler Carruthce395a92011-01-23 01:05:20 +0000267
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000268# Configure extra libraries.
Daniel Dunbar84958712013-02-05 18:03:49 +0000269exec_env = {}
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000270libraries = []
271if sys.platform == 'darwin':
Daniel Dunbar84958712013-02-05 18:03:49 +0000272 libraries += ['-lSystem']
273 if not use_system_lib:
274 exec_env['DYLD_LIBRARY_PATH'] = os.path.join(libcxx_obj_root, 'lib')
275elif sys.platform == 'linux2':
276 libraries += ['-lsupc++', '-lgcc_eh', '-lc', '-lm', '-lpthread', '-lrt', '-lgcc_s']
Daniel Dunbard2d614c2013-02-06 17:45:53 +0000277 if not use_system_lib:
278 libraries += ['-Wl,-R', libcxx_obj_root + '/lib']
Daniel Dunbar76efb572013-02-08 17:41:28 +0000279 compile_flags += ['-D__STDC_FORMAT_MACROS', '-D__STDC_LIMIT_MACROS', '-D__STDC_CONSTANT_MACROS']
Daniel Dunbar84958712013-02-05 18:03:49 +0000280else:
281 lit.fatal("unrecognized system")
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000282
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000283config.test_format = LibcxxTestFormat(
284 cxx_under_test,
285 cpp_flags = ['-nostdinc++'] + compile_flags + include_paths,
Daniel Dunbar84958712013-02-05 18:03:49 +0000286 ld_flags = ['-nodefaultlibs'] + library_paths + ['-lc++'] + libraries,
287 exec_env = exec_env)
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000288
Daniel Dunbarb6354a02013-02-05 22:28:03 +0000289# Get or infer the target triple.
290config.target_triple = lit.params.get('target_triple', None)
291# If no target triple was given, try to infer it from the compiler under test.
292if config.target_triple is None:
293 config.target_triple = lit.util.capture(
294 [cxx_under_test, '-dumpmachine']).strip()
Daniel Dunbar05abe932013-02-06 20:24:23 +0000295 lit.note("inferred target_triple as: %r" % (config.target_triple,))
Daniel Dunbar582c97d2013-02-05 21:43:30 +0000296
297# Write an "available feature" that combines the triple when use_system_lib is
298# enabled. This is so that we can easily write XFAIL markers for tests that are
299# known to fail with versions of libc++ as were shipped with a particular
300# triple.
301if use_system_lib:
302 config.available_features.add('with_system_lib=%s' % (
303 config.target_triple,))