Daniel Dunbar | 3667b99 | 2009-07-31 05:54:17 +0000 | [diff] [blame] | 1 | # -*- Python -*- |
| 2 | |
Daniel Dunbar | b5cbf77 | 2009-09-22 05:16:02 +0000 | [diff] [blame] | 3 | import os |
Daniel Dunbar | 79327b6 | 2009-09-22 10:08:03 +0000 | [diff] [blame] | 4 | import platform |
Chandler Carruth | 2837f66 | 2011-11-05 20:55:50 +0000 | [diff] [blame] | 5 | import re |
| 6 | import subprocess |
| 7 | |
Daniel Dunbar | be4253a | 2009-09-08 16:39:23 +0000 | [diff] [blame] | 8 | |
Daniel Dunbar | 3667b99 | 2009-07-31 05:54:17 +0000 | [diff] [blame] | 9 | # Configuration file for the 'lit' test runner. |
| 10 | |
Daniel Dunbar | b5cbf77 | 2009-09-22 05:16:02 +0000 | [diff] [blame] | 11 | # name: The name of this test suite. |
| 12 | config.name = 'Clang' |
Daniel Dunbar | be4253a | 2009-09-08 16:39:23 +0000 | [diff] [blame] | 13 | |
NAKAMURA Takumi | 1e8200d | 2011-02-09 04:19:57 +0000 | [diff] [blame] | 14 | # Tweak PATH for Win32 |
| 15 | if platform.system() == 'Windows': |
| 16 | # Seek sane tools in directories and set to $PATH. |
| 17 | path = getattr(config, 'lit_tools_dir', None) |
| 18 | path = lit.getToolsPath(path, |
| 19 | config.environment['PATH'], |
| 20 | ['cmp.exe', 'grep.exe', 'sed.exe']) |
| 21 | if path is not None: |
| 22 | path = os.path.pathsep.join((path, |
| 23 | config.environment['PATH'])) |
| 24 | config.environment['PATH'] = path |
| 25 | |
Daniel Dunbar | b5cbf77 | 2009-09-22 05:16:02 +0000 | [diff] [blame] | 26 | # testFormat: The test format to use to interpret tests. |
Daniel Dunbar | 3667b99 | 2009-07-31 05:54:17 +0000 | [diff] [blame] | 27 | # |
Daniel Dunbar | b5cbf77 | 2009-09-22 05:16:02 +0000 | [diff] [blame] | 28 | # For now we require '&&' between commands, until they get globally killed and |
| 29 | # the test runner updated. |
NAKAMURA Takumi | 70f5be6 | 2011-03-05 11:16:06 +0000 | [diff] [blame] | 30 | execute_external = (platform.system() != 'Windows' |
| 31 | or lit.getBashPath() not in [None, ""]) |
Daniel Dunbar | d90e0a1 | 2009-11-08 01:47:35 +0000 | [diff] [blame] | 32 | config.test_format = lit.formats.ShTest(execute_external) |
Daniel Dunbar | f87be55 | 2009-09-06 01:31:12 +0000 | [diff] [blame] | 33 | |
Daniel Dunbar | b5cbf77 | 2009-09-22 05:16:02 +0000 | [diff] [blame] | 34 | # suffixes: A list of file extensions to treat as test files. |
Jim Grosbach | 576452b | 2012-02-10 20:37:10 +0000 | [diff] [blame] | 35 | config.suffixes = ['.c', '.cpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s'] |
Daniel Dunbar | b5cbf77 | 2009-09-22 05:16:02 +0000 | [diff] [blame] | 36 | |
| 37 | # test_source_root: The root path where tests are located. |
| 38 | config.test_source_root = os.path.dirname(__file__) |
| 39 | |
| 40 | # test_exec_root: The root path where tests should be run. |
| 41 | clang_obj_root = getattr(config, 'clang_obj_root', None) |
| 42 | if clang_obj_root is not None: |
| 43 | config.test_exec_root = os.path.join(clang_obj_root, 'test') |
| 44 | |
| 45 | # Set llvm_{src,obj}_root for use by others. |
| 46 | config.llvm_src_root = getattr(config, 'llvm_src_root', None) |
| 47 | config.llvm_obj_root = getattr(config, 'llvm_obj_root', None) |
| 48 | |
Jordy Rose | 0e09fac | 2012-04-06 18:14:01 +0000 | [diff] [blame] | 49 | # Clear some environment variables that might affect Clang. |
| 50 | # |
| 51 | # This first set of vars are read by Clang, but shouldn't affect tests |
| 52 | # that aren't specifically looking for these features, or are required |
| 53 | # simply to run the tests at all. |
| 54 | # |
| 55 | # FIXME: Should we have a tool that enforces this? |
| 56 | |
| 57 | # safe_env_vars = ('TMPDIR', 'TEMP', 'TMP', 'USERPROFILE', 'PWD', |
| 58 | # 'MACOSX_DEPLOYMENT_TARGET', 'IPHONEOS_DEPLOYMENT_TARGET', |
| 59 | # 'IOS_SIMULATOR_DEPLOYMENT_TARGET', |
| 60 | # 'VCINSTALLDIR', 'VC100COMNTOOLS', 'VC90COMNTOOLS', |
| 61 | # 'VC80COMNTOOLS') |
| 62 | possibly_dangerous_env_vars = ('COMPILER_PATH', 'RC_DEBUG_OPTIONS', 'INCLUDE', |
| 63 | 'CINDEXTEST_PREAMBLE_FILE', 'LIBRARY_PATH', |
| 64 | 'CPATH', 'C_INCLUDE_PATH', 'CPLUS_INCLUDE_PATH', |
| 65 | 'OBJC_INCLUDE_PATH', 'OBJCPLUS_INCLUDE_PATH', |
| 66 | 'LIBCLANG_TIMING', 'LIBCLANG_OBJTRACKING', |
| 67 | 'LIBCLANG_LOGGING', 'LIBCLANG_BGPRIO_INDEX', |
| 68 | 'LIBCLANG_BGPRIO_EDIT', 'LIBCLANG_NOTHREADS', |
| 69 | 'LIBCLANG_RESOURCE_USAGE', |
| 70 | 'LIBCLANG_CODE_COMPLETION_LOGGING') |
| 71 | for name in possibly_dangerous_env_vars: |
| 72 | if name in config.environment: |
| 73 | del config.environment[name] |
| 74 | |
Daniel Dunbar | b5cbf77 | 2009-09-22 05:16:02 +0000 | [diff] [blame] | 75 | # Tweak the PATH to include the tools dir and the scripts dir. |
| 76 | if clang_obj_root is not None: |
| 77 | llvm_tools_dir = getattr(config, 'llvm_tools_dir', None) |
| 78 | if not llvm_tools_dir: |
| 79 | lit.fatal('No LLVM tools dir set!') |
Daniel Dunbar | c437eb8 | 2009-09-24 06:31:08 +0000 | [diff] [blame] | 80 | path = os.path.pathsep.join((llvm_tools_dir, config.environment['PATH'])) |
Daniel Dunbar | b5cbf77 | 2009-09-22 05:16:02 +0000 | [diff] [blame] | 81 | config.environment['PATH'] = path |
| 82 | |
Daniel Dunbar | a87097a | 2009-09-26 07:36:09 +0000 | [diff] [blame] | 83 | llvm_libs_dir = getattr(config, 'llvm_libs_dir', None) |
| 84 | if not llvm_libs_dir: |
| 85 | lit.fatal('No LLVM libs dir set!') |
| 86 | path = os.path.pathsep.join((llvm_libs_dir, |
| 87 | config.environment.get('LD_LIBRARY_PATH',''))) |
| 88 | config.environment['LD_LIBRARY_PATH'] = path |
| 89 | |
Daniel Dunbar | b5cbf77 | 2009-09-22 05:16:02 +0000 | [diff] [blame] | 90 | ### |
| 91 | |
| 92 | # Check that the object root is known. |
| 93 | if config.test_exec_root is None: |
| 94 | # Otherwise, we haven't loaded the site specific configuration (the user is |
| 95 | # probably trying to run on a test file directly, and either the site |
| 96 | # configuration hasn't been created by the build system, or we are in an |
| 97 | # out-of-tree build situation). |
| 98 | |
Daniel Dunbar | d3f630f | 2009-11-05 16:36:19 +0000 | [diff] [blame] | 99 | # Check for 'clang_site_config' user parameter, and use that if available. |
| 100 | site_cfg = lit.params.get('clang_site_config', None) |
| 101 | if site_cfg and os.path.exists(site_cfg): |
| 102 | lit.load_config(config, site_cfg) |
| 103 | raise SystemExit |
| 104 | |
Daniel Dunbar | b5cbf77 | 2009-09-22 05:16:02 +0000 | [diff] [blame] | 105 | # Try to detect the situation where we are using an out-of-tree build by |
| 106 | # looking for 'llvm-config'. |
| 107 | # |
| 108 | # FIXME: I debated (i.e., wrote and threw away) adding logic to |
| 109 | # automagically generate the lit.site.cfg if we are in some kind of fresh |
Daniel Dunbar | 8466a0d | 2009-11-07 23:53:17 +0000 | [diff] [blame] | 110 | # build situation. This means knowing how to invoke the build system though, |
| 111 | # and I decided it was too much magic. We should solve this by just having |
| 112 | # the .cfg files generated during the configuration step. |
Daniel Dunbar | b5cbf77 | 2009-09-22 05:16:02 +0000 | [diff] [blame] | 113 | |
| 114 | llvm_config = lit.util.which('llvm-config', config.environment['PATH']) |
| 115 | if not llvm_config: |
| 116 | lit.fatal('No site specific configuration available!') |
| 117 | |
| 118 | # Get the source and object roots. |
| 119 | llvm_src_root = lit.util.capture(['llvm-config', '--src-root']).strip() |
| 120 | llvm_obj_root = lit.util.capture(['llvm-config', '--obj-root']).strip() |
| 121 | clang_src_root = os.path.join(llvm_src_root, "tools", "clang") |
| 122 | clang_obj_root = os.path.join(llvm_obj_root, "tools", "clang") |
| 123 | |
| 124 | # Validate that we got a tree which points to here, using the standard |
| 125 | # tools/clang layout. |
| 126 | this_src_root = os.path.dirname(config.test_source_root) |
| 127 | if os.path.realpath(clang_src_root) != os.path.realpath(this_src_root): |
| 128 | lit.fatal('No site specific configuration available!') |
| 129 | |
| 130 | # Check that the site specific configuration exists. |
| 131 | site_cfg = os.path.join(clang_obj_root, 'test', 'lit.site.cfg') |
| 132 | if not os.path.exists(site_cfg): |
Nico Weber | a384e9a | 2010-09-27 20:40:32 +0000 | [diff] [blame] | 133 | lit.fatal('No site specific configuration available! You may need to ' |
| 134 | 'run "make test" in your Clang build directory.') |
Daniel Dunbar | b5cbf77 | 2009-09-22 05:16:02 +0000 | [diff] [blame] | 135 | |
| 136 | # Okay, that worked. Notify the user of the automagic, and reconfigure. |
| 137 | lit.note('using out-of-tree build at %r' % clang_obj_root) |
| 138 | lit.load_config(config, site_cfg) |
| 139 | raise SystemExit |
| 140 | |
| 141 | ### |
| 142 | |
| 143 | # Discover the 'clang' and 'clangcc' to use. |
| 144 | |
| 145 | import os |
| 146 | |
| 147 | def inferClang(PATH): |
| 148 | # Determine which clang to use. |
| 149 | clang = os.getenv('CLANG') |
| 150 | |
| 151 | # If the user set clang in the environment, definitely use that and don't |
| 152 | # try to validate. |
| 153 | if clang: |
| 154 | return clang |
| 155 | |
| 156 | # Otherwise look in the path. |
| 157 | clang = lit.util.which('clang', PATH) |
| 158 | |
| 159 | if not clang: |
| 160 | lit.fatal("couldn't find 'clang' program, try setting " |
| 161 | "CLANG in your environment") |
| 162 | |
| 163 | return clang |
| 164 | |
Daniel Dunbar | 9b9a46c | 2010-03-20 21:13:08 +0000 | [diff] [blame] | 165 | # When running under valgrind, we mangle '-vg' onto the end of the triple so we |
| 166 | # can check it with XFAIL and XTARGET. |
| 167 | if lit.useValgrind: |
| 168 | config.target_triple += '-vg' |
| 169 | |
NAKAMURA Takumi | 70f5be6 | 2011-03-05 11:16:06 +0000 | [diff] [blame] | 170 | config.clang = inferClang(config.environment['PATH']).replace('\\', '/') |
Daniel Dunbar | b5cbf77 | 2009-09-22 05:16:02 +0000 | [diff] [blame] | 171 | if not lit.quiet: |
| 172 | lit.note('using clang: %r' % config.clang) |
Chandler Carruth | c0b1b86 | 2011-11-05 10:15:27 +0000 | [diff] [blame] | 173 | |
| 174 | # Note that when substituting %clang_cc1 also fill in the include directory of |
| 175 | # the builtin headers. Those are part of even a freestanding environment, but |
| 176 | # Clang relies on the driver to locate them. |
Chandler Carruth | 34146d8 | 2011-11-05 23:29:28 +0000 | [diff] [blame] | 177 | def getClangBuiltinIncludeDir(clang): |
Chandler Carruth | 2837f66 | 2011-11-05 20:55:50 +0000 | [diff] [blame] | 178 | # FIXME: Rather than just getting the version, we should have clang print |
| 179 | # out its resource dir here in an easy to scrape form. |
Chandler Carruth | 34146d8 | 2011-11-05 23:29:28 +0000 | [diff] [blame] | 180 | cmd = subprocess.Popen([clang, '-print-file-name=include'], |
| 181 | stdout=subprocess.PIPE) |
| 182 | if not cmd.stdout: |
| 183 | lit.fatal("Couldn't find the include dir for Clang ('%s')" % clang) |
| 184 | return cmd.stdout.read().strip() |
Chandler Carruth | 2837f66 | 2011-11-05 20:55:50 +0000 | [diff] [blame] | 185 | |
Chandler Carruth | a62ba81 | 2011-11-07 09:17:31 +0000 | [diff] [blame] | 186 | config.substitutions.append( ('%clang_cc1', '%s -cc1 -internal-isystem %s' |
Chandler Carruth | 34146d8 | 2011-11-05 23:29:28 +0000 | [diff] [blame] | 187 | % (config.clang, |
| 188 | getClangBuiltinIncludeDir(config.clang))) ) |
Chandler Carruth | c0b1b86 | 2011-11-05 10:15:27 +0000 | [diff] [blame] | 189 | |
Daniel Dunbar | 8452ef0 | 2010-06-29 16:52:24 +0000 | [diff] [blame] | 190 | config.substitutions.append( ('%clangxx', ' ' + config.clang + |
| 191 | ' -ccc-clang-cxx -ccc-cxx ')) |
Daniel Dunbar | 5618e98 | 2009-12-15 22:01:24 +0000 | [diff] [blame] | 192 | config.substitutions.append( ('%clang', ' ' + config.clang + ' ') ) |
Devang Patel | 068b5b3 | 2010-09-13 20:46:23 +0000 | [diff] [blame] | 193 | config.substitutions.append( ('%test_debuginfo', ' ' + config.llvm_src_root + '/utils/test_debuginfo.pl ') ) |
Daniel Dunbar | 8fbe78f | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 194 | |
Daniel Dunbar | 5618e98 | 2009-12-15 22:01:24 +0000 | [diff] [blame] | 195 | # FIXME: Find nicer way to prohibit this. |
| 196 | config.substitutions.append( |
| 197 | (' clang ', """*** Do not use 'clang' in tests, use '%clang'. ***""") ) |
| 198 | config.substitutions.append( |
David Greene | 7b29345 | 2011-01-03 17:28:52 +0000 | [diff] [blame] | 199 | (' clang\+\+ ', """*** Do not use 'clang++' in tests, use '%clangxx'. ***""")) |
Daniel Dunbar | 78c974f | 2010-02-17 20:31:01 +0000 | [diff] [blame] | 200 | config.substitutions.append( |
Daniel Dunbar | 5618e98 | 2009-12-15 22:01:24 +0000 | [diff] [blame] | 201 | (' clang-cc ', |
| 202 | """*** Do not use 'clang-cc' in tests, use '%clang_cc1'. ***""") ) |
| 203 | config.substitutions.append( |
| 204 | (' clang -cc1 ', |
| 205 | """*** Do not use 'clang -cc1' in tests, use '%clang_cc1'. ***""") ) |
Daniel Dunbar | 8452ef0 | 2010-06-29 16:52:24 +0000 | [diff] [blame] | 206 | config.substitutions.append( |
| 207 | (' %clang-cc1 ', |
| 208 | """*** invalid substitution, use '%clang_cc1'. ***""") ) |
Daniel Dunbar | b44eb0b | 2010-08-24 21:39:55 +0000 | [diff] [blame] | 209 | |
| 210 | ### |
| 211 | |
| 212 | # Set available features we allow tests to conditionalize on. |
Andrew Trick | 3df2824 | 2011-08-26 22:46:31 +0000 | [diff] [blame] | 213 | # |
| 214 | # As of 2011.08, crash-recovery tests still do not pass on FreeBSD. |
| 215 | if platform.system() not in ['FreeBSD']: |
| 216 | config.available_features.add('crash-recovery') |
NAKAMURA Takumi | f5ea88b | 2011-02-28 09:41:07 +0000 | [diff] [blame] | 217 | |
| 218 | # Shell execution |
| 219 | if platform.system() not in ['Windows'] or lit.getBashPath() != '': |
| 220 | config.available_features.add('shell') |
Galina Kistanova | b38fd26 | 2011-06-03 18:36:30 +0000 | [diff] [blame] | 221 | |
| 222 | # Registered Targets |
NAKAMURA Takumi | cd9c3d6 | 2011-11-28 05:09:42 +0000 | [diff] [blame] | 223 | def get_llc_props(tool): |
Galina Kistanova | b38fd26 | 2011-06-03 18:36:30 +0000 | [diff] [blame] | 224 | set_of_targets = set() |
NAKAMURA Takumi | cd9c3d6 | 2011-11-28 05:09:42 +0000 | [diff] [blame] | 225 | enable_assertions = False |
Galina Kistanova | b38fd26 | 2011-06-03 18:36:30 +0000 | [diff] [blame] | 226 | |
| 227 | cmd = subprocess.Popen([tool, '-version'], stdout=subprocess.PIPE) |
| 228 | |
| 229 | # Parse the stdout to get the list of registered targets. |
| 230 | parse_targets = False |
| 231 | for line in cmd.stdout: |
| 232 | if parse_targets: |
| 233 | m = re.match( r'(.*) - ', line) |
| 234 | if m is not None: |
| 235 | set_of_targets.add(m.group(1).strip() + '-registered-target') |
| 236 | else: |
| 237 | break |
| 238 | elif "Registered Targets:" in line: |
| 239 | parse_targets = True |
| 240 | |
NAKAMURA Takumi | cd9c3d6 | 2011-11-28 05:09:42 +0000 | [diff] [blame] | 241 | if re.search(r'with assertions', line): |
| 242 | enable_assertions = True |
Galina Kistanova | b38fd26 | 2011-06-03 18:36:30 +0000 | [diff] [blame] | 243 | |
NAKAMURA Takumi | cd9c3d6 | 2011-11-28 05:09:42 +0000 | [diff] [blame] | 244 | return {"set_of_targets": set_of_targets, |
| 245 | "enable_assertions": enable_assertions} |
| 246 | |
| 247 | llc_props = get_llc_props(os.path.join(llvm_tools_dir, 'llc')) |
| 248 | if len(llc_props['set_of_targets']) > 0: |
| 249 | config.available_features.update(llc_props['set_of_targets']) |
Galina Kistanova | b38fd26 | 2011-06-03 18:36:30 +0000 | [diff] [blame] | 250 | else: |
| 251 | lit.fatal('No Targets Registered with the LLVM Tools!') |
NAKAMURA Takumi | cd9c3d6 | 2011-11-28 05:09:42 +0000 | [diff] [blame] | 252 | |
| 253 | if llc_props['enable_assertions']: |
| 254 | config.available_features.add('asserts') |