lit.cfg 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. # -*- Python -*-
  2. import os
  3. import platform
  4. import re
  5. import subprocess
  6. import tempfile
  7. import lit.formats
  8. import lit.util
  9. # Configuration file for the 'lit' test runner.
  10. # name: The name of this test suite.
  11. config.name = 'Clang'
  12. # Tweak PATH for Win32
  13. if platform.system() == 'Windows':
  14. # Seek sane tools in directories and set to $PATH.
  15. path = getattr(config, 'lit_tools_dir', None)
  16. path = lit_config.getToolsPath(path,
  17. config.environment['PATH'],
  18. ['cmp.exe', 'grep.exe', 'sed.exe'])
  19. if path is not None:
  20. path = os.path.pathsep.join((path,
  21. config.environment['PATH']))
  22. config.environment['PATH'] = path
  23. # Choose between lit's internal shell pipeline runner and a real shell. If
  24. # LIT_USE_INTERNAL_SHELL is in the environment, we use that as an override.
  25. use_lit_shell = os.environ.get("LIT_USE_INTERNAL_SHELL")
  26. if use_lit_shell:
  27. # 0 is external, "" is default, and everything else is internal.
  28. execute_external = (use_lit_shell == "0")
  29. else:
  30. # Otherwise we default to internal on Windows and external elsewhere, as
  31. # bash on Windows is usually very slow.
  32. execute_external = (not sys.platform in ['win32'])
  33. # testFormat: The test format to use to interpret tests.
  34. #
  35. # For now we require '&&' between commands, until they get globally killed and
  36. # the test runner updated.
  37. config.test_format = lit.formats.ShTest(execute_external)
  38. # suffixes: A list of file extensions to treat as test files.
  39. config.suffixes = ['.c', '.cpp', '.m', '.mm', '.cu', '.ll', '.cl', '.s', '.S', '.modulemap']
  40. # excludes: A list of directories to exclude from the testsuite. The 'Inputs'
  41. # subdirectories contain auxiliary inputs for various tests in their parent
  42. # directories.
  43. config.excludes = ['Inputs', 'CMakeLists.txt', 'README.txt', 'LICENSE.txt']
  44. # test_source_root: The root path where tests are located.
  45. config.test_source_root = os.path.dirname(__file__)
  46. # test_exec_root: The root path where tests should be run.
  47. clang_obj_root = getattr(config, 'clang_obj_root', None)
  48. if clang_obj_root is not None:
  49. config.test_exec_root = os.path.join(clang_obj_root, 'test')
  50. # Set llvm_{src,obj}_root for use by others.
  51. config.llvm_src_root = getattr(config, 'llvm_src_root', None)
  52. config.llvm_obj_root = getattr(config, 'llvm_obj_root', None)
  53. # Clear some environment variables that might affect Clang.
  54. #
  55. # This first set of vars are read by Clang, but shouldn't affect tests
  56. # that aren't specifically looking for these features, or are required
  57. # simply to run the tests at all.
  58. #
  59. # FIXME: Should we have a tool that enforces this?
  60. # safe_env_vars = ('TMPDIR', 'TEMP', 'TMP', 'USERPROFILE', 'PWD',
  61. # 'MACOSX_DEPLOYMENT_TARGET', 'IPHONEOS_DEPLOYMENT_TARGET',
  62. # 'VCINSTALLDIR', 'VC100COMNTOOLS', 'VC90COMNTOOLS',
  63. # 'VC80COMNTOOLS')
  64. possibly_dangerous_env_vars = ['COMPILER_PATH', 'RC_DEBUG_OPTIONS',
  65. 'CINDEXTEST_PREAMBLE_FILE', 'LIBRARY_PATH',
  66. 'CPATH', 'C_INCLUDE_PATH', 'CPLUS_INCLUDE_PATH',
  67. 'OBJC_INCLUDE_PATH', 'OBJCPLUS_INCLUDE_PATH',
  68. 'LIBCLANG_TIMING', 'LIBCLANG_OBJTRACKING',
  69. 'LIBCLANG_LOGGING', 'LIBCLANG_BGPRIO_INDEX',
  70. 'LIBCLANG_BGPRIO_EDIT', 'LIBCLANG_NOTHREADS',
  71. 'LIBCLANG_RESOURCE_USAGE',
  72. 'LIBCLANG_CODE_COMPLETION_LOGGING']
  73. # Clang/Win32 may refer to %INCLUDE%. vsvarsall.bat sets it.
  74. if platform.system() != 'Windows':
  75. possibly_dangerous_env_vars.append('INCLUDE')
  76. for name in possibly_dangerous_env_vars:
  77. if name in config.environment:
  78. del config.environment[name]
  79. # Tweak the PATH to include the tools dir and the scripts dir.
  80. if clang_obj_root is not None:
  81. clang_tools_dir = getattr(config, 'clang_tools_dir', None)
  82. if not clang_tools_dir:
  83. lit_config.fatal('No Clang tools dir set!')
  84. llvm_tools_dir = getattr(config, 'llvm_tools_dir', None)
  85. if not llvm_tools_dir:
  86. lit_config.fatal('No LLVM tools dir set!')
  87. path = os.path.pathsep.join((
  88. clang_tools_dir, llvm_tools_dir, config.environment['PATH']))
  89. config.environment['PATH'] = path
  90. llvm_libs_dir = getattr(config, 'llvm_libs_dir', None)
  91. if not llvm_libs_dir:
  92. lit_config.fatal('No LLVM libs dir set!')
  93. path = os.path.pathsep.join((llvm_libs_dir,
  94. config.environment.get('LD_LIBRARY_PATH','')))
  95. config.environment['LD_LIBRARY_PATH'] = path
  96. # Propagate path to symbolizer for ASan/MSan.
  97. for symbolizer in ['ASAN_SYMBOLIZER_PATH', 'MSAN_SYMBOLIZER_PATH']:
  98. if symbolizer in os.environ:
  99. config.environment[symbolizer] = os.environ[symbolizer]
  100. ###
  101. # Check that the object root is known.
  102. if config.test_exec_root is None:
  103. # Otherwise, we haven't loaded the site specific configuration (the user is
  104. # probably trying to run on a test file directly, and either the site
  105. # configuration hasn't been created by the build system, or we are in an
  106. # out-of-tree build situation).
  107. # Check for 'clang_site_config' user parameter, and use that if available.
  108. site_cfg = lit_config.params.get('clang_site_config', None)
  109. if site_cfg and os.path.exists(site_cfg):
  110. lit_config.load_config(config, site_cfg)
  111. raise SystemExit
  112. # Try to detect the situation where we are using an out-of-tree build by
  113. # looking for 'llvm-config'.
  114. #
  115. # FIXME: I debated (i.e., wrote and threw away) adding logic to
  116. # automagically generate the lit.site.cfg if we are in some kind of fresh
  117. # build situation. This means knowing how to invoke the build system though,
  118. # and I decided it was too much magic. We should solve this by just having
  119. # the .cfg files generated during the configuration step.
  120. llvm_config = lit.util.which('llvm-config', config.environment['PATH'])
  121. if not llvm_config:
  122. lit_config.fatal('No site specific configuration available!')
  123. # Get the source and object roots.
  124. llvm_src_root = lit.util.capture(['llvm-config', '--src-root']).strip()
  125. llvm_obj_root = lit.util.capture(['llvm-config', '--obj-root']).strip()
  126. clang_src_root = os.path.join(llvm_src_root, "tools", "clang")
  127. clang_obj_root = os.path.join(llvm_obj_root, "tools", "clang")
  128. # Validate that we got a tree which points to here, using the standard
  129. # tools/clang layout.
  130. this_src_root = os.path.dirname(config.test_source_root)
  131. if os.path.realpath(clang_src_root) != os.path.realpath(this_src_root):
  132. lit_config.fatal('No site specific configuration available!')
  133. # Check that the site specific configuration exists.
  134. site_cfg = os.path.join(clang_obj_root, 'test', 'lit.site.cfg')
  135. if not os.path.exists(site_cfg):
  136. lit_config.fatal(
  137. 'No site specific configuration available! You may need to '
  138. 'run "make test" in your Clang build directory.')
  139. # Okay, that worked. Notify the user of the automagic, and reconfigure.
  140. lit_config.note('using out-of-tree build at %r' % clang_obj_root)
  141. lit_config.load_config(config, site_cfg)
  142. raise SystemExit
  143. ###
  144. # Discover the 'clang' and 'clangcc' to use.
  145. import os
  146. def inferClang(PATH):
  147. # Determine which clang to use.
  148. clang = os.getenv('CLANG')
  149. # If the user set clang in the environment, definitely use that and don't
  150. # try to validate.
  151. if clang:
  152. return clang
  153. # Otherwise look in the path.
  154. clang = lit.util.which('clang', PATH)
  155. if not clang:
  156. lit_config.fatal("couldn't find 'clang' program, try setting "
  157. "CLANG in your environment")
  158. return clang
  159. config.clang = inferClang(config.environment['PATH']).replace('\\', '/')
  160. if not lit_config.quiet:
  161. lit_config.note('using clang: %r' % config.clang)
  162. # Plugins (loadable modules)
  163. # TODO: This should be supplied by Makefile or autoconf.
  164. if sys.platform in ['win32', 'cygwin']:
  165. has_plugins = (config.enable_shared == 1)
  166. else:
  167. has_plugins = True
  168. if has_plugins and config.llvm_plugin_ext:
  169. config.available_features.add('plugins')
  170. config.substitutions.append( ('%llvmshlibdir', config.llvm_shlib_dir) )
  171. config.substitutions.append( ('%pluginext', config.llvm_plugin_ext) )
  172. if config.clang_examples:
  173. config.available_features.add('examples')
  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.
  177. def getClangBuiltinIncludeDir(clang):
  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.
  180. cmd = subprocess.Popen([clang, '-print-file-name=include'],
  181. stdout=subprocess.PIPE,
  182. env=config.environment)
  183. if not cmd.stdout:
  184. lit_config.fatal("Couldn't find the include dir for Clang ('%s')" % clang)
  185. dir = cmd.stdout.read().strip()
  186. if sys.platform in ['win32'] and execute_external:
  187. # Don't pass dosish path separator to msys bash.exe.
  188. dir = dir.replace('\\', '/')
  189. # Ensure the result is an ascii string, across Python2.5+ - Python3.
  190. return str(dir.decode('ascii'))
  191. def makeItaniumABITriple(triple):
  192. m = re.match(r'(\w+)-(\w+)-(\w+)', triple)
  193. if not m:
  194. lit_config.fatal("Could not turn '%s' into Itanium ABI triple" % triple)
  195. if m.group(3).lower() != 'win32':
  196. # All non-win32 triples use the Itanium ABI.
  197. return triple
  198. return m.group(1) + '-' + m.group(2) + '-mingw32'
  199. def makeMSABITriple(triple):
  200. m = re.match(r'(\w+)-(\w+)-(\w+)', triple)
  201. if not m:
  202. lit_config.fatal("Could not turn '%s' into MS ABI triple" % triple)
  203. isa = m.group(1).lower()
  204. vendor = m.group(2).lower()
  205. os = m.group(3).lower()
  206. if os == 'win32':
  207. # If the OS is win32, we're done.
  208. return triple
  209. if isa.startswith('x86') or isa == 'amd64' or re.match(r'i\d86', isa):
  210. # For x86 ISAs, adjust the OS.
  211. return isa + '-' + vendor + '-win32'
  212. # -win32 is not supported for non-x86 targets; use a default.
  213. return 'i686-pc-win32'
  214. config.substitutions.append( ('%clang_cc1',
  215. '%s -cc1 -internal-isystem %s -nostdsysteminc'
  216. % (config.clang,
  217. getClangBuiltinIncludeDir(config.clang))) )
  218. config.substitutions.append( ('%clang_cpp', ' ' + config.clang +
  219. ' --driver-mode=cpp '))
  220. config.substitutions.append( ('%clang_cl', ' ' + config.clang +
  221. ' --driver-mode=cl '))
  222. config.substitutions.append( ('%clangxx', ' ' + config.clang +
  223. ' --driver-mode=g++ '))
  224. config.substitutions.append( ('%clang', ' ' + config.clang + ' ') )
  225. config.substitutions.append( ('%test_debuginfo', ' ' + config.llvm_src_root + '/utils/test_debuginfo.pl ') )
  226. config.substitutions.append( ('%itanium_abi_triple', makeItaniumABITriple(config.target_triple)) )
  227. config.substitutions.append( ('%ms_abi_triple', makeMSABITriple(config.target_triple)) )
  228. # The host triple might not be set, at least if we're compiling clang from
  229. # an already installed llvm.
  230. if config.host_triple and config.host_triple != '@LLVM_HOST_TRIPLE@':
  231. config.substitutions.append( ('%target_itanium_abi_host_triple', '--target=%s' % makeItaniumABITriple(config.host_triple)) )
  232. else:
  233. config.substitutions.append( ('%target_itanium_abi_host_triple', '') )
  234. # FIXME: Find nicer way to prohibit this.
  235. config.substitutions.append(
  236. (' clang ', """*** Do not use 'clang' in tests, use '%clang'. ***""") )
  237. config.substitutions.append(
  238. (' clang\+\+ ', """*** Do not use 'clang++' in tests, use '%clangxx'. ***"""))
  239. config.substitutions.append(
  240. (' clang-cc ',
  241. """*** Do not use 'clang-cc' in tests, use '%clang_cc1'. ***""") )
  242. config.substitutions.append(
  243. (' clang -cc1 ',
  244. """*** Do not use 'clang -cc1' in tests, use '%clang_cc1'. ***""") )
  245. config.substitutions.append(
  246. (' %clang-cc1 ',
  247. """*** invalid substitution, use '%clang_cc1'. ***""") )
  248. config.substitutions.append(
  249. (' %clang-cpp ',
  250. """*** invalid substitution, use '%clang_cpp'. ***""") )
  251. config.substitutions.append(
  252. (' %clang-cl ',
  253. """*** invalid substitution, use '%clang_cl'. ***""") )
  254. # For each occurrence of a clang tool name as its own word, replace it
  255. # with the full path to the build directory holding that tool. This
  256. # ensures that we are testing the tools just built and not some random
  257. # tools that might happen to be in the user's PATH.
  258. tool_dirs = os.path.pathsep.join((clang_tools_dir, llvm_tools_dir))
  259. # Regex assertions to reject neighbor hyphens/dots (seen in some tests).
  260. # For example, don't match 'clang-check-' or '.clang-format'.
  261. NoPreHyphenDot = r"(?<!(-|\.))"
  262. NoPostHyphenDot = r"(?!(-|\.))"
  263. NoPostBar = r"(?!(/|\\))"
  264. for pattern in [r"\bFileCheck\b",
  265. r"\bc-index-test\b",
  266. NoPreHyphenDot + r"\bclang-check\b" + NoPostHyphenDot,
  267. NoPreHyphenDot + r"\bclang-format\b" + NoPostHyphenDot,
  268. NoPreHyphenDot + r"\bclang-interpreter\b" + NoPostHyphenDot,
  269. # FIXME: Some clang test uses opt?
  270. NoPreHyphenDot + r"\bopt\b" + NoPostBar + NoPostHyphenDot,
  271. # Handle these specially as they are strings searched
  272. # for during testing.
  273. r"\| \bcount\b",
  274. r"\| \bnot\b"]:
  275. # Extract the tool name from the pattern. This relies on the tool
  276. # name being surrounded by \b word match operators. If the
  277. # pattern starts with "| ", include it in the string to be
  278. # substituted.
  279. tool_match = re.match(r"^(\\)?((\| )?)\W+b([0-9A-Za-z-_]+)\\b\W*$",
  280. pattern)
  281. tool_pipe = tool_match.group(2)
  282. tool_name = tool_match.group(4)
  283. tool_path = lit.util.which(tool_name, tool_dirs)
  284. if not tool_path:
  285. # Warn, but still provide a substitution.
  286. lit_config.note('Did not find ' + tool_name + ' in ' + tool_dirs)
  287. tool_path = clang_tools_dir + '/' + tool_name
  288. config.substitutions.append((pattern, tool_pipe + tool_path))
  289. ###
  290. # Set available features we allow tests to conditionalize on.
  291. #
  292. # Enabled/disabled features
  293. if config.clang_staticanalyzer != 0:
  294. config.available_features.add("staticanalyzer")
  295. # As of 2011.08, crash-recovery tests still do not pass on FreeBSD.
  296. if platform.system() not in ['FreeBSD']:
  297. config.available_features.add('crash-recovery')
  298. # Shell execution
  299. if execute_external:
  300. config.available_features.add('shell')
  301. # Exclude MSYS due to transforming '/' to 'X:/mingwroot/'.
  302. if not platform.system() in ['Windows'] or not execute_external:
  303. config.available_features.add('shell-preserves-root')
  304. # For tests that require Darwin to run.
  305. # This is used by debuginfo-tests/*block*.m and debuginfo-tests/foreach.m.
  306. if platform.system() in ['Darwin']:
  307. config.available_features.add('system-darwin')
  308. elif platform.system() in ['Windows']:
  309. # For tests that require Windows to run.
  310. config.available_features.add('system-windows')
  311. # ANSI escape sequences in non-dumb terminal
  312. if platform.system() not in ['Windows']:
  313. config.available_features.add('ansi-escape-sequences')
  314. # Capability to print utf8 to the terminal.
  315. # Windows expects codepage, unless Wide API.
  316. if platform.system() not in ['Windows']:
  317. config.available_features.add('utf8-capable-terminal')
  318. # Native compilation: Check if triples match.
  319. # FIXME: Consider cases that target can be executed
  320. # even if host_triple were different from target_triple.
  321. if config.host_triple == config.target_triple:
  322. config.available_features.add("native")
  323. # Case-insensitive file system
  324. def is_filesystem_case_insensitive():
  325. handle, path = tempfile.mkstemp(prefix='case-test', dir=config.test_exec_root)
  326. isInsensitive = os.path.exists(
  327. os.path.join(
  328. os.path.dirname(path),
  329. os.path.basename(path).upper()
  330. ))
  331. os.close(handle)
  332. os.remove(path)
  333. return isInsensitive
  334. if is_filesystem_case_insensitive():
  335. config.available_features.add('case-insensitive-filesystem')
  336. # Tests that require the /dev/fd filesystem.
  337. if os.path.exists("/dev/fd/0") and sys.platform not in ['cygwin']:
  338. config.available_features.add('dev-fd-fs')
  339. # DW2 Target
  340. if not re.match(r'.*-win32$', config.target_triple):
  341. config.available_features.add('dw2')
  342. # Not set on native MS environment.
  343. if not re.match(r'.*-win32$', config.target_triple):
  344. config.available_features.add('non-ms-sdk')
  345. # Not set on native PS4 environment.
  346. if not re.match(r'.*-scei-ps4', config.target_triple):
  347. config.available_features.add('non-ps4-sdk')
  348. # [PR8833] LLP64-incompatible tests
  349. if not re.match(r'^x86_64.*-(win32|mingw32|windows-gnu)$', config.target_triple):
  350. config.available_features.add('LP64')
  351. # [PR12920] "clang-driver" -- set if gcc driver is not used.
  352. if not re.match(r'.*-(cygwin|mingw32|windows-gnu)$', config.target_triple):
  353. config.available_features.add('clang-driver')
  354. # [PR18856] Depends to remove opened file. On win32, a file could be removed
  355. # only if all handles were closed.
  356. if platform.system() not in ['Windows']:
  357. config.available_features.add('can-remove-opened-file')
  358. # Not set for targeting tls-incapable targets.
  359. if not re.match(r'.*-cygwin$', config.target_triple):
  360. config.available_features.add('tls')
  361. # Returns set of available features, registered-target(s) and asserts.
  362. def get_llvm_config_props():
  363. set_of_features = set()
  364. cmd = subprocess.Popen(
  365. [
  366. os.path.join(llvm_tools_dir, 'llvm-config'),
  367. '--assertion-mode',
  368. '--targets-built',
  369. ],
  370. stdout=subprocess.PIPE,
  371. env=config.environment
  372. )
  373. # 1st line corresponds to --assertion-mode, "ON" or "OFF".
  374. line = cmd.stdout.readline().strip().decode('ascii')
  375. if line == "ON":
  376. set_of_features.add('asserts')
  377. # 2nd line corresponds to --targets-built, like;
  378. # AArch64 ARM CppBackend X86
  379. for arch in cmd.stdout.readline().decode('ascii').split():
  380. set_of_features.add(arch.lower() + '-registered-target')
  381. return set_of_features
  382. config.available_features.update(get_llvm_config_props())
  383. if lit.util.which('xmllint'):
  384. config.available_features.add('xmllint')
  385. # Sanitizers.
  386. if 'Address' in config.llvm_use_sanitizer:
  387. config.available_features.add("asan")
  388. else:
  389. config.available_features.add("not_asan")
  390. if 'Memory' in config.llvm_use_sanitizer:
  391. config.available_features.add("msan")
  392. if 'Undefined' in config.llvm_use_sanitizer:
  393. config.available_features.add("ubsan")
  394. else:
  395. config.available_features.add("not_ubsan")
  396. if config.enable_backtrace == "1":
  397. config.available_features.add("backtrace")
  398. # Check if we should run long running tests.
  399. if lit_config.params.get("run_long_tests", None) == "true":
  400. config.available_features.add("long_tests")
  401. # Check if we should use gmalloc.
  402. use_gmalloc_str = lit_config.params.get('use_gmalloc', None)
  403. if use_gmalloc_str is not None:
  404. if use_gmalloc_str.lower() in ('1', 'true'):
  405. use_gmalloc = True
  406. elif use_gmalloc_str.lower() in ('', '0', 'false'):
  407. use_gmalloc = False
  408. else:
  409. lit_config.fatal('user parameter use_gmalloc should be 0 or 1')
  410. else:
  411. # Default to not using gmalloc
  412. use_gmalloc = False
  413. # Allow use of an explicit path for gmalloc library.
  414. # Will default to '/usr/lib/libgmalloc.dylib' if not set.
  415. gmalloc_path_str = lit_config.params.get('gmalloc_path',
  416. '/usr/lib/libgmalloc.dylib')
  417. if use_gmalloc:
  418. config.environment.update({'DYLD_INSERT_LIBRARIES' : gmalloc_path_str})
  419. lit.util.usePlatformSdkOnDarwin(config, lit_config)