1# -*- Python -*- 2 3import os 4import platform 5import re 6import subprocess 7import tempfile 8 9import lit.formats 10import lit.util 11 12# Configuration file for the 'lit' test runner. 13 14# name: The name of this test suite. 15config.name = 'Clang' 16 17# Tweak PATH for Win32 18if platform.system() == 'Windows': 19 # Seek sane tools in directories and set to $PATH. 20 path = getattr(config, 'lit_tools_dir', None) 21 path = lit_config.getToolsPath(path, 22 config.environment['PATH'], 23 ['cmp.exe', 'grep.exe', 'sed.exe']) 24 if path is not None: 25 path = os.path.pathsep.join((path, 26 config.environment['PATH'])) 27 config.environment['PATH'] = path 28 29# Choose between lit's internal shell pipeline runner and a real shell. If 30# LIT_USE_INTERNAL_SHELL is in the environment, we use that as an override. 31use_lit_shell = os.environ.get("LIT_USE_INTERNAL_SHELL") 32if use_lit_shell: 33 # 0 is external, "" is default, and everything else is internal. 34 execute_external = (use_lit_shell == "0") 35else: 36 # Otherwise we default to internal on Windows and external elsewhere, as 37 # bash on Windows is usually very slow. 38 execute_external = (not sys.platform in ['win32']) 39 40# testFormat: The test format to use to interpret tests. 41# 42# For now we require '&&' between commands, until they get globally killed and 43# the test runner updated. 44config.test_format = lit.formats.ShTest(execute_external) 45 46# suffixes: A list of file extensions to treat as test files. 47config.suffixes = ['.c', '.cpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s'] 48 49# excludes: A list of directories to exclude from the testsuite. The 'Inputs' 50# subdirectories contain auxiliary inputs for various tests in their parent 51# directories. 52config.excludes = ['Inputs', 'CMakeLists.txt', 'README.txt', 'LICENSE.txt'] 53 54# test_source_root: The root path where tests are located. 55config.test_source_root = os.path.dirname(__file__) 56 57# test_exec_root: The root path where tests should be run. 58clang_obj_root = getattr(config, 'clang_obj_root', None) 59if clang_obj_root is not None: 60 config.test_exec_root = os.path.join(clang_obj_root, 'test') 61 62# Set llvm_{src,obj}_root for use by others. 63config.llvm_src_root = getattr(config, 'llvm_src_root', None) 64config.llvm_obj_root = getattr(config, 'llvm_obj_root', None) 65 66# Clear some environment variables that might affect Clang. 67# 68# This first set of vars are read by Clang, but shouldn't affect tests 69# that aren't specifically looking for these features, or are required 70# simply to run the tests at all. 71# 72# FIXME: Should we have a tool that enforces this? 73 74# safe_env_vars = ('TMPDIR', 'TEMP', 'TMP', 'USERPROFILE', 'PWD', 75# 'MACOSX_DEPLOYMENT_TARGET', 'IPHONEOS_DEPLOYMENT_TARGET', 76# 'IOS_SIMULATOR_DEPLOYMENT_TARGET', 77# 'VCINSTALLDIR', 'VC100COMNTOOLS', 'VC90COMNTOOLS', 78# 'VC80COMNTOOLS') 79possibly_dangerous_env_vars = ['COMPILER_PATH', 'RC_DEBUG_OPTIONS', 80 'CINDEXTEST_PREAMBLE_FILE', 'LIBRARY_PATH', 81 'CPATH', 'C_INCLUDE_PATH', 'CPLUS_INCLUDE_PATH', 82 'OBJC_INCLUDE_PATH', 'OBJCPLUS_INCLUDE_PATH', 83 'LIBCLANG_TIMING', 'LIBCLANG_OBJTRACKING', 84 'LIBCLANG_LOGGING', 'LIBCLANG_BGPRIO_INDEX', 85 'LIBCLANG_BGPRIO_EDIT', 'LIBCLANG_NOTHREADS', 86 'LIBCLANG_RESOURCE_USAGE', 87 'LIBCLANG_CODE_COMPLETION_LOGGING'] 88# Clang/Win32 may refer to %INCLUDE%. vsvarsall.bat sets it. 89if platform.system() != 'Windows': 90 possibly_dangerous_env_vars.append('INCLUDE') 91for name in possibly_dangerous_env_vars: 92 if name in config.environment: 93 del config.environment[name] 94 95# Tweak the PATH to include the tools dir and the scripts dir. 96if clang_obj_root is not None: 97 llvm_tools_dir = getattr(config, 'llvm_tools_dir', None) 98 if not llvm_tools_dir: 99 lit_config.fatal('No LLVM tools dir set!') 100 path = os.path.pathsep.join((llvm_tools_dir, config.environment['PATH'])) 101 config.environment['PATH'] = path 102 llvm_libs_dir = getattr(config, 'llvm_libs_dir', None) 103 if not llvm_libs_dir: 104 lit_config.fatal('No LLVM libs dir set!') 105 path = os.path.pathsep.join((llvm_libs_dir, 106 config.environment.get('LD_LIBRARY_PATH',''))) 107 config.environment['LD_LIBRARY_PATH'] = path 108 109# Propagate path to symbolizer for ASan/MSan. 110for symbolizer in ['ASAN_SYMBOLIZER_PATH', 'MSAN_SYMBOLIZER_PATH']: 111 if symbolizer in os.environ: 112 config.environment[symbolizer] = os.environ[symbolizer] 113 114### 115 116# Check that the object root is known. 117if config.test_exec_root is None: 118 # Otherwise, we haven't loaded the site specific configuration (the user is 119 # probably trying to run on a test file directly, and either the site 120 # configuration hasn't been created by the build system, or we are in an 121 # out-of-tree build situation). 122 123 # Check for 'clang_site_config' user parameter, and use that if available. 124 site_cfg = lit_config.params.get('clang_site_config', None) 125 if site_cfg and os.path.exists(site_cfg): 126 lit_config.load_config(config, site_cfg) 127 raise SystemExit 128 129 # Try to detect the situation where we are using an out-of-tree build by 130 # looking for 'llvm-config'. 131 # 132 # FIXME: I debated (i.e., wrote and threw away) adding logic to 133 # automagically generate the lit.site.cfg if we are in some kind of fresh 134 # build situation. This means knowing how to invoke the build system though, 135 # and I decided it was too much magic. We should solve this by just having 136 # the .cfg files generated during the configuration step. 137 138 llvm_config = lit.util.which('llvm-config', config.environment['PATH']) 139 if not llvm_config: 140 lit_config.fatal('No site specific configuration available!') 141 142 # Get the source and object roots. 143 llvm_src_root = lit.util.capture(['llvm-config', '--src-root']).strip() 144 llvm_obj_root = lit.util.capture(['llvm-config', '--obj-root']).strip() 145 clang_src_root = os.path.join(llvm_src_root, "tools", "clang") 146 clang_obj_root = os.path.join(llvm_obj_root, "tools", "clang") 147 148 # Validate that we got a tree which points to here, using the standard 149 # tools/clang layout. 150 this_src_root = os.path.dirname(config.test_source_root) 151 if os.path.realpath(clang_src_root) != os.path.realpath(this_src_root): 152 lit_config.fatal('No site specific configuration available!') 153 154 # Check that the site specific configuration exists. 155 site_cfg = os.path.join(clang_obj_root, 'test', 'lit.site.cfg') 156 if not os.path.exists(site_cfg): 157 lit_config.fatal( 158 'No site specific configuration available! You may need to ' 159 'run "make test" in your Clang build directory.') 160 161 # Okay, that worked. Notify the user of the automagic, and reconfigure. 162 lit_config.note('using out-of-tree build at %r' % clang_obj_root) 163 lit_config.load_config(config, site_cfg) 164 raise SystemExit 165 166### 167 168# Discover the 'clang' and 'clangcc' to use. 169 170import os 171 172def inferClang(PATH): 173 # Determine which clang to use. 174 clang = os.getenv('CLANG') 175 176 # If the user set clang in the environment, definitely use that and don't 177 # try to validate. 178 if clang: 179 return clang 180 181 # Otherwise look in the path. 182 clang = lit.util.which('clang', PATH) 183 184 if not clang: 185 lit_config.fatal("couldn't find 'clang' program, try setting " 186 "CLANG in your environment") 187 188 return clang 189 190config.clang = inferClang(config.environment['PATH']).replace('\\', '/') 191if not lit_config.quiet: 192 lit_config.note('using clang: %r' % config.clang) 193 194# Note that when substituting %clang_cc1 also fill in the include directory of 195# the builtin headers. Those are part of even a freestanding environment, but 196# Clang relies on the driver to locate them. 197def getClangBuiltinIncludeDir(clang): 198 # FIXME: Rather than just getting the version, we should have clang print 199 # out its resource dir here in an easy to scrape form. 200 cmd = subprocess.Popen([clang, '-print-file-name=include'], 201 stdout=subprocess.PIPE) 202 if not cmd.stdout: 203 lit_config.fatal("Couldn't find the include dir for Clang ('%s')" % clang) 204 dir = cmd.stdout.read().strip() 205 if sys.platform in ['win32'] and execute_external: 206 # Don't pass dosish path separator to msys bash.exe. 207 dir = dir.replace('\\', '/') 208 # Ensure the result is an ascii string, across Python2.5+ - Python3. 209 return str(dir.decode('ascii')) 210 211config.substitutions.append( ('%clang_cc1', '%s -cc1 -internal-isystem %s' 212 % (config.clang, 213 getClangBuiltinIncludeDir(config.clang))) ) 214config.substitutions.append( ('%clang_cpp', ' ' + config.clang + 215 ' --driver-mode=cpp ')) 216config.substitutions.append( ('%clang_cl', ' ' + config.clang + 217 ' --driver-mode=cl ')) 218config.substitutions.append( ('%clangxx', ' ' + config.clang + 219 ' --driver-mode=g++ ')) 220config.substitutions.append( ('%clang', ' ' + config.clang + ' ') ) 221config.substitutions.append( ('%test_debuginfo', ' ' + config.llvm_src_root + '/utils/test_debuginfo.pl ') ) 222 223# FIXME: Find nicer way to prohibit this. 224config.substitutions.append( 225 (' clang ', """*** Do not use 'clang' in tests, use '%clang'. ***""") ) 226config.substitutions.append( 227 (' clang\+\+ ', """*** Do not use 'clang++' in tests, use '%clangxx'. ***""")) 228config.substitutions.append( 229 (' clang-cc ', 230 """*** Do not use 'clang-cc' in tests, use '%clang_cc1'. ***""") ) 231config.substitutions.append( 232 (' clang -cc1 ', 233 """*** Do not use 'clang -cc1' in tests, use '%clang_cc1'. ***""") ) 234config.substitutions.append( 235 (' %clang-cc1 ', 236 """*** invalid substitution, use '%clang_cc1'. ***""") ) 237config.substitutions.append( 238 (' %clang-cpp ', 239 """*** invalid substitution, use '%clang_cpp'. ***""") ) 240config.substitutions.append( 241 (' %clang-cl ', 242 """*** invalid substitution, use '%clang_cl'. ***""") ) 243 244### 245 246# Set available features we allow tests to conditionalize on. 247# 248# As of 2011.08, crash-recovery tests still do not pass on FreeBSD. 249if platform.system() not in ['FreeBSD']: 250 config.available_features.add('crash-recovery') 251 252# Shell execution 253if execute_external: 254 config.available_features.add('shell') 255 256# Exclude MSYS due to transforming '/' to 'X:/mingwroot/'. 257if not platform.system() in ['Windows'] or not execute_external: 258 config.available_features.add('shell-preserves-root') 259 260# ANSI escape sequences in non-dumb terminal 261if platform.system() not in ['Windows']: 262 config.available_features.add('ansi-escape-sequences') 263 264# Native compilation: host arch == target arch 265if config.host_arch in config.target_triple: 266 config.available_features.add("native") 267 268# Case-insensitive file system 269def is_filesystem_case_insensitive(): 270 handle, path = tempfile.mkstemp(prefix='case-test', dir=config.test_exec_root) 271 isInsensitive = os.path.exists( 272 os.path.join( 273 os.path.dirname(path), 274 os.path.basename(path).upper() 275 )) 276 os.close(handle) 277 os.remove(path) 278 return isInsensitive 279 280if is_filesystem_case_insensitive(): 281 config.available_features.add('case-insensitive-filesystem') 282 283# Tests that require the /dev/fd filesystem. 284if os.path.exists("/dev/fd/0") and sys.platform not in ['cygwin']: 285 config.available_features.add('dev-fd-fs') 286 287# [PR8833] LLP64-incompatible tests 288if not re.match(r'^x86_64.*-(win32|mingw32)$', config.target_triple): 289 config.available_features.add('LP64') 290 291# [PR12920] "clang-driver" -- set if gcc driver is not used. 292if not re.match(r'.*-(cygwin|mingw32)$', config.target_triple): 293 config.available_features.add('clang-driver') 294 295# Registered Targets 296def get_llc_props(tool): 297 set_of_targets = set() 298 enable_assertions = False 299 300 cmd = subprocess.Popen([tool, '-version'], stdout=subprocess.PIPE) 301 302 # Parse the stdout to get the list of registered targets. 303 parse_targets = False 304 for line in cmd.stdout: 305 line = line.decode('ascii') 306 if parse_targets: 307 m = re.match( r'(.*) - ', line) 308 if m is not None: 309 set_of_targets.add(m.group(1).strip() + '-registered-target') 310 else: 311 break 312 elif "Registered Targets:" in line: 313 parse_targets = True 314 315 if re.search(r'with assertions', line): 316 enable_assertions = True 317 318 return {"set_of_targets": set_of_targets, 319 "enable_assertions": enable_assertions} 320 321llc_props = get_llc_props(os.path.join(llvm_tools_dir, 'llc')) 322if len(llc_props['set_of_targets']) > 0: 323 config.available_features.update(llc_props['set_of_targets']) 324else: 325 lit_config.fatal('No Targets Registered with the LLVM Tools!') 326 327if llc_props['enable_assertions']: 328 config.available_features.add('asserts') 329 330if lit.util.which('xmllint'): 331 config.available_features.add('xmllint') 332 333# Sanitizers. 334if config.llvm_use_sanitizer == "Address": 335 config.available_features.add("asan") 336if (config.llvm_use_sanitizer == "Memory" or 337 config.llvm_use_sanitizer == "MemoryWithOrigins"): 338 config.available_features.add("msan") 339 340# Check if we should run long running tests. 341if lit_config.params.get("run_long_tests", None) == "true": 342 config.available_features.add("long_tests") 343 344# Check if we should use gmalloc. 345use_gmalloc_str = lit_config.params.get('use_gmalloc', None) 346if use_gmalloc_str is not None: 347 if use_gmalloc_str.lower() in ('1', 'true'): 348 use_gmalloc = True 349 elif use_gmalloc_str.lower() in ('', '0', 'false'): 350 use_gmalloc = False 351 else: 352 lit_config.fatal('user parameter use_gmalloc should be 0 or 1') 353else: 354 # Default to not using gmalloc 355 use_gmalloc = False 356 357# Allow use of an explicit path for gmalloc library. 358# Will default to '/usr/lib/libgmalloc.dylib' if not set. 359gmalloc_path_str = lit_config.params.get('gmalloc_path', 360 '/usr/lib/libgmalloc.dylib') 361if use_gmalloc: 362 config.environment.update({'DYLD_INSERT_LIBRARIES' : gmalloc_path_str}) 363 364# On Darwin, support relocatable SDKs by providing Clang with a 365# default system root path. 366if 'darwin' in config.target_triple: 367 try: 368 cmd = subprocess.Popen(['xcrun', '--show-sdk-path'], 369 stdout=subprocess.PIPE, stderr=subprocess.PIPE) 370 out, err = cmd.communicate() 371 out = out.strip() 372 res = cmd.wait() 373 except OSError: 374 res = -1 375 if res == 0 and out: 376 sdk_path = out 377 lit_config.note('using SDKROOT: %r' % sdk_path) 378 config.environment['SDKROOT'] = sdk_path 379