forked from bazel-contrib/toolchains_llvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.bzl
307 lines (295 loc) · 15 KB
/
rules.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# Copyright 2018 The Bazel Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
load(
"//toolchain/internal:common.bzl",
_supported_os_arch_keys = "supported_os_arch_keys",
)
load(
"//toolchain/internal:configure.bzl",
_llvm_config_impl = "llvm_config_impl",
)
load(
"//toolchain/internal:repo.bzl",
_llvm_repo_impl = "llvm_repo_impl",
)
_target_pairs = ", ".join(_supported_os_arch_keys())
# Atributes common to both `llvm` and `toolchain` repository rules.
_common_attrs = {
"llvm_versions": attr.string_dict(
mandatory = False,
doc = ("LLVM version strings, keyed by host OS release name and architecture, " +
"e.g. darwin-x86_64, darwin-aarch64, ubuntu-20.04-x86_64, etc., or a " +
"less specific OS and arch pair ({}). ".format(_target_pairs) +
"An empty key is used to specify a fallback default for all hosts. " +
"If no `toolchain_roots` is given, then the toolchain will be looked up " +
"in the list of known llvm_distributions using the provided version. " +
"If unset, a default value is set from the `llvm_version` attribute."),
),
}
_llvm_repo_attrs = dict(_common_attrs)
_llvm_repo_attrs.update({
"llvm_version": attr.string(
doc = ("One of the supported versions of LLVM, e.g. 12.0.0; used with the " +
"`auto` value for the `distribution` attribute, and as a default value " +
"for the `llvm_versions` attribute."),
),
"urls": attr.string_list_dict(
mandatory = False,
doc = ("URLs to LLVM pre-built binary distribution archives, keyed by host OS " +
"release name and architecture, e.g. darwin-x86_64, darwin-aarch64, " +
"ubuntu-20.04-x86_64, etc., or a less specific OS and arch pair " +
"({}). ".format(_target_pairs) +
"May also need the `strip_prefix` attribute. " +
"Consider also setting the `sha256` attribute. An empty key is " +
"used to specify a fallback default for all hosts. This attribute " +
"overrides `distribution`, `llvm_version`, `llvm_mirror` and " +
"`alternative_llvm_sources` attributes if the host OS key is present."),
),
"sha256": attr.string_dict(
mandatory = False,
doc = "The expected SHA-256 of the file downloaded as per the `urls` attribute.",
),
"strip_prefix": attr.string_dict(
mandatory = False,
doc = "The prefix to strip from the extracted file from the `urls` attribute.",
),
"distribution": attr.string(
default = "auto",
doc = ("LLVM pre-built binary distribution filename, must be one " +
"listed on http://releases.llvm.org/download.html for the version " +
"specified in the `llvm_version` attribute. A special value of " +
"'auto' tries to detect the version based on host OS."),
),
"llvm_mirror": attr.string(
doc = "Base URL for an LLVM release mirror." +
"\n\n" +
"This mirror must follow the same structure as the official LLVM release " +
"sources (`releases.llvm.org` for versions <= 9, `llvm/llvm-project` GitHub " +
"releases for newer versions)." +
"\n\n" +
"If provided, this mirror will be given precedence over the official LLVM release " +
"sources (see: " +
"https://github.com/grailbio/bazel-toolchain/toolchain/internal/llvm_distributions.bzl).",
),
"alternative_llvm_sources": attr.string_list(
doc = "Patterns for alternative LLVM release sources. Unlike URLs specified for `llvm_mirror` " +
"these do not have to follow the same structure as the official LLVM release sources." +
"\n\n" +
"Patterns may include `{llvm_version}` (which will be substituted for the full LLVM " +
"version, i.e. 13.0.0) and `{basename}` (which will be replaced with the filename " +
"used by the official LLVM release sources for a particular distribution; i.e. " +
"`llvm-13.0.0-x86_64-linux-gnu-ubuntu-20.04.tar.xz`)." +
"\n\n" +
"As with `llvm_mirror`, these sources will take precedence over the official LLVM " +
"release sources.",
),
"netrc": attr.string(
mandatory = False,
doc = "Path to the netrc file for authenticated LLVM URL downloads.",
),
"auth_patterns": attr.string_dict(
mandatory = False,
doc = "An optional dict mapping host names to custom authorization patterns.",
),
})
_compiler_configuration_attrs = {
"sysroot": attr.string_dict(
mandatory = False,
doc = ("System path or fileset, for each target OS and arch pair you want to support " +
"({}), ".format(_target_pairs) +
"used to indicate the set of files that form the sysroot for the compiler. " +
"If the value begins with exactly one forward slash '/', then the value is " +
"assumed to be a system path. Else, the value will be assumed to be a label " +
"containing the files and the sysroot path will be taken as the path to the " +
"package of this label."),
),
"cxx_builtin_include_directories": attr.string_list_dict(
mandatory = False,
doc = ("Additional builtin include directories to be added to the default system " +
"directories, for each target OS and arch pair you want to support " +
"({}); ".format(_target_pairs) +
"see documentation for bazel's create_cc_toolchain_config_info."),
),
"stdlib": attr.string_dict(
mandatory = False,
doc = ("stdlib implementation, for each target OS and arch pair you want to support " +
"({}), ".format(_target_pairs) +
"linked to the compiled binaries. An empty key can be used to specify a " +
"value for all target pairs. Possible values are `builtin-libc++` (default) " +
"which uses the libc++ shipped with clang, `libc++` which uses libc++ available on " +
"the host or sysroot, `stdc++` which uses libstdc++ available on the host or " +
"sysroot, and `none` which uses `-nostdlib` with the compiler."),
),
"cxx_standard": attr.string_dict(
mandatory = False,
doc = ("C++ standard, for each target OS and arch pair you want to support " +
"({}), ".format(_target_pairs) +
"passed as `-std` flag to the compiler. An empty key can be used to specify a " +
"value for all target pairs. Default value is c++17."),
),
# For default values of all the below flags overrides, consult
# cc_toolchain_config.bzl in this directory.
"compile_flags": attr.string_list_dict(
mandatory = False,
doc = ("Override for compile_flags, replacing the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"cxx_flags": attr.string_list_dict(
mandatory = False,
doc = ("Override for cxx_flags, replacing the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"link_flags": attr.string_list_dict(
mandatory = False,
doc = ("Override for link_flags, replacing the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"link_libs": attr.string_list_dict(
mandatory = False,
doc = ("Override for link_libs, replacing the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"opt_compile_flags": attr.string_list_dict(
mandatory = False,
doc = ("Override for opt_compile_flags, replacing the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"opt_link_flags": attr.string_list_dict(
mandatory = False,
doc = ("Override for opt_link_flags, replacing the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"dbg_compile_flags": attr.string_list_dict(
mandatory = False,
doc = ("Override for dbg_compile_flags, replacing the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"coverage_compile_flags": attr.string_list_dict(
mandatory = False,
doc = ("Override for coverage_compile_flags, replacing the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"coverage_link_flags": attr.string_list_dict(
mandatory = False,
doc = ("Override for coverage_link_flags, replacing the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"unfiltered_compile_flags": attr.string_list_dict(
mandatory = False,
doc = ("Override for unfiltered_compile_flags, replacing the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"target_settings": attr.string_list_dict(
mandatory = False,
doc = ("Override the toolchain's `target_settings` attribute."),
),
}
_llvm_config_attrs = dict(_common_attrs)
_llvm_config_attrs.update(_compiler_configuration_attrs)
_llvm_config_attrs.update({
"toolchain_roots": attr.string_dict(
mandatory = True,
# TODO: Ideally, we should be taking a filegroup label here instead of a package path, but
# we ultimately need to subset the files to be more selective in what we include in the
# sandbox for which operations, and it is not straightforward to subset a filegroup.
doc = ("System or package path, keyed by host OS release name and architecture, e.g. " +
"darwin-x86_64, darwin-aarch64, ubuntu-20.04-x86_64, etc., or a less specific " +
"OS and arch pair ({}), to be used as the LLVM toolchain ".format(_target_pairs) +
"distributions. An empty key can be used to specify a fallback default for " +
"all hosts, e.g. with the llvm_toolchain_repo rule. " +
"If the value begins with exactly one forward slash '/', then the value is " +
"assumed to be a system path and the toolchain is configured to use absolute " +
"paths. Else, the value will be assumed to be a bazel package containing the " +
"filegroup targets as in BUILD.llvm_repo."),
),
"absolute_paths": attr.bool(
default = False,
doc = "Use absolute paths in the toolchain. Avoids sandbox overhead.",
),
"_cc_toolchain_config_bzl": attr.label(
default = "//toolchain:cc_toolchain_config.bzl",
),
"_toolchains_bzl_tpl": attr.label(
default = "//toolchain:toolchains.bzl.tpl",
),
"_build_toolchain_tpl": attr.label(
default = "//toolchain:BUILD.toolchain.tpl",
),
"_darwin_cc_wrapper_sh_tpl": attr.label(
default = "//toolchain:osx_cc_wrapper.sh.tpl",
),
"_cc_wrapper_sh_tpl": attr.label(
default = "//toolchain:cc_wrapper.sh.tpl",
),
})
llvm = repository_rule(
attrs = _llvm_repo_attrs,
local = False,
implementation = _llvm_repo_impl,
)
toolchain = repository_rule(
attrs = _llvm_config_attrs,
local = True,
configure = True,
implementation = _llvm_config_impl,
)
def llvm_toolchain(name, **kwargs):
if kwargs.get("llvm_version") == kwargs.get("llvm_versions"):
fail("Exactly one of llvm_version or llvm_versions must be set")
if not kwargs.get("toolchain_roots"):
llvm_args = {
k: v
for k, v in kwargs.items()
if (k not in _llvm_config_attrs.keys()) or (k in _common_attrs.keys())
}
llvm(name = name + "_llvm", **llvm_args)
kwargs.update(toolchain_roots = {"": "@%s_llvm//" % name})
if not kwargs.get("llvm_versions"):
kwargs.update(llvm_versions = {"": kwargs.get("llvm_version")})
toolchain_args = {
k: v
for k, v in kwargs.items()
if (k not in _llvm_repo_attrs.keys()) or (k in _common_attrs.keys())
}
toolchain(name = name, **toolchain_args)