Debian Codegraph @

utils/bazel/llvm-project-overlay/llvm/driver.bzl

raw
1 # This file is licensed under the Apache License v2.0 with LLVM Exceptions.
2 # See https://llvm.org/LICENSE.txt for license information.
3 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5 """Configuration for the llvm-driver tool."""
6
7 load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
8 load("@bazel_skylib//rules:expand_template.bzl", "expand_template")
9
10 # Mapping from every tool to the cc_library that implements the tool's entrypoint.
11 _TOOLS = {
12 "clang-scan-deps": "//clang:clang-scan-deps-lib",
13 "clang": "//clang:clang-driver",
14 "dsymutil": "//llvm:dsymutil-lib",
15 "lld": "//lld:lld-lib",
16 "llvm-ar": "//llvm:llvm-ar-lib",
17 "llvm-cxxfilt": "//llvm:llvm-cxxfilt-lib",
18 "llvm-dwp": "//llvm:llvm-dwp-lib",
19 "llvm-gsymutil": "//llvm:llvm-gsymutil-lib",
20 "llvm-ifs": "//llvm:llvm-ifs-lib",
21 "llvm-libtool-darwin": "//llvm:llvm-libtool-darwin-lib",
22 "llvm-lipo": "//llvm:llvm-lipo-lib",
23 "llvm-ml": "//llvm:llvm-ml-lib",
24 "llvm-mt": "//llvm:llvm-mt-lib",
25 "llvm-nm": "//llvm:llvm-nm-lib",
26 "llvm-objcopy": "//llvm:llvm-objcopy-lib",
27 "llvm-objdump": "//llvm:llvm-objdump-lib",
28 "llvm-profdata": "//llvm:llvm-profdata-lib",
29 "llvm-rc": "//llvm:llvm-rc-lib",
30 "llvm-readobj": "//llvm:llvm-readobj-lib",
31 "llvm-size": "//llvm:llvm-size-lib",
32 "llvm-symbolizer": "//llvm:llvm-symbolizer-lib",
33 "sancov": "//llvm:sancov-lib",
34 }
35
36 # Tools automatically get their own name as an alias, but there may be additional
37 # aliases for a given tool.
38 _EXTRA_ALIASES = {
39 "clang": ["clang++", "clang-cl", "clang-cpp"],
40 "lld": ["ld", "lld-link", "ld.lld", "ld64.lld", "wasm-ld"],
41 "llvm-ar": ["ranlib", "lib", "dlltool"],
42 "llvm-cxxfilt": ["c++filt"],
43 "llvm-objcopy": ["bitcode-strip", "install-name-tool", "strip"],
44 "llvm-objdump": ["otool"],
45 "llvm-rc": ["windres"],
46 "llvm-readobj": ["readelf"],
47 "llvm-symbolizer": ["addr2line"],
48 }
49
50 def _validated_string_list_flag_impl(ctx):
51 invalid_values = [v for v in ctx.build_setting_value if v not in ctx.attr.values]
52 if invalid_values:
53 fail("Tool(s) [{}] are not in the known list of tools: {}".format(
54 ", ".join(invalid_values),
55 ", ".join(ctx.attr.values),
56 ))
57 return BuildSettingInfo(value = ctx.build_setting_value)
58
59 # Like string_list_flag, but with the validation that string_flag provides.
60 _validated_string_list_flag = rule(
61 implementation = _validated_string_list_flag_impl,
62 build_setting = config.string_list(flag = True),
63 attrs = {
64 "values": attr.string_list(
65 doc = "The list of allowed values for this setting. An error is raised if any other value is given.",
66 ),
67 },
68 doc = "A string list-typed build setting that can be set on the command line",
69 )
70
71 def generate_driver_selects(name):
72 """Generates flags and config settings to configure the tool list.
73
74 By default, all supported tools are included in the "llvm" driver binary.
75 To build only a subset, specify just the subset you want as the flag.
76 For example, to produce a binary with just llvm-nm and llvm-size, run:
77
78 $ bazel build \
79 --@llvm-project//llvm:driver-tools=llvm-nm,llvm-size \
80 @llvm-project//llvm:llvm
81
82 Note: this assumes the flag name is "driver-tools" by being invoked as:
83 generate_driver_selects(name = "driver-tools")
84
85 Args:
86 name: the name of the flag that configures which tools are included.
87 """
88
89 _validated_string_list_flag(
90 name = name,
91 build_setting_default = _TOOLS.keys(),
92 values = _TOOLS.keys(),
93 )
94 for tool in _TOOLS.keys():
95 native.config_setting(
96 name = "{}-include-{}".format(name, tool),
97 flag_values = {name: tool},
98 )
99
100 def select_driver_tools(flag):
101 """Produce a list of tool deps based on generate_driver_selects().
102
103 Args:
104 flag: name that was used for generate_driver_selects().
105 Returns:
106 List of tool deps based on generate_driver_selects().
107 """
108 tools = []
109 for tool, target in _TOOLS.items():
110 tools += select({
111 "{}-include-{}".format(flag, tool): [target],
112 "//conditions:default": [],
113 })
114 return tools
115
116 def _generate_driver_tools_def_impl(ctx):
117 # Depending on how the LLVM build files are included,
118 # it may or may not have the @llvm-project repo prefix.
119 # Compare just on the name. We could also include the package,
120 # but the name itself is unique in practice.
121 label_to_name = {Label(v).name: k for k, v in _TOOLS.items()}
122
123 # Reverse sort by the *main* tool name, but keep aliases together.
124 # This is consistent with how tools/llvm-driver/CMakeLists.txt does it,
125 # and this makes sure that more specific tools are checked first.
126 # For example, "clang-scan-deps" should not match "clang".
127 tools = [label_to_name[tool.label.name] for tool in ctx.attr.driver_tools]
128 tool_alias_pairs = []
129 for tool_name in reversed(tools):
130 tool_alias_pairs.append((tool_name, tool_name))
131 for extra_alias in _EXTRA_ALIASES.get(tool_name, []):
132 tool_alias_pairs.append((tool_name, extra_alias))
133
134 lines = [
135 'LLVM_DRIVER_TOOL("{alias}", {tool})'.format(
136 tool = tool_name.replace("-", "_"),
137 alias = alias.removeprefix("llvm-"),
138 )
139 for (tool_name, alias) in tool_alias_pairs
140 ]
141 lines.append("#undef LLVM_DRIVER_TOOL")
142
143 ctx.actions.write(
144 output = ctx.outputs.out,
145 content = "\n".join(lines),
146 )
147
148 generate_driver_tools_def = rule(
149 implementation = _generate_driver_tools_def_impl,
150 doc = """Generate a list of LLVM_DRIVER_TOOL macros.
151 See tools/llvm-driver/CMakeLists.txt for the reference implementation.""",
152 attrs = {
153 "driver_tools": attr.label_list(
154 doc = "List of tools to include in the generated header. Use select_driver_tools() to provide this.",
155 providers = [CcInfo],
156 ),
157 "out": attr.output(
158 doc = "Name of the generated .def output file.",
159 mandatory = True,
160 ),
161 },
162 )
163
164 def llvm_driver_cc_binary(
165 name,
166 deps = None,
167 **kwargs):
168 """cc_binary wrapper for binaries using the llvm-driver template."""
169 expand_template(
170 name = "_gen_" + name,
171 out = name + "-driver.cpp",
172 substitutions = {"@TOOL_NAME@": name.replace("-", "_")},
173 template = "//llvm:cmake/modules/llvm-driver-template.cpp.in",
174 )
175 deps = deps or []
176 native.cc_binary(
177 name = name,
178 srcs = [name + "-driver.cpp"],
179 deps = deps + ["//llvm:Support"],
180 **kwargs
181 )