blob: a46e142649df2ced48bd048835f24a6d6676f22c [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)
212 if cxx_under_test is None:
213 lit.fatal('must specify user parameter cxx_under_test '
214 '(e.g., --param=cxx_under_test=clang++)')
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000215
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000216libcxx_src_root = lit.params.get('libcxx_src_root', None)
217if libcxx_src_root is None:
218 libcxx_src_root = getattr(config, 'libcxx_src_root', None)
219 if libcxx_src_root is None:
220 libcxx_src_root = os.path.dirname(config.test_source_root)
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000221
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000222libcxx_obj_root = lit.params.get('libcxx_obj_root', None)
223if libcxx_obj_root is None:
224 libcxx_obj_root = getattr(config, 'libcxx_obj_root', None)
225 if libcxx_obj_root is None:
226 libcxx_obj_root = libcxx_src_root
227
228cxx_has_stdcxx0x_flag_str = lit.params.get('cxx_has_stdcxx0x_flag', None)
229if cxx_has_stdcxx0x_flag_str is not None:
Daniel Dunbar84958712013-02-05 18:03:49 +0000230 if cxx_has_stdcxx0x_flag_str.lower() in ('1', 'true'):
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000231 cxx_has_stdcxx0x_flag = True
Daniel Dunbar84958712013-02-05 18:03:49 +0000232 elif cxx_has_stdcxx0x_flag_str.lower() in ('', '0', 'false'):
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000233 cxx_has_stdcxx0x_flag = False
234 else:
235 lit.fatal('user parameter cxx_has_stdcxx0x_flag_str should be 0 or 1')
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000236else:
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000237 cxx_has_stdcxx0x_flag = getattr(config, 'cxx_has_stdcxx0x_flag', True)
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000238
Daniel Dunbar84958712013-02-05 18:03:49 +0000239# This test suite supports testing against either the system library or the
240# locally built one; the former mode is useful for testing ABI compatibility
241# between the current headers and a shipping dynamic library. We require the
242# user to explicitly pick one of the two modes.
243use_system_lib_str = lit.params.get('use_system_lib', None)
244if use_system_lib_str is not None:
245 if use_system_lib_str.lower() in ('1', 'true'):
246 use_system_lib = True
247 elif use_system_lib_str.lower() in ('', '0', 'false'):
248 use_system_lib = False
249 else:
250 lit.fatal('user parameter use_system_lib should be 0 or 1')
251else:
252 use_system_lib = True
253
Chandler Carruthce395a92011-01-23 01:05:20 +0000254# Configure extra compiler flags.
Howard Hinnantf1e633c2013-01-14 17:07:27 +0000255include_paths = ['-I' + libcxx_src_root + '/include', '-I' + libcxx_src_root + '/test/support']
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000256library_paths = ['-L' + libcxx_obj_root + '/lib']
Chandler Carruthce395a92011-01-23 01:05:20 +0000257compile_flags = []
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000258if cxx_has_stdcxx0x_flag:
Daniel Dunbar84958712013-02-05 18:03:49 +0000259 compile_flags += ['-std=c++0x']
Chandler Carruthce395a92011-01-23 01:05:20 +0000260
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000261# Configure extra libraries.
Daniel Dunbar84958712013-02-05 18:03:49 +0000262exec_env = {}
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000263libraries = []
264if sys.platform == 'darwin':
Daniel Dunbar84958712013-02-05 18:03:49 +0000265 libraries += ['-lSystem']
266 if not use_system_lib:
267 exec_env['DYLD_LIBRARY_PATH'] = os.path.join(libcxx_obj_root, 'lib')
268elif sys.platform == 'linux2':
269 libraries += ['-lsupc++', '-lgcc_eh', '-lc', '-lm', '-lpthread', '-lrt', '-lgcc_s']
Daniel Dunbard2d614c2013-02-06 17:45:53 +0000270 if not use_system_lib:
271 libraries += ['-Wl,-R', libcxx_obj_root + '/lib']
Daniel Dunbar84958712013-02-05 18:03:49 +0000272else:
273 lit.fatal("unrecognized system")
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000274
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000275config.test_format = LibcxxTestFormat(
276 cxx_under_test,
277 cpp_flags = ['-nostdinc++'] + compile_flags + include_paths,
Daniel Dunbar84958712013-02-05 18:03:49 +0000278 ld_flags = ['-nodefaultlibs'] + library_paths + ['-lc++'] + libraries,
279 exec_env = exec_env)
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000280
Daniel Dunbarb6354a02013-02-05 22:28:03 +0000281# Get or infer the target triple.
282config.target_triple = lit.params.get('target_triple', None)
283# If no target triple was given, try to infer it from the compiler under test.
284if config.target_triple is None:
285 config.target_triple = lit.util.capture(
286 [cxx_under_test, '-dumpmachine']).strip()
287 lit.note("inferred target triple as: %r" % (config.target_triple,))
Daniel Dunbar582c97d2013-02-05 21:43:30 +0000288
289# Write an "available feature" that combines the triple when use_system_lib is
290# enabled. This is so that we can easily write XFAIL markers for tests that are
291# known to fail with versions of libc++ as were shipped with a particular
292# triple.
293if use_system_lib:
294 config.available_features.add('with_system_lib=%s' % (
295 config.target_triple,))