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