blob: 5e64e54de35f672605444d85044a023b55d2899d [file] [log] [blame]
Daniel Dunbar3bc6a982013-02-12 19:28:51 +00001# -*- Python -*- vim: set syntax=python tabstop=4 expandtab cc=80:
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +00002
3# Configuration file for the 'lit' test runner.
4
Daniel Dunbar9e9d0762013-08-30 19:52:12 +00005import errno
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +00006import os
7import platform
Daniel Dunbar9e9d0762013-08-30 19:52:12 +00008import re
9import shlex
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +000010import signal
11import subprocess
Daniel Dunbar9e9d0762013-08-30 19:52:12 +000012import sys
13import tempfile
Howard Hinnantb4ebb0e2013-01-14 17:12:54 +000014import time
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +000015
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +000016import lit.Test
17import lit.formats
18import lit.util
19
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +000020class LibcxxTestFormat(lit.formats.FileBasedTest):
21 """
22 Custom test format handler for use with the test format use by libc++.
23
24 Tests fall into two categories:
25 FOO.pass.cpp - Executable test which should compile, run, and exit with
26 code 0.
27 FOO.fail.cpp - Negative test case which is expected to fail compilation.
28 """
29
Daniel Dunbarcccf2552013-02-05 18:03:49 +000030 def __init__(self, cxx_under_test, cpp_flags, ld_flags, exec_env):
Daniel Dunbar7e0c57b2010-09-15 04:11:29 +000031 self.cxx_under_test = cxx_under_test
Daniel Dunbar611581b2010-09-15 04:31:58 +000032 self.cpp_flags = list(cpp_flags)
33 self.ld_flags = list(ld_flags)
Daniel Dunbarcccf2552013-02-05 18:03:49 +000034 self.exec_env = dict(exec_env)
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +000035
Howard Hinnantb4ebb0e2013-01-14 17:12:54 +000036 def execute_command(self, command, in_dir=None):
37 kwargs = {
38 'stdin' :subprocess.PIPE,
39 'stdout':subprocess.PIPE,
40 'stderr':subprocess.PIPE,
41 }
42 if in_dir:
43 kwargs['cwd'] = in_dir
44 p = subprocess.Popen(command, **kwargs)
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +000045 out,err = p.communicate()
46 exitCode = p.wait()
47
48 # Detect Ctrl-C in subprocess.
49 if exitCode == -signal.SIGINT:
50 raise KeyboardInterrupt
51
52 return out, err, exitCode
53
54 def execute(self, test, lit_config):
Howard Hinnantb4ebb0e2013-01-14 17:12:54 +000055 while True:
56 try:
57 return self._execute(test, lit_config)
58 except OSError, oe:
59 if oe.errno != errno.ETXTBSY:
60 raise
61 time.sleep(0.1)
62
63 def _execute(self, test, lit_config):
Daniel Dunbar81d1ef72013-02-05 21:03:25 +000064 # Extract test metadata from the test file.
Daniel Dunbar81d1ef72013-02-05 21:03:25 +000065 requires = []
66 with open(test.getSourcePath()) as f:
67 for ln in f:
68 if 'XFAIL:' in ln:
69 items = ln[ln.index('XFAIL:') + 6:].split(',')
Daniel Dunbar585b48d2013-08-21 23:06:32 +000070 test.xfails.extend([s.strip() for s in items])
Daniel Dunbar81d1ef72013-02-05 21:03:25 +000071 elif 'REQUIRES:' in ln:
72 items = ln[ln.index('REQUIRES:') + 9:].split(',')
73 requires.extend([s.strip() for s in items])
74 elif not ln.startswith("//") and ln.strip():
75 # Stop at the first non-empty line that is not a C++
76 # comment.
77 break
78
79 # Check that we have the required features.
80 #
81 # FIXME: For now, this is cribbed from lit.TestRunner, to avoid
82 # introducing a dependency there. What we more ideally would like to do
Daniel Dunbar585b48d2013-08-21 23:06:32 +000083 # is lift the "requires" handling to be a core lit framework feature.
Daniel Dunbar81d1ef72013-02-05 21:03:25 +000084 missing_required_features = [f for f in requires
85 if f not in test.config.available_features]
86 if missing_required_features:
87 return (lit.Test.UNSUPPORTED,
88 "Test requires the following features: %s" % (
89 ', '.join(missing_required_features),))
90
Daniel Dunbar81d1ef72013-02-05 21:03:25 +000091 # Evaluate the test.
Daniel Dunbar585b48d2013-08-21 23:06:32 +000092 return self._evaluate_test(test, lit_config)
Daniel Dunbar81d1ef72013-02-05 21:03:25 +000093
94 def _evaluate_test(self, test, lit_config):
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +000095 name = test.path_in_suite[-1]
96 source_path = test.getSourcePath()
Howard Hinnantb4ebb0e2013-01-14 17:12:54 +000097 source_dir = os.path.dirname(source_path)
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +000098
99 # Check what kind of test this is.
100 assert name.endswith('.pass.cpp') or name.endswith('.fail.cpp')
101 expected_compile_fail = name.endswith('.fail.cpp')
102
103 # If this is a compile (failure) test, build it and check for failure.
104 if expected_compile_fail:
105 cmd = [self.cxx_under_test, '-c',
Daniel Dunbar611581b2010-09-15 04:31:58 +0000106 '-o', '/dev/null', source_path] + self.cpp_flags
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +0000107 out, err, exitCode = self.execute_command(cmd)
108 if exitCode == 1:
109 return lit.Test.PASS, ""
110 else:
111 report = """Command: %s\n""" % ' '.join(["'%s'" % a
112 for a in cmd])
113 report += """Exit Code: %d\n""" % exitCode
114 if out:
115 report += """Standard Output:\n--\n%s--""" % out
116 if err:
117 report += """Standard Error:\n--\n%s--""" % err
118 report += "\n\nExpected compilation to fail!"
Daniel Dunbar611581b2010-09-15 04:31:58 +0000119 return lit.Test.FAIL, report
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +0000120 else:
121 exec_file = tempfile.NamedTemporaryFile(suffix="exe", delete=False)
122 exec_path = exec_file.name
123 exec_file.close()
124
125 try:
Michael J. Spencer626916f2010-12-10 19:47:54 +0000126 compile_cmd = [self.cxx_under_test, '-o', exec_path,
Daniel Dunbar611581b2010-09-15 04:31:58 +0000127 source_path] + self.cpp_flags + self.ld_flags
Michael J. Spencer626916f2010-12-10 19:47:54 +0000128 cmd = compile_cmd
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +0000129 out, err, exitCode = self.execute_command(cmd)
130 if exitCode != 0:
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +0000131 report = """Command: %s\n""" % ' '.join(["'%s'" % a
132 for a in cmd])
133 report += """Exit Code: %d\n""" % exitCode
134 if out:
135 report += """Standard Output:\n--\n%s--""" % out
136 if err:
137 report += """Standard Error:\n--\n%s--""" % err
138 report += "\n\nCompilation failed unexpectedly!"
139 return lit.Test.FAIL, report
140
Daniel Dunbarcccf2552013-02-05 18:03:49 +0000141 cmd = []
142 if self.exec_env:
143 cmd.append('env')
144 cmd.extend('%s=%s' % (name, value)
145 for name,value in self.exec_env.items())
146 cmd.append(exec_path)
Howard Hinnant63b2f4f2012-08-02 18:36:47 +0000147 if lit_config.useValgrind:
148 cmd = lit_config.valgrindArgs + cmd
Howard Hinnantb4ebb0e2013-01-14 17:12:54 +0000149 out, err, exitCode = self.execute_command(cmd, source_dir)
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +0000150 if exitCode != 0:
Daniel Dunbar3bc6a982013-02-12 19:28:51 +0000151 report = """Compiled With: %s\n""" % \
152 ' '.join(["'%s'" % a for a in compile_cmd])
153 report += """Command: %s\n""" % \
154 ' '.join(["'%s'" % a for a in cmd])
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +0000155 report += """Exit Code: %d\n""" % exitCode
156 if out:
157 report += """Standard Output:\n--\n%s--""" % out
158 if err:
159 report += """Standard Error:\n--\n%s--""" % err
160 report += "\n\nCompiled test failed unexpectedly!"
161 return lit.Test.FAIL, report
162 finally:
163 try:
164 os.remove(exec_path)
165 except:
166 pass
167 return lit.Test.PASS, ""
168
169# name: The name of this test suite.
170config.name = 'libc++'
171
172# suffixes: A list of file extensions to treat as test files.
173config.suffixes = ['.cpp']
174
175# test_source_root: The root path where tests are located.
176config.test_source_root = os.path.dirname(__file__)
177
Daniel Dunbaraf01e702012-11-27 23:56:28 +0000178# Gather various compiler parameters.
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000179cxx_under_test = lit_config.params.get('cxx_under_test', None)
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +0000180if cxx_under_test is None:
Michael J. Spencer626916f2010-12-10 19:47:54 +0000181 cxx_under_test = getattr(config, 'cxx_under_test', None)
Daniel Dunbar7fa0ca72013-02-06 20:24:23 +0000182
183 # If no specific cxx_under_test was given, attempt to infer it as clang++.
David Fanga612c622014-01-29 01:54:52 +0000184 if cxx_under_test is None:
185 clangxx = lit.util.which('clang++', config.environment['PATH'])
186 if clangxx is not None:
187 cxx_under_test = clangxx
188 lit_config.note("inferred cxx_under_test as: %r" % (cxx_under_test,))
Daniel Dunbar7fa0ca72013-02-06 20:24:23 +0000189if cxx_under_test is None:
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000190 lit_config.fatal('must specify user parameter cxx_under_test '
191 '(e.g., --param=cxx_under_test=clang++)')
Michael J. Spencer626916f2010-12-10 19:47:54 +0000192
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000193libcxx_src_root = lit_config.params.get('libcxx_src_root', None)
Daniel Dunbaraf01e702012-11-27 23:56:28 +0000194if libcxx_src_root is None:
195 libcxx_src_root = getattr(config, 'libcxx_src_root', None)
196 if libcxx_src_root is None:
197 libcxx_src_root = os.path.dirname(config.test_source_root)
Michael J. Spencer626916f2010-12-10 19:47:54 +0000198
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000199libcxx_obj_root = lit_config.params.get('libcxx_obj_root', None)
Daniel Dunbaraf01e702012-11-27 23:56:28 +0000200if libcxx_obj_root is None:
201 libcxx_obj_root = getattr(config, 'libcxx_obj_root', None)
202 if libcxx_obj_root is None:
203 libcxx_obj_root = libcxx_src_root
204
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000205cxx_has_stdcxx0x_flag_str = lit_config.params.get('cxx_has_stdcxx0x_flag', None)
Daniel Dunbaraf01e702012-11-27 23:56:28 +0000206if cxx_has_stdcxx0x_flag_str is not None:
Daniel Dunbarcccf2552013-02-05 18:03:49 +0000207 if cxx_has_stdcxx0x_flag_str.lower() in ('1', 'true'):
Daniel Dunbaraf01e702012-11-27 23:56:28 +0000208 cxx_has_stdcxx0x_flag = True
Daniel Dunbarcccf2552013-02-05 18:03:49 +0000209 elif cxx_has_stdcxx0x_flag_str.lower() in ('', '0', 'false'):
Daniel Dunbaraf01e702012-11-27 23:56:28 +0000210 cxx_has_stdcxx0x_flag = False
211 else:
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000212 lit_config.fatal(
213 'user parameter cxx_has_stdcxx0x_flag_str should be 0 or 1')
Michael J. Spencer626916f2010-12-10 19:47:54 +0000214else:
Daniel Dunbaraf01e702012-11-27 23:56:28 +0000215 cxx_has_stdcxx0x_flag = getattr(config, 'cxx_has_stdcxx0x_flag', True)
Michael J. Spencer626916f2010-12-10 19:47:54 +0000216
Daniel Dunbarcccf2552013-02-05 18:03:49 +0000217# This test suite supports testing against either the system library or the
218# locally built one; the former mode is useful for testing ABI compatibility
Daniel Dunbar88dec1e2013-02-06 17:47:08 +0000219# between the current headers and a shipping dynamic library.
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000220use_system_lib_str = lit_config.params.get('use_system_lib', None)
Daniel Dunbarcccf2552013-02-05 18:03:49 +0000221if use_system_lib_str is not None:
222 if use_system_lib_str.lower() in ('1', 'true'):
223 use_system_lib = True
224 elif use_system_lib_str.lower() in ('', '0', 'false'):
225 use_system_lib = False
226 else:
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000227 lit_config.fatal('user parameter use_system_lib should be 0 or 1')
Daniel Dunbarcccf2552013-02-05 18:03:49 +0000228else:
Daniel Dunbar88dec1e2013-02-06 17:47:08 +0000229 # Default to testing against the locally built libc++ library.
230 use_system_lib = False
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000231 lit_config.note("inferred use_system_lib as: %r" % (use_system_lib,))
Daniel Dunbarcccf2552013-02-05 18:03:49 +0000232
Daniel Dunbar3bc6a982013-02-12 19:28:51 +0000233link_flags = []
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000234link_flags_str = lit_config.params.get('link_flags', None)
Daniel Dunbar3bc6a982013-02-12 19:28:51 +0000235if link_flags_str is None:
236 link_flags_str = getattr(config, 'link_flags', None)
237 if link_flags_str is None:
Howard Hinnant71b52152013-10-14 18:02:02 +0000238 cxx_abi = getattr(config, 'cxx_abi', 'libcxxabi')
Peter Collingbourned0d308f2013-10-06 22:13:19 +0000239 if cxx_abi == 'libstdc++':
240 link_flags += ['-lstdc++']
241 elif cxx_abi == 'libsupc++':
242 link_flags += ['-lsupc++']
243 elif cxx_abi == 'libcxxabi':
244 link_flags += ['-lc++abi']
245 elif cxx_abi == 'none':
246 pass
247 else:
248 lit_config.fatal('C++ ABI setting %s unsupported for tests' % cxx_abi)
249
250 if sys.platform == 'darwin':
251 link_flags += ['-lSystem']
252 elif sys.platform == 'linux2':
253 link_flags += [ '-lgcc_eh', '-lc', '-lm', '-lpthread',
254 '-lrt', '-lgcc_s']
255 else:
256 lit_config.fatal("unrecognized system")
257
258 lit_config.note("inferred link_flags as: %r" % (link_flags,))
Daniel Dunbar3bc6a982013-02-12 19:28:51 +0000259if not link_flags_str is None:
260 link_flags += shlex.split(link_flags_str)
261
Chandler Carruthe76496c2011-01-23 01:05:20 +0000262# Configure extra compiler flags.
Daniel Dunbar3bc6a982013-02-12 19:28:51 +0000263include_paths = ['-I' + libcxx_src_root + '/include',
264 '-I' + libcxx_src_root + '/test/support']
Daniel Dunbaraf01e702012-11-27 23:56:28 +0000265library_paths = ['-L' + libcxx_obj_root + '/lib']
Chandler Carruthe76496c2011-01-23 01:05:20 +0000266compile_flags = []
Daniel Dunbaraf01e702012-11-27 23:56:28 +0000267if cxx_has_stdcxx0x_flag:
Daniel Dunbarcccf2552013-02-05 18:03:49 +0000268 compile_flags += ['-std=c++0x']
Chandler Carruthe76496c2011-01-23 01:05:20 +0000269
Daniel Dunbar3bc6a982013-02-12 19:28:51 +0000270# Configure extra linker parameters.
Daniel Dunbarcccf2552013-02-05 18:03:49 +0000271exec_env = {}
Michael J. Spencer626916f2010-12-10 19:47:54 +0000272if sys.platform == 'darwin':
Daniel Dunbarcccf2552013-02-05 18:03:49 +0000273 if not use_system_lib:
274 exec_env['DYLD_LIBRARY_PATH'] = os.path.join(libcxx_obj_root, 'lib')
275elif sys.platform == 'linux2':
Daniel Dunbar6b8b9922013-02-06 17:45:53 +0000276 if not use_system_lib:
Daniel Dunbar3bc6a982013-02-12 19:28:51 +0000277 link_flags += ['-Wl,-R', libcxx_obj_root + '/lib']
278 compile_flags += ['-D__STDC_FORMAT_MACROS', '-D__STDC_LIMIT_MACROS',
279 '-D__STDC_CONSTANT_MACROS']
Daniel Dunbarcccf2552013-02-05 18:03:49 +0000280else:
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000281 lit_config.fatal("unrecognized system")
Michael J. Spencer626916f2010-12-10 19:47:54 +0000282
Daniel Dunbaraf01e702012-11-27 23:56:28 +0000283config.test_format = LibcxxTestFormat(
284 cxx_under_test,
285 cpp_flags = ['-nostdinc++'] + compile_flags + include_paths,
Daniel Dunbar3bc6a982013-02-12 19:28:51 +0000286 ld_flags = ['-nodefaultlibs'] + library_paths + ['-lc++'] + link_flags,
Daniel Dunbarcccf2552013-02-05 18:03:49 +0000287 exec_env = exec_env)
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +0000288
Daniel Dunbar4cceb7a2013-02-05 22:28:03 +0000289# Get or infer the target triple.
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000290config.target_triple = lit_config.params.get('target_triple', None)
Daniel Dunbar4cceb7a2013-02-05 22:28:03 +0000291# 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 Dunbarbd7b48a2013-08-09 14:44:11 +0000295 lit_config.note("inferred target_triple as: %r" % (config.target_triple,))
Daniel Dunbara5b51962013-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:
Daniel Dunbar9e9d0762013-08-30 19:52:12 +0000302 # Drop sub-major version components from the triple, because the current
303 # XFAIL handling expects exact matches for feature checks.
304 sanitized_triple = re.sub(r"([^-]+)-([^-]+)-([^-.]+).*", r"\1-\2-\3",
305 config.target_triple)
306 config.available_features.add('with_system_lib=%s' % (sanitized_triple,))