blob: ae88434280356f7a7049e3ae87101b3bd2c62f53 [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
Daniel Dunbarb6b3e502013-08-30 19:52:12 +00005import errno
Dan Alberta85b27f2014-08-04 18:44:48 +00006import locale
Daniel Dunbar42ea4632010-09-15 03:57:04 +00007import os
8import platform
Daniel Dunbarb6b3e502013-08-30 19:52:12 +00009import re
10import shlex
Daniel Dunbar42ea4632010-09-15 03:57:04 +000011import signal
12import subprocess
Daniel Dunbarb6b3e502013-08-30 19:52:12 +000013import sys
14import tempfile
Howard Hinnant3778f272013-01-14 17:12:54 +000015import time
Daniel Dunbar42ea4632010-09-15 03:57:04 +000016
Daniel Dunbar4a381292013-08-09 14:44:11 +000017import lit.Test
18import lit.formats
19import lit.util
20
Daniel Dunbar42ea4632010-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 Dunbar84958712013-02-05 18:03:49 +000031 def __init__(self, cxx_under_test, cpp_flags, ld_flags, exec_env):
Daniel Dunbarbc9a8482010-09-15 04:11:29 +000032 self.cxx_under_test = cxx_under_test
Daniel Dunbar5f09d9e02010-09-15 04:31:58 +000033 self.cpp_flags = list(cpp_flags)
34 self.ld_flags = list(ld_flags)
Daniel Dunbar84958712013-02-05 18:03:49 +000035 self.exec_env = dict(exec_env)
Daniel Dunbar42ea4632010-09-15 03:57:04 +000036
Howard Hinnant3778f272013-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 Dunbar42ea4632010-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 Hinnant3778f272013-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 Dunbarf51f0312013-02-05 21:03:25 +000065 # Extract test metadata from the test file.
Daniel Dunbarf51f0312013-02-05 21:03:25 +000066 requires = []
Eric Fiselier339bdc32014-08-18 06:43:06 +000067 unsupported = []
Daniel Dunbarf51f0312013-02-05 21:03:25 +000068 with open(test.getSourcePath()) as f:
69 for ln in f:
70 if 'XFAIL:' in ln:
71 items = ln[ln.index('XFAIL:') + 6:].split(',')
Daniel Dunbar019c5902013-08-21 23:06:32 +000072 test.xfails.extend([s.strip() for s in items])
Daniel Dunbarf51f0312013-02-05 21:03:25 +000073 elif 'REQUIRES:' in ln:
74 items = ln[ln.index('REQUIRES:') + 9:].split(',')
75 requires.extend([s.strip() for s in items])
Eric Fiselier339bdc32014-08-18 06:43:06 +000076 elif 'UNSUPPORTED:' in ln:
77 items = ln[ln.index('UNSUPPORTED:') + 12:].split(',')
78 unsupported.extend([s.strip() for s in items])
Eric Fiselier993dfb12014-07-31 22:56:52 +000079 elif not ln.strip().startswith("//") and ln.strip():
Daniel Dunbarf51f0312013-02-05 21:03:25 +000080 # Stop at the first non-empty line that is not a C++
81 # comment.
82 break
83
84 # Check that we have the required features.
85 #
86 # FIXME: For now, this is cribbed from lit.TestRunner, to avoid
87 # introducing a dependency there. What we more ideally would like to do
Daniel Dunbar019c5902013-08-21 23:06:32 +000088 # is lift the "requires" handling to be a core lit framework feature.
Daniel Dunbarf51f0312013-02-05 21:03:25 +000089 missing_required_features = [f for f in requires
90 if f not in test.config.available_features]
91 if missing_required_features:
92 return (lit.Test.UNSUPPORTED,
93 "Test requires the following features: %s" % (
94 ', '.join(missing_required_features),))
95
Eric Fiselier339bdc32014-08-18 06:43:06 +000096 unsupported_features = [f for f in unsupported
97 if f in test.config.available_features]
98 if unsupported_features:
99 return (lit.Test.UNSUPPORTED,
100 "Test is unsupported with the following features: %s" % (
101 ', '.join(unsupported_features),))
102
Daniel Dunbarf51f0312013-02-05 21:03:25 +0000103 # Evaluate the test.
Daniel Dunbar019c5902013-08-21 23:06:32 +0000104 return self._evaluate_test(test, lit_config)
Daniel Dunbarf51f0312013-02-05 21:03:25 +0000105
106 def _evaluate_test(self, test, lit_config):
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000107 name = test.path_in_suite[-1]
108 source_path = test.getSourcePath()
Howard Hinnant3778f272013-01-14 17:12:54 +0000109 source_dir = os.path.dirname(source_path)
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000110
111 # Check what kind of test this is.
112 assert name.endswith('.pass.cpp') or name.endswith('.fail.cpp')
113 expected_compile_fail = name.endswith('.fail.cpp')
114
115 # If this is a compile (failure) test, build it and check for failure.
116 if expected_compile_fail:
117 cmd = [self.cxx_under_test, '-c',
Daniel Dunbar5f09d9e02010-09-15 04:31:58 +0000118 '-o', '/dev/null', source_path] + self.cpp_flags
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000119 out, err, exitCode = self.execute_command(cmd)
120 if exitCode == 1:
121 return lit.Test.PASS, ""
122 else:
123 report = """Command: %s\n""" % ' '.join(["'%s'" % a
124 for a in cmd])
125 report += """Exit Code: %d\n""" % exitCode
126 if out:
127 report += """Standard Output:\n--\n%s--""" % out
128 if err:
129 report += """Standard Error:\n--\n%s--""" % err
130 report += "\n\nExpected compilation to fail!"
Daniel Dunbar5f09d9e02010-09-15 04:31:58 +0000131 return lit.Test.FAIL, report
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000132 else:
133 exec_file = tempfile.NamedTemporaryFile(suffix="exe", delete=False)
134 exec_path = exec_file.name
135 exec_file.close()
136
137 try:
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000138 compile_cmd = [self.cxx_under_test, '-o', exec_path,
Daniel Dunbar5f09d9e02010-09-15 04:31:58 +0000139 source_path] + self.cpp_flags + self.ld_flags
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000140 cmd = compile_cmd
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000141 out, err, exitCode = self.execute_command(cmd)
142 if exitCode != 0:
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000143 report = """Command: %s\n""" % ' '.join(["'%s'" % a
144 for a in cmd])
145 report += """Exit Code: %d\n""" % exitCode
146 if out:
147 report += """Standard Output:\n--\n%s--""" % out
148 if err:
149 report += """Standard Error:\n--\n%s--""" % err
150 report += "\n\nCompilation failed unexpectedly!"
151 return lit.Test.FAIL, report
152
Daniel Dunbar84958712013-02-05 18:03:49 +0000153 cmd = []
154 if self.exec_env:
155 cmd.append('env')
156 cmd.extend('%s=%s' % (name, value)
157 for name,value in self.exec_env.items())
158 cmd.append(exec_path)
Howard Hinnantc1a45fb2012-08-02 18:36:47 +0000159 if lit_config.useValgrind:
160 cmd = lit_config.valgrindArgs + cmd
Howard Hinnant3778f272013-01-14 17:12:54 +0000161 out, err, exitCode = self.execute_command(cmd, source_dir)
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000162 if exitCode != 0:
Daniel Dunbar62b94392013-02-12 19:28:51 +0000163 report = """Compiled With: %s\n""" % \
164 ' '.join(["'%s'" % a for a in compile_cmd])
165 report += """Command: %s\n""" % \
166 ' '.join(["'%s'" % a for a in cmd])
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000167 report += """Exit Code: %d\n""" % exitCode
168 if out:
169 report += """Standard Output:\n--\n%s--""" % out
170 if err:
171 report += """Standard Error:\n--\n%s--""" % err
172 report += "\n\nCompiled test failed unexpectedly!"
173 return lit.Test.FAIL, report
174 finally:
175 try:
176 os.remove(exec_path)
177 except:
178 pass
179 return lit.Test.PASS, ""
180
181# name: The name of this test suite.
182config.name = 'libc++'
183
184# suffixes: A list of file extensions to treat as test files.
185config.suffixes = ['.cpp']
186
187# test_source_root: The root path where tests are located.
188config.test_source_root = os.path.dirname(__file__)
189
Dan Alberta85b27f2014-08-04 18:44:48 +0000190# Figure out which of the required locales we support
191locales = {
192 'Darwin': {
193 'en_US.UTF-8': 'en_US.UTF-8',
194 'cs_CZ.ISO8859-2': 'cs_CZ.ISO8859-2',
195 'fr_FR.UTF-8': 'fr_FR.UTF-8',
196 'fr_CA.ISO8859-1': 'cs_CZ.ISO8859-1',
197 'ru_RU.UTF-8': 'ru_RU.UTF-8',
198 'zh_CN.UTF-8': 'zh_CN.UTF-8',
199 },
Eric Fiselier983484f2014-08-15 23:24:00 +0000200 'FreeBSD' : {
201 'en_US.UTF-8': 'en_US.UTF-8',
202 'cs_CZ.ISO8859-2': 'cs_CZ.ISO8859-2',
203 'fr_FR.UTF-8': 'fr_FR.UTF-8',
204 'fr_CA.ISO8859-1': 'fr_CA.ISO8859-1',
205 'ru_RU.UTF-8': 'ru_RU.UTF-8',
206 'zh_CN.UTF-8': 'zh_CN.UTF-8',
207 },
Dan Alberta85b27f2014-08-04 18:44:48 +0000208 'Linux': {
209 'en_US.UTF-8': 'en_US.UTF-8',
210 'cs_CZ.ISO8859-2': 'cs_CZ.ISO-8859-2',
211 'fr_FR.UTF-8': 'fr_FR.UTF-8',
212 'fr_CA.ISO8859-1': 'fr_CA.ISO-8859-1',
213 'ru_RU.UTF-8': 'ru_RU.UTF-8',
214 'zh_CN.UTF-8': 'zh_CN.UTF-8',
215 },
216 'Windows': {
217 'en_US.UTF-8': 'English_United States.1252',
218 'cs_CZ.ISO8859-2': 'Czech_Czech Republic.1250',
219 'fr_FR.UTF-8': 'French_France.1252',
220 'fr_CA.ISO8859-1': 'French_Canada.1252',
221 'ru_RU.UTF-8': 'Russian_Russia.1251',
222 'zh_CN.UTF-8': 'Chinese_China.936',
223 },
224}
225
226for feature, loc in locales[platform.system()].items():
227 try:
228 locale.setlocale(locale.LC_ALL, loc)
229 config.available_features.add('locale.{}'.format(feature))
230 except:
Dan Albert48e28e02014-08-04 20:27:45 +0000231 lit_config.warning('The locale {} is not supported by your platform. '
Dan Alberta85b27f2014-08-04 18:44:48 +0000232 'Some tests will be unsupported.'.format(loc))
233
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000234# Gather various compiler parameters.
Daniel Dunbar4a381292013-08-09 14:44:11 +0000235cxx_under_test = lit_config.params.get('cxx_under_test', None)
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000236if cxx_under_test is None:
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000237 cxx_under_test = getattr(config, 'cxx_under_test', None)
Daniel Dunbar05abe932013-02-06 20:24:23 +0000238
239 # If no specific cxx_under_test was given, attempt to infer it as clang++.
David Fang75842382014-01-29 01:54:52 +0000240 if cxx_under_test is None:
241 clangxx = lit.util.which('clang++', config.environment['PATH'])
242 if clangxx is not None:
243 cxx_under_test = clangxx
244 lit_config.note("inferred cxx_under_test as: %r" % (cxx_under_test,))
Daniel Dunbar05abe932013-02-06 20:24:23 +0000245if cxx_under_test is None:
Daniel Dunbar4a381292013-08-09 14:44:11 +0000246 lit_config.fatal('must specify user parameter cxx_under_test '
247 '(e.g., --param=cxx_under_test=clang++)')
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000248
Daniel Dunbar4a381292013-08-09 14:44:11 +0000249libcxx_src_root = lit_config.params.get('libcxx_src_root', None)
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000250if libcxx_src_root is None:
251 libcxx_src_root = getattr(config, 'libcxx_src_root', None)
252 if libcxx_src_root is None:
253 libcxx_src_root = os.path.dirname(config.test_source_root)
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000254
Daniel Dunbar4a381292013-08-09 14:44:11 +0000255libcxx_obj_root = lit_config.params.get('libcxx_obj_root', None)
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000256if libcxx_obj_root is None:
257 libcxx_obj_root = getattr(config, 'libcxx_obj_root', None)
258 if libcxx_obj_root is None:
259 libcxx_obj_root = libcxx_src_root
260
Daniel Dunbar84958712013-02-05 18:03:49 +0000261# This test suite supports testing against either the system library or the
262# locally built one; the former mode is useful for testing ABI compatibility
Daniel Dunbar51789422013-02-06 17:47:08 +0000263# between the current headers and a shipping dynamic library.
Daniel Dunbar4a381292013-08-09 14:44:11 +0000264use_system_lib_str = lit_config.params.get('use_system_lib', None)
Daniel Dunbar84958712013-02-05 18:03:49 +0000265if use_system_lib_str is not None:
266 if use_system_lib_str.lower() in ('1', 'true'):
267 use_system_lib = True
268 elif use_system_lib_str.lower() in ('', '0', 'false'):
269 use_system_lib = False
270 else:
Daniel Dunbar4a381292013-08-09 14:44:11 +0000271 lit_config.fatal('user parameter use_system_lib should be 0 or 1')
Daniel Dunbar84958712013-02-05 18:03:49 +0000272else:
Daniel Dunbar51789422013-02-06 17:47:08 +0000273 # Default to testing against the locally built libc++ library.
274 use_system_lib = False
Daniel Dunbar4a381292013-08-09 14:44:11 +0000275 lit_config.note("inferred use_system_lib as: %r" % (use_system_lib,))
Daniel Dunbar84958712013-02-05 18:03:49 +0000276
Daniel Dunbar62b94392013-02-12 19:28:51 +0000277link_flags = []
Daniel Dunbar4a381292013-08-09 14:44:11 +0000278link_flags_str = lit_config.params.get('link_flags', None)
Daniel Dunbar62b94392013-02-12 19:28:51 +0000279if link_flags_str is None:
280 link_flags_str = getattr(config, 'link_flags', None)
281 if link_flags_str is None:
Howard Hinnant58af7e12013-10-14 18:02:02 +0000282 cxx_abi = getattr(config, 'cxx_abi', 'libcxxabi')
Peter Collingbourne26dd09e2013-10-06 22:13:19 +0000283 if cxx_abi == 'libstdc++':
284 link_flags += ['-lstdc++']
285 elif cxx_abi == 'libsupc++':
286 link_flags += ['-lsupc++']
287 elif cxx_abi == 'libcxxabi':
288 link_flags += ['-lc++abi']
Eric Fiselier983484f2014-08-15 23:24:00 +0000289 elif cxx_abi == 'libcxxrt':
290 link_flags += ['-lcxxrt']
Peter Collingbourne26dd09e2013-10-06 22:13:19 +0000291 elif cxx_abi == 'none':
292 pass
293 else:
294 lit_config.fatal('C++ ABI setting %s unsupported for tests' % cxx_abi)
295
296 if sys.platform == 'darwin':
297 link_flags += ['-lSystem']
298 elif sys.platform == 'linux2':
299 link_flags += [ '-lgcc_eh', '-lc', '-lm', '-lpthread',
300 '-lrt', '-lgcc_s']
Eric Fiselier983484f2014-08-15 23:24:00 +0000301 elif sys.platform.startswith('freebsd'):
302 link_flags += ['-lc', '-lm', '-pthread', '-lgcc_s']
Peter Collingbourne26dd09e2013-10-06 22:13:19 +0000303 else:
304 lit_config.fatal("unrecognized system")
305
306 lit_config.note("inferred link_flags as: %r" % (link_flags,))
Daniel Dunbar62b94392013-02-12 19:28:51 +0000307if not link_flags_str is None:
308 link_flags += shlex.split(link_flags_str)
309
Chandler Carruthce395a92011-01-23 01:05:20 +0000310# Configure extra compiler flags.
Daniel Dunbar62b94392013-02-12 19:28:51 +0000311include_paths = ['-I' + libcxx_src_root + '/include',
312 '-I' + libcxx_src_root + '/test/support']
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000313library_paths = ['-L' + libcxx_obj_root + '/lib']
Chandler Carruthce395a92011-01-23 01:05:20 +0000314compile_flags = []
Eric Fiselier1b44db22014-08-16 01:35:36 +0000315
316# Try and get the std version from the command line. Fall back to default given
317# in lit.site.cfg is not present. If default is not present then force c++11.
318std = lit_config.params.get('std', None)
319if std is None:
320 std = getattr(config, 'std', None)
321 if std is None:
322 std = 'c++11'
323 lit_config.note('using default std: \'-std=c++11\'')
324else:
325 lit_config.note('using user specified std: \'-std={}\''.format(std))
326compile_flags += ['-std={}'.format(std)]
Chandler Carruthce395a92011-01-23 01:05:20 +0000327
Eric Fiselier0058c802014-08-18 05:03:46 +0000328built_w_san = getattr(config, 'llvm_use_sanitizer')
329if built_w_san and built_w_san.strip():
330 built_w_san = built_w_san.strip()
331 compile_flags += ['-fno-omit-frame-pointer']
332 if built_w_san == 'Address':
333 compile_flags += ['-fsanitize=address']
334 config.available_features.add('asan')
335 elif built_w_san == 'Memory' or built_w_san == 'MemoryWithOrigins':
336 compile_flags += ['-fsanitize=memory']
337 if built_w_san == 'MemoryWithOrigins':
338 compile_flags += ['-fsanitize-memory-track-origins']
339 config.available_features.add('msan')
340 else:
341 lit_config.fatal(
342 'unsupported value for libcxx_use_sanitizer: {}'.format(built_w_san))
343
Daniel Dunbar62b94392013-02-12 19:28:51 +0000344# Configure extra linker parameters.
Daniel Dunbar84958712013-02-05 18:03:49 +0000345exec_env = {}
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000346if sys.platform == 'darwin':
Daniel Dunbar84958712013-02-05 18:03:49 +0000347 if not use_system_lib:
348 exec_env['DYLD_LIBRARY_PATH'] = os.path.join(libcxx_obj_root, 'lib')
349elif sys.platform == 'linux2':
Daniel Dunbard2d614c2013-02-06 17:45:53 +0000350 if not use_system_lib:
Daniel Dunbar62b94392013-02-12 19:28:51 +0000351 link_flags += ['-Wl,-R', libcxx_obj_root + '/lib']
352 compile_flags += ['-D__STDC_FORMAT_MACROS', '-D__STDC_LIMIT_MACROS',
353 '-D__STDC_CONSTANT_MACROS']
Eric Fiselier983484f2014-08-15 23:24:00 +0000354elif sys.platform.startswith('freebsd'):
355 if not use_system_lib:
356 link_flags += ['-Wl,-R', libcxx_obj_root + '/lib']
Daniel Dunbar84958712013-02-05 18:03:49 +0000357else:
Daniel Dunbar4a381292013-08-09 14:44:11 +0000358 lit_config.fatal("unrecognized system")
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000359
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000360config.test_format = LibcxxTestFormat(
361 cxx_under_test,
362 cpp_flags = ['-nostdinc++'] + compile_flags + include_paths,
Daniel Dunbar62b94392013-02-12 19:28:51 +0000363 ld_flags = ['-nodefaultlibs'] + library_paths + ['-lc++'] + link_flags,
Daniel Dunbar84958712013-02-05 18:03:49 +0000364 exec_env = exec_env)
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000365
Daniel Dunbarb6354a02013-02-05 22:28:03 +0000366# Get or infer the target triple.
Daniel Dunbar4a381292013-08-09 14:44:11 +0000367config.target_triple = lit_config.params.get('target_triple', None)
Daniel Dunbarb6354a02013-02-05 22:28:03 +0000368# If no target triple was given, try to infer it from the compiler under test.
369if config.target_triple is None:
370 config.target_triple = lit.util.capture(
371 [cxx_under_test, '-dumpmachine']).strip()
Daniel Dunbar4a381292013-08-09 14:44:11 +0000372 lit_config.note("inferred target_triple as: %r" % (config.target_triple,))
Daniel Dunbar582c97d2013-02-05 21:43:30 +0000373
374# Write an "available feature" that combines the triple when use_system_lib is
375# enabled. This is so that we can easily write XFAIL markers for tests that are
376# known to fail with versions of libc++ as were shipped with a particular
377# triple.
378if use_system_lib:
Daniel Dunbarb6b3e502013-08-30 19:52:12 +0000379 # Drop sub-major version components from the triple, because the current
380 # XFAIL handling expects exact matches for feature checks.
381 sanitized_triple = re.sub(r"([^-]+)-([^-]+)-([^-.]+).*", r"\1-\2-\3",
382 config.target_triple)
383 config.available_features.add('with_system_lib=%s' % (sanitized_triple,))