blob: e37e641a9585138dd67ca15236700fe40017f1a7 [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 = []
Eric Fiselierda98ce02014-08-18 06:43:06 +000067 unsupported = []
Daniel Dunbar81d1ef72013-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 Dunbar585b48d2013-08-21 23:06:32 +000072 test.xfails.extend([s.strip() for s in items])
Daniel Dunbar81d1ef72013-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 Fiselierda98ce02014-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 Fiselierd3d01ea2014-07-31 22:56:52 +000079 elif not ln.strip().startswith("//") and ln.strip():
Daniel Dunbar81d1ef72013-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 Dunbar585b48d2013-08-21 23:06:32 +000088 # is lift the "requires" handling to be a core lit framework feature.
Daniel Dunbar81d1ef72013-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 Fiselierda98ce02014-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 Dunbar81d1ef72013-02-05 21:03:25 +0000103 # Evaluate the test.
Daniel Dunbar585b48d2013-08-21 23:06:32 +0000104 return self._evaluate_test(test, lit_config)
Daniel Dunbar81d1ef72013-02-05 21:03:25 +0000105
106 def _evaluate_test(self, test, lit_config):
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +0000107 name = test.path_in_suite[-1]
108 source_path = test.getSourcePath()
Howard Hinnantb4ebb0e2013-01-14 17:12:54 +0000109 source_dir = os.path.dirname(source_path)
Daniel Dunbarf5eadcd2010-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 Dunbar611581b2010-09-15 04:31:58 +0000118 '-o', '/dev/null', source_path] + self.cpp_flags
Daniel Dunbarf5eadcd2010-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 Dunbar611581b2010-09-15 04:31:58 +0000131 return lit.Test.FAIL, report
Daniel Dunbarf5eadcd2010-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. Spencer626916f2010-12-10 19:47:54 +0000138 compile_cmd = [self.cxx_under_test, '-o', exec_path,
Daniel Dunbar611581b2010-09-15 04:31:58 +0000139 source_path] + self.cpp_flags + self.ld_flags
Michael J. Spencer626916f2010-12-10 19:47:54 +0000140 cmd = compile_cmd
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +0000141 out, err, exitCode = self.execute_command(cmd)
142 if exitCode != 0:
Daniel Dunbarf5eadcd2010-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 Dunbarcccf2552013-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 Hinnant63b2f4f2012-08-02 18:36:47 +0000159 if lit_config.useValgrind:
160 cmd = lit_config.valgrindArgs + cmd
Howard Hinnantb4ebb0e2013-01-14 17:12:54 +0000161 out, err, exitCode = self.execute_command(cmd, source_dir)
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +0000162 if exitCode != 0:
Daniel Dunbar3bc6a982013-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 Dunbarf5eadcd2010-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
Dan Albert2c262132014-08-21 17:30:44 +0000181
182class Configuration(object):
183 def __init__(self, lit_config, config):
184 self.lit_config = lit_config
185 self.config = config
186 self.cxx = None
187 self.src_root = None
188 self.obj_root = None
189 self.env = {}
190 self.compile_flags = []
191 self.link_flags = []
192 self.use_system_lib = False
193
194 if platform.system() not in ('Darwin', 'FreeBSD', 'Linux'):
195 self.lit_config.fatal("unrecognized system")
196
197 def get_lit_conf(self, name, default=None):
198 val = self.lit_config.params.get(name, None)
199 if val is None:
200 val = getattr(self.config, name, None)
201 if val is None:
202 val = default
203 return val
204
205 def configure(self):
206 self.configure_cxx()
207 self.configure_triple()
208 self.configure_src_root()
209 self.configure_obj_root()
210 self.configure_use_system_lib()
211 self.configure_env()
212 self.configure_std_flag()
213 self.configure_compile_flags()
214 self.configure_link_flags()
215 self.configure_sanitizer()
216 self.configure_features()
217
218 def get_test_format(self):
219 return LibcxxTestFormat(
220 self.cxx,
221 cpp_flags=['-nostdinc++'] + self.compile_flags,
222 ld_flags=['-nodefaultlibs'] + self.link_flags,
223 exec_env=self.env)
224
225 def configure_cxx(self):
226 # Gather various compiler parameters.
227 self.cxx = self.get_lit_conf('cxx_under_test')
228
229 # If no specific cxx_under_test was given, attempt to infer it as
230 # clang++.
231 if self.cxx is None:
232 clangxx = lit.util.which('clang++',
233 self.config.environment['PATH'])
234 if clangxx:
235 self.cxx = clangxx
236 self.lit_config.note(
237 "inferred cxx_under_test as: %r" % self.cxx)
238 if not self.cxx:
239 self.lit_config.fatal('must specify user parameter cxx_under_test '
240 '(e.g., --param=cxx_under_test=clang++)')
241
242 def configure_src_root(self):
243 self.src_root = self.get_lit_conf(
244 'libcxx_src_root', os.path.dirname(self.config.test_source_root))
245
246 def configure_obj_root(self):
247 self.obj_root = self.get_lit_conf('libcxx_obj_root', self.src_root)
248
249 def configure_use_system_lib(self):
250 # This test suite supports testing against either the system library or
251 # the locally built one; the former mode is useful for testing ABI
252 # compatibility between the current headers and a shipping dynamic
253 # library.
254 use_system_lib_str = self.get_lit_conf('use_system_lib')
255 if use_system_lib_str:
256 if use_system_lib_str.lower() in ('1', 'true'):
257 self.use_system_lib = True
258 elif use_system_lib_str.lower() in ('', '0', 'false'):
259 self.use_system_lib = False
260 else:
261 self.lit_config.fatal(
262 'user parameter use_system_lib should be 0 or 1')
263 else:
264 # Default to testing against the locally built libc++ library.
265 self.use_system_lib = False
266 self.lit_config.note(
267 "inferred use_system_lib as: %r" % self.use_system_lib)
268
269 def configure_features(self):
270 # Figure out which of the required locales we support
271 locales = {
272 'Darwin': {
273 'en_US.UTF-8': 'en_US.UTF-8',
274 'cs_CZ.ISO8859-2': 'cs_CZ.ISO8859-2',
275 'fr_FR.UTF-8': 'fr_FR.UTF-8',
276 'fr_CA.ISO8859-1': 'cs_CZ.ISO8859-1',
277 'ru_RU.UTF-8': 'ru_RU.UTF-8',
278 'zh_CN.UTF-8': 'zh_CN.UTF-8',
279 },
280 'FreeBSD': {
281 'en_US.UTF-8': 'en_US.UTF-8',
282 'cs_CZ.ISO8859-2': 'cs_CZ.ISO8859-2',
283 'fr_FR.UTF-8': 'fr_FR.UTF-8',
284 'fr_CA.ISO8859-1': 'fr_CA.ISO8859-1',
285 'ru_RU.UTF-8': 'ru_RU.UTF-8',
286 'zh_CN.UTF-8': 'zh_CN.UTF-8',
287 },
288 'Linux': {
289 'en_US.UTF-8': 'en_US.UTF-8',
290 'cs_CZ.ISO8859-2': 'cs_CZ.ISO-8859-2',
291 'fr_FR.UTF-8': 'fr_FR.UTF-8',
292 'fr_CA.ISO8859-1': 'fr_CA.ISO-8859-1',
293 'ru_RU.UTF-8': 'ru_RU.UTF-8',
294 'zh_CN.UTF-8': 'zh_CN.UTF-8',
295 },
296 'Windows': {
297 'en_US.UTF-8': 'English_United States.1252',
298 'cs_CZ.ISO8859-2': 'Czech_Czech Republic.1250',
299 'fr_FR.UTF-8': 'French_France.1252',
300 'fr_CA.ISO8859-1': 'French_Canada.1252',
301 'ru_RU.UTF-8': 'Russian_Russia.1251',
302 'zh_CN.UTF-8': 'Chinese_China.936',
303 },
304 }
305
306 default_locale = locale.setlocale(locale.LC_ALL)
307 for feature, loc in locales[platform.system()].items():
308 try:
309 locale.setlocale(locale.LC_ALL, loc)
310 self.config.available_features.add('locale.{}'.format(feature))
311 except:
312 self.lit_config.warning('The locale {} is not supported by '
313 'your platform. Some tests will be '
314 'unsupported.'.format(loc))
315 locale.setlocale(locale.LC_ALL, default_locale)
316
317 # Write an "available feature" that combines the triple when
318 # use_system_lib is enabled. This is so that we can easily write XFAIL
319 # markers for tests that are known to fail with versions of libc++ as
320 # were shipped with a particular triple.
321 if self.use_system_lib:
322 # Drop sub-major version components from the triple, because the
323 # current XFAIL handling expects exact matches for feature checks.
324 sanitized_triple = re.sub(
325 r"([^-]+)-([^-]+)-([^-.]+).*", r"\1-\2-\3",
326 self.config.target_triple)
327 self.config.available_features.add(
328 'with_system_lib=%s' % sanitized_triple)
329
330 def configure_compile_flags(self):
331 # Configure extra compiler flags.
332 self.compile_flags += ['-I' + self.src_root + '/include',
333 '-I' + self.src_root + '/test/support']
334
335 def configure_link_flags(self):
336 self.link_flags += ['-L' + self.obj_root + '/lib', '-lc++']
337 link_flags_str = self.get_lit_conf('link_flags')
338 if link_flags_str is None:
339 cxx_abi = self.get_lit_conf('cxx_abi', 'libcxxabi')
340 if cxx_abi == 'libstdc++':
341 self.link_flags += ['-lstdc++']
342 elif cxx_abi == 'libsupc++':
343 self.link_flags += ['-lsupc++']
344 elif cxx_abi == 'libcxxabi':
345 self.link_flags += ['-lc++abi']
346 elif cxx_abi == 'libcxxrt':
347 self.link_flags += ['-lcxxrt']
348 elif cxx_abi == 'none':
349 pass
350 else:
351 self.lit_config.fatal(
352 'C++ ABI setting %s unsupported for tests' % cxx_abi)
353
354 if sys.platform == 'darwin':
355 self.link_flags += ['-lSystem']
356 elif sys.platform == 'linux2':
357 self.link_flags += ['-lgcc_eh', '-lc', '-lm', '-lpthread',
358 '-lrt', '-lgcc_s']
359 elif sys.platform.startswith('freebsd'):
360 self.link_flags += ['-lc', '-lm', '-pthread', '-lgcc_s']
361 else:
362 self.lit_config.fatal("unrecognized system")
363
364 self.lit_config.note(
365 "inferred link_flags as: %r" % self.link_flags)
366 if link_flags_str:
367 self.link_flags += shlex.split(link_flags_str)
368
369 if sys.platform == 'linux2':
370 if not self.use_system_lib:
371 self.link_flags += ['-Wl,-R', self.obj_root + '/lib']
372 self.compile_flags += ['-D__STDC_FORMAT_MACROS',
373 '-D__STDC_LIMIT_MACROS',
374 '-D__STDC_CONSTANT_MACROS']
375 elif sys.platform.startswith('freebsd'):
376 if not self.use_system_lib:
377 self.link_flags += ['-Wl,-R', self.obj_root + '/lib']
378
379 def configure_std_flag(self):
380 # Try and get the std version from the command line. Fall back to
381 # default given in lit.site.cfg is not present. If default is not
382 # present then force c++11.
383 std = self.get_lit_conf('std')
384 if std is None:
385 std = 'c++11'
386 self.lit_config.note('using default std: \'-std=c++11\'')
387 self.compile_flags += ['-std={}'.format(std)]
388 self.config.available_features.add(std)
389
390 def configure_sanitizer(self):
391 san = self.get_lit_conf('llvm_use_sanitizer', '').strip()
392 if san:
393 self.compile_flags += ['-fno-omit-frame-pointer']
394 if san == 'Address':
395 self.compile_flags += ['-fsanitize=address']
396 self.config.available_features.add('asan')
397 elif san == 'Memory' or san == 'MemoryWithOrigins':
398 self.compile_flags += ['-fsanitize=memory']
399 if san == 'MemoryWithOrigins':
400 self.compile_flags += ['-fsanitize-memory-track-origins']
401 self.config.available_features.add('msan')
402 else:
403 self.lit_config.fatal('unsupported value for '
404 'libcxx_use_san: {}'.format(san))
405
406 def configure_triple(self):
407 # Get or infer the target triple.
408 self.config.target_triple = self.get_lit_conf('target_triple')
409 # If no target triple was given, try to infer it from the compiler
410 # under test.
411 if not self.config.target_triple:
412 self.config.target_triple = lit.util.capture(
413 [self.cxx, '-dumpmachine']).strip()
414 self.lit_config.note(
415 "inferred target_triple as: %r" % self.config.target_triple)
416
417 def configure_env(self):
418 # Configure extra linker parameters.
419 if sys.platform == 'darwin':
420 if not self.use_system_lib:
421 self.env['DYLD_LIBRARY_PATH'] = os.path.join(self.obj_root,
422 'lib')
423
424
Daniel Dunbarf5eadcd2010-09-15 03:57:04 +0000425# name: The name of this test suite.
426config.name = 'libc++'
427
428# suffixes: A list of file extensions to treat as test files.
429config.suffixes = ['.cpp']
430
431# test_source_root: The root path where tests are located.
432config.test_source_root = os.path.dirname(__file__)
433
Dan Albert2c262132014-08-21 17:30:44 +0000434configuration = Configuration(lit_config, config)
435configuration.configure()
436config.test_format = configuration.get_test_format()