Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lldb / test / Shell / helper / toolchain.py
blob255955fc70d8c4170c2fbc964b9e637722007763
1 import os
2 import itertools
3 import platform
4 import subprocess
5 import sys
7 import lit.util
8 from lit.llvm import llvm_config
9 from lit.llvm.subst import FindTool
10 from lit.llvm.subst import ToolSubst
13 def _get_lldb_init_path(config):
14 return os.path.join(config.test_exec_root, "lit-lldb-init-quiet")
17 def _disallow(config, execName):
18 warning = """
19 echo '*** Do not use \'{0}\' in tests; use \'%''{0}\'. ***' &&
20 exit 1 && echo
21 """
22 config.substitutions.append((" {0} ".format(execName), warning.format(execName)))
25 def use_lldb_substitutions(config):
26 # Set up substitutions for primary tools. These tools must come from config.lldb_tools_dir
27 # which is basically the build output directory. We do not want to find these in path or
28 # anywhere else, since they are specifically the programs which are actually being tested.
30 dsname = "debugserver" if platform.system() in ["Darwin"] else "lldb-server"
31 dsargs = [] if platform.system() in ["Darwin"] else ["gdbserver"]
33 build_script = os.path.dirname(__file__)
34 build_script = os.path.join(build_script, "build.py")
35 build_script_args = [
36 build_script,
37 "--compiler=any", # Default to best compiler
38 "--arch=" + str(config.lldb_bitness),
40 if config.lldb_lit_tools_dir:
41 build_script_args.append("--tools-dir={0}".format(config.lldb_lit_tools_dir))
42 if config.lldb_tools_dir:
43 build_script_args.append("--tools-dir={0}".format(config.lldb_tools_dir))
44 if config.llvm_libs_dir:
45 build_script_args.append("--libs-dir={0}".format(config.llvm_libs_dir))
46 if config.objc_gnustep_dir:
47 build_script_args.append(
48 '--objc-gnustep-dir="{0}"'.format(config.objc_gnustep_dir)
50 if config.cmake_sysroot:
51 build_script_args.append("--sysroot={0}".format(config.cmake_sysroot))
53 lldb_init = _get_lldb_init_path(config)
55 primary_tools = [
56 ToolSubst(
57 "%lldb",
58 command=FindTool("lldb"),
59 extra_args=["--no-lldbinit", "-S", lldb_init],
60 unresolved="fatal",
62 ToolSubst(
63 "%lldb-init",
64 command=FindTool("lldb"),
65 extra_args=["-S", lldb_init],
66 unresolved="fatal",
68 ToolSubst(
69 "%lldb-noinit",
70 command=FindTool("lldb"),
71 extra_args=["--no-lldbinit"],
72 unresolved="fatal",
74 ToolSubst(
75 "%lldb-server",
76 command=FindTool("lldb-server"),
77 extra_args=[],
78 unresolved="ignore",
80 ToolSubst(
81 "%debugserver",
82 command=FindTool(dsname),
83 extra_args=dsargs,
84 unresolved="ignore",
86 ToolSubst(
87 "%platformserver",
88 command=FindTool("lldb-server"),
89 extra_args=["platform"],
90 unresolved="ignore",
92 "lldb-test",
93 "lldb-dap",
94 ToolSubst(
95 "%build", command="'" + sys.executable + "'", extra_args=build_script_args
99 _disallow(config, "lldb")
100 _disallow(config, "lldb-server")
101 _disallow(config, "debugserver")
102 _disallow(config, "platformserver")
104 llvm_config.add_tool_substitutions(primary_tools, [config.lldb_tools_dir])
107 def _use_msvc_substitutions(config):
108 # If running from a Visual Studio Command prompt (e.g. vcvars), this will
109 # detect the include and lib paths, and find cl.exe and link.exe and create
110 # substitutions for each of them that explicitly specify /I and /L paths
111 cl = lit.util.which("cl")
113 if not cl:
114 return
116 # Don't use lit.util.which() for link.exe: In `git bash`, it will pick
117 # up /usr/bin/link (another name for ln).
118 link = os.path.join(os.path.dirname(cl), "link.exe")
120 cl = '"' + cl + '"'
121 link = '"' + link + '"'
122 includes = os.getenv("INCLUDE", "").split(";")
123 libs = os.getenv("LIB", "").split(";")
125 config.available_features.add("msvc")
126 compiler_flags = ['"/I{}"'.format(x) for x in includes if os.path.exists(x)]
127 linker_flags = ['"/LIBPATH:{}"'.format(x) for x in libs if os.path.exists(x)]
129 tools = [
130 ToolSubst("%msvc_cl", command=cl, extra_args=compiler_flags),
131 ToolSubst("%msvc_link", command=link, extra_args=linker_flags),
133 llvm_config.add_tool_substitutions(tools)
134 return
137 def use_support_substitutions(config):
138 # Set up substitutions for support tools. These tools can be overridden at the CMake
139 # level (by specifying -DLLDB_LIT_TOOLS_DIR), installed, or as a last resort, we can use
140 # the just-built version.
141 host_flags = ["--target=" + config.host_triple]
142 if platform.system() in ["Darwin"]:
143 try:
144 out = subprocess.check_output(["xcrun", "--show-sdk-path"]).strip()
145 res = 0
146 except OSError:
147 res = -1
148 if res == 0 and out:
149 sdk_path = lit.util.to_string(out)
150 llvm_config.lit_config.note("using SDKROOT: %r" % sdk_path)
151 host_flags += ["-isysroot", sdk_path]
152 elif sys.platform != "win32":
153 host_flags += ["-pthread"]
155 if sys.platform.startswith("netbsd"):
156 # needed e.g. to use freshly built libc++
157 host_flags += [
158 "-L" + config.llvm_libs_dir,
159 "-Wl,-rpath," + config.llvm_libs_dir,
162 # The clang module cache is used for building inferiors.
163 host_flags += ["-fmodules-cache-path={}".format(config.clang_module_cache)]
165 if config.cmake_sysroot:
166 host_flags += ["--sysroot={}".format(config.cmake_sysroot)]
168 host_flags = " ".join(host_flags)
169 config.substitutions.append(("%clang_host", "%clang " + host_flags))
170 config.substitutions.append(("%clangxx_host", "%clangxx " + host_flags))
171 config.substitutions.append(
172 ("%clang_cl_host", "%clang_cl --target=" + config.host_triple)
175 additional_tool_dirs = []
176 if config.lldb_lit_tools_dir:
177 additional_tool_dirs.append(config.lldb_lit_tools_dir)
179 llvm_config.use_clang(
180 additional_flags=["--target=specify-a-target-or-use-a-_host-substitution"],
181 additional_tool_dirs=additional_tool_dirs,
182 required=True,
183 use_installed=True,
186 if sys.platform == "win32":
187 _use_msvc_substitutions(config)
189 have_lld = llvm_config.use_lld(
190 additional_tool_dirs=additional_tool_dirs, required=False, use_installed=True
192 if have_lld:
193 config.available_features.add("lld")
195 support_tools = [
196 "yaml2obj",
197 "obj2yaml",
198 "llvm-dwp",
199 "llvm-pdbutil",
200 "llvm-mc",
201 "llvm-readobj",
202 "llvm-objdump",
203 "llvm-objcopy",
204 "lli",
206 additional_tool_dirs += [config.lldb_tools_dir, config.llvm_tools_dir]
207 llvm_config.add_tool_substitutions(support_tools, additional_tool_dirs)
209 _disallow(config, "clang")
212 def use_lldb_repro_substitutions(config, mode):
213 lldb_init = _get_lldb_init_path(config)
214 substitutions = [
215 ToolSubst(
216 "%lldb",
217 command=FindTool("lldb-repro"),
218 extra_args=[mode, "--no-lldbinit", "-S", lldb_init],
220 ToolSubst(
221 "%lldb-init",
222 command=FindTool("lldb-repro"),
223 extra_args=[mode, "-S", lldb_init],
226 llvm_config.add_tool_substitutions(substitutions, [config.lldb_tools_dir])