1# -*- Python -*-
2
3# Configuration file for the 'lit' test runner.
4
5import os
6import sys
7import re
8import platform
9
10import lit.util
11import lit.formats
12
13# name: The name of this test suite.
14config.name = 'LLVM'
15
16# Tweak PATH for Win32 to decide to use bash.exe or not.
17if sys.platform in ['win32']:
18    # Seek sane tools in directories and set to $PATH.
19    path = getattr(config, 'lit_tools_dir', None)
20    path = lit_config.getToolsPath(path,
21                                   config.environment['PATH'],
22                                   ['cmp.exe', 'grep.exe', 'sed.exe'])
23    if path is not None:
24        path = os.path.pathsep.join((path,
25                                     config.environment['PATH']))
26        config.environment['PATH'] = path
27
28# Choose between lit's internal shell pipeline runner and a real shell.  If
29# LIT_USE_INTERNAL_SHELL is in the environment, we use that as an override.
30use_lit_shell = os.environ.get("LIT_USE_INTERNAL_SHELL")
31if use_lit_shell:
32    # 0 is external, "" is default, and everything else is internal.
33    execute_external = (use_lit_shell == "0")
34else:
35    # Otherwise we default to internal on Windows and external elsewhere, as
36    # bash on Windows is usually very slow.
37    execute_external = (not sys.platform in ['win32'])
38
39# testFormat: The test format to use to interpret tests.
40config.test_format = lit.formats.ShTest(execute_external)
41
42# suffixes: A list of file extensions to treat as test files. This is overriden
43# by individual lit.local.cfg files in the test subdirectories.
44config.suffixes = ['.ll', '.c', '.cpp', '.test', '.txt', '.s']
45
46# excludes: A list of directories to exclude from the testsuite. The 'Inputs'
47# subdirectories contain auxiliary inputs for various tests in their parent
48# directories.
49config.excludes = ['Inputs', 'CMakeLists.txt', 'README.txt', 'LICENSE.txt']
50
51# test_source_root: The root path where tests are located.
52config.test_source_root = os.path.dirname(__file__)
53
54# test_exec_root: The root path where tests should be run.
55llvm_obj_root = getattr(config, 'llvm_obj_root', None)
56if llvm_obj_root is not None:
57    config.test_exec_root = os.path.join(llvm_obj_root, 'test')
58
59# Tweak the PATH to include the tools dir.
60if llvm_obj_root is not None:
61    llvm_tools_dir = getattr(config, 'llvm_tools_dir', None)
62    if not llvm_tools_dir:
63        lit_config.fatal('No LLVM tools dir set!')
64    path = os.path.pathsep.join((llvm_tools_dir, config.environment['PATH']))
65    config.environment['PATH'] = path
66
67# Propagate 'HOME' through the environment.
68if 'HOME' in os.environ:
69    config.environment['HOME'] = os.environ['HOME']
70
71# Propagate 'INCLUDE' through the environment.
72if 'INCLUDE' in os.environ:
73    config.environment['INCLUDE'] = os.environ['INCLUDE']
74
75# Propagate 'LIB' through the environment.
76if 'LIB' in os.environ:
77    config.environment['LIB'] = os.environ['LIB']
78
79# Propagate the temp directory. Windows requires this because it uses \Windows\
80# if none of these are present.
81if 'TMP' in os.environ:
82    config.environment['TMP'] = os.environ['TMP']
83if 'TEMP' in os.environ:
84    config.environment['TEMP'] = os.environ['TEMP']
85
86# Propagate LLVM_SRC_ROOT into the environment.
87config.environment['LLVM_SRC_ROOT'] = getattr(config, 'llvm_src_root', '')
88
89# Propagate PYTHON_EXECUTABLE into the environment
90config.environment['PYTHON_EXECUTABLE'] = getattr(config, 'python_executable',
91                                                  '')
92
93# Propagate path to symbolizer for ASan/MSan.
94for symbolizer in ['ASAN_SYMBOLIZER_PATH', 'MSAN_SYMBOLIZER_PATH']:
95    if symbolizer in os.environ:
96        config.environment[symbolizer] = os.environ[symbolizer]
97
98# Propagate options for sanitizers.
99for options in ['ASAN_OPTIONS']:
100    if options in os.environ:
101        config.environment[options] = os.environ[options]
102
103###
104
105import os
106
107# Check that the object root is known.
108if config.test_exec_root is None:
109    # Otherwise, we haven't loaded the site specific configuration (the user is
110    # probably trying to run on a test file directly, and either the site
111    # configuration hasn't been created by the build system, or we are in an
112    # out-of-tree build situation).
113
114    # Check for 'llvm_site_config' user parameter, and use that if available.
115    site_cfg = lit_config.params.get('llvm_site_config', None)
116    if site_cfg and os.path.exists(site_cfg):
117        lit_config.load_config(config, site_cfg)
118        raise SystemExit
119
120    # Try to detect the situation where we are using an out-of-tree build by
121    # looking for 'llvm-config'.
122    #
123    # FIXME: I debated (i.e., wrote and threw away) adding logic to
124    # automagically generate the lit.site.cfg if we are in some kind of fresh
125    # build situation. This means knowing how to invoke the build system
126    # though, and I decided it was too much magic.
127
128    llvm_config = lit.util.which('llvm-config', config.environment['PATH'])
129    if not llvm_config:
130        lit_config.fatal('No site specific configuration available!')
131
132    # Get the source and object roots.
133    llvm_src_root = lit.util.capture(['llvm-config', '--src-root']).strip()
134    llvm_obj_root = lit.util.capture(['llvm-config', '--obj-root']).strip()
135
136    # Validate that we got a tree which points to here.
137    this_src_root = os.path.dirname(config.test_source_root)
138    if os.path.realpath(llvm_src_root) != os.path.realpath(this_src_root):
139        lit_config.fatal('No site specific configuration available!')
140
141    # Check that the site specific configuration exists.
142    site_cfg = os.path.join(llvm_obj_root, 'test', 'lit.site.cfg')
143    if not os.path.exists(site_cfg):
144        lit_config.fatal('No site specific configuration available!')
145
146    # Okay, that worked. Notify the user of the automagic, and reconfigure.
147    lit_config.note('using out-of-tree build at %r' % llvm_obj_root)
148    lit_config.load_config(config, site_cfg)
149    raise SystemExit
150
151###
152
153# Provide a command line for mcjit tests
154lli_mcjit = 'lli -use-mcjit'
155# The target triple used by default by lli is the process target triple (some
156# triple appropriate for generating code for the current process) but because
157# we don't support COFF in MCJIT well enough for the tests, force ELF format on
158# Windows.  FIXME: the process target triple should be used here, but this is
159# difficult to obtain on Windows.
160if re.search(r'cygwin|mingw32|win32', config.host_triple):
161  lli_mcjit += ' -mtriple='+config.host_triple+'-elf'
162config.substitutions.append( ('%lli_mcjit', lli_mcjit) )
163
164# Similarly, have a macro to use llc with DWARF even when the host is win32.
165llc_dwarf = 'llc'
166if re.search(r'win32', config.target_triple):
167  llc_dwarf += ' -mtriple='+config.target_triple.replace('-win32', '-mingw32')
168config.substitutions.append( ('%llc_dwarf', llc_dwarf) )
169
170# Provide a substition for those tests that need to run the jit to obtain data
171# but simply want use the currently considered most reliable jit for platform
172# FIXME: ppc32 is not ready for mcjit.
173if 'arm' in config.target_triple \
174   or 'aarch64' in config.target_triple \
175   or 'powerpc64' in config.target_triple \
176   or 's390x' in config.target_triple:
177    defaultIsMCJIT = 'true'
178else:
179    defaultIsMCJIT = 'false'
180config.substitutions.append( ('%defaultjit', '-use-mcjit='+defaultIsMCJIT) )
181
182# Process jit implementation option
183jit_impl_cfg = lit_config.params.get('jit_impl', None)
184if jit_impl_cfg == 'mcjit':
185  # When running with mcjit, mangle -mcjit into target triple
186  # and add -use-mcjit flag to lli invocation
187  if 'i386' in config.target_triple or 'i686' in config.target_triple:
188    config.target_triple += jit_impl_cfg + '-ia32'
189  elif 'x86_64' in config.target_triple:
190    config.target_triple += jit_impl_cfg + '-ia64'
191  else:
192    config.target_triple += jit_impl_cfg
193
194  config.substitutions.append( ('%lli', 'lli -use-mcjit') )
195else:
196  config.substitutions.append( ('%lli', 'lli') )
197
198# Add site-specific substitutions.
199config.substitutions.append( ('%ocamlopt', config.ocamlopt_executable) )
200config.substitutions.append( ('%llvmshlibdir', config.llvm_shlib_dir) )
201config.substitutions.append( ('%shlibext', config.llvm_shlib_ext) )
202config.substitutions.append( ('%exeext', config.llvm_exe_ext) )
203
204# For each occurrence of an llvm tool name as its own word, replace it
205# with the full path to the build directory holding that tool.  This
206# ensures that we are testing the tools just built and not some random
207# tools that might happen to be in the user's PATH.  Thus this list
208# includes every tool placed in $(LLVM_OBJ_ROOT)/$(BuildMode)/bin
209# (llvm_tools_dir in lit parlance).
210
211# Avoid matching RUN line fragments that are actually part of
212# path names or options or whatever.
213# The regex is a pre-assertion to avoid matching a preceding
214# dot, hyphen, carat, or slash (.foo, -foo, etc.).  Some patterns
215# also have a post-assertion to not match a trailing hyphen (foo-).
216NOJUNK = r"(?<!\.|-|\^|/)"
217
218for pattern in [r"\bbugpoint\b(?!-)",
219                NOJUNK + r"\bllc\b",
220                r"\blli\b",
221                r"\bllvm-ar\b",
222                r"\bllvm-as\b",
223                r"\bllvm-bcanalyzer\b",
224                r"\bllvm-config\b",
225                r"\bllvm-cov\b",
226                r"\bllvm-diff\b",
227                r"\bllvm-dis\b",
228                r"\bllvm-dwarfdump\b",
229                r"\bllvm-extract\b",
230                r"\bllvm-link\b",
231                r"\bllvm-lto\b",
232                r"\bllvm-mc\b",
233                r"\bllvm-mcmarkup\b",
234                r"\bllvm-nm\b",
235                r"\bllvm-objdump\b",
236                r"\bllvm-profdata\b",
237                r"\bllvm-ranlib\b",
238                r"\bllvm-readobj\b",
239                r"\bllvm-rtdyld\b",
240                r"\bllvm-size\b",
241                r"\bllvm-tblgen\b",
242                r"\bllvm-c-test\b",
243                r"\bmacho-dump\b",
244                NOJUNK + r"\bopt\b",
245                r"\bFileCheck\b",
246                r"\bobj2yaml\b",
247                r"\byaml2obj\b",
248                # Handle these specially as they are strings searched
249                # for during testing.
250                r"\| \bcount\b",
251                r"\| \bnot\b"]:
252    # Extract the tool name from the pattern.  This relies on the tool
253    # name being surrounded by \b word match operators.  If the
254    # pattern starts with "| ", include it in the string to be
255    # substituted.
256    tool_match = re.match(r"^(\\)?((\| )?)\W+b([0-9A-Za-z-_]+)\\b\W*$",
257                          pattern)
258    tool_pipe = tool_match.group(2)
259    tool_name = tool_match.group(4)
260    tool_path = lit.util.which(tool_name, llvm_tools_dir)
261    if not tool_path:
262        # Warn, but still provide a substitution.
263        lit_config.note('Did not find ' + tool_name + ' in ' + llvm_tools_dir)
264        tool_path = llvm_tools_dir + '/' + tool_name
265    config.substitutions.append((pattern, tool_pipe + tool_path))
266
267### Targets
268
269config.targets = frozenset(config.targets_to_build.split())
270
271### Features
272
273# Shell execution
274if execute_external:
275    config.available_features.add('shell')
276
277# Others/can-execute.txt
278if sys.platform not in ['win32']:
279    config.available_features.add('can-execute')
280
281# Loadable module
282# FIXME: This should be supplied by Makefile or autoconf.
283if sys.platform in ['win32', 'cygwin']:
284    loadable_module = (config.enable_shared == 1)
285else:
286    loadable_module = True
287
288if loadable_module:
289    config.available_features.add('loadable_module')
290
291# Sanitizers.
292if config.llvm_use_sanitizer == "Address":
293    config.available_features.add("asan")
294if (config.llvm_use_sanitizer == "Memory" or
295        config.llvm_use_sanitizer == "MemoryWithOrigins"):
296    config.available_features.add("msan")
297
298# Direct object generation
299if not 'hexagon' in config.target_triple:
300    config.available_features.add("object-emission")
301
302if config.have_zlib == "1":
303    config.available_features.add("zlib")
304else:
305    config.available_features.add("nozlib")
306
307# Native compilation: host arch == target arch
308# FIXME: Consider cases that target can be executed
309# even if host_triple were different from target_triple.
310if config.host_triple == config.target_triple:
311    config.available_features.add("native")
312
313# Ask llvm-config about assertion mode.
314import subprocess
315try:
316    llvm_config_cmd = subprocess.Popen(
317        [os.path.join(llvm_tools_dir, 'llvm-config'), '--assertion-mode'],
318        stdout = subprocess.PIPE)
319except OSError:
320    print("Could not find llvm-config in " + llvm_tools_dir)
321    exit(42)
322
323if re.search(r'ON', llvm_config_cmd.stdout.read().decode('ascii')):
324    config.available_features.add('asserts')
325llvm_config_cmd.wait()
326
327if 'darwin' == sys.platform:
328    try:
329        sysctl_cmd = subprocess.Popen(['sysctl', 'hw.optional.fma'],
330                                    stdout = subprocess.PIPE)
331    except OSError:
332        print("Could not exec sysctl")
333    result = sysctl_cmd.stdout.read().decode('ascii')
334    if -1 != result.find("hw.optional.fma: 1"):
335        config.available_features.add('fma3')
336    sysctl_cmd.wait()
337
338# .debug_frame is not emitted for targeting Windows x64.
339if not re.match(r'^x86_64.*-(mingw32|win32)', config.target_triple):
340    config.available_features.add('debug_frame')
341
342# Check if we should use gmalloc.
343use_gmalloc_str = lit_config.params.get('use_gmalloc', None)
344if use_gmalloc_str is not None:
345    if use_gmalloc_str.lower() in ('1', 'true'):
346        use_gmalloc = True
347    elif use_gmalloc_str.lower() in ('', '0', 'false'):
348        use_gmalloc = False
349    else:
350        lit_config.fatal('user parameter use_gmalloc should be 0 or 1')
351else:
352    # Default to not using gmalloc
353    use_gmalloc = False
354
355# Allow use of an explicit path for gmalloc library.
356# Will default to '/usr/lib/libgmalloc.dylib' if not set.
357gmalloc_path_str = lit_config.params.get('gmalloc_path',
358                                         '/usr/lib/libgmalloc.dylib')
359
360if use_gmalloc:
361     config.environment.update({'DYLD_INSERT_LIBRARIES' : gmalloc_path_str})
362