blob: 51c1ff56305a34eeb18c5735d1cd2284b02c0ce1 [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
Dan Albertb4ed5ca2014-08-04 18:44:48 +00006import locale
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +00007import os
8import platform
Daniel Dunbar9e9d0762013-08-30 19:52:12 +00009import re
10import shlex
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +000011import signal
12import subprocess
Daniel Dunbar9e9d0762013-08-30 19:52:12 +000013import sys
14import tempfile
Howard Hinnantb4ebb0e2013-01-14 17:12:54 +000015import time
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +000016
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +000017import lit.Test
18import lit.formats
19import lit.util
20
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +000021class LibcxxTestFormat(lit.formats.FileBasedTest):
22 """
23 Custom test format handler for use with the test format use by libc++.
24
25 Tests fall into two categories:
26 FOO.pass.cpp - Executable test which should compile, run, and exit with
27 code 0.
28 FOO.fail.cpp - Negative test case which is expected to fail compilation.
29 """
30
Daniel Dunbarcccf2552013-02-05 18:03:49 +000031 def __init__(self, cxx_under_test, cpp_flags, ld_flags, exec_env):
Daniel Dunbar7e0c57b2010-09-15 04:11:29 +000032 self.cxx_under_test = cxx_under_test
Daniel Dunbar611581b2010-09-15 04:31:58 +000033 self.cpp_flags = list(cpp_flags)
34 self.ld_flags = list(ld_flags)
Daniel Dunbarcccf2552013-02-05 18:03:49 +000035 self.exec_env = dict(exec_env)
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +000036
Howard Hinnantb4ebb0e2013-01-14 17:12:54 +000037 def execute_command(self, command, in_dir=None):
38 kwargs = {
39 'stdin' :subprocess.PIPE,
40 'stdout':subprocess.PIPE,
41 'stderr':subprocess.PIPE,
42 }
43 if in_dir:
44 kwargs['cwd'] = in_dir
45 p = subprocess.Popen(command, **kwargs)
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +000046 out,err = p.communicate()
47 exitCode = p.wait()
48
49 # Detect Ctrl-C in subprocess.
50 if exitCode == -signal.SIGINT:
51 raise KeyboardInterrupt
52
53 return out, err, exitCode
54
55 def execute(self, test, lit_config):
Howard Hinnantb4ebb0e2013-01-14 17:12:54 +000056 while True:
57 try:
58 return self._execute(test, lit_config)
59 except OSError, oe:
60 if oe.errno != errno.ETXTBSY:
61 raise
62 time.sleep(0.1)
63
64 def _execute(self, test, lit_config):
Daniel Dunbar81d1ef72013-02-05 21:03:25 +000065 # Extract test metadata from the test file.
Daniel Dunbar81d1ef72013-02-05 21:03:25 +000066 requires = []
67 with open(test.getSourcePath()) as f:
68 for ln in f:
69 if 'XFAIL:' in ln:
70 items = ln[ln.index('XFAIL:') + 6:].split(',')
Daniel Dunbar585b48d2013-08-21 23:06:32 +000071 test.xfails.extend([s.strip() for s in items])
Daniel Dunbar81d1ef72013-02-05 21:03:25 +000072 elif 'REQUIRES:' in ln:
73 items = ln[ln.index('REQUIRES:') + 9:].split(',')
74 requires.extend([s.strip() for s in items])
Eric Fiselierd3d01ea2014-07-31 22:56:52 +000075 elif not ln.strip().startswith("//") and ln.strip():
Daniel Dunbar81d1ef72013-02-05 21:03:25 +000076 # Stop at the first non-empty line that is not a C++
77 # comment.
78 break
79
80 # Check that we have the required features.
81 #
82 # FIXME: For now, this is cribbed from lit.TestRunner, to avoid
83 # introducing a dependency there. What we more ideally would like to do
Daniel Dunbar585b48d2013-08-21 23:06:32 +000084 # is lift the "requires" handling to be a core lit framework feature.
Daniel Dunbar81d1ef72013-02-05 21:03:25 +000085 missing_required_features = [f for f in requires
86 if f not in test.config.available_features]
87 if missing_required_features:
88 return (lit.Test.UNSUPPORTED,
89 "Test requires the following features: %s" % (
90 ', '.join(missing_required_features),))
91
Daniel Dunbar81d1ef72013-02-05 21:03:25 +000092 # Evaluate the test.
Daniel Dunbar585b48d2013-08-21 23:06:32 +000093 return self._evaluate_test(test, lit_config)
Daniel Dunbar81d1ef72013-02-05 21:03:25 +000094
95 def _evaluate_test(self, test, lit_config):
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +000096 name = test.path_in_suite[-1]
97 source_path = test.getSourcePath()
Howard Hinnantb4ebb0e2013-01-14 17:12:54 +000098 source_dir = os.path.dirname(source_path)
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +000099
100 # Check what kind of test this is.
101 assert name.endswith('.pass.cpp') or name.endswith('.fail.cpp')
102 expected_compile_fail = name.endswith('.fail.cpp')
103
104 # If this is a compile (failure) test, build it and check for failure.
105 if expected_compile_fail:
106 cmd = [self.cxx_under_test, '-c',
Daniel Dunbar611581b2010-09-15 04:31:58 +0000107 '-o', '/dev/null', source_path] + self.cpp_flags
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +0000108 out, err, exitCode = self.execute_command(cmd)
109 if exitCode == 1:
110 return lit.Test.PASS, ""
111 else:
112 report = """Command: %s\n""" % ' '.join(["'%s'" % a
113 for a in cmd])
114 report += """Exit Code: %d\n""" % exitCode
115 if out:
116 report += """Standard Output:\n--\n%s--""" % out
117 if err:
118 report += """Standard Error:\n--\n%s--""" % err
119 report += "\n\nExpected compilation to fail!"
Daniel Dunbar611581b2010-09-15 04:31:58 +0000120 return lit.Test.FAIL, report
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +0000121 else:
122 exec_file = tempfile.NamedTemporaryFile(suffix="exe", delete=False)
123 exec_path = exec_file.name
124 exec_file.close()
125
126 try:
Michael J. Spencer626916f2010-12-10 19:47:54 +0000127 compile_cmd = [self.cxx_under_test, '-o', exec_path,
Daniel Dunbar611581b2010-09-15 04:31:58 +0000128 source_path] + self.cpp_flags + self.ld_flags
Michael J. Spencer626916f2010-12-10 19:47:54 +0000129 cmd = compile_cmd
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +0000130 out, err, exitCode = self.execute_command(cmd)
131 if exitCode != 0:
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +0000132 report = """Command: %s\n""" % ' '.join(["'%s'" % a
133 for a in cmd])
134 report += """Exit Code: %d\n""" % exitCode
135 if out:
136 report += """Standard Output:\n--\n%s--""" % out
137 if err:
138 report += """Standard Error:\n--\n%s--""" % err
139 report += "\n\nCompilation failed unexpectedly!"
140 return lit.Test.FAIL, report
141
Daniel Dunbarcccf2552013-02-05 18:03:49 +0000142 cmd = []
143 if self.exec_env:
144 cmd.append('env')
145 cmd.extend('%s=%s' % (name, value)
146 for name,value in self.exec_env.items())
147 cmd.append(exec_path)
Howard Hinnant63b2f4f2012-08-02 18:36:47 +0000148 if lit_config.useValgrind:
149 cmd = lit_config.valgrindArgs + cmd
Howard Hinnantb4ebb0e2013-01-14 17:12:54 +0000150 out, err, exitCode = self.execute_command(cmd, source_dir)
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +0000151 if exitCode != 0:
Daniel Dunbar3bc6a982013-02-12 19:28:51 +0000152 report = """Compiled With: %s\n""" % \
153 ' '.join(["'%s'" % a for a in compile_cmd])
154 report += """Command: %s\n""" % \
155 ' '.join(["'%s'" % a for a in cmd])
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +0000156 report += """Exit Code: %d\n""" % exitCode
157 if out:
158 report += """Standard Output:\n--\n%s--""" % out
159 if err:
160 report += """Standard Error:\n--\n%s--""" % err
161 report += "\n\nCompiled test failed unexpectedly!"
162 return lit.Test.FAIL, report
163 finally:
164 try:
165 os.remove(exec_path)
166 except:
167 pass
168 return lit.Test.PASS, ""
169
170# name: The name of this test suite.
171config.name = 'libc++'
172
173# suffixes: A list of file extensions to treat as test files.
174config.suffixes = ['.cpp']
175
176# test_source_root: The root path where tests are located.
177config.test_source_root = os.path.dirname(__file__)
178
Dan Albertb4ed5ca2014-08-04 18:44:48 +0000179# Figure out which of the required locales we support
180locales = {
181 'Darwin': {
182 'en_US.UTF-8': 'en_US.UTF-8',
183 'cs_CZ.ISO8859-2': 'cs_CZ.ISO8859-2',
184 'fr_FR.UTF-8': 'fr_FR.UTF-8',
185 'fr_CA.ISO8859-1': 'cs_CZ.ISO8859-1',
186 'ru_RU.UTF-8': 'ru_RU.UTF-8',
187 'zh_CN.UTF-8': 'zh_CN.UTF-8',
188 },
189 'Linux': {
190 'en_US.UTF-8': 'en_US.UTF-8',
191 'cs_CZ.ISO8859-2': 'cs_CZ.ISO-8859-2',
192 'fr_FR.UTF-8': 'fr_FR.UTF-8',
193 'fr_CA.ISO8859-1': 'fr_CA.ISO-8859-1',
194 'ru_RU.UTF-8': 'ru_RU.UTF-8',
195 'zh_CN.UTF-8': 'zh_CN.UTF-8',
196 },
197 'Windows': {
198 'en_US.UTF-8': 'English_United States.1252',
199 'cs_CZ.ISO8859-2': 'Czech_Czech Republic.1250',
200 'fr_FR.UTF-8': 'French_France.1252',
201 'fr_CA.ISO8859-1': 'French_Canada.1252',
202 'ru_RU.UTF-8': 'Russian_Russia.1251',
203 'zh_CN.UTF-8': 'Chinese_China.936',
204 },
205}
206
207for feature, loc in locales[platform.system()].items():
208 try:
209 locale.setlocale(locale.LC_ALL, loc)
210 config.available_features.add('locale.{}'.format(feature))
211 except:
212 lit_config.warning('The locale {} is not supported by your platoform. '
213 'Some tests will be unsupported.'.format(loc))
214
Daniel Dunbaraf01e702012-11-27 23:56:28 +0000215# Gather various compiler parameters.
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000216cxx_under_test = lit_config.params.get('cxx_under_test', None)
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +0000217if cxx_under_test is None:
Michael J. Spencer626916f2010-12-10 19:47:54 +0000218 cxx_under_test = getattr(config, 'cxx_under_test', None)
Daniel Dunbar7fa0ca72013-02-06 20:24:23 +0000219
220 # If no specific cxx_under_test was given, attempt to infer it as clang++.
David Fanga612c622014-01-29 01:54:52 +0000221 if cxx_under_test is None:
222 clangxx = lit.util.which('clang++', config.environment['PATH'])
223 if clangxx is not None:
224 cxx_under_test = clangxx
225 lit_config.note("inferred cxx_under_test as: %r" % (cxx_under_test,))
Daniel Dunbar7fa0ca72013-02-06 20:24:23 +0000226if cxx_under_test is None:
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000227 lit_config.fatal('must specify user parameter cxx_under_test '
228 '(e.g., --param=cxx_under_test=clang++)')
Michael J. Spencer626916f2010-12-10 19:47:54 +0000229
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000230libcxx_src_root = lit_config.params.get('libcxx_src_root', None)
Daniel Dunbaraf01e702012-11-27 23:56:28 +0000231if libcxx_src_root is None:
232 libcxx_src_root = getattr(config, 'libcxx_src_root', None)
233 if libcxx_src_root is None:
234 libcxx_src_root = os.path.dirname(config.test_source_root)
Michael J. Spencer626916f2010-12-10 19:47:54 +0000235
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000236libcxx_obj_root = lit_config.params.get('libcxx_obj_root', None)
Daniel Dunbaraf01e702012-11-27 23:56:28 +0000237if libcxx_obj_root is None:
238 libcxx_obj_root = getattr(config, 'libcxx_obj_root', None)
239 if libcxx_obj_root is None:
240 libcxx_obj_root = libcxx_src_root
241
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000242cxx_has_stdcxx0x_flag_str = lit_config.params.get('cxx_has_stdcxx0x_flag', None)
Daniel Dunbaraf01e702012-11-27 23:56:28 +0000243if cxx_has_stdcxx0x_flag_str is not None:
Daniel Dunbarcccf2552013-02-05 18:03:49 +0000244 if cxx_has_stdcxx0x_flag_str.lower() in ('1', 'true'):
Daniel Dunbaraf01e702012-11-27 23:56:28 +0000245 cxx_has_stdcxx0x_flag = True
Daniel Dunbarcccf2552013-02-05 18:03:49 +0000246 elif cxx_has_stdcxx0x_flag_str.lower() in ('', '0', 'false'):
Daniel Dunbaraf01e702012-11-27 23:56:28 +0000247 cxx_has_stdcxx0x_flag = False
248 else:
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000249 lit_config.fatal(
250 'user parameter cxx_has_stdcxx0x_flag_str should be 0 or 1')
Michael J. Spencer626916f2010-12-10 19:47:54 +0000251else:
Daniel Dunbaraf01e702012-11-27 23:56:28 +0000252 cxx_has_stdcxx0x_flag = getattr(config, 'cxx_has_stdcxx0x_flag', True)
Michael J. Spencer626916f2010-12-10 19:47:54 +0000253
Daniel Dunbarcccf2552013-02-05 18:03:49 +0000254# This test suite supports testing against either the system library or the
255# locally built one; the former mode is useful for testing ABI compatibility
Daniel Dunbar88dec1e2013-02-06 17:47:08 +0000256# between the current headers and a shipping dynamic library.
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000257use_system_lib_str = lit_config.params.get('use_system_lib', None)
Daniel Dunbarcccf2552013-02-05 18:03:49 +0000258if use_system_lib_str is not None:
259 if use_system_lib_str.lower() in ('1', 'true'):
260 use_system_lib = True
261 elif use_system_lib_str.lower() in ('', '0', 'false'):
262 use_system_lib = False
263 else:
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000264 lit_config.fatal('user parameter use_system_lib should be 0 or 1')
Daniel Dunbarcccf2552013-02-05 18:03:49 +0000265else:
Daniel Dunbar88dec1e2013-02-06 17:47:08 +0000266 # Default to testing against the locally built libc++ library.
267 use_system_lib = False
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000268 lit_config.note("inferred use_system_lib as: %r" % (use_system_lib,))
Daniel Dunbarcccf2552013-02-05 18:03:49 +0000269
Daniel Dunbar3bc6a982013-02-12 19:28:51 +0000270link_flags = []
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000271link_flags_str = lit_config.params.get('link_flags', None)
Daniel Dunbar3bc6a982013-02-12 19:28:51 +0000272if link_flags_str is None:
273 link_flags_str = getattr(config, 'link_flags', None)
274 if link_flags_str is None:
Howard Hinnant71b52152013-10-14 18:02:02 +0000275 cxx_abi = getattr(config, 'cxx_abi', 'libcxxabi')
Peter Collingbourned0d308f2013-10-06 22:13:19 +0000276 if cxx_abi == 'libstdc++':
277 link_flags += ['-lstdc++']
278 elif cxx_abi == 'libsupc++':
279 link_flags += ['-lsupc++']
280 elif cxx_abi == 'libcxxabi':
281 link_flags += ['-lc++abi']
282 elif cxx_abi == 'none':
283 pass
284 else:
285 lit_config.fatal('C++ ABI setting %s unsupported for tests' % cxx_abi)
286
287 if sys.platform == 'darwin':
288 link_flags += ['-lSystem']
289 elif sys.platform == 'linux2':
290 link_flags += [ '-lgcc_eh', '-lc', '-lm', '-lpthread',
291 '-lrt', '-lgcc_s']
292 else:
293 lit_config.fatal("unrecognized system")
294
295 lit_config.note("inferred link_flags as: %r" % (link_flags,))
Daniel Dunbar3bc6a982013-02-12 19:28:51 +0000296if not link_flags_str is None:
297 link_flags += shlex.split(link_flags_str)
298
Chandler Carruthe76496c2011-01-23 01:05:20 +0000299# Configure extra compiler flags.
Daniel Dunbar3bc6a982013-02-12 19:28:51 +0000300include_paths = ['-I' + libcxx_src_root + '/include',
301 '-I' + libcxx_src_root + '/test/support']
Daniel Dunbaraf01e702012-11-27 23:56:28 +0000302library_paths = ['-L' + libcxx_obj_root + '/lib']
Chandler Carruthe76496c2011-01-23 01:05:20 +0000303compile_flags = []
Daniel Dunbaraf01e702012-11-27 23:56:28 +0000304if cxx_has_stdcxx0x_flag:
Daniel Dunbarcccf2552013-02-05 18:03:49 +0000305 compile_flags += ['-std=c++0x']
Chandler Carruthe76496c2011-01-23 01:05:20 +0000306
Daniel Dunbar3bc6a982013-02-12 19:28:51 +0000307# Configure extra linker parameters.
Daniel Dunbarcccf2552013-02-05 18:03:49 +0000308exec_env = {}
Michael J. Spencer626916f2010-12-10 19:47:54 +0000309if sys.platform == 'darwin':
Daniel Dunbarcccf2552013-02-05 18:03:49 +0000310 if not use_system_lib:
311 exec_env['DYLD_LIBRARY_PATH'] = os.path.join(libcxx_obj_root, 'lib')
312elif sys.platform == 'linux2':
Daniel Dunbar6b8b9922013-02-06 17:45:53 +0000313 if not use_system_lib:
Daniel Dunbar3bc6a982013-02-12 19:28:51 +0000314 link_flags += ['-Wl,-R', libcxx_obj_root + '/lib']
315 compile_flags += ['-D__STDC_FORMAT_MACROS', '-D__STDC_LIMIT_MACROS',
316 '-D__STDC_CONSTANT_MACROS']
Daniel Dunbarcccf2552013-02-05 18:03:49 +0000317else:
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000318 lit_config.fatal("unrecognized system")
Michael J. Spencer626916f2010-12-10 19:47:54 +0000319
Daniel Dunbaraf01e702012-11-27 23:56:28 +0000320config.test_format = LibcxxTestFormat(
321 cxx_under_test,
322 cpp_flags = ['-nostdinc++'] + compile_flags + include_paths,
Daniel Dunbar3bc6a982013-02-12 19:28:51 +0000323 ld_flags = ['-nodefaultlibs'] + library_paths + ['-lc++'] + link_flags,
Daniel Dunbarcccf2552013-02-05 18:03:49 +0000324 exec_env = exec_env)
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +0000325
Daniel Dunbar4cceb7a2013-02-05 22:28:03 +0000326# Get or infer the target triple.
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000327config.target_triple = lit_config.params.get('target_triple', None)
Daniel Dunbar4cceb7a2013-02-05 22:28:03 +0000328# If no target triple was given, try to infer it from the compiler under test.
329if config.target_triple is None:
330 config.target_triple = lit.util.capture(
331 [cxx_under_test, '-dumpmachine']).strip()
Daniel Dunbarbd7b48a2013-08-09 14:44:11 +0000332 lit_config.note("inferred target_triple as: %r" % (config.target_triple,))
Daniel Dunbara5b51962013-02-05 21:43:30 +0000333
334# Write an "available feature" that combines the triple when use_system_lib is
335# enabled. This is so that we can easily write XFAIL markers for tests that are
336# known to fail with versions of libc++ as were shipped with a particular
337# triple.
338if use_system_lib:
Daniel Dunbar9e9d0762013-08-30 19:52:12 +0000339 # Drop sub-major version components from the triple, because the current
340 # XFAIL handling expects exact matches for feature checks.
341 sanitized_triple = re.sub(r"([^-]+)-([^-]+)-([^-.]+).*", r"\1-\2-\3",
342 config.target_triple)
343 config.available_features.add('with_system_lib=%s' % (sanitized_triple,))