Daniel Dunbar | 42ea463 | 2010-09-15 03:57:04 +0000 | [diff] [blame] | 1 | # -*- Python -*- |
| 2 | |
| 3 | # Configuration file for the 'lit' test runner. |
| 4 | |
| 5 | import os |
Michael J. Spencer | f5799be | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 6 | import sys |
Daniel Dunbar | 42ea463 | 2010-09-15 03:57:04 +0000 | [diff] [blame] | 7 | import platform |
| 8 | import tempfile |
| 9 | import signal |
| 10 | import subprocess |
Howard Hinnant | 3778f27 | 2013-01-14 17:12:54 +0000 | [diff] [blame] | 11 | import errno |
| 12 | import time |
Daniel Dunbar | 42ea463 | 2010-09-15 03:57:04 +0000 | [diff] [blame] | 13 | |
Daniel Dunbar | f51f031 | 2013-02-05 21:03:25 +0000 | [diff] [blame] | 14 | # FIXME: For now, this is cribbed from lit.TestRunner, to avoid introducing a |
| 15 | # dependency there. What we more ideally would like to do is lift the "xfail" |
| 16 | # and "requires" handling to be a core lit framework feature. |
| 17 | def isExpectedFail(test, xfails): |
| 18 | # Check if any of the xfails match an available feature or the target. |
| 19 | for item in xfails: |
| 20 | # If this is the wildcard, it always fails. |
| 21 | if item == '*': |
| 22 | return True |
| 23 | |
Daniel Dunbar | ba65d61 | 2013-02-06 00:04:52 +0000 | [diff] [blame^] | 24 | # If this is a part of any of the features, it fails. |
| 25 | for feature in test.config.available_features: |
| 26 | if item in feature: |
| 27 | return True |
Daniel Dunbar | f51f031 | 2013-02-05 21:03:25 +0000 | [diff] [blame] | 28 | |
| 29 | # If this is a part of the target triple, it fails. |
| 30 | if item in test.suite.config.target_triple: |
| 31 | return True |
| 32 | |
| 33 | return False |
| 34 | |
Daniel Dunbar | 42ea463 | 2010-09-15 03:57:04 +0000 | [diff] [blame] | 35 | class LibcxxTestFormat(lit.formats.FileBasedTest): |
| 36 | """ |
| 37 | Custom test format handler for use with the test format use by libc++. |
| 38 | |
| 39 | Tests fall into two categories: |
| 40 | FOO.pass.cpp - Executable test which should compile, run, and exit with |
| 41 | code 0. |
| 42 | FOO.fail.cpp - Negative test case which is expected to fail compilation. |
| 43 | """ |
| 44 | |
Daniel Dunbar | 8495871 | 2013-02-05 18:03:49 +0000 | [diff] [blame] | 45 | def __init__(self, cxx_under_test, cpp_flags, ld_flags, exec_env): |
Daniel Dunbar | bc9a848 | 2010-09-15 04:11:29 +0000 | [diff] [blame] | 46 | self.cxx_under_test = cxx_under_test |
Daniel Dunbar | 5f09d9e0 | 2010-09-15 04:31:58 +0000 | [diff] [blame] | 47 | self.cpp_flags = list(cpp_flags) |
| 48 | self.ld_flags = list(ld_flags) |
Daniel Dunbar | 8495871 | 2013-02-05 18:03:49 +0000 | [diff] [blame] | 49 | self.exec_env = dict(exec_env) |
Daniel Dunbar | 42ea463 | 2010-09-15 03:57:04 +0000 | [diff] [blame] | 50 | |
Howard Hinnant | 3778f27 | 2013-01-14 17:12:54 +0000 | [diff] [blame] | 51 | def execute_command(self, command, in_dir=None): |
| 52 | kwargs = { |
| 53 | 'stdin' :subprocess.PIPE, |
| 54 | 'stdout':subprocess.PIPE, |
| 55 | 'stderr':subprocess.PIPE, |
| 56 | } |
| 57 | if in_dir: |
| 58 | kwargs['cwd'] = in_dir |
| 59 | p = subprocess.Popen(command, **kwargs) |
Daniel Dunbar | 42ea463 | 2010-09-15 03:57:04 +0000 | [diff] [blame] | 60 | out,err = p.communicate() |
| 61 | exitCode = p.wait() |
| 62 | |
| 63 | # Detect Ctrl-C in subprocess. |
| 64 | if exitCode == -signal.SIGINT: |
| 65 | raise KeyboardInterrupt |
| 66 | |
| 67 | return out, err, exitCode |
| 68 | |
| 69 | def execute(self, test, lit_config): |
Howard Hinnant | 3778f27 | 2013-01-14 17:12:54 +0000 | [diff] [blame] | 70 | while True: |
| 71 | try: |
| 72 | return self._execute(test, lit_config) |
| 73 | except OSError, oe: |
| 74 | if oe.errno != errno.ETXTBSY: |
| 75 | raise |
| 76 | time.sleep(0.1) |
| 77 | |
| 78 | def _execute(self, test, lit_config): |
Daniel Dunbar | f51f031 | 2013-02-05 21:03:25 +0000 | [diff] [blame] | 79 | # Extract test metadata from the test file. |
| 80 | xfails = [] |
| 81 | requires = [] |
| 82 | with open(test.getSourcePath()) as f: |
| 83 | for ln in f: |
| 84 | if 'XFAIL:' in ln: |
| 85 | items = ln[ln.index('XFAIL:') + 6:].split(',') |
| 86 | xfails.extend([s.strip() for s in items]) |
| 87 | elif 'REQUIRES:' in ln: |
| 88 | items = ln[ln.index('REQUIRES:') + 9:].split(',') |
| 89 | requires.extend([s.strip() for s in items]) |
| 90 | elif not ln.startswith("//") and ln.strip(): |
| 91 | # Stop at the first non-empty line that is not a C++ |
| 92 | # comment. |
| 93 | break |
| 94 | |
| 95 | # Check that we have the required features. |
| 96 | # |
| 97 | # FIXME: For now, this is cribbed from lit.TestRunner, to avoid |
| 98 | # introducing a dependency there. What we more ideally would like to do |
| 99 | # is lift the "xfail" and "requires" handling to be a core lit framework |
| 100 | # feature. |
| 101 | missing_required_features = [f for f in requires |
| 102 | if f not in test.config.available_features] |
| 103 | if missing_required_features: |
| 104 | return (lit.Test.UNSUPPORTED, |
| 105 | "Test requires the following features: %s" % ( |
| 106 | ', '.join(missing_required_features),)) |
| 107 | |
| 108 | # Determine if this test is an expected failure. |
| 109 | isXFail = isExpectedFail(test, xfails) |
| 110 | |
| 111 | # Evaluate the test. |
| 112 | result, report = self._evaluate_test(test, lit_config) |
| 113 | |
| 114 | # Convert the test result based on whether this is an expected failure. |
| 115 | if isXFail: |
| 116 | if result != lit.Test.FAIL: |
| 117 | report += "\n\nTest was expected to FAIL, but did not.\n" |
| 118 | result = lit.Test.XPASS |
| 119 | else: |
| 120 | result = lit.Test.XFAIL |
| 121 | |
| 122 | return result, report |
| 123 | |
| 124 | def _evaluate_test(self, test, lit_config): |
Daniel Dunbar | 42ea463 | 2010-09-15 03:57:04 +0000 | [diff] [blame] | 125 | name = test.path_in_suite[-1] |
| 126 | source_path = test.getSourcePath() |
Howard Hinnant | 3778f27 | 2013-01-14 17:12:54 +0000 | [diff] [blame] | 127 | source_dir = os.path.dirname(source_path) |
Daniel Dunbar | 42ea463 | 2010-09-15 03:57:04 +0000 | [diff] [blame] | 128 | |
| 129 | # Check what kind of test this is. |
| 130 | assert name.endswith('.pass.cpp') or name.endswith('.fail.cpp') |
| 131 | expected_compile_fail = name.endswith('.fail.cpp') |
| 132 | |
| 133 | # If this is a compile (failure) test, build it and check for failure. |
| 134 | if expected_compile_fail: |
| 135 | cmd = [self.cxx_under_test, '-c', |
Daniel Dunbar | 5f09d9e0 | 2010-09-15 04:31:58 +0000 | [diff] [blame] | 136 | '-o', '/dev/null', source_path] + self.cpp_flags |
Daniel Dunbar | 42ea463 | 2010-09-15 03:57:04 +0000 | [diff] [blame] | 137 | out, err, exitCode = self.execute_command(cmd) |
| 138 | if exitCode == 1: |
| 139 | return lit.Test.PASS, "" |
| 140 | else: |
| 141 | report = """Command: %s\n""" % ' '.join(["'%s'" % a |
| 142 | for a in cmd]) |
| 143 | report += """Exit Code: %d\n""" % exitCode |
| 144 | if out: |
| 145 | report += """Standard Output:\n--\n%s--""" % out |
| 146 | if err: |
| 147 | report += """Standard Error:\n--\n%s--""" % err |
| 148 | report += "\n\nExpected compilation to fail!" |
Daniel Dunbar | 5f09d9e0 | 2010-09-15 04:31:58 +0000 | [diff] [blame] | 149 | return lit.Test.FAIL, report |
Daniel Dunbar | 42ea463 | 2010-09-15 03:57:04 +0000 | [diff] [blame] | 150 | else: |
| 151 | exec_file = tempfile.NamedTemporaryFile(suffix="exe", delete=False) |
| 152 | exec_path = exec_file.name |
| 153 | exec_file.close() |
| 154 | |
| 155 | try: |
Michael J. Spencer | f5799be | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 156 | compile_cmd = [self.cxx_under_test, '-o', exec_path, |
Daniel Dunbar | 5f09d9e0 | 2010-09-15 04:31:58 +0000 | [diff] [blame] | 157 | source_path] + self.cpp_flags + self.ld_flags |
Michael J. Spencer | f5799be | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 158 | cmd = compile_cmd |
Daniel Dunbar | 42ea463 | 2010-09-15 03:57:04 +0000 | [diff] [blame] | 159 | out, err, exitCode = self.execute_command(cmd) |
| 160 | if exitCode != 0: |
Daniel Dunbar | 42ea463 | 2010-09-15 03:57:04 +0000 | [diff] [blame] | 161 | report = """Command: %s\n""" % ' '.join(["'%s'" % a |
| 162 | for a in cmd]) |
| 163 | report += """Exit Code: %d\n""" % exitCode |
| 164 | if out: |
| 165 | report += """Standard Output:\n--\n%s--""" % out |
| 166 | if err: |
| 167 | report += """Standard Error:\n--\n%s--""" % err |
| 168 | report += "\n\nCompilation failed unexpectedly!" |
| 169 | return lit.Test.FAIL, report |
| 170 | |
Daniel Dunbar | 8495871 | 2013-02-05 18:03:49 +0000 | [diff] [blame] | 171 | cmd = [] |
| 172 | if self.exec_env: |
| 173 | cmd.append('env') |
| 174 | cmd.extend('%s=%s' % (name, value) |
| 175 | for name,value in self.exec_env.items()) |
| 176 | cmd.append(exec_path) |
Howard Hinnant | c1a45fb | 2012-08-02 18:36:47 +0000 | [diff] [blame] | 177 | if lit_config.useValgrind: |
| 178 | cmd = lit_config.valgrindArgs + cmd |
Howard Hinnant | 3778f27 | 2013-01-14 17:12:54 +0000 | [diff] [blame] | 179 | out, err, exitCode = self.execute_command(cmd, source_dir) |
Daniel Dunbar | 42ea463 | 2010-09-15 03:57:04 +0000 | [diff] [blame] | 180 | if exitCode != 0: |
Michael J. Spencer | f5799be | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 181 | report = """Compiled With: %s\n""" % ' '.join(["'%s'" % a |
| 182 | for a in compile_cmd]) |
| 183 | report += """Command: %s\n""" % ' '.join(["'%s'" % a |
Daniel Dunbar | 42ea463 | 2010-09-15 03:57:04 +0000 | [diff] [blame] | 184 | for a in cmd]) |
| 185 | report += """Exit Code: %d\n""" % exitCode |
| 186 | if out: |
| 187 | report += """Standard Output:\n--\n%s--""" % out |
| 188 | if err: |
| 189 | report += """Standard Error:\n--\n%s--""" % err |
| 190 | report += "\n\nCompiled test failed unexpectedly!" |
| 191 | return lit.Test.FAIL, report |
| 192 | finally: |
| 193 | try: |
| 194 | os.remove(exec_path) |
| 195 | except: |
| 196 | pass |
| 197 | return lit.Test.PASS, "" |
| 198 | |
| 199 | # name: The name of this test suite. |
| 200 | config.name = 'libc++' |
| 201 | |
| 202 | # suffixes: A list of file extensions to treat as test files. |
| 203 | config.suffixes = ['.cpp'] |
| 204 | |
| 205 | # test_source_root: The root path where tests are located. |
| 206 | config.test_source_root = os.path.dirname(__file__) |
| 207 | |
Daniel Dunbar | 7c4b853 | 2012-11-27 23:56:28 +0000 | [diff] [blame] | 208 | # Gather various compiler parameters. |
Daniel Dunbar | 42ea463 | 2010-09-15 03:57:04 +0000 | [diff] [blame] | 209 | cxx_under_test = lit.params.get('cxx_under_test', None) |
| 210 | if cxx_under_test is None: |
Michael J. Spencer | f5799be | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 211 | cxx_under_test = getattr(config, 'cxx_under_test', None) |
| 212 | if cxx_under_test is None: |
| 213 | lit.fatal('must specify user parameter cxx_under_test ' |
| 214 | '(e.g., --param=cxx_under_test=clang++)') |
Michael J. Spencer | f5799be | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 215 | |
Daniel Dunbar | 7c4b853 | 2012-11-27 23:56:28 +0000 | [diff] [blame] | 216 | libcxx_src_root = lit.params.get('libcxx_src_root', None) |
| 217 | if libcxx_src_root is None: |
| 218 | libcxx_src_root = getattr(config, 'libcxx_src_root', None) |
| 219 | if libcxx_src_root is None: |
| 220 | libcxx_src_root = os.path.dirname(config.test_source_root) |
Michael J. Spencer | f5799be | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 221 | |
Daniel Dunbar | 7c4b853 | 2012-11-27 23:56:28 +0000 | [diff] [blame] | 222 | libcxx_obj_root = lit.params.get('libcxx_obj_root', None) |
| 223 | if libcxx_obj_root is None: |
| 224 | libcxx_obj_root = getattr(config, 'libcxx_obj_root', None) |
| 225 | if libcxx_obj_root is None: |
| 226 | libcxx_obj_root = libcxx_src_root |
| 227 | |
| 228 | cxx_has_stdcxx0x_flag_str = lit.params.get('cxx_has_stdcxx0x_flag', None) |
| 229 | if cxx_has_stdcxx0x_flag_str is not None: |
Daniel Dunbar | 8495871 | 2013-02-05 18:03:49 +0000 | [diff] [blame] | 230 | if cxx_has_stdcxx0x_flag_str.lower() in ('1', 'true'): |
Daniel Dunbar | 7c4b853 | 2012-11-27 23:56:28 +0000 | [diff] [blame] | 231 | cxx_has_stdcxx0x_flag = True |
Daniel Dunbar | 8495871 | 2013-02-05 18:03:49 +0000 | [diff] [blame] | 232 | elif cxx_has_stdcxx0x_flag_str.lower() in ('', '0', 'false'): |
Daniel Dunbar | 7c4b853 | 2012-11-27 23:56:28 +0000 | [diff] [blame] | 233 | cxx_has_stdcxx0x_flag = False |
| 234 | else: |
| 235 | lit.fatal('user parameter cxx_has_stdcxx0x_flag_str should be 0 or 1') |
Michael J. Spencer | f5799be | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 236 | else: |
Daniel Dunbar | 7c4b853 | 2012-11-27 23:56:28 +0000 | [diff] [blame] | 237 | cxx_has_stdcxx0x_flag = getattr(config, 'cxx_has_stdcxx0x_flag', True) |
Michael J. Spencer | f5799be | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 238 | |
Daniel Dunbar | 8495871 | 2013-02-05 18:03:49 +0000 | [diff] [blame] | 239 | # This test suite supports testing against either the system library or the |
| 240 | # locally built one; the former mode is useful for testing ABI compatibility |
| 241 | # between the current headers and a shipping dynamic library. We require the |
| 242 | # user to explicitly pick one of the two modes. |
| 243 | use_system_lib_str = lit.params.get('use_system_lib', None) |
| 244 | if use_system_lib_str is not None: |
| 245 | if use_system_lib_str.lower() in ('1', 'true'): |
| 246 | use_system_lib = True |
| 247 | elif use_system_lib_str.lower() in ('', '0', 'false'): |
| 248 | use_system_lib = False |
| 249 | else: |
| 250 | lit.fatal('user parameter use_system_lib should be 0 or 1') |
| 251 | else: |
| 252 | use_system_lib = True |
| 253 | |
Chandler Carruth | ce395a9 | 2011-01-23 01:05:20 +0000 | [diff] [blame] | 254 | # Configure extra compiler flags. |
Howard Hinnant | f1e633c | 2013-01-14 17:07:27 +0000 | [diff] [blame] | 255 | include_paths = ['-I' + libcxx_src_root + '/include', '-I' + libcxx_src_root + '/test/support'] |
Daniel Dunbar | 7c4b853 | 2012-11-27 23:56:28 +0000 | [diff] [blame] | 256 | library_paths = ['-L' + libcxx_obj_root + '/lib'] |
Chandler Carruth | ce395a9 | 2011-01-23 01:05:20 +0000 | [diff] [blame] | 257 | compile_flags = [] |
Daniel Dunbar | 7c4b853 | 2012-11-27 23:56:28 +0000 | [diff] [blame] | 258 | if cxx_has_stdcxx0x_flag: |
Daniel Dunbar | 8495871 | 2013-02-05 18:03:49 +0000 | [diff] [blame] | 259 | compile_flags += ['-std=c++0x'] |
Chandler Carruth | ce395a9 | 2011-01-23 01:05:20 +0000 | [diff] [blame] | 260 | |
Michael J. Spencer | f5799be | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 261 | # Configure extra libraries. |
Daniel Dunbar | 8495871 | 2013-02-05 18:03:49 +0000 | [diff] [blame] | 262 | exec_env = {} |
Michael J. Spencer | f5799be | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 263 | libraries = [] |
| 264 | if sys.platform == 'darwin': |
Daniel Dunbar | 8495871 | 2013-02-05 18:03:49 +0000 | [diff] [blame] | 265 | libraries += ['-lSystem'] |
| 266 | if not use_system_lib: |
| 267 | exec_env['DYLD_LIBRARY_PATH'] = os.path.join(libcxx_obj_root, 'lib') |
| 268 | elif sys.platform == 'linux2': |
| 269 | libraries += ['-lsupc++', '-lgcc_eh', '-lc', '-lm', '-lpthread', '-lrt', '-lgcc_s'] |
| 270 | libraries += ['-Wl,-R', libcxx_obj_root + '/lib'] |
| 271 | else: |
| 272 | lit.fatal("unrecognized system") |
Michael J. Spencer | f5799be | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 273 | |
Daniel Dunbar | 7c4b853 | 2012-11-27 23:56:28 +0000 | [diff] [blame] | 274 | config.test_format = LibcxxTestFormat( |
| 275 | cxx_under_test, |
| 276 | cpp_flags = ['-nostdinc++'] + compile_flags + include_paths, |
Daniel Dunbar | 8495871 | 2013-02-05 18:03:49 +0000 | [diff] [blame] | 277 | ld_flags = ['-nodefaultlibs'] + library_paths + ['-lc++'] + libraries, |
| 278 | exec_env = exec_env) |
Daniel Dunbar | 42ea463 | 2010-09-15 03:57:04 +0000 | [diff] [blame] | 279 | |
Daniel Dunbar | b6354a0 | 2013-02-05 22:28:03 +0000 | [diff] [blame] | 280 | # Get or infer the target triple. |
| 281 | config.target_triple = lit.params.get('target_triple', None) |
| 282 | # If no target triple was given, try to infer it from the compiler under test. |
| 283 | if config.target_triple is None: |
| 284 | config.target_triple = lit.util.capture( |
| 285 | [cxx_under_test, '-dumpmachine']).strip() |
| 286 | lit.note("inferred target triple as: %r" % (config.target_triple,)) |
Daniel Dunbar | 582c97d | 2013-02-05 21:43:30 +0000 | [diff] [blame] | 287 | |
| 288 | # Write an "available feature" that combines the triple when use_system_lib is |
| 289 | # enabled. This is so that we can easily write XFAIL markers for tests that are |
| 290 | # known to fail with versions of libc++ as were shipped with a particular |
| 291 | # triple. |
| 292 | if use_system_lib: |
| 293 | config.available_features.add('with_system_lib=%s' % ( |
| 294 | config.target_triple,)) |