blob: f41187b86a0743302ea0cce4a81f5f4d5b1b2791 [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 = []
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 Dunbar019c5902013-08-21 23:06:32 +000071 test.xfails.extend([s.strip() for s in items])
Daniel Dunbarf51f0312013-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 Fiselier993dfb12014-07-31 22:56:52 +000075 elif not ln.strip().startswith("//") and ln.strip():
Daniel Dunbarf51f0312013-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 Dunbar019c5902013-08-21 23:06:32 +000084 # is lift the "requires" handling to be a core lit framework feature.
Daniel Dunbarf51f0312013-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 Dunbarf51f0312013-02-05 21:03:25 +000092 # Evaluate the test.
Daniel Dunbar019c5902013-08-21 23:06:32 +000093 return self._evaluate_test(test, lit_config)
Daniel Dunbarf51f0312013-02-05 21:03:25 +000094
95 def _evaluate_test(self, test, lit_config):
Daniel Dunbar42ea4632010-09-15 03:57:04 +000096 name = test.path_in_suite[-1]
97 source_path = test.getSourcePath()
Howard Hinnant3778f272013-01-14 17:12:54 +000098 source_dir = os.path.dirname(source_path)
Daniel Dunbar42ea4632010-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 Dunbar5f09d9e02010-09-15 04:31:58 +0000107 '-o', '/dev/null', source_path] + self.cpp_flags
Daniel Dunbar42ea4632010-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 Dunbar5f09d9e02010-09-15 04:31:58 +0000120 return lit.Test.FAIL, report
Daniel Dunbar42ea4632010-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. Spencerf5799be2010-12-10 19:47:54 +0000127 compile_cmd = [self.cxx_under_test, '-o', exec_path,
Daniel Dunbar5f09d9e02010-09-15 04:31:58 +0000128 source_path] + self.cpp_flags + self.ld_flags
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000129 cmd = compile_cmd
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000130 out, err, exitCode = self.execute_command(cmd)
131 if exitCode != 0:
Daniel Dunbar42ea4632010-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 Dunbar84958712013-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 Hinnantc1a45fb2012-08-02 18:36:47 +0000148 if lit_config.useValgrind:
149 cmd = lit_config.valgrindArgs + cmd
Howard Hinnant3778f272013-01-14 17:12:54 +0000150 out, err, exitCode = self.execute_command(cmd, source_dir)
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000151 if exitCode != 0:
Daniel Dunbar62b94392013-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 Dunbar42ea4632010-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 Alberta85b27f2014-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 },
Eric Fiselier983484f2014-08-15 23:24:00 +0000189 'FreeBSD' : {
190 'en_US.UTF-8': 'en_US.UTF-8',
191 'cs_CZ.ISO8859-2': 'cs_CZ.ISO8859-2',
192 'fr_FR.UTF-8': 'fr_FR.UTF-8',
193 'fr_CA.ISO8859-1': 'fr_CA.ISO8859-1',
194 'ru_RU.UTF-8': 'ru_RU.UTF-8',
195 'zh_CN.UTF-8': 'zh_CN.UTF-8',
196 },
Dan Alberta85b27f2014-08-04 18:44:48 +0000197 'Linux': {
198 'en_US.UTF-8': 'en_US.UTF-8',
199 'cs_CZ.ISO8859-2': 'cs_CZ.ISO-8859-2',
200 'fr_FR.UTF-8': 'fr_FR.UTF-8',
201 'fr_CA.ISO8859-1': 'fr_CA.ISO-8859-1',
202 'ru_RU.UTF-8': 'ru_RU.UTF-8',
203 'zh_CN.UTF-8': 'zh_CN.UTF-8',
204 },
205 'Windows': {
206 'en_US.UTF-8': 'English_United States.1252',
207 'cs_CZ.ISO8859-2': 'Czech_Czech Republic.1250',
208 'fr_FR.UTF-8': 'French_France.1252',
209 'fr_CA.ISO8859-1': 'French_Canada.1252',
210 'ru_RU.UTF-8': 'Russian_Russia.1251',
211 'zh_CN.UTF-8': 'Chinese_China.936',
212 },
213}
214
215for feature, loc in locales[platform.system()].items():
216 try:
217 locale.setlocale(locale.LC_ALL, loc)
218 config.available_features.add('locale.{}'.format(feature))
219 except:
Dan Albert48e28e02014-08-04 20:27:45 +0000220 lit_config.warning('The locale {} is not supported by your platform. '
Dan Alberta85b27f2014-08-04 18:44:48 +0000221 'Some tests will be unsupported.'.format(loc))
222
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000223# Gather various compiler parameters.
Daniel Dunbar4a381292013-08-09 14:44:11 +0000224cxx_under_test = lit_config.params.get('cxx_under_test', None)
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000225if cxx_under_test is None:
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000226 cxx_under_test = getattr(config, 'cxx_under_test', None)
Daniel Dunbar05abe932013-02-06 20:24:23 +0000227
228 # If no specific cxx_under_test was given, attempt to infer it as clang++.
David Fang75842382014-01-29 01:54:52 +0000229 if cxx_under_test is None:
230 clangxx = lit.util.which('clang++', config.environment['PATH'])
231 if clangxx is not None:
232 cxx_under_test = clangxx
233 lit_config.note("inferred cxx_under_test as: %r" % (cxx_under_test,))
Daniel Dunbar05abe932013-02-06 20:24:23 +0000234if cxx_under_test is None:
Daniel Dunbar4a381292013-08-09 14:44:11 +0000235 lit_config.fatal('must specify user parameter cxx_under_test '
236 '(e.g., --param=cxx_under_test=clang++)')
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000237
Daniel Dunbar4a381292013-08-09 14:44:11 +0000238libcxx_src_root = lit_config.params.get('libcxx_src_root', None)
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000239if libcxx_src_root is None:
240 libcxx_src_root = getattr(config, 'libcxx_src_root', None)
241 if libcxx_src_root is None:
242 libcxx_src_root = os.path.dirname(config.test_source_root)
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000243
Daniel Dunbar4a381292013-08-09 14:44:11 +0000244libcxx_obj_root = lit_config.params.get('libcxx_obj_root', None)
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000245if libcxx_obj_root is None:
246 libcxx_obj_root = getattr(config, 'libcxx_obj_root', None)
247 if libcxx_obj_root is None:
248 libcxx_obj_root = libcxx_src_root
249
Daniel Dunbar84958712013-02-05 18:03:49 +0000250# This test suite supports testing against either the system library or the
251# locally built one; the former mode is useful for testing ABI compatibility
Daniel Dunbar51789422013-02-06 17:47:08 +0000252# between the current headers and a shipping dynamic library.
Daniel Dunbar4a381292013-08-09 14:44:11 +0000253use_system_lib_str = lit_config.params.get('use_system_lib', None)
Daniel Dunbar84958712013-02-05 18:03:49 +0000254if use_system_lib_str is not None:
255 if use_system_lib_str.lower() in ('1', 'true'):
256 use_system_lib = True
257 elif use_system_lib_str.lower() in ('', '0', 'false'):
258 use_system_lib = False
259 else:
Daniel Dunbar4a381292013-08-09 14:44:11 +0000260 lit_config.fatal('user parameter use_system_lib should be 0 or 1')
Daniel Dunbar84958712013-02-05 18:03:49 +0000261else:
Daniel Dunbar51789422013-02-06 17:47:08 +0000262 # Default to testing against the locally built libc++ library.
263 use_system_lib = False
Daniel Dunbar4a381292013-08-09 14:44:11 +0000264 lit_config.note("inferred use_system_lib as: %r" % (use_system_lib,))
Daniel Dunbar84958712013-02-05 18:03:49 +0000265
Daniel Dunbar62b94392013-02-12 19:28:51 +0000266link_flags = []
Daniel Dunbar4a381292013-08-09 14:44:11 +0000267link_flags_str = lit_config.params.get('link_flags', None)
Daniel Dunbar62b94392013-02-12 19:28:51 +0000268if link_flags_str is None:
269 link_flags_str = getattr(config, 'link_flags', None)
270 if link_flags_str is None:
Howard Hinnant58af7e12013-10-14 18:02:02 +0000271 cxx_abi = getattr(config, 'cxx_abi', 'libcxxabi')
Peter Collingbourne26dd09e2013-10-06 22:13:19 +0000272 if cxx_abi == 'libstdc++':
273 link_flags += ['-lstdc++']
274 elif cxx_abi == 'libsupc++':
275 link_flags += ['-lsupc++']
276 elif cxx_abi == 'libcxxabi':
277 link_flags += ['-lc++abi']
Eric Fiselier983484f2014-08-15 23:24:00 +0000278 elif cxx_abi == 'libcxxrt':
279 link_flags += ['-lcxxrt']
Peter Collingbourne26dd09e2013-10-06 22:13:19 +0000280 elif cxx_abi == 'none':
281 pass
282 else:
283 lit_config.fatal('C++ ABI setting %s unsupported for tests' % cxx_abi)
284
285 if sys.platform == 'darwin':
286 link_flags += ['-lSystem']
287 elif sys.platform == 'linux2':
288 link_flags += [ '-lgcc_eh', '-lc', '-lm', '-lpthread',
289 '-lrt', '-lgcc_s']
Eric Fiselier983484f2014-08-15 23:24:00 +0000290 elif sys.platform.startswith('freebsd'):
291 link_flags += ['-lc', '-lm', '-pthread', '-lgcc_s']
Peter Collingbourne26dd09e2013-10-06 22:13:19 +0000292 else:
293 lit_config.fatal("unrecognized system")
294
295 lit_config.note("inferred link_flags as: %r" % (link_flags,))
Daniel Dunbar62b94392013-02-12 19:28:51 +0000296if not link_flags_str is None:
297 link_flags += shlex.split(link_flags_str)
298
Chandler Carruthce395a92011-01-23 01:05:20 +0000299# Configure extra compiler flags.
Daniel Dunbar62b94392013-02-12 19:28:51 +0000300include_paths = ['-I' + libcxx_src_root + '/include',
301 '-I' + libcxx_src_root + '/test/support']
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000302library_paths = ['-L' + libcxx_obj_root + '/lib']
Chandler Carruthce395a92011-01-23 01:05:20 +0000303compile_flags = []
Eric Fiselier1b44db22014-08-16 01:35:36 +0000304
305# Try and get the std version from the command line. Fall back to default given
306# in lit.site.cfg is not present. If default is not present then force c++11.
307std = lit_config.params.get('std', None)
308if std is None:
309 std = getattr(config, 'std', None)
310 if std is None:
311 std = 'c++11'
312 lit_config.note('using default std: \'-std=c++11\'')
313else:
314 lit_config.note('using user specified std: \'-std={}\''.format(std))
315compile_flags += ['-std={}'.format(std)]
Chandler Carruthce395a92011-01-23 01:05:20 +0000316
Eric Fiselier0058c802014-08-18 05:03:46 +0000317built_w_san = getattr(config, 'llvm_use_sanitizer')
318if built_w_san and built_w_san.strip():
319 built_w_san = built_w_san.strip()
320 compile_flags += ['-fno-omit-frame-pointer']
321 if built_w_san == 'Address':
322 compile_flags += ['-fsanitize=address']
323 config.available_features.add('asan')
324 elif built_w_san == 'Memory' or built_w_san == 'MemoryWithOrigins':
325 compile_flags += ['-fsanitize=memory']
326 if built_w_san == 'MemoryWithOrigins':
327 compile_flags += ['-fsanitize-memory-track-origins']
328 config.available_features.add('msan')
329 else:
330 lit_config.fatal(
331 'unsupported value for libcxx_use_sanitizer: {}'.format(built_w_san))
332
Daniel Dunbar62b94392013-02-12 19:28:51 +0000333# Configure extra linker parameters.
Daniel Dunbar84958712013-02-05 18:03:49 +0000334exec_env = {}
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000335if sys.platform == 'darwin':
Daniel Dunbar84958712013-02-05 18:03:49 +0000336 if not use_system_lib:
337 exec_env['DYLD_LIBRARY_PATH'] = os.path.join(libcxx_obj_root, 'lib')
338elif sys.platform == 'linux2':
Daniel Dunbard2d614c2013-02-06 17:45:53 +0000339 if not use_system_lib:
Daniel Dunbar62b94392013-02-12 19:28:51 +0000340 link_flags += ['-Wl,-R', libcxx_obj_root + '/lib']
341 compile_flags += ['-D__STDC_FORMAT_MACROS', '-D__STDC_LIMIT_MACROS',
342 '-D__STDC_CONSTANT_MACROS']
Eric Fiselier983484f2014-08-15 23:24:00 +0000343elif sys.platform.startswith('freebsd'):
344 if not use_system_lib:
345 link_flags += ['-Wl,-R', libcxx_obj_root + '/lib']
Daniel Dunbar84958712013-02-05 18:03:49 +0000346else:
Daniel Dunbar4a381292013-08-09 14:44:11 +0000347 lit_config.fatal("unrecognized system")
Michael J. Spencerf5799be2010-12-10 19:47:54 +0000348
Daniel Dunbar7c4b8532012-11-27 23:56:28 +0000349config.test_format = LibcxxTestFormat(
350 cxx_under_test,
351 cpp_flags = ['-nostdinc++'] + compile_flags + include_paths,
Daniel Dunbar62b94392013-02-12 19:28:51 +0000352 ld_flags = ['-nodefaultlibs'] + library_paths + ['-lc++'] + link_flags,
Daniel Dunbar84958712013-02-05 18:03:49 +0000353 exec_env = exec_env)
Daniel Dunbar42ea4632010-09-15 03:57:04 +0000354
Daniel Dunbarb6354a02013-02-05 22:28:03 +0000355# Get or infer the target triple.
Daniel Dunbar4a381292013-08-09 14:44:11 +0000356config.target_triple = lit_config.params.get('target_triple', None)
Daniel Dunbarb6354a02013-02-05 22:28:03 +0000357# If no target triple was given, try to infer it from the compiler under test.
358if config.target_triple is None:
359 config.target_triple = lit.util.capture(
360 [cxx_under_test, '-dumpmachine']).strip()
Daniel Dunbar4a381292013-08-09 14:44:11 +0000361 lit_config.note("inferred target_triple as: %r" % (config.target_triple,))
Daniel Dunbar582c97d2013-02-05 21:43:30 +0000362
363# Write an "available feature" that combines the triple when use_system_lib is
364# enabled. This is so that we can easily write XFAIL markers for tests that are
365# known to fail with versions of libc++ as were shipped with a particular
366# triple.
367if use_system_lib:
Daniel Dunbarb6b3e502013-08-30 19:52:12 +0000368 # Drop sub-major version components from the triple, because the current
369 # XFAIL handling expects exact matches for feature checks.
370 sanitized_triple = re.sub(r"([^-]+)-([^-]+)-([^-.]+).*", r"\1-\2-\3",
371 config.target_triple)
372 config.available_features.add('with_system_lib=%s' % (sanitized_triple,))