Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / lit.common.cfg.py
blob230efd580199cb66773e54933f52f95e11833008
1 # -*- Python -*-
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.
6 import os
7 import platform
8 import re
9 import shlex
10 import subprocess
11 import json
13 import lit.formats
14 import lit.util
17 def find_compiler_libdir():
18 """
19 Returns the path to library resource directory used
20 by the compiler.
21 """
22 if config.compiler_id != "Clang":
23 lit_config.warning(
24 f"Determining compiler's runtime directory is not supported for {config.compiler_id}"
26 # TODO: Support other compilers.
27 return None
29 def get_path_from_clang(args, allow_failure):
30 clang_cmd = [
31 config.clang.strip(),
32 f"--target={config.target_triple}",
34 clang_cmd.extend(args)
35 path = None
36 try:
37 result = subprocess.run(
38 clang_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True
40 path = result.stdout.decode().strip()
41 except subprocess.CalledProcessError as e:
42 msg = f"Failed to run {clang_cmd}\nrc:{e.returncode}\nstdout:{e.stdout}\ne.stderr{e.stderr}"
43 if allow_failure:
44 lit_config.warning(msg)
45 else:
46 lit_config.fatal(msg)
47 return path, clang_cmd
49 # Try using `-print-runtime-dir`. This is only supported by very new versions of Clang.
50 # so allow failure here.
51 runtime_dir, clang_cmd = get_path_from_clang(
52 shlex.split(config.target_cflags) + ["-print-runtime-dir"], allow_failure=True
54 if runtime_dir:
55 if os.path.exists(runtime_dir):
56 return os.path.realpath(runtime_dir)
57 # TODO(dliew): This should be a fatal error but it seems to trip the `llvm-clang-win-x-aarch64`
58 # bot which is likely misconfigured
59 lit_config.warning(
60 f'Path reported by clang does not exist: "{runtime_dir}". '
61 f"This path was found by running {clang_cmd}."
63 return None
65 # Fall back for older AppleClang that doesn't support `-print-runtime-dir`
66 # Note `-print-file-name=<path to compiler-rt lib>` was broken for Apple
67 # platforms so we can't use that approach here (see https://reviews.llvm.org/D101682).
68 if config.host_os == "Darwin":
69 lib_dir, _ = get_path_from_clang(["-print-file-name=lib"], allow_failure=False)
70 runtime_dir = os.path.join(lib_dir, "darwin")
71 if not os.path.exists(runtime_dir):
72 lit_config.fatal(f"Path reported by clang does not exist: {runtime_dir}")
73 return os.path.realpath(runtime_dir)
75 lit_config.warning("Failed to determine compiler's runtime directory")
76 return None
79 def push_dynamic_library_lookup_path(config, new_path):
80 if platform.system() == "Windows":
81 dynamic_library_lookup_var = "PATH"
82 elif platform.system() == "Darwin":
83 dynamic_library_lookup_var = "DYLD_LIBRARY_PATH"
84 else:
85 dynamic_library_lookup_var = "LD_LIBRARY_PATH"
87 new_ld_library_path = os.path.pathsep.join(
88 (new_path, config.environment.get(dynamic_library_lookup_var, ""))
90 config.environment[dynamic_library_lookup_var] = new_ld_library_path
92 if platform.system() == "FreeBSD":
93 dynamic_library_lookup_var = "LD_32_LIBRARY_PATH"
94 new_ld_32_library_path = os.path.pathsep.join(
95 (new_path, config.environment.get(dynamic_library_lookup_var, ""))
97 config.environment[dynamic_library_lookup_var] = new_ld_32_library_path
99 if platform.system() == "SunOS":
100 dynamic_library_lookup_var = "LD_LIBRARY_PATH_32"
101 new_ld_library_path_32 = os.path.pathsep.join(
102 (new_path, config.environment.get(dynamic_library_lookup_var, ""))
104 config.environment[dynamic_library_lookup_var] = new_ld_library_path_32
106 dynamic_library_lookup_var = "LD_LIBRARY_PATH_64"
107 new_ld_library_path_64 = os.path.pathsep.join(
108 (new_path, config.environment.get(dynamic_library_lookup_var, ""))
110 config.environment[dynamic_library_lookup_var] = new_ld_library_path_64
113 # Choose between lit's internal shell pipeline runner and a real shell. If
114 # LIT_USE_INTERNAL_SHELL is in the environment, we use that as an override.
115 use_lit_shell = os.environ.get("LIT_USE_INTERNAL_SHELL")
116 if use_lit_shell:
117 # 0 is external, "" is default, and everything else is internal.
118 execute_external = use_lit_shell == "0"
119 else:
120 # Otherwise we default to internal on Windows and external elsewhere, as
121 # bash on Windows is usually very slow.
122 execute_external = not sys.platform in ["win32"]
124 # Allow expanding substitutions that are based on other substitutions
125 config.recursiveExpansionLimit = 10
127 # Setup test format.
128 config.test_format = lit.formats.ShTest(execute_external)
129 if execute_external:
130 config.available_features.add("shell")
132 target_is_msvc = bool(re.match(r".*-windows-msvc$", config.target_triple))
134 compiler_id = getattr(config, "compiler_id", None)
135 if compiler_id == "Clang":
136 if not (platform.system() == "Windows" and target_is_msvc):
137 config.cxx_mode_flags = ["--driver-mode=g++"]
138 else:
139 config.cxx_mode_flags = []
140 # We assume that sanitizers should provide good enough error
141 # reports and stack traces even with minimal debug info.
142 config.debug_info_flags = ["-gline-tables-only"]
143 if platform.system() == "Windows" and target_is_msvc:
144 # On MSVC, use CodeView with column info instead of DWARF. Both VS and
145 # windbg do not behave well when column info is enabled, but users have
146 # requested it because it makes ASan reports more precise.
147 config.debug_info_flags.append("-gcodeview")
148 config.debug_info_flags.append("-gcolumn-info")
149 elif compiler_id == "GNU":
150 config.cxx_mode_flags = ["-x c++"]
151 config.debug_info_flags = ["-g"]
152 else:
153 lit_config.fatal("Unsupported compiler id: %r" % compiler_id)
154 # Add compiler ID to the list of available features.
155 config.available_features.add(compiler_id)
157 # When LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=on, the initial value of
158 # config.compiler_rt_libdir (COMPILER_RT_RESOLVED_LIBRARY_OUTPUT_DIR) has the
159 # triple as the trailing path component. The value is incorrect for -m32/-m64.
160 # Adjust config.compiler_rt accordingly.
161 if config.enable_per_target_runtime_dir:
162 if "-m32" in shlex.split(config.target_cflags):
163 config.compiler_rt_libdir = re.sub(
164 r"/x86_64(?=-[^/]+$)", "/i386", config.compiler_rt_libdir
166 elif "-m64" in shlex.split(config.target_cflags):
167 config.compiler_rt_libdir = re.sub(
168 r"/i386(?=-[^/]+$)", "/x86_64", config.compiler_rt_libdir
171 # Ask the compiler for the path to libraries it is going to use. If this
172 # doesn't match config.compiler_rt_libdir then it means we might be testing the
173 # compiler's own runtime libraries rather than the ones we just built.
174 # Warn about about this and handle appropriately.
175 compiler_libdir = find_compiler_libdir()
176 if compiler_libdir:
177 compiler_rt_libdir_real = os.path.realpath(config.compiler_rt_libdir)
178 if compiler_libdir != compiler_rt_libdir_real:
179 lit_config.warning(
180 "Compiler lib dir != compiler-rt lib dir\n"
181 f'Compiler libdir: "{compiler_libdir}"\n'
182 f'compiler-rt libdir: "{compiler_rt_libdir_real}"'
184 if config.test_standalone_build_libs:
185 # Use just built runtime libraries, i.e. the the libraries this built just built.
186 if not config.test_suite_supports_overriding_runtime_lib_path:
187 # Test suite doesn't support this configuration.
188 # TODO(dliew): This should be an error but it seems several bots are
189 # testing incorrectly and having this as an error breaks them.
190 lit_config.warning(
191 "COMPILER_RT_TEST_STANDALONE_BUILD_LIBS=ON, but this test suite "
192 "does not support testing the just-built runtime libraries "
193 "when the test compiler is configured to use different runtime "
194 "libraries. Either modify this test suite to support this test "
195 "configuration, or set COMPILER_RT_TEST_STANDALONE_BUILD_LIBS=OFF "
196 "to test the runtime libraries included in the compiler instead."
198 else:
199 # Use Compiler's resource library directory instead.
200 config.compiler_rt_libdir = compiler_libdir
201 lit_config.note(f'Testing using libraries in "{config.compiler_rt_libdir}"')
203 # If needed, add cflag for shadow scale.
204 if config.asan_shadow_scale != "":
205 config.target_cflags += " -mllvm -asan-mapping-scale=" + config.asan_shadow_scale
206 if config.memprof_shadow_scale != "":
207 config.target_cflags += (
208 " -mllvm -memprof-mapping-scale=" + config.memprof_shadow_scale
211 # Clear some environment variables that might affect Clang.
212 possibly_dangerous_env_vars = [
213 "ASAN_OPTIONS",
214 "DFSAN_OPTIONS",
215 "HWASAN_OPTIONS",
216 "LSAN_OPTIONS",
217 "MSAN_OPTIONS",
218 "UBSAN_OPTIONS",
219 "COMPILER_PATH",
220 "RC_DEBUG_OPTIONS",
221 "CINDEXTEST_PREAMBLE_FILE",
222 "LIBRARY_PATH",
223 "CPATH",
224 "C_INCLUDE_PATH",
225 "CPLUS_INCLUDE_PATH",
226 "OBJC_INCLUDE_PATH",
227 "OBJCPLUS_INCLUDE_PATH",
228 "LIBCLANG_TIMING",
229 "LIBCLANG_OBJTRACKING",
230 "LIBCLANG_LOGGING",
231 "LIBCLANG_BGPRIO_INDEX",
232 "LIBCLANG_BGPRIO_EDIT",
233 "LIBCLANG_NOTHREADS",
234 "LIBCLANG_RESOURCE_USAGE",
235 "LIBCLANG_CODE_COMPLETION_LOGGING",
236 "XRAY_OPTIONS",
238 # Clang/MSVC may refer to %INCLUDE%. vsvarsall.bat sets it.
239 if not (platform.system() == "Windows" and target_is_msvc):
240 possibly_dangerous_env_vars.append("INCLUDE")
241 for name in possibly_dangerous_env_vars:
242 if name in config.environment:
243 del config.environment[name]
245 # Tweak PATH to include llvm tools dir.
246 if (not config.llvm_tools_dir) or (not os.path.exists(config.llvm_tools_dir)):
247 lit_config.fatal(
248 "Invalid llvm_tools_dir config attribute: %r" % config.llvm_tools_dir
250 path = os.path.pathsep.join((config.llvm_tools_dir, config.environment["PATH"]))
251 config.environment["PATH"] = path
253 # Help MSVS link.exe find the standard libraries.
254 # Make sure we only try to use it when targetting Windows.
255 if platform.system() == "Windows" and target_is_msvc:
256 config.environment["LIB"] = os.environ["LIB"]
258 config.available_features.add(config.host_os.lower())
260 if config.target_triple.startswith("ppc") or config.target_triple.startswith("powerpc"):
261 config.available_features.add("ppc")
263 if re.match(r"^x86_64.*-linux", config.target_triple):
264 config.available_features.add("x86_64-linux")
266 config.available_features.add("host-byteorder-" + sys.byteorder + "-endian")
268 if config.have_zlib:
269 config.available_features.add("zlib")
271 if config.have_internal_symbolizer:
272 config.available_features.add("internal_symbolizer")
274 # Use ugly construction to explicitly prohibit "clang", "clang++" etc.
275 # in RUN lines.
276 config.substitutions.append(
278 " clang",
279 """\n\n*** Do not use 'clangXXX' in tests,
280 instead define '%clangXXX' substitution in lit config. ***\n\n""",
284 if config.host_os == "NetBSD":
285 nb_commands_dir = os.path.join(
286 config.compiler_rt_src_root, "test", "sanitizer_common", "netbsd_commands"
288 config.netbsd_noaslr_prefix = "sh " + os.path.join(nb_commands_dir, "run_noaslr.sh")
289 config.netbsd_nomprotect_prefix = "sh " + os.path.join(
290 nb_commands_dir, "run_nomprotect.sh"
292 config.substitutions.append(("%run_nomprotect", config.netbsd_nomprotect_prefix))
293 else:
294 config.substitutions.append(("%run_nomprotect", "%run"))
296 # Copied from libcxx's config.py
297 def get_lit_conf(name, default=None):
298 # Allow overriding on the command line using --param=<name>=<val>
299 val = lit_config.params.get(name, None)
300 if val is None:
301 val = getattr(config, name, None)
302 if val is None:
303 val = default
304 return val
307 emulator = get_lit_conf("emulator", None)
310 def get_ios_commands_dir():
311 return os.path.join(
312 config.compiler_rt_src_root, "test", "sanitizer_common", "ios_commands"
316 # Allow tests to be executed on a simulator or remotely.
317 if emulator:
318 config.substitutions.append(("%run", emulator))
319 config.substitutions.append(("%env ", "env "))
320 # TODO: Implement `%device_rm` to perform removal of files in the emulator.
321 # For now just make it a no-op.
322 lit_config.warning("%device_rm is not implemented")
323 config.substitutions.append(("%device_rm", "echo "))
324 config.compile_wrapper = ""
325 elif config.host_os == "Darwin" and config.apple_platform != "osx":
326 # Darwin tests can be targetting macOS, a device or a simulator. All devices
327 # are declared as "ios", even for iOS derivatives (tvOS, watchOS). Similarly,
328 # all simulators are "iossim". See the table below.
330 # =========================================================================
331 # Target | Feature set
332 # =========================================================================
333 # macOS | darwin
334 # iOS device | darwin, ios
335 # iOS simulator | darwin, ios, iossim
336 # tvOS device | darwin, ios, tvos
337 # tvOS simulator | darwin, ios, iossim, tvos, tvossim
338 # watchOS device | darwin, ios, watchos
339 # watchOS simulator | darwin, ios, iossim, watchos, watchossim
340 # =========================================================================
342 ios_or_iossim = "iossim" if config.apple_platform.endswith("sim") else "ios"
344 config.available_features.add("ios")
345 device_id_env = "SANITIZER_" + ios_or_iossim.upper() + "_TEST_DEVICE_IDENTIFIER"
346 if ios_or_iossim == "iossim":
347 config.available_features.add("iossim")
348 if device_id_env not in os.environ:
349 lit_config.fatal(
350 "{} must be set in the environment when running iossim tests".format(
351 device_id_env
354 if config.apple_platform != "ios" and config.apple_platform != "iossim":
355 config.available_features.add(config.apple_platform)
357 ios_commands_dir = get_ios_commands_dir()
359 run_wrapper = os.path.join(ios_commands_dir, ios_or_iossim + "_run.py")
360 env_wrapper = os.path.join(ios_commands_dir, ios_or_iossim + "_env.py")
361 compile_wrapper = os.path.join(ios_commands_dir, ios_or_iossim + "_compile.py")
362 prepare_script = os.path.join(ios_commands_dir, ios_or_iossim + "_prepare.py")
364 if device_id_env in os.environ:
365 config.environment[device_id_env] = os.environ[device_id_env]
366 config.substitutions.append(("%run", run_wrapper))
367 config.substitutions.append(("%env ", env_wrapper + " "))
368 # Current implementation of %device_rm uses the run_wrapper to do
369 # the work.
370 config.substitutions.append(("%device_rm", "{} rm ".format(run_wrapper)))
371 config.compile_wrapper = compile_wrapper
373 try:
374 prepare_output = (
375 subprocess.check_output(
376 [prepare_script, config.apple_platform, config.clang]
378 .decode()
379 .strip()
381 except subprocess.CalledProcessError as e:
382 print("Command failed:")
383 print(e.output)
384 raise e
385 if len(prepare_output) > 0:
386 print(prepare_output)
387 prepare_output_json = prepare_output.split("\n")[-1]
388 prepare_output = json.loads(prepare_output_json)
389 config.environment.update(prepare_output["env"])
390 elif config.android:
391 config.available_features.add("android")
392 compile_wrapper = (
393 os.path.join(
394 config.compiler_rt_src_root,
395 "test",
396 "sanitizer_common",
397 "android_commands",
398 "android_compile.py",
400 + " "
402 config.compile_wrapper = compile_wrapper
403 config.substitutions.append(("%run", ""))
404 config.substitutions.append(("%env ", "env "))
405 else:
406 config.substitutions.append(("%run", ""))
407 config.substitutions.append(("%env ", "env "))
408 # When running locally %device_rm is a no-op.
409 config.substitutions.append(("%device_rm", "echo "))
410 config.compile_wrapper = ""
412 # Define CHECK-%os to check for OS-dependent output.
413 config.substitutions.append(("CHECK-%os", ("CHECK-" + config.host_os)))
415 # Define %arch to check for architecture-dependent output.
416 config.substitutions.append(("%arch", (config.host_arch)))
418 if config.host_os == "Windows":
419 # FIXME: This isn't quite right. Specifically, it will succeed if the program
420 # does not crash but exits with a non-zero exit code. We ought to merge
421 # KillTheDoctor and not --crash to make the latter more useful and remove the
422 # need for this substitution.
423 config.expect_crash = "not KillTheDoctor "
424 else:
425 config.expect_crash = "not --crash "
427 config.substitutions.append(("%expect_crash ", config.expect_crash))
429 target_arch = getattr(config, "target_arch", None)
430 if target_arch:
431 config.available_features.add(target_arch + "-target-arch")
432 if target_arch in ["x86_64", "i386"]:
433 config.available_features.add("x86-target-arch")
434 config.available_features.add(target_arch + "-" + config.host_os.lower())
436 compiler_rt_debug = getattr(config, "compiler_rt_debug", False)
437 if not compiler_rt_debug:
438 config.available_features.add("compiler-rt-optimized")
440 libdispatch = getattr(config, "compiler_rt_intercept_libdispatch", False)
441 if libdispatch:
442 config.available_features.add("libdispatch")
444 sanitizer_can_use_cxxabi = getattr(config, "sanitizer_can_use_cxxabi", True)
445 if sanitizer_can_use_cxxabi:
446 config.available_features.add("cxxabi")
448 if not getattr(config, "sanitizer_uses_static_cxxabi", False):
449 config.available_features.add("shared_cxxabi")
451 if not getattr(config, "sanitizer_uses_static_unwind", False):
452 config.available_features.add("shared_unwind")
454 if config.has_lld:
455 config.available_features.add("lld-available")
457 if config.use_lld:
458 config.available_features.add("lld")
460 if config.can_symbolize:
461 config.available_features.add("can-symbolize")
463 if config.gwp_asan:
464 config.available_features.add("gwp_asan")
466 lit.util.usePlatformSdkOnDarwin(config, lit_config)
468 min_macos_deployment_target_substitutions = [
469 (10, 11),
470 (10, 12),
472 # TLS requires watchOS 3+
473 config.substitutions.append(
474 ("%darwin_min_target_with_tls_support", "%min_macos_deployment_target=10.12")
477 if config.host_os == "Darwin":
478 osx_version = (10, 0, 0)
479 try:
480 osx_version = subprocess.check_output(
481 ["sw_vers", "-productVersion"], universal_newlines=True
483 osx_version = tuple(int(x) for x in osx_version.split("."))
484 if len(osx_version) == 2:
485 osx_version = (osx_version[0], osx_version[1], 0)
486 if osx_version >= (10, 11):
487 config.available_features.add("osx-autointerception")
488 config.available_features.add("osx-ld64-live_support")
489 if osx_version >= (13, 1):
490 config.available_features.add("jit-compatible-osx-swift-runtime")
491 except subprocess.CalledProcessError:
492 pass
494 config.darwin_osx_version = osx_version
496 # Detect x86_64h
497 try:
498 output = subprocess.check_output(["sysctl", "hw.cpusubtype"])
499 output_re = re.match("^hw.cpusubtype: ([0-9]+)$", output)
500 if output_re:
501 cpu_subtype = int(output_re.group(1))
502 if cpu_subtype == 8: # x86_64h
503 config.available_features.add("x86_64h")
504 except:
505 pass
507 # 32-bit iOS simulator is deprecated and removed in latest Xcode.
508 if config.apple_platform == "iossim":
509 if config.target_arch == "i386":
510 config.unsupported = True
512 def get_macos_aligned_version(macos_vers):
513 platform = config.apple_platform
514 if platform == "osx":
515 return macos_vers
517 macos_major, macos_minor = macos_vers
518 assert macos_major >= 10
520 if macos_major == 10: # macOS 10.x
521 major = macos_minor
522 minor = 0
523 else: # macOS 11+
524 major = macos_major + 5
525 minor = macos_minor
527 assert major >= 11
529 if platform.startswith("ios") or platform.startswith("tvos"):
530 major -= 2
531 elif platform.startswith("watch"):
532 major -= 9
533 else:
534 lit_config.fatal("Unsupported apple platform '{}'".format(platform))
536 return (major, minor)
538 for vers in min_macos_deployment_target_substitutions:
539 flag = config.apple_platform_min_deployment_target_flag
540 major, minor = get_macos_aligned_version(vers)
541 if "mtargetos" in flag:
542 sim = "-simulator" if "sim" in config.apple_platform else ""
543 config.substitutions.append(
545 "%%min_macos_deployment_target=%s.%s" % vers,
546 "{}{}.{}{}".format(flag, major, minor, sim),
549 else:
550 config.substitutions.append(
552 "%%min_macos_deployment_target=%s.%s" % vers,
553 "{}={}.{}".format(flag, major, minor),
556 else:
557 for vers in min_macos_deployment_target_substitutions:
558 config.substitutions.append(("%%min_macos_deployment_target=%s.%s" % vers, ""))
560 if config.android:
561 env = os.environ.copy()
562 if config.android_serial:
563 env["ANDROID_SERIAL"] = config.android_serial
564 config.environment["ANDROID_SERIAL"] = config.android_serial
566 adb = os.environ.get("ADB", "adb")
568 # These are needed for tests to upload/download temp files, such as
569 # suppression-files, to device.
570 config.substitutions.append(("%device_rundir/", "/data/local/tmp/Output/"))
571 config.substitutions.append(
572 ("%push_to_device", "%s -s '%s' push " % (adb, env["ANDROID_SERIAL"]))
574 config.substitutions.append(
575 ("%adb_shell ", "%s -s '%s' shell " % (adb, env["ANDROID_SERIAL"]))
577 config.substitutions.append(
578 ("%device_rm", "%s -s '%s' shell 'rm ' " % (adb, env["ANDROID_SERIAL"]))
581 try:
582 android_api_level_str = subprocess.check_output(
583 [adb, "shell", "getprop", "ro.build.version.sdk"], env=env
584 ).rstrip()
585 android_api_codename = (
586 subprocess.check_output(
587 [adb, "shell", "getprop", "ro.build.version.codename"], env=env
589 .rstrip()
590 .decode("utf-8")
592 except (subprocess.CalledProcessError, OSError):
593 lit_config.fatal(
594 "Failed to read ro.build.version.sdk (using '%s' as adb)" % adb
596 try:
597 android_api_level = int(android_api_level_str)
598 except ValueError:
599 lit_config.fatal(
600 "Failed to read ro.build.version.sdk (using '%s' as adb): got '%s'"
601 % (adb, android_api_level_str)
603 android_api_level = min(android_api_level, int(config.android_api_level))
604 for required in [26, 28, 29, 30]:
605 if android_api_level >= required:
606 config.available_features.add("android-%s" % required)
607 # FIXME: Replace with appropriate version when availible.
608 if android_api_level > 30 or (
609 android_api_level == 30 and android_api_codename == "S"
611 config.available_features.add("android-thread-properties-api")
613 # Prepare the device.
614 android_tmpdir = "/data/local/tmp/Output"
615 subprocess.check_call([adb, "shell", "mkdir", "-p", android_tmpdir], env=env)
616 for file in config.android_files_to_push:
617 subprocess.check_call([adb, "push", file, android_tmpdir], env=env)
618 else:
619 config.substitutions.append(("%device_rundir/", ""))
620 config.substitutions.append(("%push_to_device", "echo "))
621 config.substitutions.append(("%adb_shell", "echo "))
623 if config.host_os == "Linux":
624 def add_glibc_versions(ver_string):
625 if config.android:
626 return
628 from distutils.version import LooseVersion
630 ver = LooseVersion(ver_string)
631 any_glibc = False
632 for required in ["2.19", "2.27", "2.30", "2.34", "2.37"]:
633 if ver >= LooseVersion(required):
634 config.available_features.add("glibc-" + required)
635 any_glibc = True
636 if any_glibc:
637 config.available_features.add("glibc")
639 # detect whether we are using glibc, and which version
640 cmd_args = [
641 config.clang.strip(),
642 f"--target={config.target_triple}",
643 "-xc",
644 "-",
645 "-o",
646 "-",
647 "-dM",
648 "-E",
649 ] + shlex.split(config.target_cflags)
650 cmd = subprocess.Popen(
651 cmd_args,
652 stdout=subprocess.PIPE,
653 stdin=subprocess.PIPE,
654 stderr=subprocess.DEVNULL,
655 env={"LANG": "C"},
657 try:
658 sout, _ = cmd.communicate(b"#include <features.h>")
659 m = dict(re.findall(r"#define (__GLIBC__|__GLIBC_MINOR__) (\d+)", str(sout)))
660 add_glibc_versions(f"{m['__GLIBC__']}.{m['__GLIBC_MINOR__']}")
661 except:
662 pass
664 sancovcc_path = os.path.join(config.llvm_tools_dir, "sancov")
665 if os.path.exists(sancovcc_path):
666 config.available_features.add("has_sancovcc")
667 config.substitutions.append(("%sancovcc ", sancovcc_path))
670 def liblto_path():
671 return os.path.join(config.llvm_shlib_dir, "libLTO.dylib")
674 def is_darwin_lto_supported():
675 return os.path.exists(liblto_path())
678 def is_binutils_lto_supported():
679 if not os.path.exists(os.path.join(config.llvm_shlib_dir, "LLVMgold.so")):
680 return False
682 # We require both ld.bfd and ld.gold exist and support plugins. They are in
683 # the same repository 'binutils-gdb' and usually built together.
684 for exe in (config.gnu_ld_executable, config.gold_executable):
685 try:
686 ld_cmd = subprocess.Popen(
687 [exe, "--help"], stdout=subprocess.PIPE, env={"LANG": "C"}
689 ld_out = ld_cmd.stdout.read().decode()
690 ld_cmd.wait()
691 except OSError:
692 return False
693 if not "-plugin" in ld_out:
694 return False
696 return True
699 def is_windows_lto_supported():
700 if not target_is_msvc:
701 return True
702 return os.path.exists(os.path.join(config.llvm_tools_dir, "lld-link.exe"))
705 if config.host_os == "Darwin" and is_darwin_lto_supported():
706 config.lto_supported = True
707 config.lto_flags = ["-Wl,-lto_library," + liblto_path()]
708 elif config.host_os in ["Linux", "FreeBSD", "NetBSD"]:
709 config.lto_supported = False
710 if config.use_lld:
711 config.lto_supported = True
712 if is_binutils_lto_supported():
713 config.available_features.add("binutils_lto")
714 config.lto_supported = True
716 if config.lto_supported:
717 if config.use_lld:
718 config.lto_flags = ["-fuse-ld=lld"]
719 else:
720 config.lto_flags = ["-fuse-ld=gold"]
721 elif config.host_os == "Windows" and is_windows_lto_supported():
722 config.lto_supported = True
723 config.lto_flags = ["-fuse-ld=lld"]
724 else:
725 config.lto_supported = False
727 if config.lto_supported:
728 config.available_features.add("lto")
729 if config.use_thinlto:
730 config.available_features.add("thinlto")
731 config.lto_flags += ["-flto=thin"]
732 else:
733 config.lto_flags += ["-flto"]
735 if config.have_rpc_xdr_h:
736 config.available_features.add("sunrpc")
738 # Ask llvm-config about assertion mode.
739 try:
740 llvm_config_cmd = subprocess.Popen(
741 [os.path.join(config.llvm_tools_dir, "llvm-config"), "--assertion-mode"],
742 stdout=subprocess.PIPE,
743 env=config.environment,
745 except OSError as e:
746 print("Could not launch llvm-config in " + config.llvm_tools_dir)
747 print(" Failed with error #{0}: {1}".format(e.errno, e.strerror))
748 exit(42)
750 if re.search(r"ON", llvm_config_cmd.stdout.read().decode("ascii")):
751 config.available_features.add("asserts")
752 llvm_config_cmd.wait()
754 # Sanitizer tests tend to be flaky on Windows due to PR24554, so add some
755 # retries. We don't do this on otther platforms because it's slower.
756 if platform.system() == "Windows":
757 config.test_retry_attempts = 2
759 # No throttling on non-Darwin platforms.
760 lit_config.parallelism_groups["shadow-memory"] = None
762 if platform.system() == "Darwin":
763 ios_device = config.apple_platform != "osx" and not config.apple_platform.endswith(
764 "sim"
766 # Force sequential execution when running tests on iOS devices.
767 if ios_device:
768 lit_config.warning("Forcing sequential execution for iOS device tests")
769 lit_config.parallelism_groups["ios-device"] = 1
770 config.parallelism_group = "ios-device"
772 # Only run up to 3 processes that require shadow memory simultaneously on
773 # 64-bit Darwin. Using more scales badly and hogs the system due to
774 # inefficient handling of large mmap'd regions (terabytes) by the kernel.
775 else:
776 lit_config.warning(
777 "Throttling sanitizer tests that require shadow memory on Darwin"
779 lit_config.parallelism_groups["shadow-memory"] = 3
781 # Multiple substitutions are necessary to support multiple shared objects used
782 # at once.
783 # Note that substitutions with numbers have to be defined first to avoid
784 # being subsumed by substitutions with smaller postfix.
785 for postfix in ["2", "1", ""]:
786 if config.host_os == "Darwin":
787 config.substitutions.append(
789 "%ld_flags_rpath_exe" + postfix,
790 "-Wl,-rpath,@executable_path/ %dynamiclib" + postfix,
793 config.substitutions.append(
795 "%ld_flags_rpath_so" + postfix,
796 "-install_name @rpath/`basename %dynamiclib{}`".format(postfix),
799 elif config.host_os in ("FreeBSD", "NetBSD", "OpenBSD"):
800 config.substitutions.append(
802 "%ld_flags_rpath_exe" + postfix,
803 "-Wl,-z,origin -Wl,-rpath,\$ORIGIN -L%T -l%xdynamiclib_namespec"
804 + postfix,
807 config.substitutions.append(("%ld_flags_rpath_so" + postfix, ""))
808 elif config.host_os == "Linux":
809 config.substitutions.append(
811 "%ld_flags_rpath_exe" + postfix,
812 "-Wl,-rpath,\$ORIGIN -L%T -l%xdynamiclib_namespec" + postfix,
815 config.substitutions.append(("%ld_flags_rpath_so" + postfix, ""))
816 elif config.host_os == "SunOS":
817 config.substitutions.append(
819 "%ld_flags_rpath_exe" + postfix,
820 "-Wl,-R\$ORIGIN -L%T -l%xdynamiclib_namespec" + postfix,
823 config.substitutions.append(("%ld_flags_rpath_so" + postfix, ""))
825 # Must be defined after the substitutions that use %dynamiclib.
826 config.substitutions.append(
827 ("%dynamiclib" + postfix, "%T/%xdynamiclib_filename" + postfix)
829 config.substitutions.append(
831 "%xdynamiclib_filename" + postfix,
832 "lib%xdynamiclib_namespec{}.so".format(postfix),
835 config.substitutions.append(("%xdynamiclib_namespec", "%basename_t.dynamic"))
837 config.default_sanitizer_opts = []
838 if config.host_os == "Darwin":
839 # On Darwin, we default to `abort_on_error=1`, which would make tests run
840 # much slower. Let's override this and run lit tests with 'abort_on_error=0'.
841 config.default_sanitizer_opts += ["abort_on_error=0"]
842 config.default_sanitizer_opts += ["log_to_syslog=0"]
843 if lit.util.which("log"):
844 # Querying the log can only done by a privileged user so
845 # so check if we can query the log.
846 exit_code = -1
847 with open("/dev/null", "r") as f:
848 # Run a `log show` command the should finish fairly quickly and produce very little output.
849 exit_code = subprocess.call(
850 ["log", "show", "--last", "1m", "--predicate", "1 == 0"],
851 stdout=f,
852 stderr=f,
854 if exit_code == 0:
855 config.available_features.add("darwin_log_cmd")
856 else:
857 lit_config.warning("log command found but cannot queried")
858 else:
859 lit_config.warning("log command not found. Some tests will be skipped.")
860 elif config.android:
861 config.default_sanitizer_opts += ["abort_on_error=0"]
863 # Allow tests to use REQUIRES=stable-runtime. For use when you cannot use XFAIL
864 # because the test hangs or fails on one configuration and not the other.
865 if config.android or (config.target_arch not in ["arm", "armhf", "aarch64"]):
866 config.available_features.add("stable-runtime")
868 if config.asan_shadow_scale:
869 config.available_features.add("shadow-scale-%s" % config.asan_shadow_scale)
870 else:
871 config.available_features.add("shadow-scale-3")
873 if config.memprof_shadow_scale:
874 config.available_features.add(
875 "memprof-shadow-scale-%s" % config.memprof_shadow_scale
877 else:
878 config.available_features.add("memprof-shadow-scale-3")
880 if config.expensive_checks:
881 config.available_features.add("expensive_checks")
883 # Propagate the LLD/LTO into the clang config option, so nothing else is needed.
884 run_wrapper = []
885 target_cflags = [getattr(config, "target_cflags", None)]
886 extra_cflags = []
888 if config.use_lto and config.lto_supported:
889 extra_cflags += config.lto_flags
890 elif config.use_lto and (not config.lto_supported):
891 config.unsupported = True
893 if config.use_lld and config.has_lld and not config.use_lto:
894 extra_cflags += ["-fuse-ld=lld"]
895 elif config.use_lld and (not config.has_lld):
896 config.unsupported = True
898 # Append any extra flags passed in lit_config
899 append_target_cflags = lit_config.params.get("append_target_cflags", None)
900 if append_target_cflags:
901 lit_config.note('Appending to extra_cflags: "{}"'.format(append_target_cflags))
902 extra_cflags += [append_target_cflags]
904 config.clang = (
905 " " + " ".join(run_wrapper + [config.compile_wrapper, config.clang]) + " "
907 config.target_cflags = " " + " ".join(target_cflags + extra_cflags) + " "
909 if config.host_os == "Darwin":
910 config.substitutions.append(
912 "%get_pid_from_output",
913 "{} {}/get_pid_from_output.py".format(
914 shlex.quote(config.python_executable),
915 shlex.quote(get_ios_commands_dir()),
919 config.substitutions.append(
921 "%print_crashreport_for_pid",
922 "{} {}/print_crashreport_for_pid.py".format(
923 shlex.quote(config.python_executable),
924 shlex.quote(get_ios_commands_dir()),
929 # It is not realistically possible to account for all options that could
930 # possibly be present in system and user configuration files, so disable
931 # default configs for the test runs. In particular, anything hardening
932 # related is likely to cause issues with sanitizer tests, because it may
933 # preempt something we're looking to trap (e.g. _FORTIFY_SOURCE vs our ASAN).
934 config.environment["CLANG_NO_DEFAULT_CONFIG"] = "1"
936 if config.has_compiler_rt_libatomic:
937 base_lib = os.path.join(config.compiler_rt_libdir, "libclang_rt.atomic%s.so"
938 % config.target_suffix)
939 if sys.platform in ['win32'] and execute_external:
940 # Don't pass dosish path separator to msys bash.exe.
941 base_lib = base_lib.replace('\\', '/')
942 config.substitutions.append(("%libatomic", base_lib + f" -Wl,-rpath,{config.compiler_rt_libdir}"))
943 else:
944 config.substitutions.append(("%libatomic", "-latomic"))
946 # Set LD_LIBRARY_PATH to pick dynamic runtime up properly.
947 push_dynamic_library_lookup_path(config, config.compiler_rt_libdir)
949 # GCC-ASan uses dynamic runtime by default.
950 if config.compiler_id == "GNU":
951 gcc_dir = os.path.dirname(config.clang)
952 libasan_dir = os.path.join(gcc_dir, "..", "lib" + config.bits)
953 push_dynamic_library_lookup_path(config, libasan_dir)