-
Notifications
You must be signed in to change notification settings - Fork 441
/
repositories.bzl
745 lines (640 loc) · 27.2 KB
/
repositories.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
# buildifier: disable=module-docstring
load(":known_shas.bzl", "FILE_KEY_TO_SHA")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load(
"//rust/platform:triple_mappings.bzl",
"system_to_binary_ext",
"system_to_dylib_ext",
"system_to_staticlib_ext",
"system_to_stdlib_linkflags",
"triple_to_constraint_set",
"triple_to_system",
)
DEFAULT_TOOLCHAIN_NAME_PREFIX = "toolchain_for"
# buildifier: disable=unnamed-macro
def rust_repositories(
version = "1.47.0",
iso_date = None,
rustfmt_version = "1.4.20",
edition = None,
dev_components = False,
sha256s = None):
"""Emits a default set of toolchains for Linux, OSX, and Freebsd
Skip this macro and call the `rust_repository_set` macros directly if you need a compiler for \
other hosts or for additional target triples.
The `sha256` attribute represents a dict associating tool subdirectories to sha256 hashes. As an example:
```python
{
"rust-1.46.0-x86_64-unknown-linux-gnu": "e3b98bc3440fe92817881933f9564389eccb396f5f431f33d48b979fa2fbdcf5",
"rustfmt-1.4.12-x86_64-unknown-linux-gnu": "1894e76913303d66bf40885a601462844eec15fca9e76a6d13c390d7000d64b0",
"rust-std-1.46.0-x86_64-unknown-linux-gnu": "ac04aef80423f612c0079829b504902de27a6997214eb58ab0765d02f7ec1dbc",
}
```
This would match for `exec_triple = "x86_64-unknown-linux-gnu"`. If not specified, rules_rust pulls from a non-exhaustive \
list of known checksums..
See `load_arbitrary_tool` in `@io_bazel_rules_rust//rust:repositories.bzl` for more details.
Args:
version: The version of Rust. Either "nightly", "beta", or an exact version.
rustfmt_version: The version of rustfmt. Either "nightly", "beta", or an exact version.
iso_date: The date of the nightly or beta release (or None, if the version is a specific version).
edition: The rust edition to be used by default (2015 (default) or 2018)
dev_components: Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly".
sha256s: A dict associating tool subdirectories to sha256 hashes.
"""
if dev_components and version != "nightly":
fail("Rust version must be set to \"nightly\" to enable rustc-dev components")
maybe(
http_archive,
name = "rules_cc",
url = "https://github.com/bazelbuild/rules_cc/archive/624b5d59dfb45672d4239422fa1e3de1822ee110.zip",
sha256 = "8c7e8bf24a2bf515713445199a677ee2336e1c487fa1da41037c6026de04bbc3",
strip_prefix = "rules_cc-624b5d59dfb45672d4239422fa1e3de1822ee110",
type = "zip",
)
maybe(
http_archive,
name = "bazel_skylib",
sha256 = "1c531376ac7e5a180e0237938a2536de0c54d93f5c278634818e0efc952dd56c",
urls = [
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.0.3/bazel-skylib-1.0.3.tar.gz",
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.0.3/bazel-skylib-1.0.3.tar.gz",
],
)
rust_repository_set(
name = "rust_linux_x86_64",
exec_triple = "x86_64-unknown-linux-gnu",
extra_target_triples = ["wasm32-unknown-unknown", "wasm32-wasi"],
version = version,
iso_date = iso_date,
rustfmt_version = rustfmt_version,
edition = edition,
dev_components = dev_components,
sha256s = sha256s,
)
rust_repository_set(
name = "rust_linux_aarch64",
exec_triple = "aarch64-unknown-linux-gnu",
extra_target_triples = ["wasm32-unknown-unknown", "wasm32-wasi"],
version = version,
iso_date = iso_date,
rustfmt_version = rustfmt_version,
edition = edition,
dev_components = dev_components,
sha256s = sha256s,
)
rust_repository_set(
name = "rust_darwin_x86_64",
exec_triple = "x86_64-apple-darwin",
extra_target_triples = ["wasm32-unknown-unknown", "wasm32-wasi"],
version = version,
iso_date = iso_date,
rustfmt_version = rustfmt_version,
edition = edition,
dev_components = dev_components,
sha256s = sha256s,
)
rust_repository_set(
name = "rust_darwin_aarch64",
exec_triple = "aarch64-apple-darwin",
extra_target_triples = ["wasm32-unknown-unknown", "wasm32-wasi"],
version = version,
iso_date = iso_date,
rustfmt_version = rustfmt_version,
edition = edition,
dev_components = dev_components,
sha256s = sha256s,
)
rust_repository_set(
name = "rust_freebsd_x86_64",
exec_triple = "x86_64-unknown-freebsd",
extra_target_triples = ["wasm32-unknown-unknown", "wasm32-wasi"],
version = version,
iso_date = iso_date,
rustfmt_version = rustfmt_version,
edition = edition,
dev_components = dev_components,
sha256s = sha256s,
)
rust_repository_set(
name = "rust_windows_x86_64",
exec_triple = "x86_64-pc-windows-msvc",
extra_target_triples = ["wasm32-unknown-unknown", "wasm32-wasi"],
version = version,
iso_date = iso_date,
rustfmt_version = rustfmt_version,
sha256s = sha256s,
edition = edition,
)
def _check_version_valid(version, iso_date, param_prefix = ""):
"""Verifies that the provided rust version and iso_date make sense."""
if not version and iso_date:
fail("{param_prefix}iso_date must be paired with a {param_prefix}version".format(param_prefix = param_prefix))
if version in ("beta", "nightly") and not iso_date:
fail("{param_prefix}iso_date must be specified if version is 'beta' or 'nightly'".format(param_prefix = param_prefix))
if version not in ("beta", "nightly") and iso_date:
# buildifier: disable=print
print("{param_prefix}iso_date is ineffective if an exact version is specified".format(param_prefix = param_prefix))
def serialized_constraint_set_from_triple(target_triple):
"""Returns a string representing a set of constraints
Args:
target_triple (str): The target triple of the constraint set
Returns:
str: Formatted string representing the serialized constraint
"""
constraint_set = triple_to_constraint_set(target_triple)
constraint_set_strs = []
for constraint in constraint_set:
constraint_set_strs.append("\"{}\"".format(constraint))
return "[{}]".format(", ".join(constraint_set_strs))
_build_file_for_compiler_template = """\
load("@io_bazel_rules_rust//rust:toolchain.bzl", "rust_toolchain")
filegroup(
name = "rustc",
srcs = ["bin/rustc{binary_ext}"],
visibility = ["//visibility:public"],
)
filegroup(
name = "rustc_lib",
srcs = glob(
[
"bin/*{dylib_ext}",
"lib/*{dylib_ext}",
"lib/rustlib/{target_triple}/codegen-backends/*{dylib_ext}",
"lib/rustlib/{target_triple}/bin/rust-lld{binary_ext}",
"lib/rustlib/{target_triple}/lib/*{dylib_ext}",
],
allow_empty = True,
),
visibility = ["//visibility:public"],
)
filegroup(
name = "rustdoc",
srcs = ["bin/rustdoc{binary_ext}"],
visibility = ["//visibility:public"],
)
"""
def BUILD_for_compiler(target_triple):
"""Emits a BUILD file the compiler .tar.gz."""
system = triple_to_system(target_triple)
return _build_file_for_compiler_template.format(
binary_ext = system_to_binary_ext(system),
staticlib_ext = system_to_staticlib_ext(system),
dylib_ext = system_to_dylib_ext(system),
target_triple = target_triple,
)
_build_file_for_cargo_template = """\
load("@io_bazel_rules_rust//rust:toolchain.bzl", "rust_toolchain")
filegroup(
name = "cargo",
srcs = ["bin/cargo{binary_ext}"],
visibility = ["//visibility:public"],
)"""
def BUILD_for_cargo(target_triple):
"""Emits a BUILD file the cargo .tar.gz."""
system = triple_to_system(target_triple)
return _build_file_for_cargo_template.format(
binary_ext = system_to_binary_ext(system),
)
_build_file_for_rustfmt_template = """\
load("@io_bazel_rules_rust//rust:toolchain.bzl", "rust_toolchain")
filegroup(
name = "rustfmt_bin",
srcs = ["bin/rustfmt{binary_ext}"],
visibility = ["//visibility:public"],
)
sh_binary(
name = "rustfmt",
srcs = [":rustfmt_bin"],
visibility = ["//visibility:public"],
)
"""
def BUILD_for_rustfmt(target_triple):
"""Emits a BUILD file the rustfmt .tar.gz."""
system = triple_to_system(target_triple)
return _build_file_for_rustfmt_template.format(
binary_ext = system_to_binary_ext(system),
)
_build_file_for_clippy_template = """\
load("@io_bazel_rules_rust//rust:toolchain.bzl", "rust_toolchain")
filegroup(
name = "clippy_driver_bin",
srcs = ["bin/clippy-driver{binary_ext}"],
visibility = ["//visibility:public"],
)
"""
def BUILD_for_clippy(target_triple):
"""Emits a BUILD file the clippy's extracted files."""
system = triple_to_system(target_triple)
return _build_file_for_clippy_template.format(binary_ext = system_to_binary_ext(system))
_build_file_for_stdlib_template = """\
filegroup(
name = "rust_lib-{target_triple}",
srcs = glob(
[
"lib/rustlib/{target_triple}/lib/*.rlib",
"lib/rustlib/{target_triple}/lib/*{dylib_ext}",
"lib/rustlib/{target_triple}/lib/*{staticlib_ext}",
],
# Some patterns (e.g. `lib/*.a`) don't match anything, see https://github.com/bazelbuild/rules_rust/pull/245
allow_empty = True,
),
visibility = ["//visibility:public"],
)
"""
def BUILD_for_stdlib(target_triple):
"""Emits a BUILD file the stdlib .tar.gz."""
system = triple_to_system(target_triple)
return _build_file_for_stdlib_template.format(
binary_ext = system_to_binary_ext(system),
staticlib_ext = system_to_staticlib_ext(system),
dylib_ext = system_to_dylib_ext(system),
target_triple = target_triple,
)
_build_file_for_rust_toolchain_template = """\
rust_toolchain(
name = "{toolchain_name}_impl",
rust_doc = "@{workspace_name}//:rustdoc",
rust_lib = "@{workspace_name}//:rust_lib-{target_triple}",
rustc = "@{workspace_name}//:rustc",
rustfmt = "@{workspace_name}//:rustfmt_bin",
cargo = "@{workspace_name}//:cargo",
clippy_driver = "@{workspace_name}//:clippy_driver_bin",
rustc_lib = "@{workspace_name}//:rustc_lib",
binary_ext = "{binary_ext}",
staticlib_ext = "{staticlib_ext}",
dylib_ext = "{dylib_ext}",
stdlib_linkflags = [{stdlib_linkflags}],
os = "{system}",
default_edition = "{default_edition}",
exec_triple = "{exec_triple}",
target_triple = "{target_triple}",
visibility = ["//visibility:public"],
)
"""
def BUILD_for_rust_toolchain(
workspace_name,
name,
exec_triple,
target_triple,
stdlib_linkflags = None,
default_edition = "2015"):
"""Emits a toolchain declaration to match an existing compiler and stdlib.
Args:
workspace_name (str): The name of the workspace that this toolchain resides in
name (str): The name of the toolchain declaration
exec_triple (str): The rust-style target that this compiler runs on
target_triple (str): The rust-style target triple of the tool
stdlib_linkflags (list, optional): Overriden flags needed for linking to rust
stdlib, akin to BAZEL_LINKLIBS. Defaults to
None.
default_edition (str, optional): Default Rust edition. Defaults to "2015".
Returns:
str: A rendered template of a `rust_toolchain` declaration
"""
system = triple_to_system(target_triple)
if stdlib_linkflags == None:
stdlib_linkflags = ", ".join(['"%s"' % x for x in system_to_stdlib_linkflags(system)])
return _build_file_for_rust_toolchain_template.format(
toolchain_name = name,
workspace_name = workspace_name,
binary_ext = system_to_binary_ext(system),
staticlib_ext = system_to_staticlib_ext(system),
dylib_ext = system_to_dylib_ext(system),
stdlib_linkflags = stdlib_linkflags,
system = system,
default_edition = default_edition,
exec_triple = exec_triple,
target_triple = target_triple,
)
_build_file_for_toolchain_template = """\
toolchain(
name = "{name}",
exec_compatible_with = {exec_constraint_sets_serialized},
target_compatible_with = {target_constraint_sets_serialized},
toolchain = "@{parent_workspace_name}//:{name}_impl",
toolchain_type = "@io_bazel_rules_rust//rust:toolchain",
)
"""
def BUILD_for_toolchain(name, parent_workspace_name, exec_triple, target_triple):
return _build_file_for_toolchain_template.format(
name = name,
exec_constraint_sets_serialized = serialized_constraint_set_from_triple(exec_triple),
target_constraint_sets_serialized = serialized_constraint_set_from_triple(target_triple),
parent_workspace_name = parent_workspace_name,
)
def produce_tool_suburl(tool_name, target_triple, version, iso_date = None):
"""Produces a fully qualified Rust tool name for URL
Args:
tool_name: The name of the tool per static.rust-lang.org
target_triple: The rust-style target triple of the tool
version: The version of the tool among "nightly", "beta', or an exact version.
iso_date: The date of the tool (or None, if the version is a specific version).
"""
if iso_date:
return "{}/{}-{}-{}".format(iso_date, tool_name, version, target_triple)
else:
return "{}-{}-{}".format(tool_name, version, target_triple)
def produce_tool_path(tool_name, target_triple, version):
"""Produces a qualified Rust tool name
Args:
tool_name: The name of the tool per static.rust-lang.org
target_triple: The rust-style target triple of the tool
version: The version of the tool among "nightly", "beta', or an exact version.
"""
return "{}-{}-{}".format(tool_name, version, target_triple)
def load_arbitrary_tool(ctx, tool_name, tool_subdirectories, version, iso_date, target_triple, sha256 = ""):
"""Loads a Rust tool, downloads, and extracts into the common workspace.
This function sources the tool from the Rust-lang static file server. The index is available
at: https://static.rust-lang.org/dist/index.html (or the path specified by
"${RUST_STATIC_URL}/dist/index.html" if the RUST_STATIC_URL envinronment variable is set).
Args:
ctx (ctx): A repository_ctx (no attrs required).
tool_name (str): The name of the given tool per the archive naming.
tool_subdirectories (str): The subdirectories of the tool files (at a level below the root directory of
the archive). The root directory of the archive is expected to match
$TOOL_NAME-$VERSION-$TARGET_TRIPLE.
Example:
tool_name
| version
| | target_triple
v v v
rust-1.39.0-x86_64-unknown-linux-gnu/clippy-preview
.../rustc
.../etc
tool_subdirectories = ["clippy-preview", "rustc"]
version (str): The version of the tool among "nightly", "beta', or an exact version.
iso_date (bool): The date of the tool (or None, if the version is a specific version).
target_triple (str): The rust-style target triple of the tool
sha256 (str, optional): The expected hash of hash of the Rust tool. Defaults to "".
"""
_check_version_valid(version, iso_date, param_prefix = tool_name + "_")
# N.B. See https://static.rust-lang.org/dist/index.html to find the tool_suburl for a given
# tool.
tool_suburl = produce_tool_suburl(tool_name, target_triple, version, iso_date)
static_rust = ctx.os.environ["STATIC_RUST_URL"] if "STATIC_RUST_URL" in ctx.os.environ else "https://static.rust-lang.org"
url = "{}/dist/{}.tar.gz".format(static_rust, tool_suburl)
tool_path = produce_tool_path(tool_name, target_triple, version)
archive_path = tool_path + ".tar.gz"
ctx.download(
url,
output = archive_path,
sha256 = ctx.attr.sha256s.get(tool_suburl) or FILE_KEY_TO_SHA.get(tool_suburl) or sha256,
)
for subdirectory in tool_subdirectories:
ctx.extract(
archive_path,
output = "",
stripPrefix = "{}/{}".format(tool_path, subdirectory),
)
def _load_rustfmt(ctx):
target_triple = ctx.attr.exec_triple
if ctx.attr.rustfmt_version in ("beta", "nightly"):
iso_date = ctx.attr.iso_date
else:
iso_date = None
load_arbitrary_tool(
ctx,
iso_date = iso_date,
target_triple = target_triple,
tool_name = "rustfmt",
tool_subdirectories = ["rustfmt-preview"],
version = ctx.attr.rustfmt_version,
)
return BUILD_for_rustfmt(target_triple)
def _load_rust_compiler(ctx):
"""Loads a rust compiler and yields corresponding BUILD for it
Args:
ctx: A repository_ctx.
Returns:
The BUILD file contents for this compiler and compiler library
"""
target_triple = ctx.attr.exec_triple
load_arbitrary_tool(
ctx,
iso_date = ctx.attr.iso_date,
target_triple = target_triple,
tool_name = "rust",
tool_subdirectories = ["rustc", "clippy-preview", "cargo"],
version = ctx.attr.version,
)
compiler_build_file = BUILD_for_compiler(target_triple) + BUILD_for_clippy(target_triple) + BUILD_for_cargo(target_triple)
return compiler_build_file
def _load_rust_stdlib(ctx, target_triple):
"""Loads a rust standard library and yields corresponding BUILD for it
Args:
ctx: A repository_ctx.
target_triple: The rust-style target triple of the tool
Returns:
The BUILD file contents for this stdlib, and a toolchain decl to match
"""
load_arbitrary_tool(
ctx,
iso_date = ctx.attr.iso_date,
target_triple = target_triple,
tool_name = "rust-std",
tool_subdirectories = ["rust-std-{}".format(target_triple)],
version = ctx.attr.version,
)
toolchain_prefix = ctx.attr.toolchain_name_prefix or DEFAULT_TOOLCHAIN_NAME_PREFIX
stdlib_build_file = BUILD_for_stdlib(target_triple)
stdlib_linkflags = None
if "BAZEL_RUST_STDLIB_LINKFLAGS" in ctx.os.environ:
stdlib_linkflags = ctx.os.environ["BAZEL_RUST_STDLIB_LINKFLAGS"].split(":")
toolchain_build_file = BUILD_for_rust_toolchain(
name = "{toolchain_prefix}_{target_triple}".format(
toolchain_prefix = toolchain_prefix,
target_triple = target_triple,
),
exec_triple = ctx.attr.exec_triple,
target_triple = target_triple,
stdlib_linkflags = stdlib_linkflags,
workspace_name = ctx.attr.name,
default_edition = ctx.attr.edition,
)
return stdlib_build_file + toolchain_build_file
def _load_rustc_dev_nightly(ctx, target_triple):
"""Loads the nightly rustc dev component
Args:
ctx: A repository_ctx.
target_triple: The rust-style target triple of the tool
"""
load_arbitrary_tool(
ctx,
iso_date = ctx.attr.iso_date,
target_triple = target_triple,
tool_name = "rustc-dev",
tool_subdirectories = ["rustc-dev-{}".format(target_triple)],
version = ctx.attr.version,
)
return
def _load_llvm_tools(ctx, target_triple):
"""Loads the llvm tools
Args:
ctx: A repository_ctx.
target_triple: The rust-style target triple of the tool
"""
load_arbitrary_tool(
ctx,
iso_date = ctx.attr.iso_date,
target_triple = target_triple,
tool_name = "llvm-tools",
tool_subdirectories = ["llvm-tools-preview"],
version = ctx.attr.version,
)
return
def _rust_toolchain_repository_impl(ctx):
"""The implementation of the rust toolchain repository rule."""
_check_version_valid(ctx.attr.version, ctx.attr.iso_date)
build_components = [_load_rust_compiler(ctx)]
if ctx.attr.rustfmt_version:
build_components.append(_load_rustfmt(ctx))
# Rust 1.45.0 and nightly builds after 2020-05-22 need the llvm-tools gzip to get the libLLVM dylib
if ctx.attr.version >= "1.45.0" or (ctx.attr.version == "nightly" and ctx.attr.iso_date > "2020-05-22"):
_load_llvm_tools(ctx, ctx.attr.exec_triple)
for target_triple in [ctx.attr.exec_triple] + ctx.attr.extra_target_triples:
build_components.append(_load_rust_stdlib(ctx, target_triple))
# extra_target_triples contains targets such as wasm, which don't have rustc_dev components
if ctx.attr.dev_components and target_triple not in ctx.attr.extra_target_triples:
_load_rustc_dev_nightly(ctx, target_triple)
ctx.file("WORKSPACE", "")
ctx.file("BUILD", "\n".join(build_components))
def _rust_toolchain_repository_proxy_impl(ctx):
build_components = []
for target_triple in [ctx.attr.exec_triple] + ctx.attr.extra_target_triples:
build_components.append(BUILD_for_toolchain(
name = "{toolchain_prefix}_{target_triple}".format(
toolchain_prefix = ctx.attr.toolchain_name_prefix,
target_triple = target_triple,
),
exec_triple = ctx.attr.exec_triple,
parent_workspace_name = ctx.attr.parent_workspace_name,
target_triple = target_triple,
))
ctx.file("WORKSPACE", "")
ctx.file("BUILD", "\n".join(build_components))
rust_toolchain_repository = repository_rule(
doc = (
"Composes a single workspace containing the toolchain components for compiling on a given " +
"platform to a series of target platforms.\n" +
"\n" +
"A given instance of this rule should be accompanied by a rust_toolchain_repository_proxy " +
"invocation to declare its toolchains to Bazel; the indirection allows separating toolchain " +
"selection from toolchain fetching."
),
attrs = {
"version": attr.string(
doc = "The version of the tool among \"nightly\", \"beta\", or an exact version.",
mandatory = True,
),
"rustfmt_version": attr.string(
doc = "The version of the tool among \"nightly\", \"beta\", or an exact version.",
),
"iso_date": attr.string(
doc = "The date of the tool (or None, if the version is a specific version).",
),
"exec_triple": attr.string(
doc = "The Rust-style target that this compiler runs on",
mandatory = True,
),
"extra_target_triples": attr.string_list(
doc = "Additional rust-style targets that this set of toolchains should support.",
),
"toolchain_name_prefix": attr.string(
doc = "The per-target prefix expected for the rust_toolchain declarations in the parent workspace.",
),
"edition": attr.string(
doc = "The rust edition to be used by default.",
default = "2015",
),
"dev_components": attr.bool(
doc = "Whether to download the rustc-dev components (defaults to False). Requires version to be \"nightly\".",
default = False,
),
"sha256s": attr.string_dict(
doc = "A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details.",
),
},
implementation = _rust_toolchain_repository_impl,
)
rust_toolchain_repository_proxy = repository_rule(
doc = (
"Generates a toolchain-bearing repository that declares the toolchains from some other " +
"rust_toolchain_repository."
),
attrs = {
"parent_workspace_name": attr.string(
doc = "The name of the other rust_toolchain_repository",
mandatory = True,
),
"exec_triple": attr.string(
doc = "The Rust-style target triple for the compilation platform",
mandatory = True,
),
"extra_target_triples": attr.string_list(
doc = "The Rust-style triples for extra compilation targets",
),
"toolchain_name_prefix": attr.string(
doc = "The per-target prefix expected for the rust_toolchain declarations in the parent workspace.",
),
},
implementation = _rust_toolchain_repository_proxy_impl,
)
def rust_repository_set(
name,
version,
exec_triple,
extra_target_triples = [],
iso_date = None,
rustfmt_version = None,
edition = None,
dev_components = False,
sha256s = None):
"""Assembles a remote repository for the given toolchain params, produces a proxy repository \
to contain the toolchain declaration, and registers the toolchains.
N.B. A "proxy repository" is needed to allow for registering the toolchain (with constraints) \
without actually downloading the toolchain.
Args:
name (str): The name of the generated repository
version (str): The version of the tool among "nightly", "beta', or an exact version.
exec_triple (str): The Rust-style target that this compiler runs on
extra_target_triples (list, optional): Additional rust-style targets that this set of
toolchains should support. Defaults to [].
iso_date (str, optional): The date of the tool. Defaults to None.
rustfmt_version (str, optional): The version of rustfmt to be associated with the
toolchain. Defaults to None.
edition (str, optional): The rust edition to be used by default (2015 (if None) or 2018).
dev_components (bool, optional): Whether to download the rustc-dev components.
Requires version to be "nightly". Defaults to False.
sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See
[rust_repositories](#rust_repositories) for more details.
"""
rust_toolchain_repository(
name = name,
exec_triple = exec_triple,
extra_target_triples = extra_target_triples,
iso_date = iso_date,
toolchain_name_prefix = DEFAULT_TOOLCHAIN_NAME_PREFIX,
version = version,
rustfmt_version = rustfmt_version,
edition = edition,
dev_components = dev_components,
sha256s = sha256s,
)
rust_toolchain_repository_proxy(
name = name + "_toolchains",
exec_triple = exec_triple,
extra_target_triples = extra_target_triples,
parent_workspace_name = name,
toolchain_name_prefix = DEFAULT_TOOLCHAIN_NAME_PREFIX,
)
all_toolchain_names = []
for target_triple in [exec_triple] + extra_target_triples:
all_toolchain_names.append("@{name}_toolchains//:{toolchain_name_prefix}_{triple}".format(
name = name,
toolchain_name_prefix = DEFAULT_TOOLCHAIN_NAME_PREFIX,
triple = target_triple,
))
# Register toolchains
native.register_toolchains(*all_toolchain_names)
native.register_toolchains("@io_bazel_rules_rust//rust/private/dummy_cc_toolchain:dummy_cc_wasm32_toolchain")