xref: /minix/external/bsd/llvm/dist/clang/test/lit.cfg (revision 0a6a1f1d)
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']
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                NoPreHyphenDot + r"\bopt\b" + NoPostBar + NoPostHyphenDot,
317                # Handle these specially as they are strings searched
318                # for during testing.
319                r"\| \bcount\b",
320                r"\| \bnot\b"]:
321    # Extract the tool name from the pattern.  This relies on the tool
322    # name being surrounded by \b word match operators.  If the
323    # pattern starts with "| ", include it in the string to be
324    # substituted.
325    tool_match = re.match(r"^(\\)?((\| )?)\W+b([0-9A-Za-z-_]+)\\b\W*$",
326                          pattern)
327    tool_pipe = tool_match.group(2)
328    tool_name = tool_match.group(4)
329    tool_path = lit.util.which(tool_name, tool_dirs)
330    if not tool_path:
331        # Warn, but still provide a substitution.
332        lit_config.note('Did not find ' + tool_name + ' in ' + tool_dirs)
333        tool_path = clang_tools_dir + '/' + tool_name
334    config.substitutions.append((pattern, tool_pipe + tool_path))
335
336###
337
338# Set available features we allow tests to conditionalize on.
339#
340# Enabled/disabled features
341if config.clang_staticanalyzer != 0:
342    config.available_features.add("staticanalyzer")
343
344# As of 2011.08, crash-recovery tests still do not pass on FreeBSD.
345if platform.system() not in ['FreeBSD']:
346    config.available_features.add('crash-recovery')
347
348# Shell execution
349if execute_external:
350    config.available_features.add('shell')
351
352# Exclude MSYS due to transforming '/' to 'X:/mingwroot/'.
353if not platform.system() in ['Windows'] or not execute_external:
354    config.available_features.add('shell-preserves-root')
355
356# For tests that require Darwin to run.
357# This is used by debuginfo-tests/*block*.m and debuginfo-tests/foreach.m.
358if platform.system() in ['Darwin']:
359    config.available_features.add('system-darwin')
360elif platform.system() in ['Windows']:
361    # For tests that require Windows to run.
362    config.available_features.add('system-windows')
363
364# ANSI escape sequences in non-dumb terminal
365if platform.system() not in ['Windows']:
366    config.available_features.add('ansi-escape-sequences')
367
368# Capability to print utf8 to the terminal.
369# Windows expects codepage, unless Wide API.
370if platform.system() not in ['Windows']:
371    config.available_features.add('utf8-capable-terminal')
372
373# Native compilation: Check if triples match.
374# FIXME: Consider cases that target can be executed
375# even if host_triple were different from target_triple.
376if config.host_triple == config.target_triple:
377    config.available_features.add("native")
378
379# Case-insensitive file system
380def is_filesystem_case_insensitive():
381    handle, path = tempfile.mkstemp(prefix='case-test', dir=config.test_exec_root)
382    isInsensitive = os.path.exists(
383        os.path.join(
384            os.path.dirname(path),
385            os.path.basename(path).upper()
386            ))
387    os.close(handle)
388    os.remove(path)
389    return isInsensitive
390
391if is_filesystem_case_insensitive():
392    config.available_features.add('case-insensitive-filesystem')
393
394# Tests that require the /dev/fd filesystem.
395if os.path.exists("/dev/fd/0") and sys.platform not in ['cygwin']:
396    config.available_features.add('dev-fd-fs')
397
398# DW2 Target
399if not re.match(r'.*-win32$', config.target_triple):
400    config.available_features.add('dw2')
401
402# Not set on native MS environment.
403if not re.match(r'.*-win32$', config.target_triple):
404    config.available_features.add('non-ms-sdk')
405
406# [PR8833] LLP64-incompatible tests
407if not re.match(r'^x86_64.*-(win32|mingw32|windows-gnu)$', config.target_triple):
408    config.available_features.add('LP64')
409
410# [PR12920] "clang-driver" -- set if gcc driver is not used.
411if not re.match(r'.*-(cygwin|mingw32|windows-gnu)$', config.target_triple):
412    config.available_features.add('clang-driver')
413
414# [PR18856] Depends to remove opened file. On win32, a file could be removed
415# only if all handles were closed.
416if platform.system() not in ['Windows']:
417    config.available_features.add('can-remove-opened-file')
418
419# Returns set of available features, registered-target(s) and asserts.
420def get_llvm_config_props():
421    set_of_features = set()
422
423    cmd = subprocess.Popen(
424        [
425            os.path.join(llvm_tools_dir, 'llvm-config'),
426            '--assertion-mode',
427            '--targets-built',
428            ],
429        stdout=subprocess.PIPE,
430        env=config.environment
431        )
432    # 1st line corresponds to --assertion-mode, "ON" or "OFF".
433    line = cmd.stdout.readline().strip().decode('ascii')
434    if line == "ON":
435        set_of_features.add('asserts')
436
437    # 2nd line corresponds to --targets-built, like;
438    # AArch64 ARM CppBackend X86
439    for arch in cmd.stdout.readline().decode('ascii').split():
440        set_of_features.add(arch.lower() + '-registered-target')
441
442    return set_of_features
443
444config.available_features.update(get_llvm_config_props())
445
446if lit.util.which('xmllint'):
447    config.available_features.add('xmllint')
448
449# Sanitizers.
450if config.llvm_use_sanitizer == "Address":
451    config.available_features.add("asan")
452else:
453    config.available_features.add("not_asan")
454if (config.llvm_use_sanitizer == "Memory" or
455        config.llvm_use_sanitizer == "MemoryWithOrigins"):
456    config.available_features.add("msan")
457if config.llvm_use_sanitizer == "Undefined":
458    config.available_features.add("ubsan")
459else:
460    config.available_features.add("not_ubsan")
461
462# Check if we should run long running tests.
463if lit_config.params.get("run_long_tests", None) == "true":
464    config.available_features.add("long_tests")
465
466# Check if we should use gmalloc.
467use_gmalloc_str = lit_config.params.get('use_gmalloc', None)
468if use_gmalloc_str is not None:
469    if use_gmalloc_str.lower() in ('1', 'true'):
470        use_gmalloc = True
471    elif use_gmalloc_str.lower() in ('', '0', 'false'):
472        use_gmalloc = False
473    else:
474        lit_config.fatal('user parameter use_gmalloc should be 0 or 1')
475else:
476    # Default to not using gmalloc
477    use_gmalloc = False
478
479# Allow use of an explicit path for gmalloc library.
480# Will default to '/usr/lib/libgmalloc.dylib' if not set.
481gmalloc_path_str = lit_config.params.get('gmalloc_path',
482                                         '/usr/lib/libgmalloc.dylib')
483if use_gmalloc:
484     config.environment.update({'DYLD_INSERT_LIBRARIES' : gmalloc_path_str})
485
486lit.util.usePlatformSdkOnDarwin(config, lit_config)
487