1# -*- Python -*-
2
3# Configuration file for the 'lit' test runner.
4
5import os
6import sys
7import re
8import platform
9import subprocess
10
11import lit.util
12import lit.formats
13from lit.llvm import llvm_config
14from lit.llvm.subst import FindTool
15from lit.llvm.subst import ToolSubst
16
17# name: The name of this test suite.
18config.name = 'LLVM'
19
20# testFormat: The test format to use to interpret tests.
21config.test_format = lit.formats.ShTest(not llvm_config.use_lit_shell)
22
23# suffixes: A list of file extensions to treat as test files. This is overriden
24# by individual lit.local.cfg files in the test subdirectories.
25config.suffixes = ['.ll', '.c', '.test', '.txt', '.s', '.mir', '.yaml']
26
27# excludes: A list of directories to exclude from the testsuite. The 'Inputs'
28# subdirectories contain auxiliary inputs for various tests in their parent
29# directories.
30config.excludes = ['Inputs', 'CMakeLists.txt', 'README.txt', 'LICENSE.txt']
31
32# test_source_root: The root path where tests are located.
33config.test_source_root = os.path.dirname(__file__)
34
35# test_exec_root: The root path where tests should be run.
36config.test_exec_root = os.path.join(config.llvm_obj_root, 'test')
37
38# Tweak the PATH to include the tools dir.
39llvm_config.with_environment('PATH', config.llvm_tools_dir, append_path=True)
40
41# Propagate some variables from the host environment.
42llvm_config.with_system_environment(
43    ['HOME', 'INCLUDE', 'LIB', 'TMP', 'TEMP', 'ASAN_SYMBOLIZER_PATH', 'MSAN_SYMBOLIZER_PATH'])
44
45
46# Set up OCAMLPATH to include newly built OCaml libraries.
47top_ocaml_lib = os.path.join(config.llvm_lib_dir, 'ocaml')
48llvm_ocaml_lib = os.path.join(top_ocaml_lib, 'llvm')
49
50llvm_config.with_system_environment('OCAMLPATH')
51llvm_config.with_environment('OCAMLPATH', top_ocaml_lib, append_path=True)
52llvm_config.with_environment('OCAMLPATH', llvm_ocaml_lib, append_path=True)
53
54llvm_config.with_system_environment('CAML_LD_LIBRARY_PATH')
55llvm_config.with_environment(
56    'CAML_LD_LIBRARY_PATH', llvm_ocaml_lib, append_path=True)
57
58# Set up OCAMLRUNPARAM to enable backtraces in OCaml tests.
59llvm_config.with_environment('OCAMLRUNPARAM', 'b')
60
61# Provide the path to asan runtime lib 'libclang_rt.asan_osx_dynamic.dylib' if
62# available. This is darwin specific since it's currently only needed on darwin.
63
64
65def get_asan_rtlib():
66    if not 'Address' in config.llvm_use_sanitizer or \
67       not 'Darwin' in config.host_os or \
68       not 'x86' in config.host_triple:
69        return ''
70    try:
71        import glob
72    except:
73        print('glob module not found, skipping get_asan_rtlib() lookup')
74        return ''
75    # The libclang_rt.asan_osx_dynamic.dylib path is obtained using the relative
76    # path from the host cc.
77    host_lib_dir = os.path.join(os.path.dirname(config.host_cc), '../lib')
78    asan_dylib_dir_pattern = host_lib_dir + \
79        '/clang/*/lib/darwin/libclang_rt.asan_osx_dynamic.dylib'
80    found_dylibs = glob.glob(asan_dylib_dir_pattern)
81    if len(found_dylibs) != 1:
82        return ''
83    return found_dylibs[0]
84
85
86####################################################
87# FIXME: remove this when we flip the default value for --allow-unused-prefixes
88# to false.
89fc = ToolSubst('FileCheck', unresolved='fatal')
90# Insert this first. Then, we'll first update the blank FileCheck command; then,
91# the default substitution of FileCheck will replace it to its full path.
92config.substitutions.insert(0, (fc.regex,
93    'FileCheck --allow-unused-prefixes=false'))
94# When addressing this fixme, replace %FileCheckRaw% with just FileCheck.
95config.substitutions.append(('%FileCheckRaw%', 'FileCheck'))
96# Also remove the lit.local.cfg under llvm/test/Reduce
97# and the pertinent FIXME in llvm/test/FileCheck
98####################################################
99
100llvm_config.use_default_substitutions()
101
102# Add site-specific substitutions.
103config.substitutions.append(('%llvmshlibdir', config.llvm_shlib_dir))
104config.substitutions.append(('%shlibext', config.llvm_shlib_ext))
105config.substitutions.append(('%exeext', config.llvm_exe_ext))
106
107
108lli_args = []
109# The target triple used by default by lli is the process target triple (some
110# triple appropriate for generating code for the current process) but because
111# we don't support COFF in MCJIT well enough for the tests, force ELF format on
112# Windows.  FIXME: the process target triple should be used here, but this is
113# difficult to obtain on Windows.
114if re.search(r'cygwin|windows-gnu|windows-msvc', config.host_triple):
115    lli_args = ['-mtriple=' + config.host_triple + '-elf']
116
117llc_args = []
118
119# Similarly, have a macro to use llc with DWARF even when the host is Windows
120if re.search(r'windows-msvc', config.target_triple):
121    llc_args = [' -mtriple=' +
122                config.target_triple.replace('-msvc', '-gnu')]
123
124# Provide the path to asan runtime lib if available. On darwin, this lib needs
125# to be loaded via DYLD_INSERT_LIBRARIES before libLTO.dylib in case the files
126# to be linked contain instrumented sanitizer code.
127ld64_cmd = config.ld64_executable
128asan_rtlib = get_asan_rtlib()
129if asan_rtlib:
130    ld64_cmd = 'DYLD_INSERT_LIBRARIES={} {}'.format(asan_rtlib, ld64_cmd)
131
132ocamlc_command = '%s ocamlc -cclib -L%s %s' % (
133    config.ocamlfind_executable, config.llvm_lib_dir, config.ocaml_flags)
134ocamlopt_command = 'true'
135if config.have_ocamlopt:
136    ocamlopt_command = '%s ocamlopt -cclib -L%s -cclib -Wl,-rpath,%s %s' % (
137        config.ocamlfind_executable, config.llvm_lib_dir, config.llvm_lib_dir, config.ocaml_flags)
138
139opt_viewer_cmd = '%s %s/tools/opt-viewer/opt-viewer.py' % (sys.executable, config.llvm_src_root)
140
141llvm_locstats_tool = os.path.join(config.llvm_tools_dir, 'llvm-locstats')
142config.substitutions.append(
143    ('%llvm-locstats', "'%s' %s" % (config.python_executable, llvm_locstats_tool)))
144config.llvm_locstats_used = os.path.exists(llvm_locstats_tool)
145
146tools = [
147    ToolSubst('%lli', FindTool('lli'), post='.', extra_args=lli_args),
148    ToolSubst('%llc_dwarf', FindTool('llc'), extra_args=llc_args),
149    ToolSubst('%go', config.go_executable, unresolved='ignore'),
150    ToolSubst('%gold', config.gold_executable, unresolved='ignore'),
151    ToolSubst('%ld64', ld64_cmd, unresolved='ignore'),
152    ToolSubst('%ocamlc', ocamlc_command, unresolved='ignore'),
153    ToolSubst('%ocamlopt', ocamlopt_command, unresolved='ignore'),
154    ToolSubst('%opt-viewer', opt_viewer_cmd),
155    ToolSubst('%llvm-objcopy', FindTool('llvm-objcopy')),
156    ToolSubst('%llvm-strip', FindTool('llvm-strip')),
157    ToolSubst('%llvm-install-name-tool', FindTool('llvm-install-name-tool')),
158    ToolSubst('%llvm-bitcode-strip', FindTool('llvm-bitcode-strip')),
159    ToolSubst('%split-file', FindTool('split-file')),
160]
161
162# FIXME: Why do we have both `lli` and `%lli` that do slightly different things?
163tools.extend([
164    'dsymutil', 'lli', 'lli-child-target', 'llvm-ar', 'llvm-as',
165    'llvm-addr2line', 'llvm-bcanalyzer', 'llvm-bitcode-strip', 'llvm-config',
166    'llvm-cov', 'llvm-cxxdump', 'llvm-cvtres', 'llvm-diff', 'llvm-dis',
167    'llvm-dwarfdump', 'llvm-dlltool', 'llvm-exegesis', 'llvm-extract',
168    'llvm-isel-fuzzer', 'llvm-ifs',
169    'llvm-install-name-tool', 'llvm-jitlink', 'llvm-opt-fuzzer', 'llvm-lib',
170    'llvm-link', 'llvm-lto', 'llvm-lto2', 'llvm-mc', 'llvm-mca',
171    'llvm-modextract', 'llvm-nm', 'llvm-objcopy', 'llvm-objdump',
172    'llvm-pdbutil', 'llvm-profdata', 'llvm-ranlib', 'llvm-rc', 'llvm-readelf',
173    'llvm-readobj', 'llvm-rtdyld', 'llvm-size', 'llvm-split', 'llvm-strings',
174    'llvm-strip', 'llvm-tblgen', 'llvm-undname', 'llvm-c-test', 'llvm-cxxfilt',
175    'llvm-xray', 'yaml2obj', 'obj2yaml', 'yaml-bench', 'verify-uselistorder',
176    'bugpoint', 'llc', 'llvm-symbolizer', 'opt', 'sancov', 'sanstats'])
177
178# The following tools are optional
179tools.extend([
180    ToolSubst('llvm-go', unresolved='ignore'),
181    ToolSubst('llvm-mt', unresolved='ignore'),
182    ToolSubst('Kaleidoscope-Ch3', unresolved='ignore'),
183    ToolSubst('Kaleidoscope-Ch4', unresolved='ignore'),
184    ToolSubst('Kaleidoscope-Ch5', unresolved='ignore'),
185    ToolSubst('Kaleidoscope-Ch6', unresolved='ignore'),
186    ToolSubst('Kaleidoscope-Ch7', unresolved='ignore'),
187    ToolSubst('Kaleidoscope-Ch8', unresolved='ignore'),
188    ToolSubst('LLJITWithThinLTOSummaries', unresolved='ignore')])
189
190llvm_config.add_tool_substitutions(tools, config.llvm_tools_dir)
191
192# Targets
193
194config.targets = frozenset(config.targets_to_build.split())
195
196for arch in config.targets_to_build.split():
197    config.available_features.add(arch.lower() + '-registered-target')
198
199# Features
200known_arches = ["x86_64", "mips64", "ppc64", "aarch64"]
201if (config.host_ldflags.find("-m32") < 0
202    and any(config.llvm_host_triple.startswith(x) for x in known_arches)):
203  config.available_features.add("llvm-64-bits")
204
205config.available_features.add("host-byteorder-" + sys.byteorder + "-endian")
206
207if sys.platform in ['win32']:
208    # ExecutionEngine, no weak symbols in COFF.
209    config.available_features.add('uses_COFF')
210else:
211    # Others/can-execute.txt
212    config.available_features.add('can-execute')
213
214# Loadable module
215if config.has_plugins:
216    config.available_features.add('plugins')
217
218if config.build_examples:
219    config.available_features.add('examples')
220
221if config.linked_bye_extension:
222    config.substitutions.append(('%llvmcheckext', 'CHECK-EXT'))
223    config.substitutions.append(('%loadbye', ''))
224    config.substitutions.append(('%loadnewpmbye', ''))
225else:
226    config.substitutions.append(('%llvmcheckext', 'CHECK-NOEXT'))
227    config.substitutions.append(('%loadbye',
228                                 '-load={}/Bye{}'.format(config.llvm_shlib_dir,
229                                                         config.llvm_shlib_ext)))
230    config.substitutions.append(('%loadnewpmbye',
231                                 '-load-pass-plugin={}/Bye{}'
232                                 .format(config.llvm_shlib_dir,
233                                         config.llvm_shlib_ext)))
234
235
236# Static libraries are not built if BUILD_SHARED_LIBS is ON.
237if not config.build_shared_libs and not config.link_llvm_dylib:
238    config.available_features.add('static-libs')
239
240if config.have_tf_aot:
241    config.available_features.add("have_tf_aot")
242
243if config.have_tf_api:
244    config.available_features.add("have_tf_api")
245
246def have_cxx_shared_library():
247    readobj_exe = lit.util.which('llvm-readobj', config.llvm_tools_dir)
248    if not readobj_exe:
249        print('llvm-readobj not found')
250        return False
251
252    try:
253        readobj_cmd = subprocess.Popen(
254            [readobj_exe, '-needed-libs', readobj_exe], stdout=subprocess.PIPE)
255    except OSError:
256        print('could not exec llvm-readobj')
257        return False
258
259    readobj_out = readobj_cmd.stdout.read().decode('ascii')
260    readobj_cmd.wait()
261
262    regex = re.compile(r'(libc\+\+|libstdc\+\+|msvcp).*\.(so|dylib|dll)')
263    needed_libs = False
264    for line in readobj_out.splitlines():
265        if 'NeededLibraries [' in line:
266            needed_libs = True
267        if ']' in line:
268            needed_libs = False
269        if needed_libs and regex.search(line.lower()):
270            return True
271    return False
272
273if have_cxx_shared_library():
274    config.available_features.add('cxx-shared-library')
275
276if config.libcxx_used:
277    config.available_features.add('libcxx-used')
278
279# LLVM can be configured with an empty default triple
280# Some tests are "generic" and require a valid default triple
281if config.target_triple:
282    config.available_features.add('default_triple')
283
284import subprocess
285
286
287def have_ld_plugin_support():
288    if not os.path.exists(os.path.join(config.llvm_shlib_dir, 'LLVMgold' + config.llvm_shlib_ext)):
289        return False
290
291    ld_cmd = subprocess.Popen(
292        [config.gold_executable, '--help'], stdout=subprocess.PIPE, env={'LANG': 'C'})
293    ld_out = ld_cmd.stdout.read().decode()
294    ld_cmd.wait()
295
296    if not '-plugin' in ld_out:
297        return False
298
299    # check that the used emulations are supported.
300    emu_line = [l for l in ld_out.split('\n') if 'supported emulations' in l]
301    if len(emu_line) != 1:
302        return False
303    emu_line = emu_line[0]
304    fields = emu_line.split(':')
305    if len(fields) != 3:
306        return False
307    emulations = fields[2].split()
308    if 'elf_x86_64' not in emulations:
309        return False
310    if 'elf32ppc' in emulations:
311        config.available_features.add('ld_emu_elf32ppc')
312
313    ld_version = subprocess.Popen(
314        [config.gold_executable, '--version'], stdout=subprocess.PIPE, env={'LANG': 'C'})
315    if not 'GNU gold' in ld_version.stdout.read().decode():
316        return False
317    ld_version.wait()
318
319    return True
320
321
322if have_ld_plugin_support():
323    config.available_features.add('ld_plugin')
324
325
326def have_ld64_plugin_support():
327    if not os.path.exists(os.path.join(config.llvm_shlib_dir, 'libLTO' + config.llvm_shlib_ext)):
328        return False
329
330    if config.ld64_executable == '':
331        return False
332
333    ld_cmd = subprocess.Popen(
334        [config.ld64_executable, '-v'], stderr=subprocess.PIPE)
335    ld_out = ld_cmd.stderr.read().decode()
336    ld_cmd.wait()
337
338    if 'ld64' not in ld_out or 'LTO' not in ld_out:
339        return False
340
341    return True
342
343
344if have_ld64_plugin_support():
345    config.available_features.add('ld64_plugin')
346
347# Ask llvm-config about asserts
348llvm_config.feature_config(
349    [('--assertion-mode', {'ON': 'asserts'}),
350     ('--build-mode', {'[Dd][Ee][Bb][Uu][Gg]': 'debug'})])
351
352if 'darwin' == sys.platform:
353    cmd = ['sysctl', 'hw.optional.fma']
354    sysctl_cmd = subprocess.Popen(cmd, stdout=subprocess.PIPE)
355
356    # Non zero return, probably a permission issue
357    if sysctl_cmd.wait():
358        print(
359          "Warning: sysctl exists but calling \"{}\" failed, defaulting to no fma3.".format(
360          " ".join(cmd)))
361    else:
362        result = sysctl_cmd.stdout.read().decode('ascii')
363        if 'hw.optional.fma: 1' in result:
364            config.available_features.add('fma3')
365
366# .debug_frame is not emitted for targeting Windows x64.
367if not re.match(r'^x86_64.*-(windows-gnu|windows-msvc)', config.target_triple):
368    config.available_features.add('debug_frame')
369
370if config.have_libxar:
371    config.available_features.add('xar')
372
373if config.enable_threads:
374    config.available_features.add('thread_support')
375
376if config.have_libxml2:
377    config.available_features.add('libxml2')
378
379if config.have_opt_viewer_modules:
380    config.available_features.add('have_opt_viewer_modules')
381
382if config.expensive_checks:
383    config.available_features.add('expensive_checks')
384