1# -*- Python -*-
2
3# Configuration file for 'lit' test runner.
4# This file contains common rules for various compiler-rt testsuites.
5# It is mostly copied from lit.cfg.py used by Clang.
6import os
7import platform
8import re
9import subprocess
10import json
11
12import lit.formats
13import lit.util
14
15# Get shlex.quote if available (added in 3.3), and fall back to pipes.quote if
16# it's not available.
17try:
18  import shlex
19  sh_quote = shlex.quote
20except:
21  import pipes
22  sh_quote = pipes.quote
23
24def find_compiler_libdir():
25  """
26    Returns the path to library resource directory used
27    by the compiler.
28  """
29  if config.compiler_id != 'Clang':
30    lit_config.warning(f'Determining compiler\'s runtime directory is not supported for {config.compiler_id}')
31    # TODO: Support other compilers.
32    return None
33  def get_path_from_clang(args, allow_failure):
34    clang_cmd = [
35      config.clang.strip(),
36      f'--target={config.target_triple}',
37    ]
38    clang_cmd.extend(args)
39    path = None
40    try:
41      result = subprocess.run(
42        clang_cmd,
43        stdout=subprocess.PIPE,
44        stderr=subprocess.PIPE,
45        check=True
46      )
47      path = result.stdout.decode().strip()
48    except subprocess.CalledProcessError as e:
49      msg = f'Failed to run {clang_cmd}\nrc:{e.returncode}\nstdout:{e.stdout}\ne.stderr{e.stderr}'
50      if allow_failure:
51        lit_config.warning(msg)
52      else:
53        lit_config.fatal(msg)
54    return path, clang_cmd
55
56  # Try using `-print-runtime-dir`. This is only supported by very new versions of Clang.
57  # so allow failure here.
58  runtime_dir, clang_cmd = get_path_from_clang(shlex.split(config.target_cflags)
59                                               + ['-print-runtime-dir'],
60                                               allow_failure=True)
61  if runtime_dir:
62    if os.path.exists(runtime_dir):
63      return os.path.realpath(runtime_dir)
64    # TODO(dliew): This should be a fatal error but it seems to trip the `llvm-clang-win-x-aarch64`
65    # bot which is likely misconfigured
66    lit_config.warning(
67      f'Path reported by clang does not exist: \"{runtime_dir}\". '
68      f'This path was found by running {clang_cmd}.'
69    )
70    return None
71
72  # Fall back for older AppleClang that doesn't support `-print-runtime-dir`
73  # Note `-print-file-name=<path to compiler-rt lib>` was broken for Apple
74  # platforms so we can't use that approach here (see https://reviews.llvm.org/D101682).
75  if config.host_os == 'Darwin':
76    lib_dir, _ = get_path_from_clang(['-print-file-name=lib'], allow_failure=False)
77    runtime_dir = os.path.join(lib_dir, 'darwin')
78    if not os.path.exists(runtime_dir):
79      lit_config.fatal(f'Path reported by clang does not exist: {runtime_dir}')
80    return os.path.realpath(runtime_dir)
81
82  lit_config.warning('Failed to determine compiler\'s runtime directory')
83  return None
84
85
86# Choose between lit's internal shell pipeline runner and a real shell.  If
87# LIT_USE_INTERNAL_SHELL is in the environment, we use that as an override.
88use_lit_shell = os.environ.get("LIT_USE_INTERNAL_SHELL")
89if use_lit_shell:
90    # 0 is external, "" is default, and everything else is internal.
91    execute_external = (use_lit_shell == "0")
92else:
93    # Otherwise we default to internal on Windows and external elsewhere, as
94    # bash on Windows is usually very slow.
95    execute_external = (not sys.platform in ['win32'])
96
97# Allow expanding substitutions that are based on other substitutions
98config.recursiveExpansionLimit = 10
99
100# Setup test format.
101config.test_format = lit.formats.ShTest(execute_external)
102if execute_external:
103  config.available_features.add('shell')
104
105compiler_id = getattr(config, 'compiler_id', None)
106if compiler_id == "Clang":
107  if platform.system() != 'Windows':
108    config.cxx_mode_flags = ["--driver-mode=g++"]
109  else:
110    config.cxx_mode_flags = []
111  # We assume that sanitizers should provide good enough error
112  # reports and stack traces even with minimal debug info.
113  config.debug_info_flags = ["-gline-tables-only"]
114  if platform.system() == 'Windows':
115    # On Windows, use CodeView with column info instead of DWARF. Both VS and
116    # windbg do not behave well when column info is enabled, but users have
117    # requested it because it makes ASan reports more precise.
118    config.debug_info_flags.append("-gcodeview")
119    config.debug_info_flags.append("-gcolumn-info")
120elif compiler_id == 'GNU':
121  config.cxx_mode_flags = ["-x c++"]
122  config.debug_info_flags = ["-g"]
123else:
124  lit_config.fatal("Unsupported compiler id: %r" % compiler_id)
125# Add compiler ID to the list of available features.
126config.available_features.add(compiler_id)
127
128# When LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=on, the initial value of
129# config.compiler_rt_libdir (COMPILER_RT_RESOLVED_LIBRARY_OUTPUT_DIR) has the
130# triple as the trailing path component. The value is incorrect for -m32/-m64.
131# Adjust config.compiler_rt accordingly.
132if config.enable_per_target_runtime_dir:
133    if '-m32' in shlex.split(config.target_cflags):
134        config.compiler_rt_libdir = re.sub(r'/x86_64(?=-[^/]+$)', '/i386', config.compiler_rt_libdir)
135    elif '-m64' in shlex.split(config.target_cflags):
136        config.compiler_rt_libdir = re.sub(r'/i386(?=-[^/]+$)', '/x86_64', config.compiler_rt_libdir)
137
138# Ask the compiler for the path to libraries it is going to use. If this
139# doesn't match config.compiler_rt_libdir then it means we might be testing the
140# compiler's own runtime libraries rather than the ones we just built.
141# Warn about about this and handle appropriately.
142compiler_libdir = find_compiler_libdir()
143if compiler_libdir:
144  compiler_rt_libdir_real = os.path.realpath(config.compiler_rt_libdir)
145  if compiler_libdir != compiler_rt_libdir_real:
146    lit_config.warning(
147      'Compiler lib dir != compiler-rt lib dir\n'
148      f'Compiler libdir:     "{compiler_libdir}"\n'
149      f'compiler-rt libdir:  "{compiler_rt_libdir_real}"')
150    if config.test_standalone_build_libs:
151      # Use just built runtime libraries, i.e. the the libraries this built just built.
152      if not config.test_suite_supports_overriding_runtime_lib_path:
153        # Test suite doesn't support this configuration.
154        # TODO(dliew): This should be an error but it seems several bots are
155        # testing incorrectly and having this as an error breaks them.
156        lit_config.warning(
157            'COMPILER_RT_TEST_STANDALONE_BUILD_LIBS=ON, but this test suite '
158            'does not support testing the just-built runtime libraries '
159            'when the test compiler is configured to use different runtime '
160            'libraries. Either modify this test suite to support this test '
161            'configuration, or set COMPILER_RT_TEST_STANDALONE_BUILD_LIBS=OFF '
162            'to test the runtime libraries included in the compiler instead.'
163        )
164    else:
165      # Use Compiler's resource library directory instead.
166      config.compiler_rt_libdir = compiler_libdir
167    lit_config.note(f'Testing using libraries in "{config.compiler_rt_libdir}"')
168
169# If needed, add cflag for shadow scale.
170if config.asan_shadow_scale != '':
171  config.target_cflags += " -mllvm -asan-mapping-scale=" + config.asan_shadow_scale
172if config.memprof_shadow_scale != '':
173  config.target_cflags += " -mllvm -memprof-mapping-scale=" + config.memprof_shadow_scale
174
175config.environment = dict(os.environ)
176
177# Clear some environment variables that might affect Clang.
178possibly_dangerous_env_vars = ['ASAN_OPTIONS', 'DFSAN_OPTIONS', 'LSAN_OPTIONS',
179                               'MSAN_OPTIONS', 'UBSAN_OPTIONS',
180                               'COMPILER_PATH', 'RC_DEBUG_OPTIONS',
181                               'CINDEXTEST_PREAMBLE_FILE', 'LIBRARY_PATH',
182                               'CPATH', 'C_INCLUDE_PATH', 'CPLUS_INCLUDE_PATH',
183                               'OBJC_INCLUDE_PATH', 'OBJCPLUS_INCLUDE_PATH',
184                               'LIBCLANG_TIMING', 'LIBCLANG_OBJTRACKING',
185                               'LIBCLANG_LOGGING', 'LIBCLANG_BGPRIO_INDEX',
186                               'LIBCLANG_BGPRIO_EDIT', 'LIBCLANG_NOTHREADS',
187                               'LIBCLANG_RESOURCE_USAGE',
188                               'LIBCLANG_CODE_COMPLETION_LOGGING',
189                               'XRAY_OPTIONS']
190# Clang/Win32 may refer to %INCLUDE%. vsvarsall.bat sets it.
191if platform.system() != 'Windows':
192    possibly_dangerous_env_vars.append('INCLUDE')
193for name in possibly_dangerous_env_vars:
194  if name in config.environment:
195    del config.environment[name]
196
197# Tweak PATH to include llvm tools dir.
198if (not config.llvm_tools_dir) or (not os.path.exists(config.llvm_tools_dir)):
199  lit_config.fatal("Invalid llvm_tools_dir config attribute: %r" % config.llvm_tools_dir)
200path = os.path.pathsep.join((config.llvm_tools_dir, config.environment['PATH']))
201config.environment['PATH'] = path
202
203# Help MSVS link.exe find the standard libraries.
204# Make sure we only try to use it when targetting Windows.
205if platform.system() == 'Windows' and '-win' in config.target_triple:
206  config.environment['LIB'] = os.environ['LIB']
207
208config.available_features.add(config.host_os.lower())
209
210if re.match(r'^x86_64.*-linux', config.target_triple):
211  config.available_features.add("x86_64-linux")
212
213config.available_features.add("host-byteorder-" + sys.byteorder + "-endian")
214
215if config.have_zlib == "1":
216  config.available_features.add("zlib")
217
218# Use ugly construction to explicitly prohibit "clang", "clang++" etc.
219# in RUN lines.
220config.substitutions.append(
221    (' clang', """\n\n*** Do not use 'clangXXX' in tests,
222     instead define '%clangXXX' substitution in lit config. ***\n\n""") )
223
224if config.host_os == 'NetBSD':
225  nb_commands_dir = os.path.join(config.compiler_rt_src_root,
226                                 "test", "sanitizer_common", "netbsd_commands")
227  config.netbsd_noaslr_prefix = ('sh ' +
228                                 os.path.join(nb_commands_dir, 'run_noaslr.sh'))
229  config.netbsd_nomprotect_prefix = ('sh ' +
230                                     os.path.join(nb_commands_dir,
231                                                  'run_nomprotect.sh'))
232  config.substitutions.append( ('%run_nomprotect',
233                                config.netbsd_nomprotect_prefix) )
234else:
235  config.substitutions.append( ('%run_nomprotect', '%run') )
236
237# Copied from libcxx's config.py
238def get_lit_conf(name, default=None):
239    # Allow overriding on the command line using --param=<name>=<val>
240    val = lit_config.params.get(name, None)
241    if val is None:
242        val = getattr(config, name, None)
243        if val is None:
244            val = default
245    return val
246
247emulator = get_lit_conf('emulator', None)
248
249def get_ios_commands_dir():
250  return os.path.join(config.compiler_rt_src_root, "test", "sanitizer_common", "ios_commands")
251
252# Allow tests to be executed on a simulator or remotely.
253if emulator:
254  config.substitutions.append( ('%run', emulator) )
255  config.substitutions.append( ('%env ', "env ") )
256  # TODO: Implement `%device_rm` to perform removal of files in the emulator.
257  # For now just make it a no-op.
258  lit_config.warning('%device_rm is not implemented')
259  config.substitutions.append( ('%device_rm', 'echo ') )
260  config.compile_wrapper = ""
261elif config.host_os == 'Darwin' and config.apple_platform != "osx":
262  # Darwin tests can be targetting macOS, a device or a simulator. All devices
263  # are declared as "ios", even for iOS derivatives (tvOS, watchOS). Similarly,
264  # all simulators are "iossim". See the table below.
265  #
266  # =========================================================================
267  # Target             | Feature set
268  # =========================================================================
269  # macOS              | darwin
270  # iOS device         | darwin, ios
271  # iOS simulator      | darwin, ios, iossim
272  # tvOS device        | darwin, ios, tvos
273  # tvOS simulator     | darwin, ios, iossim, tvos, tvossim
274  # watchOS device     | darwin, ios, watchos
275  # watchOS simulator  | darwin, ios, iossim, watchos, watchossim
276  # =========================================================================
277
278  ios_or_iossim = "iossim" if config.apple_platform.endswith("sim") else "ios"
279
280  config.available_features.add('ios')
281  device_id_env = "SANITIZER_" + ios_or_iossim.upper() + "_TEST_DEVICE_IDENTIFIER"
282  if ios_or_iossim == "iossim":
283    config.available_features.add('iossim')
284    if device_id_env not in os.environ:
285      lit_config.fatal(
286        '{} must be set in the environment when running iossim tests'.format(
287          device_id_env))
288  if config.apple_platform != "ios" and config.apple_platform != "iossim":
289    config.available_features.add(config.apple_platform)
290
291  ios_commands_dir = get_ios_commands_dir()
292
293  run_wrapper = os.path.join(ios_commands_dir, ios_or_iossim + "_run.py")
294  env_wrapper = os.path.join(ios_commands_dir, ios_or_iossim + "_env.py")
295  compile_wrapper = os.path.join(ios_commands_dir, ios_or_iossim + "_compile.py")
296  prepare_script = os.path.join(ios_commands_dir, ios_or_iossim + "_prepare.py")
297
298  if device_id_env in os.environ:
299    config.environment[device_id_env] = os.environ[device_id_env]
300  config.substitutions.append(('%run', run_wrapper))
301  config.substitutions.append(('%env ', env_wrapper + " "))
302  # Current implementation of %device_rm uses the run_wrapper to do
303  # the work.
304  config.substitutions.append(('%device_rm', '{} rm '.format(run_wrapper)))
305  config.compile_wrapper = compile_wrapper
306
307  try:
308    prepare_output = subprocess.check_output([prepare_script, config.apple_platform, config.clang]).decode().strip()
309  except subprocess.CalledProcessError as e:
310    print("Command failed:")
311    print(e.output)
312    raise e
313  if len(prepare_output) > 0: print(prepare_output)
314  prepare_output_json = prepare_output.split("\n")[-1]
315  prepare_output = json.loads(prepare_output_json)
316  config.environment.update(prepare_output["env"])
317elif config.android:
318  config.available_features.add('android')
319  compile_wrapper = os.path.join(config.compiler_rt_src_root, "test", "sanitizer_common", "android_commands", "android_compile.py") + " "
320  config.compile_wrapper = compile_wrapper
321  config.substitutions.append( ('%run', "") )
322  config.substitutions.append( ('%env ', "env ") )
323else:
324  config.substitutions.append( ('%run', "") )
325  config.substitutions.append( ('%env ', "env ") )
326  # When running locally %device_rm is a no-op.
327  config.substitutions.append( ('%device_rm', 'echo ') )
328  config.compile_wrapper = ""
329
330# Define CHECK-%os to check for OS-dependent output.
331config.substitutions.append( ('CHECK-%os', ("CHECK-" + config.host_os)))
332
333# Define %arch to check for architecture-dependent output.
334config.substitutions.append( ('%arch', (config.host_arch)))
335
336if config.host_os == 'Windows':
337  # FIXME: This isn't quite right. Specifically, it will succeed if the program
338  # does not crash but exits with a non-zero exit code. We ought to merge
339  # KillTheDoctor and not --crash to make the latter more useful and remove the
340  # need for this substitution.
341  config.expect_crash = "not KillTheDoctor "
342else:
343  config.expect_crash = "not --crash "
344
345config.substitutions.append( ("%expect_crash ", config.expect_crash) )
346
347target_arch = getattr(config, 'target_arch', None)
348if target_arch:
349  config.available_features.add(target_arch + '-target-arch')
350  if target_arch in ['x86_64', 'i386']:
351    config.available_features.add('x86-target-arch')
352  config.available_features.add(target_arch + '-' + config.host_os.lower())
353
354compiler_rt_debug = getattr(config, 'compiler_rt_debug', False)
355if not compiler_rt_debug:
356  config.available_features.add('compiler-rt-optimized')
357
358libdispatch = getattr(config, 'compiler_rt_intercept_libdispatch', False)
359if libdispatch:
360  config.available_features.add('libdispatch')
361
362sanitizer_can_use_cxxabi = getattr(config, 'sanitizer_can_use_cxxabi', True)
363if sanitizer_can_use_cxxabi:
364  config.available_features.add('cxxabi')
365
366if not getattr(config, 'sanitizer_uses_static_cxxabi', False):
367  config.available_features.add('shared_cxxabi')
368
369if not getattr(config, 'sanitizer_uses_static_unwind', False):
370  config.available_features.add('shared_unwind')
371
372if config.has_lld:
373  config.available_features.add('lld-available')
374
375if config.use_lld:
376  config.available_features.add('lld')
377
378if config.can_symbolize:
379  config.available_features.add('can-symbolize')
380
381if config.gwp_asan:
382  config.available_features.add('gwp_asan')
383
384lit.util.usePlatformSdkOnDarwin(config, lit_config)
385
386min_macos_deployment_target_substitutions = [
387  (10, 11),
388  (10, 12),
389]
390# TLS requires watchOS 3+
391config.substitutions.append( ('%darwin_min_target_with_tls_support', '%min_macos_deployment_target=10.12') )
392
393if config.host_os == 'Darwin':
394  osx_version = (10, 0, 0)
395  try:
396    osx_version = subprocess.check_output(["sw_vers", "-productVersion"],
397                                          universal_newlines=True)
398    osx_version = tuple(int(x) for x in osx_version.split('.'))
399    if len(osx_version) == 2: osx_version = (osx_version[0], osx_version[1], 0)
400    if osx_version >= (10, 11):
401      config.available_features.add('osx-autointerception')
402      config.available_features.add('osx-ld64-live_support')
403  except subprocess.CalledProcessError:
404    pass
405
406  config.darwin_osx_version = osx_version
407
408  # Detect x86_64h
409  try:
410    output = subprocess.check_output(["sysctl", "hw.cpusubtype"])
411    output_re = re.match("^hw.cpusubtype: ([0-9]+)$", output)
412    if output_re:
413      cpu_subtype = int(output_re.group(1))
414      if cpu_subtype == 8: # x86_64h
415        config.available_features.add('x86_64h')
416  except:
417    pass
418
419  # 32-bit iOS simulator is deprecated and removed in latest Xcode.
420  if config.apple_platform == "iossim":
421    if config.target_arch == "i386":
422      config.unsupported = True
423
424  def get_macos_aligned_version(macos_vers):
425    platform = config.apple_platform
426    if platform == 'osx':
427      return macos_vers
428
429    macos_major, macos_minor = macos_vers
430    assert macos_major >= 10
431
432    if macos_major == 10:  # macOS 10.x
433      major = macos_minor
434      minor = 0
435    else:                  # macOS 11+
436      major = macos_major + 5
437      minor = macos_minor
438
439    assert major >= 11
440
441    if platform.startswith('ios') or platform.startswith('tvos'):
442      major -= 2
443    elif platform.startswith('watch'):
444      major -= 9
445    else:
446      lit_config.fatal("Unsupported apple platform '{}'".format(platform))
447
448    return (major, minor)
449
450  for vers in min_macos_deployment_target_substitutions:
451    flag = config.apple_platform_min_deployment_target_flag
452    major, minor = get_macos_aligned_version(vers)
453    config.substitutions.append( ('%%min_macos_deployment_target=%s.%s' % vers, '{}={}.{}'.format(flag, major, minor)) )
454else:
455  for vers in min_macos_deployment_target_substitutions:
456    config.substitutions.append( ('%%min_macos_deployment_target=%s.%s' % vers, '') )
457
458if config.android:
459  env = os.environ.copy()
460  if config.android_serial:
461    env['ANDROID_SERIAL'] = config.android_serial
462    config.environment['ANDROID_SERIAL'] = config.android_serial
463
464  adb = os.environ.get('ADB', 'adb')
465
466  # These are needed for tests to upload/download temp files, such as
467  # suppression-files, to device.
468  config.substitutions.append( ('%device_rundir/', "/data/local/tmp/Output/") )
469  config.substitutions.append( ('%push_to_device', "%s -s '%s' push " % (adb, env['ANDROID_SERIAL']) ) )
470  config.substitutions.append( ('%adb_shell ', "%s -s '%s' shell " % (adb, env['ANDROID_SERIAL']) ) )
471  config.substitutions.append( ('%device_rm', "%s -s '%s' shell 'rm ' " % (adb, env['ANDROID_SERIAL']) ) )
472
473  try:
474    android_api_level_str = subprocess.check_output([adb, "shell", "getprop", "ro.build.version.sdk"], env=env).rstrip()
475    android_api_codename = subprocess.check_output([adb, "shell", "getprop", "ro.build.version.codename"], env=env).rstrip().decode("utf-8")
476  except (subprocess.CalledProcessError, OSError):
477    lit_config.fatal("Failed to read ro.build.version.sdk (using '%s' as adb)" % adb)
478  try:
479    android_api_level = int(android_api_level_str)
480  except ValueError:
481    lit_config.fatal("Failed to read ro.build.version.sdk (using '%s' as adb): got '%s'" % (adb, android_api_level_str))
482  android_api_level = min(android_api_level, int(config.android_api_level))
483  for required in [26, 28, 29, 30]:
484    if android_api_level >= required:
485      config.available_features.add('android-%s' % required)
486  # FIXME: Replace with appropriate version when availible.
487  if android_api_level > 30 or (android_api_level == 30 and android_api_codename == 'S'):
488    config.available_features.add('android-thread-properties-api')
489
490  # Prepare the device.
491  android_tmpdir = '/data/local/tmp/Output'
492  subprocess.check_call([adb, "shell", "mkdir", "-p", android_tmpdir], env=env)
493  for file in config.android_files_to_push:
494    subprocess.check_call([adb, "push", file, android_tmpdir], env=env)
495else:
496  config.substitutions.append( ('%device_rundir/', "") )
497  config.substitutions.append( ('%push_to_device', "echo ") )
498  config.substitutions.append( ('%adb_shell', "echo ") )
499
500if config.host_os == 'Linux':
501  # detect whether we are using glibc, and which version
502  # NB: 'ldd' is just one of the tools commonly installed as part of glibc/musl
503  ldd_ver_cmd = subprocess.Popen(['ldd', '--version'],
504                                 stdout=subprocess.PIPE,
505                                 stderr=subprocess.DEVNULL,
506                                 env={'LANG': 'C'})
507  sout, _ = ldd_ver_cmd.communicate()
508  ver_lines = sout.splitlines()
509  if not config.android and len(ver_lines) and ver_lines[0].startswith(b"ldd "):
510    from distutils.version import LooseVersion
511    ver = LooseVersion(ver_lines[0].split()[-1].decode())
512    for required in ["2.27", "2.30"]:
513      if ver >= LooseVersion(required):
514        config.available_features.add("glibc-" + required)
515
516sancovcc_path = os.path.join(config.llvm_tools_dir, "sancov")
517if os.path.exists(sancovcc_path):
518  config.available_features.add("has_sancovcc")
519  config.substitutions.append( ("%sancovcc ", sancovcc_path) )
520
521def is_darwin_lto_supported():
522  return os.path.exists(os.path.join(config.llvm_shlib_dir, 'libLTO.dylib'))
523
524def is_binutils_lto_supported():
525  if not os.path.exists(os.path.join(config.llvm_shlib_dir, 'LLVMgold.so')):
526    return False
527
528  # We require both ld.bfd and ld.gold exist and support plugins. They are in
529  # the same repository 'binutils-gdb' and usually built together.
530  for exe in (config.gnu_ld_executable, config.gold_executable):
531    ld_cmd = subprocess.Popen([exe, '--help'], stdout=subprocess.PIPE, env={'LANG': 'C'})
532    ld_out = ld_cmd.stdout.read().decode()
533    ld_cmd.wait()
534    if not '-plugin' in ld_out:
535      return False
536
537  return True
538
539def is_windows_lto_supported():
540  return os.path.exists(os.path.join(config.llvm_tools_dir, 'lld-link.exe'))
541
542if config.host_os == 'Darwin' and is_darwin_lto_supported():
543  config.lto_supported = True
544  config.lto_launch = ["env", "DYLD_LIBRARY_PATH=" + config.llvm_shlib_dir]
545  config.lto_flags = []
546elif config.host_os in ['Linux', 'FreeBSD', 'NetBSD']:
547  config.lto_supported = False
548  if config.use_lld:
549    config.lto_supported = True
550  if is_binutils_lto_supported():
551    config.available_features.add('binutils_lto')
552    config.lto_supported = True
553
554  if config.lto_supported:
555    config.lto_launch = []
556    if config.use_lld:
557      config.lto_flags = ["-fuse-ld=lld"]
558    else:
559      config.lto_flags = ["-fuse-ld=gold"]
560elif config.host_os == 'Windows' and is_windows_lto_supported():
561  config.lto_supported = True
562  config.lto_launch = []
563  config.lto_flags = ["-fuse-ld=lld"]
564else:
565  config.lto_supported = False
566
567if config.lto_supported:
568  config.available_features.add('lto')
569  if config.use_thinlto:
570    config.available_features.add('thinlto')
571    config.lto_flags += ["-flto=thin"]
572  else:
573    config.lto_flags += ["-flto"]
574  if config.use_newpm:
575    config.lto_flags += ["-fexperimental-new-pass-manager"]
576
577if config.have_rpc_xdr_h:
578  config.available_features.add('sunrpc')
579
580# Ask llvm-config about assertion mode.
581try:
582  llvm_config_cmd = subprocess.Popen(
583      [os.path.join(config.llvm_tools_dir, 'llvm-config'), '--assertion-mode'],
584      stdout = subprocess.PIPE,
585      env=config.environment)
586except OSError as e:
587  print("Could not launch llvm-config in " + config.llvm_tools_dir)
588  print("    Failed with error #{0}: {1}".format(e.errno, e.strerror))
589  exit(42)
590
591if re.search(r'ON', llvm_config_cmd.stdout.read().decode('ascii')):
592  config.available_features.add('asserts')
593llvm_config_cmd.wait()
594
595# Sanitizer tests tend to be flaky on Windows due to PR24554, so add some
596# retries. We don't do this on otther platforms because it's slower.
597if platform.system() == 'Windows':
598  config.test_retry_attempts = 2
599
600# No throttling on non-Darwin platforms.
601lit_config.parallelism_groups['shadow-memory'] = None
602
603if platform.system() == 'Darwin':
604  ios_device = config.apple_platform != 'osx' and not config.apple_platform.endswith('sim')
605  # Force sequential execution when running tests on iOS devices.
606  if ios_device:
607    lit_config.warning('Forcing sequential execution for iOS device tests')
608    lit_config.parallelism_groups['ios-device'] = 1
609    config.parallelism_group = 'ios-device'
610
611  # Only run up to 3 processes that require shadow memory simultaneously on
612  # 64-bit Darwin. Using more scales badly and hogs the system due to
613  # inefficient handling of large mmap'd regions (terabytes) by the kernel.
614  elif config.target_arch in ['x86_64', 'x86_64h']:
615    lit_config.warning('Throttling sanitizer tests that require shadow memory on Darwin 64bit')
616    lit_config.parallelism_groups['shadow-memory'] = 3
617
618# Multiple substitutions are necessary to support multiple shared objects used
619# at once.
620# Note that substitutions with numbers have to be defined first to avoid
621# being subsumed by substitutions with smaller postfix.
622for postfix in ["2", "1", ""]:
623  if config.host_os == 'Darwin':
624    config.substitutions.append( ("%ld_flags_rpath_exe" + postfix, '-Wl,-rpath,@executable_path/ %dynamiclib' + postfix) )
625    config.substitutions.append( ("%ld_flags_rpath_so" + postfix, '-install_name @rpath/`basename %dynamiclib{}`'.format(postfix)) )
626  elif config.host_os in ('FreeBSD', 'NetBSD', 'OpenBSD'):
627    config.substitutions.append( ("%ld_flags_rpath_exe" + postfix, "-Wl,-z,origin -Wl,-rpath,\$ORIGIN -L%T -l%xdynamiclib_namespec" + postfix) )
628    config.substitutions.append( ("%ld_flags_rpath_so" + postfix, '') )
629  elif config.host_os == 'Linux':
630    config.substitutions.append( ("%ld_flags_rpath_exe" + postfix, "-Wl,-rpath,\$ORIGIN -L%T -l%xdynamiclib_namespec" + postfix) )
631    config.substitutions.append( ("%ld_flags_rpath_so" + postfix, '') )
632  elif config.host_os == 'SunOS':
633    config.substitutions.append( ("%ld_flags_rpath_exe" + postfix, "-Wl,-R\$ORIGIN -L%T -l%xdynamiclib_namespec" + postfix) )
634    config.substitutions.append( ("%ld_flags_rpath_so" + postfix, '') )
635
636  # Must be defined after the substitutions that use %dynamiclib.
637  config.substitutions.append( ("%dynamiclib" + postfix, '%T/%xdynamiclib_filename' + postfix) )
638  config.substitutions.append( ("%xdynamiclib_filename" + postfix, 'lib%xdynamiclib_namespec{}.so'.format(postfix)) )
639  config.substitutions.append( ("%xdynamiclib_namespec", '%basename_t.dynamic') )
640
641# Provide a substitution that can be used to tell Clang to use a static libstdc++.
642# The substitution expands to nothing on non Linux platforms.
643# FIXME: This should check the target OS, not the host OS.
644if config.host_os == 'Linux':
645  config.substitutions.append( ("%linux_static_libstdcplusplus", "-stdlib=libstdc++ -static-libstdc++") )
646else:
647  config.substitutions.append( ("%linux_static_libstdcplusplus", "") )
648
649config.default_sanitizer_opts = []
650if config.host_os == 'Darwin':
651  # On Darwin, we default to `abort_on_error=1`, which would make tests run
652  # much slower. Let's override this and run lit tests with 'abort_on_error=0'.
653  config.default_sanitizer_opts += ['abort_on_error=0']
654  config.default_sanitizer_opts += ['log_to_syslog=0']
655  if lit.util.which('log'):
656    # Querying the log can only done by a privileged user so
657    # so check if we can query the log.
658    exit_code = -1
659    with open('/dev/null', 'r') as f:
660      # Run a `log show` command the should finish fairly quickly and produce very little output.
661      exit_code = subprocess.call(['log', 'show', '--last', '1m', '--predicate', '1 == 0'], stdout=f, stderr=f)
662    if exit_code == 0:
663      config.available_features.add('darwin_log_cmd')
664    else:
665      lit_config.warning('log command found but cannot queried')
666  else:
667    lit_config.warning('log command not found. Some tests will be skipped.')
668elif config.android:
669  config.default_sanitizer_opts += ['abort_on_error=0']
670
671# Allow tests to use REQUIRES=stable-runtime.  For use when you cannot use XFAIL
672# because the test hangs or fails on one configuration and not the other.
673if config.android or (config.target_arch not in ['arm', 'armhf', 'aarch64']):
674  config.available_features.add('stable-runtime')
675
676if config.asan_shadow_scale:
677  config.available_features.add("shadow-scale-%s" % config.asan_shadow_scale)
678else:
679  config.available_features.add("shadow-scale-3")
680
681if config.memprof_shadow_scale:
682  config.available_features.add("memprof-shadow-scale-%s" % config.memprof_shadow_scale)
683else:
684  config.available_features.add("memprof-shadow-scale-3")
685
686if config.expensive_checks:
687  config.available_features.add("expensive_checks")
688
689# Propagate the LLD/LTO into the clang config option, so nothing else is needed.
690run_wrapper = []
691target_cflags = [getattr(config, 'target_cflags', None)]
692extra_cflags = []
693
694if config.use_lto and config.lto_supported:
695  run_wrapper += config.lto_launch
696  extra_cflags += config.lto_flags
697elif config.use_lto and (not config.lto_supported):
698  config.unsupported = True
699
700if config.use_lld and config.has_lld and not config.use_lto:
701  extra_cflags += ["-fuse-ld=lld"]
702elif config.use_lld and (not config.has_lld):
703  config.unsupported = True
704
705# Append any extra flags passed in lit_config
706append_target_cflags = lit_config.params.get('append_target_cflags', None)
707if append_target_cflags:
708  lit_config.note('Appending to extra_cflags: "{}"'.format(append_target_cflags))
709  extra_cflags += [append_target_cflags]
710
711config.clang = " " + " ".join(run_wrapper + [config.compile_wrapper, config.clang]) + " "
712config.target_cflags = " " + " ".join(target_cflags + extra_cflags) + " "
713
714if config.host_os == 'Darwin':
715  config.substitutions.append((
716    "%get_pid_from_output",
717    "{} {}/get_pid_from_output.py".format(
718      sh_quote(config.python_executable),
719      sh_quote(get_ios_commands_dir())
720    ))
721  )
722  config.substitutions.append(
723    ("%print_crashreport_for_pid",
724    "{} {}/print_crashreport_for_pid.py".format(
725      sh_quote(config.python_executable),
726      sh_quote(get_ios_commands_dir())
727    ))
728  )
729