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', '.S', '.modulemap'] 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# 'VCINSTALLDIR', 'VC100COMNTOOLS', 'VC90COMNTOOLS', 77# 'VC80COMNTOOLS') 78possibly_dangerous_env_vars = ['COMPILER_PATH', 'RC_DEBUG_OPTIONS', 79 'CINDEXTEST_PREAMBLE_FILE', 'LIBRARY_PATH', 80 'CPATH', 'C_INCLUDE_PATH', 'CPLUS_INCLUDE_PATH', 81 'OBJC_INCLUDE_PATH', 'OBJCPLUS_INCLUDE_PATH', 82 'LIBCLANG_TIMING', 'LIBCLANG_OBJTRACKING', 83 'LIBCLANG_LOGGING', 'LIBCLANG_BGPRIO_INDEX', 84 'LIBCLANG_BGPRIO_EDIT', 'LIBCLANG_NOTHREADS', 85 'LIBCLANG_RESOURCE_USAGE', 86 'LIBCLANG_CODE_COMPLETION_LOGGING'] 87# Clang/Win32 may refer to %INCLUDE%. vsvarsall.bat sets it. 88if platform.system() != 'Windows': 89 possibly_dangerous_env_vars.append('INCLUDE') 90for name in possibly_dangerous_env_vars: 91 if name in config.environment: 92 del config.environment[name] 93 94# Tweak the PATH to include the tools dir and the scripts dir. 95if clang_obj_root is not None: 96 clang_tools_dir = getattr(config, 'clang_tools_dir', None) 97 if not clang_tools_dir: 98 lit_config.fatal('No Clang tools dir set!') 99 llvm_tools_dir = getattr(config, 'llvm_tools_dir', None) 100 if not llvm_tools_dir: 101 lit_config.fatal('No LLVM tools dir set!') 102 path = os.path.pathsep.join(( 103 clang_tools_dir, llvm_tools_dir, config.environment['PATH'])) 104 config.environment['PATH'] = path 105 llvm_libs_dir = getattr(config, 'llvm_libs_dir', None) 106 if not llvm_libs_dir: 107 lit_config.fatal('No LLVM libs dir set!') 108 path = os.path.pathsep.join((llvm_libs_dir, 109 config.environment.get('LD_LIBRARY_PATH',''))) 110 config.environment['LD_LIBRARY_PATH'] = path 111 112# Propagate path to symbolizer for ASan/MSan. 113for symbolizer in ['ASAN_SYMBOLIZER_PATH', 'MSAN_SYMBOLIZER_PATH']: 114 if symbolizer in os.environ: 115 config.environment[symbolizer] = os.environ[symbolizer] 116 117### 118 119# Check that the object root is known. 120if config.test_exec_root is None: 121 # Otherwise, we haven't loaded the site specific configuration (the user is 122 # probably trying to run on a test file directly, and either the site 123 # configuration hasn't been created by the build system, or we are in an 124 # out-of-tree build situation). 125 126 # Check for 'clang_site_config' user parameter, and use that if available. 127 site_cfg = lit_config.params.get('clang_site_config', None) 128 if site_cfg and os.path.exists(site_cfg): 129 lit_config.load_config(config, site_cfg) 130 raise SystemExit 131 132 # Try to detect the situation where we are using an out-of-tree build by 133 # looking for 'llvm-config'. 134 # 135 # FIXME: I debated (i.e., wrote and threw away) adding logic to 136 # automagically generate the lit.site.cfg if we are in some kind of fresh 137 # build situation. This means knowing how to invoke the build system though, 138 # and I decided it was too much magic. We should solve this by just having 139 # the .cfg files generated during the configuration step. 140 141 llvm_config = lit.util.which('llvm-config', config.environment['PATH']) 142 if not llvm_config: 143 lit_config.fatal('No site specific configuration available!') 144 145 # Get the source and object roots. 146 llvm_src_root = lit.util.capture(['llvm-config', '--src-root']).strip() 147 llvm_obj_root = lit.util.capture(['llvm-config', '--obj-root']).strip() 148 clang_src_root = os.path.join(llvm_src_root, "tools", "clang") 149 clang_obj_root = os.path.join(llvm_obj_root, "tools", "clang") 150 151 # Validate that we got a tree which points to here, using the standard 152 # tools/clang layout. 153 this_src_root = os.path.dirname(config.test_source_root) 154 if os.path.realpath(clang_src_root) != os.path.realpath(this_src_root): 155 lit_config.fatal('No site specific configuration available!') 156 157 # Check that the site specific configuration exists. 158 site_cfg = os.path.join(clang_obj_root, 'test', 'lit.site.cfg') 159 if not os.path.exists(site_cfg): 160 lit_config.fatal( 161 'No site specific configuration available! You may need to ' 162 'run "make test" in your Clang build directory.') 163 164 # Okay, that worked. Notify the user of the automagic, and reconfigure. 165 lit_config.note('using out-of-tree build at %r' % clang_obj_root) 166 lit_config.load_config(config, site_cfg) 167 raise SystemExit 168 169### 170 171# Discover the 'clang' and 'clangcc' to use. 172 173import os 174 175def inferClang(PATH): 176 # Determine which clang to use. 177 clang = os.getenv('CLANG') 178 179 # If the user set clang in the environment, definitely use that and don't 180 # try to validate. 181 if clang: 182 return clang 183 184 # Otherwise look in the path. 185 clang = lit.util.which('clang', PATH) 186 187 if not clang: 188 lit_config.fatal("couldn't find 'clang' program, try setting " 189 "CLANG in your environment") 190 191 return clang 192 193config.clang = inferClang(config.environment['PATH']).replace('\\', '/') 194if not lit_config.quiet: 195 lit_config.note('using clang: %r' % config.clang) 196 197# Plugins (loadable modules) 198# TODO: This should be supplied by Makefile or autoconf. 199if sys.platform in ['win32', 'cygwin']: 200 has_plugins = (config.enable_shared == 1) 201else: 202 has_plugins = True 203 204if has_plugins and config.llvm_plugin_ext: 205 config.available_features.add('plugins') 206 207config.substitutions.append( ('%llvmshlibdir', config.llvm_shlib_dir) ) 208config.substitutions.append( ('%pluginext', config.llvm_plugin_ext) ) 209 210if config.clang_examples: 211 config.available_features.add('examples') 212 213# Note that when substituting %clang_cc1 also fill in the include directory of 214# the builtin headers. Those are part of even a freestanding environment, but 215# Clang relies on the driver to locate them. 216def getClangBuiltinIncludeDir(clang): 217 # FIXME: Rather than just getting the version, we should have clang print 218 # out its resource dir here in an easy to scrape form. 219 cmd = subprocess.Popen([clang, '-print-file-name=include'], 220 stdout=subprocess.PIPE, 221 env=config.environment) 222 if not cmd.stdout: 223 lit_config.fatal("Couldn't find the include dir for Clang ('%s')" % clang) 224 dir = cmd.stdout.read().strip() 225 if sys.platform in ['win32'] and execute_external: 226 # Don't pass dosish path separator to msys bash.exe. 227 dir = dir.replace('\\', '/') 228 # Ensure the result is an ascii string, across Python2.5+ - Python3. 229 return str(dir.decode('ascii')) 230 231def makeItaniumABITriple(triple): 232 m = re.match(r'(\w+)-(\w+)-(\w+)', triple) 233 if not m: 234 lit_config.fatal("Could not turn '%s' into Itanium ABI triple" % triple) 235 if m.group(3).lower() != 'win32': 236 # All non-win32 triples use the Itanium ABI. 237 return triple 238 return m.group(1) + '-' + m.group(2) + '-mingw32' 239 240def makeMSABITriple(triple): 241 m = re.match(r'(\w+)-(\w+)-(\w+)', triple) 242 if not m: 243 lit_config.fatal("Could not turn '%s' into MS ABI triple" % triple) 244 isa = m.group(1).lower() 245 vendor = m.group(2).lower() 246 os = m.group(3).lower() 247 if os == 'win32': 248 # If the OS is win32, we're done. 249 return triple 250 if isa.startswith('x86') or isa == 'amd64' or re.match(r'i\d86', isa): 251 # For x86 ISAs, adjust the OS. 252 return isa + '-' + vendor + '-win32' 253 # -win32 is not supported for non-x86 targets; use a default. 254 return 'i686-pc-win32' 255 256config.substitutions.append( ('%clang_cc1', 257 '%s -cc1 -internal-isystem %s -nostdsysteminc' 258 % (config.clang, 259 getClangBuiltinIncludeDir(config.clang))) ) 260config.substitutions.append( ('%clang_cpp', ' ' + config.clang + 261 ' --driver-mode=cpp ')) 262config.substitutions.append( ('%clang_cl', ' ' + config.clang + 263 ' --driver-mode=cl ')) 264config.substitutions.append( ('%clangxx', ' ' + config.clang + 265 ' --driver-mode=g++ ')) 266config.substitutions.append( ('%clang', ' ' + config.clang + ' ') ) 267config.substitutions.append( ('%test_debuginfo', ' ' + config.llvm_src_root + '/utils/test_debuginfo.pl ') ) 268config.substitutions.append( ('%itanium_abi_triple', makeItaniumABITriple(config.target_triple)) ) 269config.substitutions.append( ('%ms_abi_triple', makeMSABITriple(config.target_triple)) ) 270 271# The host triple might not be set, at least if we're compiling clang from 272# an already installed llvm. 273if config.host_triple and config.host_triple != '@LLVM_HOST_TRIPLE@': 274 config.substitutions.append( ('%target_itanium_abi_host_triple', '--target=%s' % makeItaniumABITriple(config.host_triple)) ) 275else: 276 config.substitutions.append( ('%target_itanium_abi_host_triple', '') ) 277 278# FIXME: Find nicer way to prohibit this. 279config.substitutions.append( 280 (' clang ', """*** Do not use 'clang' in tests, use '%clang'. ***""") ) 281config.substitutions.append( 282 (' clang\+\+ ', """*** Do not use 'clang++' in tests, use '%clangxx'. ***""")) 283config.substitutions.append( 284 (' clang-cc ', 285 """*** Do not use 'clang-cc' in tests, use '%clang_cc1'. ***""") ) 286config.substitutions.append( 287 (' clang -cc1 ', 288 """*** Do not use 'clang -cc1' in tests, use '%clang_cc1'. ***""") ) 289config.substitutions.append( 290 (' %clang-cc1 ', 291 """*** invalid substitution, use '%clang_cc1'. ***""") ) 292config.substitutions.append( 293 (' %clang-cpp ', 294 """*** invalid substitution, use '%clang_cpp'. ***""") ) 295config.substitutions.append( 296 (' %clang-cl ', 297 """*** invalid substitution, use '%clang_cl'. ***""") ) 298 299# For each occurrence of a clang tool name as its own word, replace it 300# with the full path to the build directory holding that tool. This 301# ensures that we are testing the tools just built and not some random 302# tools that might happen to be in the user's PATH. 303tool_dirs = os.path.pathsep.join((clang_tools_dir, llvm_tools_dir)) 304 305# Regex assertions to reject neighbor hyphens/dots (seen in some tests). 306# For example, don't match 'clang-check-' or '.clang-format'. 307NoPreHyphenDot = r"(?<!(-|\.))" 308NoPostHyphenDot = r"(?!(-|\.))" 309NoPostBar = r"(?!(/|\\))" 310 311for pattern in [r"\bFileCheck\b", 312 r"\bc-index-test\b", 313 NoPreHyphenDot + r"\bclang-check\b" + NoPostHyphenDot, 314 NoPreHyphenDot + r"\bclang-format\b" + NoPostHyphenDot, 315 NoPreHyphenDot + r"\bclang-interpreter\b" + NoPostHyphenDot, 316 # FIXME: Some clang test uses opt? 317 NoPreHyphenDot + r"\bopt\b" + NoPostBar + NoPostHyphenDot, 318 # Handle these specially as they are strings searched 319 # for during testing. 320 r"\| \bcount\b", 321 r"\| \bnot\b"]: 322 # Extract the tool name from the pattern. This relies on the tool 323 # name being surrounded by \b word match operators. If the 324 # pattern starts with "| ", include it in the string to be 325 # substituted. 326 tool_match = re.match(r"^(\\)?((\| )?)\W+b([0-9A-Za-z-_]+)\\b\W*$", 327 pattern) 328 tool_pipe = tool_match.group(2) 329 tool_name = tool_match.group(4) 330 tool_path = lit.util.which(tool_name, tool_dirs) 331 if not tool_path: 332 # Warn, but still provide a substitution. 333 lit_config.note('Did not find ' + tool_name + ' in ' + tool_dirs) 334 tool_path = clang_tools_dir + '/' + tool_name 335 config.substitutions.append((pattern, tool_pipe + tool_path)) 336 337### 338 339# Set available features we allow tests to conditionalize on. 340# 341# Enabled/disabled features 342if config.clang_staticanalyzer != 0: 343 config.available_features.add("staticanalyzer") 344 345# As of 2011.08, crash-recovery tests still do not pass on FreeBSD. 346if platform.system() not in ['FreeBSD']: 347 config.available_features.add('crash-recovery') 348 349# Shell execution 350if execute_external: 351 config.available_features.add('shell') 352 353# Exclude MSYS due to transforming '/' to 'X:/mingwroot/'. 354if not platform.system() in ['Windows'] or not execute_external: 355 config.available_features.add('shell-preserves-root') 356 357# For tests that require Darwin to run. 358# This is used by debuginfo-tests/*block*.m and debuginfo-tests/foreach.m. 359if platform.system() in ['Darwin']: 360 config.available_features.add('system-darwin') 361elif platform.system() in ['Windows']: 362 # For tests that require Windows to run. 363 config.available_features.add('system-windows') 364 365# ANSI escape sequences in non-dumb terminal 366if platform.system() not in ['Windows']: 367 config.available_features.add('ansi-escape-sequences') 368 369# Capability to print utf8 to the terminal. 370# Windows expects codepage, unless Wide API. 371if platform.system() not in ['Windows']: 372 config.available_features.add('utf8-capable-terminal') 373 374# Native compilation: Check if triples match. 375# FIXME: Consider cases that target can be executed 376# even if host_triple were different from target_triple. 377if config.host_triple == config.target_triple: 378 config.available_features.add("native") 379 380# Case-insensitive file system 381def is_filesystem_case_insensitive(): 382 handle, path = tempfile.mkstemp(prefix='case-test', dir=config.test_exec_root) 383 isInsensitive = os.path.exists( 384 os.path.join( 385 os.path.dirname(path), 386 os.path.basename(path).upper() 387 )) 388 os.close(handle) 389 os.remove(path) 390 return isInsensitive 391 392if is_filesystem_case_insensitive(): 393 config.available_features.add('case-insensitive-filesystem') 394 395# Tests that require the /dev/fd filesystem. 396if os.path.exists("/dev/fd/0") and sys.platform not in ['cygwin']: 397 config.available_features.add('dev-fd-fs') 398 399# DW2 Target 400if not re.match(r'.*-win32$', config.target_triple): 401 config.available_features.add('dw2') 402 403# Not set on native MS environment. 404if not re.match(r'.*-win32$', config.target_triple): 405 config.available_features.add('non-ms-sdk') 406 407# Not set on native PS4 environment. 408if not re.match(r'.*-scei-ps4', config.target_triple): 409 config.available_features.add('non-ps4-sdk') 410 411# [PR8833] LLP64-incompatible tests 412if not re.match(r'^x86_64.*-(win32|mingw32|windows-gnu)$', config.target_triple): 413 config.available_features.add('LP64') 414 415# [PR12920] "clang-driver" -- set if gcc driver is not used. 416if not re.match(r'.*-(cygwin|mingw32|windows-gnu)$', config.target_triple): 417 config.available_features.add('clang-driver') 418 419# [PR18856] Depends to remove opened file. On win32, a file could be removed 420# only if all handles were closed. 421if platform.system() not in ['Windows']: 422 config.available_features.add('can-remove-opened-file') 423 424# Returns set of available features, registered-target(s) and asserts. 425def get_llvm_config_props(): 426 set_of_features = set() 427 428 cmd = subprocess.Popen( 429 [ 430 os.path.join(llvm_tools_dir, 'llvm-config'), 431 '--assertion-mode', 432 '--targets-built', 433 ], 434 stdout=subprocess.PIPE, 435 env=config.environment 436 ) 437 # 1st line corresponds to --assertion-mode, "ON" or "OFF". 438 line = cmd.stdout.readline().strip().decode('ascii') 439 if line == "ON": 440 set_of_features.add('asserts') 441 442 # 2nd line corresponds to --targets-built, like; 443 # AArch64 ARM CppBackend X86 444 for arch in cmd.stdout.readline().decode('ascii').split(): 445 set_of_features.add(arch.lower() + '-registered-target') 446 447 return set_of_features 448 449config.available_features.update(get_llvm_config_props()) 450 451if lit.util.which('xmllint'): 452 config.available_features.add('xmllint') 453 454# Sanitizers. 455if config.llvm_use_sanitizer == "Address": 456 config.available_features.add("asan") 457else: 458 config.available_features.add("not_asan") 459if (config.llvm_use_sanitizer == "Memory" or 460 config.llvm_use_sanitizer == "MemoryWithOrigins"): 461 config.available_features.add("msan") 462if config.llvm_use_sanitizer == "Undefined": 463 config.available_features.add("ubsan") 464else: 465 config.available_features.add("not_ubsan") 466 467if config.enable_backtrace == "1": 468 config.available_features.add("backtrace") 469 470# Check if we should run long running tests. 471if lit_config.params.get("run_long_tests", None) == "true": 472 config.available_features.add("long_tests") 473 474# Check if we should use gmalloc. 475use_gmalloc_str = lit_config.params.get('use_gmalloc', None) 476if use_gmalloc_str is not None: 477 if use_gmalloc_str.lower() in ('1', 'true'): 478 use_gmalloc = True 479 elif use_gmalloc_str.lower() in ('', '0', 'false'): 480 use_gmalloc = False 481 else: 482 lit_config.fatal('user parameter use_gmalloc should be 0 or 1') 483else: 484 # Default to not using gmalloc 485 use_gmalloc = False 486 487# Allow use of an explicit path for gmalloc library. 488# Will default to '/usr/lib/libgmalloc.dylib' if not set. 489gmalloc_path_str = lit_config.params.get('gmalloc_path', 490 '/usr/lib/libgmalloc.dylib') 491if use_gmalloc: 492 config.environment.update({'DYLD_INSERT_LIBRARIES' : gmalloc_path_str}) 493 494lit.util.usePlatformSdkOnDarwin(config, lit_config) 495