[GlobalISel] Avoid repeated hash lookups (NFC) (#124393)
[llvm-project.git] / compiler-rt / test / builtins / Unit / lit.cfg.py
blobc030f89c66e42135473fdb8cf0cd7a393b791a62
1 # -*- Python -*-
3 import os
4 import platform
5 import shlex
6 import subprocess
8 import lit.formats
10 # Choose between lit's internal shell pipeline runner and a real shell. If
11 # LIT_USE_INTERNAL_SHELL is in the environment, we use that as an override.
12 use_lit_shell = os.environ.get("LIT_USE_INTERNAL_SHELL")
13 if use_lit_shell:
14 # 0 is external, "" is default, and everything else is internal.
15 execute_external = use_lit_shell == "0"
16 else:
17 # Otherwise we default to internal on Windows and external elsewhere, as
18 # bash on Windows is usually very slow.
19 execute_external = not sys.platform in ["win32"]
22 def get_required_attr(config, attr_name):
23 attr_value = getattr(config, attr_name, None)
24 if attr_value is None:
25 lit_config.fatal(
26 "No attribute %r in test configuration! You may need to run "
27 "tests from your build directory or add this attribute "
28 "to lit.site.cfg.py " % attr_name
30 return attr_value
33 def get_library_path(file):
34 cmd = subprocess.Popen(
35 [config.clang.strip(), "-print-file-name=%s" % file]
36 + shlex.split(config.target_cflags),
37 stdout=subprocess.PIPE,
38 env=config.environment,
39 universal_newlines=True,
41 if not cmd.stdout:
42 lit_config.fatal("Couldn't find the library path for '%s'" % file)
43 dir = cmd.stdout.read().strip()
44 if sys.platform in ["win32"] and execute_external:
45 # Don't pass dosish path separator to msys bash.exe.
46 dir = dir.replace("\\", "/")
47 return dir
50 def get_libgcc_file_name():
51 cmd = subprocess.Popen(
52 [config.clang.strip(), "-print-libgcc-file-name"]
53 + shlex.split(config.target_cflags),
54 stdout=subprocess.PIPE,
55 env=config.environment,
56 universal_newlines=True,
58 if not cmd.stdout:
59 lit_config.fatal("Couldn't find the library path for '%s'" % file)
60 dir = cmd.stdout.read().strip()
61 if sys.platform in ["win32"] and execute_external:
62 # Don't pass dosish path separator to msys bash.exe.
63 dir = dir.replace("\\", "/")
64 return dir
67 # Setup config name.
68 config.name = "Builtins" + config.name_suffix
70 # Platform-specific default Builtins_OPTIONS for lit tests.
71 default_builtins_opts = ""
73 # Setup source root.
74 config.test_source_root = os.path.dirname(__file__)
76 # Path to the static library
77 is_msvc = get_required_attr(config, "is_msvc")
78 if is_msvc:
79 base_lib = os.path.join(
80 config.compiler_rt_libdir, "clang_rt.builtins%s.lib " % config.target_suffix
82 config.substitutions.append(("%librt ", base_lib))
83 elif config.host_os == "Darwin":
84 base_lib = os.path.join(config.compiler_rt_libdir, "libclang_rt.osx.a ")
85 config.substitutions.append(("%librt ", base_lib + " -lSystem "))
86 elif config.host_os == "Windows":
87 base_lib = os.path.join(
88 config.compiler_rt_libdir, "libclang_rt.builtins%s.a" % config.target_suffix
90 if sys.platform in ["win32"] and execute_external:
91 # Don't pass dosish path separator to msys bash.exe.
92 base_lib = base_lib.replace("\\", "/")
93 config.substitutions.append(
95 "%librt ",
96 base_lib
97 + " -lmingw32 -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 ",
100 else:
101 base_lib = os.path.join(
102 config.compiler_rt_libdir, "libclang_rt.builtins%s.a" % config.target_suffix
104 if sys.platform in ["win32"] and execute_external:
105 # Don't pass dosish path separator to msys bash.exe.
106 base_lib = base_lib.replace("\\", "/")
107 if config.host_os == "Haiku":
108 config.substitutions.append(("%librt ", base_lib + " -lroot "))
109 else:
110 config.substitutions.append(("%librt ", base_lib + " -lc -lm "))
112 builtins_build_crt = get_required_attr(config, "builtins_build_crt")
113 if builtins_build_crt:
114 base_obj = os.path.join(
115 config.compiler_rt_libdir, "clang_rt.%%s%s.o" % config.target_suffix
117 if sys.platform in ["win32"] and execute_external:
118 # Don't pass dosish path separator to msys bash.exe.
119 base_obj = base_obj.replace("\\", "/")
121 config.substitutions.append(("%crtbegin", base_obj % "crtbegin"))
122 config.substitutions.append(("%crtend", base_obj % "crtend"))
124 config.substitutions.append(("%crt1", get_library_path("crt1.o")))
125 config.substitutions.append(("%crti", get_library_path("crti.o")))
126 config.substitutions.append(("%crtn", get_library_path("crtn.o")))
128 config.substitutions.append(("%libgcc", get_libgcc_file_name()))
129 config.substitutions.append(
130 ("%libc", "-lroot" if sys.platform.startswith("haiku") else "-lc")
133 config.substitutions.append(
134 ("%libstdcxx", "-l" + config.sanitizer_cxx_lib.lstrip("lib"))
137 config.available_features.add("crt")
139 builtins_source_dir = os.path.join(
140 get_required_attr(config, "compiler_rt_src_root"), "lib", "builtins"
142 if sys.platform in ["win32"] and execute_external:
143 # Don't pass dosish path separator to msys bash.exe.
144 builtins_source_dir = builtins_source_dir.replace("\\", "/")
145 builtins_lit_source_dir = get_required_attr(config, "builtins_lit_source_dir")
147 extra_link_flags = ["-nodefaultlibs"]
149 target_cflags = [get_required_attr(config, "target_cflags")]
150 target_cflags += ["-fno-builtin", "-I", builtins_source_dir]
151 target_cflags += extra_link_flags
152 target_cxxflags = config.cxx_mode_flags + target_cflags
153 clang_builtins_static_cflags = [""] + config.debug_info_flags + target_cflags
154 clang_builtins_static_cxxflags = config.cxx_mode_flags + clang_builtins_static_cflags
156 clang_builtins_cflags = clang_builtins_static_cflags
157 clang_builtins_cxxflags = clang_builtins_static_cxxflags
159 # FIXME: Right now we don't compile the C99 complex builtins when using
160 # clang-cl. Fix that.
161 if not is_msvc:
162 config.available_features.add("c99-complex")
164 builtins_is_msvc = get_required_attr(config, "builtins_is_msvc")
165 if not builtins_is_msvc:
166 config.available_features.add("int128")
168 clang_wrapper = ""
171 def build_invocation(compile_flags):
172 return " " + " ".join([clang_wrapper, config.clang] + compile_flags) + " "
175 config.substitutions.append(("%clang ", build_invocation(target_cflags)))
176 config.substitutions.append(("%clangxx ", build_invocation(target_cxxflags)))
177 config.substitutions.append(
178 ("%clang_builtins ", build_invocation(clang_builtins_cflags))
180 config.substitutions.append(
181 ("%clangxx_builtins ", build_invocation(clang_builtins_cxxflags))
184 # Default test suffixes.
185 config.suffixes = [".c", ".cpp"]
187 if not config.emulator:
188 config.available_features.add("native-run")
190 # Add features for available sources
191 builtins_source_features = config.builtins_lit_source_features.split(";")
192 # Sanity checks
193 if not builtins_source_features:
194 lit_config.fatal("builtins_source_features cannot be empty")
195 builtins_source_features_set = set()
196 builtins_source_feature_duplicates = []
197 for builtin_source_feature in builtins_source_features:
198 if len(builtin_source_feature) == 0:
199 lit_config.fatal("builtins_source_feature cannot contain empty features")
200 if builtin_source_feature not in builtins_source_features_set:
201 builtins_source_features_set.add(builtin_source_feature)
202 else:
203 builtins_source_feature_duplicates.append(builtin_source_feature)
205 if len(builtins_source_feature_duplicates) > 0:
206 lit_config.fatal(
207 "builtins_source_features contains duplicates: {}".format(
208 builtins_source_feature_duplicates
211 config.available_features.update(builtins_source_features)