fork/rules_rust

git clone https://git.julia.blue/fork/rules_rust

UebelAndreAdded support for compact windows repository names, avoiding MAX_PATH (#3023)8e962a16

main
34.2 KiB991 linesraw
1"""Utility macros for use in rules_rust repository rules"""
2
3load(
4    "@bazel_tools//tools/build_defs/repo:utils.bzl",
5    "read_netrc",
6    "read_user_netrc",
7    "use_netrc",
8)
9load("//rust:known_shas.bzl", "FILE_KEY_TO_SHA")
10load(
11    "//rust/platform:triple_mappings.bzl",
12    "system_to_binary_ext",
13    "system_to_dylib_ext",
14    "system_to_staticlib_ext",
15    "system_to_stdlib_linkflags",
16)
17load("//rust/private:common.bzl", "DEFAULT_NIGHTLY_ISO_DATE")
18
19DEFAULT_TOOLCHAIN_NAME_PREFIX = "toolchain_for"
20DEFAULT_STATIC_RUST_URL_TEMPLATES = ["https://static.rust-lang.org/dist/{}.tar.xz"]
21DEFAULT_NIGHTLY_VERSION = "nightly/{}".format(DEFAULT_NIGHTLY_ISO_DATE)
22DEFAULT_EXTRA_TARGET_TRIPLES = ["wasm32-unknown-unknown", "wasm32-wasip1"]
23
24TINYJSON_KWARGS = dict(
25    name = "rules_rust_tinyjson",
26    sha256 = "9ab95735ea2c8fd51154d01e39cf13912a78071c2d89abc49a7ef102a7dd725a",
27    url = "https://static.crates.io/crates/tinyjson/tinyjson-2.5.1.crate",
28    strip_prefix = "tinyjson-2.5.1",
29    type = "tar.gz",
30    build_file = "@rules_rust//util/process_wrapper:BUILD.tinyjson.bazel",
31)
32
33_build_file_for_compiler_template = """\
34filegroup(
35    name = "rustc",
36    srcs = ["bin/rustc{binary_ext}"],
37    visibility = ["//visibility:public"],
38)
39
40filegroup(
41    name = "rustc_lib",
42    srcs = glob(
43        [
44            "bin/*{dylib_ext}",
45            "lib/*{dylib_ext}*",
46            "lib/rustlib/{target_triple}/codegen-backends/*{dylib_ext}",
47            "lib/rustlib/{target_triple}/bin/rust-lld{binary_ext}",
48            "lib/rustlib/{target_triple}/lib/*{dylib_ext}*",
49        ],
50        allow_empty = True,
51    ),
52    visibility = ["//visibility:public"],
53)
54
55filegroup(
56    name = "rustdoc",
57    srcs = ["bin/rustdoc{binary_ext}"],
58    visibility = ["//visibility:public"],
59)
60"""
61
62def BUILD_for_compiler(target_triple):
63    """Emits a BUILD file the compiler archive.
64
65    Args:
66        target_triple (str): The triple of the target platform
67
68    Returns:
69        str: The contents of a BUILD file
70    """
71    return _build_file_for_compiler_template.format(
72        binary_ext = system_to_binary_ext(target_triple.system),
73        staticlib_ext = system_to_staticlib_ext(target_triple.system),
74        dylib_ext = system_to_dylib_ext(target_triple.system),
75        target_triple = target_triple.str,
76    )
77
78_build_file_for_cargo_template = """\
79filegroup(
80    name = "cargo",
81    srcs = ["bin/cargo{binary_ext}"],
82    visibility = ["//visibility:public"],
83)"""
84
85def BUILD_for_cargo(target_triple):
86    """Emits a BUILD file the cargo archive.
87
88    Args:
89        target_triple (str): The triple of the target platform
90
91    Returns:
92        str: The contents of a BUILD file
93    """
94    return _build_file_for_cargo_template.format(
95        binary_ext = system_to_binary_ext(target_triple.system),
96    )
97
98_build_file_for_rustfmt_template = """\
99filegroup(
100    name = "rustfmt_bin",
101    srcs = ["bin/rustfmt{binary_ext}"],
102    visibility = ["//visibility:public"],
103)
104
105sh_binary(
106    name = "rustfmt",
107    srcs = [":rustfmt_bin"],
108    visibility = ["//visibility:public"],
109)
110"""
111
112def BUILD_for_rustfmt(target_triple):
113    """Emits a BUILD file the rustfmt archive.
114
115    Args:
116        target_triple (str): The triple of the target platform
117
118    Returns:
119        str: The contents of a BUILD file
120    """
121    return _build_file_for_rustfmt_template.format(
122        binary_ext = system_to_binary_ext(target_triple.system),
123    )
124
125_build_file_for_rust_analyzer_proc_macro_srv = """\
126filegroup(
127   name = "rust_analyzer_proc_macro_srv",
128   srcs = ["libexec/rust-analyzer-proc-macro-srv{binary_ext}"],
129   visibility = ["//visibility:public"],
130)
131"""
132
133def BUILD_for_rust_analyzer_proc_macro_srv(exec_triple):
134    """Emits a BUILD file the rust_analyzer_proc_macro_srv archive.
135
136    Args:
137        exec_triple (str): The triple of the exec platform
138    Returns:
139        str: The contents of a BUILD file
140    """
141    return _build_file_for_rust_analyzer_proc_macro_srv.format(
142        binary_ext = system_to_binary_ext(exec_triple.system),
143    )
144
145_build_file_for_clippy_template = """\
146filegroup(
147    name = "clippy_driver_bin",
148    srcs = ["bin/clippy-driver{binary_ext}"],
149    visibility = ["//visibility:public"],
150)
151filegroup(
152    name = "cargo_clippy_bin",
153    srcs = ["bin/cargo-clippy{binary_ext}"],
154    visibility = ["//visibility:public"],
155)
156"""
157
158def BUILD_for_clippy(target_triple):
159    """Emits a BUILD file the clippy archive.
160
161    Args:
162        target_triple (str): The triple of the target platform
163
164    Returns:
165        str: The contents of a BUILD file
166    """
167    return _build_file_for_clippy_template.format(
168        binary_ext = system_to_binary_ext(target_triple.system),
169    )
170
171_build_file_for_llvm_tools = """\
172filegroup(
173    name = "llvm_cov_bin",
174    srcs = ["lib/rustlib/{target_triple}/bin/llvm-cov{binary_ext}"],
175    visibility = ["//visibility:public"],
176)
177
178filegroup(
179    name = "llvm_profdata_bin",
180    srcs = ["lib/rustlib/{target_triple}/bin/llvm-profdata{binary_ext}"],
181    visibility = ["//visibility:public"],
182)
183"""
184
185def BUILD_for_llvm_tools(target_triple):
186    """Emits a BUILD file the llvm-tools binaries.
187
188    Args:
189        target_triple (struct): The triple of the target platform
190
191    Returns:
192        str: The contents of a BUILD file
193    """
194    return _build_file_for_llvm_tools.format(
195        binary_ext = system_to_binary_ext(target_triple.system),
196        target_triple = target_triple.str,
197    )
198
199_build_file_for_stdlib_template = """\
200load("@rules_rust//rust:toolchain.bzl", "rust_stdlib_filegroup")
201
202rust_stdlib_filegroup(
203    name = "rust_std-{target_triple}",
204    srcs = glob(
205        [
206            "lib/rustlib/{target_triple}/lib/*.rlib",
207            "lib/rustlib/{target_triple}/lib/*{dylib_ext}*",
208            "lib/rustlib/{target_triple}/lib/*{staticlib_ext}",
209            "lib/rustlib/{target_triple}/lib/self-contained/**",
210        ],
211        # Some patterns (e.g. `lib/*.a`) don't match anything, see https://github.com/bazelbuild/rules_rust/pull/245
212        allow_empty = True,
213    ),
214    visibility = ["//visibility:public"],
215)
216
217# For legacy support
218alias(
219    name = "rust_lib-{target_triple}",
220    actual = "rust_std-{target_triple}",
221    visibility = ["//visibility:public"],
222)
223"""
224
225def BUILD_for_stdlib(target_triple):
226    """Emits a BUILD file the stdlib archive.
227
228    Args:
229        target_triple (triple): The triple of the target platform
230
231    Returns:
232        str: The contents of a BUILD file
233    """
234    return _build_file_for_stdlib_template.format(
235        binary_ext = system_to_binary_ext(target_triple.system),
236        staticlib_ext = system_to_staticlib_ext(target_triple.system),
237        dylib_ext = system_to_dylib_ext(target_triple.system),
238        target_triple = target_triple.str,
239    )
240
241_build_file_for_rust_toolchain_template = """\
242load("@rules_rust//rust:toolchain.bzl", "rust_toolchain")
243
244rust_toolchain(
245    name = "{toolchain_name}",
246    rust_doc = "//:rustdoc",
247    rust_std = "//:rust_std-{target_triple}",
248    rustc = "//:rustc",
249    rustfmt = {rustfmt_label},
250    cargo = "//:cargo",
251    clippy_driver = "//:clippy_driver_bin",
252    cargo_clippy = "//:cargo_clippy_bin",
253    llvm_cov = {llvm_cov_label},
254    llvm_profdata = {llvm_profdata_label},
255    rustc_lib = "//:rustc_lib",
256    allocator_library = {allocator_library},
257    global_allocator_library = {global_allocator_library},
258    binary_ext = "{binary_ext}",
259    staticlib_ext = "{staticlib_ext}",
260    dylib_ext = "{dylib_ext}",
261    stdlib_linkflags = [{stdlib_linkflags}],
262    default_edition = "{default_edition}",
263    exec_triple = "{exec_triple}",
264    target_triple = "{target_triple}",
265    visibility = ["//visibility:public"],
266    extra_rustc_flags = {extra_rustc_flags},
267    extra_exec_rustc_flags = {extra_exec_rustc_flags},
268    opt_level = {opt_level},
269    tags = ["rust_version={version}"],
270)
271"""
272
273def BUILD_for_rust_toolchain(
274        name,
275        exec_triple,
276        target_triple,
277        version,
278        allocator_library,
279        global_allocator_library,
280        default_edition,
281        include_rustfmt,
282        include_llvm_tools,
283        stdlib_linkflags = None,
284        extra_rustc_flags = None,
285        extra_exec_rustc_flags = None,
286        opt_level = None):
287    """Emits a toolchain declaration to match an existing compiler and stdlib.
288
289    Args:
290        name (str): The name of the toolchain declaration
291        exec_triple (triple): The rust-style target that this compiler runs on
292        target_triple (triple): The rust-style target triple of the tool
293        version (str): The Rust version for the toolchain.
294        allocator_library (str, optional): Target that provides allocator functions when rust_library targets are embedded in a cc_binary.
295        global_allocator_library (str, optional): Target that provides allocator functions when a global allocator is used with cc_common_link.
296                                                  This target is only used in the target configuration; exec builds still use the symbols provided
297                                                  by the `allocator_library` target.
298        default_edition (str): Default Rust edition.
299        include_rustfmt (bool): Whether rustfmt is present in the toolchain.
300        include_llvm_tools (bool): Whether llvm-tools are present in the toolchain.
301        stdlib_linkflags (list, optional): Overriden flags needed for linking to rust
302                                           stdlib, akin to BAZEL_LINKLIBS. Defaults to
303                                           None.
304        extra_rustc_flags (list, optional): Extra flags to pass to rustc in non-exec configuration.
305        extra_exec_rustc_flags (list, optional): Extra flags to pass to rustc in exec configuration.
306        opt_level (dict, optional): Optimization level config for this toolchain.
307
308    Returns:
309        str: A rendered template of a `rust_toolchain` declaration
310    """
311    if stdlib_linkflags == None:
312        stdlib_linkflags = ", ".join(['"%s"' % x for x in system_to_stdlib_linkflags(target_triple.system)])
313
314    rustfmt_label = "None"
315    if include_rustfmt:
316        rustfmt_label = "\"//:rustfmt_bin\""
317    llvm_cov_label = "None"
318    llvm_profdata_label = "None"
319    if include_llvm_tools:
320        llvm_cov_label = "\"//:llvm_cov_bin\""
321        llvm_profdata_label = "\"//:llvm_profdata_bin\""
322    allocator_library_label = "None"
323    if allocator_library:
324        allocator_library_label = "\"{allocator_library}\"".format(allocator_library = allocator_library)
325    global_allocator_library_label = "None"
326    if global_allocator_library:
327        global_allocator_library_label = "\"{global_allocator_library}\"".format(global_allocator_library = global_allocator_library)
328
329    return _build_file_for_rust_toolchain_template.format(
330        toolchain_name = name,
331        binary_ext = system_to_binary_ext(target_triple.system),
332        staticlib_ext = system_to_staticlib_ext(target_triple.system),
333        dylib_ext = system_to_dylib_ext(target_triple.system),
334        allocator_library = allocator_library_label,
335        global_allocator_library = global_allocator_library_label,
336        stdlib_linkflags = stdlib_linkflags,
337        default_edition = default_edition,
338        exec_triple = exec_triple.str,
339        target_triple = target_triple.str,
340        rustfmt_label = rustfmt_label,
341        llvm_cov_label = llvm_cov_label,
342        llvm_profdata_label = llvm_profdata_label,
343        extra_rustc_flags = extra_rustc_flags,
344        extra_exec_rustc_flags = extra_exec_rustc_flags,
345        opt_level = opt_level,
346        version = version,
347    )
348
349_build_file_for_toolchain_template = """\
350toolchain(
351    name = "{name}",
352    exec_compatible_with = {exec_constraint_sets_serialized},
353    target_compatible_with = {target_constraint_sets_serialized},
354    toolchain = "{toolchain}",
355    toolchain_type = "{toolchain_type}",
356    {target_settings}
357)
358"""
359
360def BUILD_for_toolchain(
361        name,
362        toolchain,
363        toolchain_type,
364        target_settings,
365        target_compatible_with,
366        exec_compatible_with):
367    target_settings_value = "target_settings = {},".format(json.encode(target_settings)) if target_settings else "# target_settings = []"
368
369    return _build_file_for_toolchain_template.format(
370        name = name,
371        exec_constraint_sets_serialized = json.encode(exec_compatible_with),
372        target_constraint_sets_serialized = json.encode(target_compatible_with),
373        toolchain = toolchain,
374        toolchain_type = toolchain_type,
375        target_settings = target_settings_value,
376    )
377
378def load_rustfmt(ctx, target_triple, version, iso_date):
379    """Loads a rustfmt binary and yields corresponding BUILD for it
380
381    Args:
382        ctx (repository_ctx): The repository rule's context object.
383        target_triple (struct): The platform triple to download rustfmt for.
384        version (str): The version or channel of rustfmt.
385        iso_date (str): The date of the tool (or None, if the version is a specific version).
386
387    Returns:
388        Tuple[str, Dict[str, str]]: The BUILD file contents for this rustfmt binary and sha256 of it's artifact.
389    """
390
391    sha256 = load_arbitrary_tool(
392        ctx,
393        iso_date = iso_date,
394        target_triple = target_triple,
395        tool_name = "rustfmt",
396        tool_subdirectories = ["rustfmt-preview"],
397        version = version,
398    )
399
400    return BUILD_for_rustfmt(target_triple), sha256
401
402def load_rust_compiler(ctx, iso_date, target_triple, version):
403    """Loads a rust compiler and yields corresponding BUILD for it
404
405    Args:
406        ctx (repository_ctx): A repository_ctx.
407        iso_date (str): The date of the tool (or None, if the version is a specific version).
408        target_triple (struct): The Rust-style target that this compiler runs on.
409        version (str): The version of the tool among \"nightly\", \"beta\", or an exact version.
410
411    Returns:
412        Tuple[str, Dict[str, str]]: The BUILD file contents for this compiler and compiler library
413            and sha256 of it's artifact.
414    """
415
416    sha256 = load_arbitrary_tool(
417        ctx,
418        iso_date = iso_date,
419        target_triple = target_triple,
420        tool_name = "rustc",
421        tool_subdirectories = ["rustc"],
422        version = version,
423    )
424
425    return BUILD_for_compiler(target_triple), sha256
426
427def load_clippy(ctx, iso_date, target_triple, version):
428    """Loads Clippy and yields corresponding BUILD for it
429
430    Args:
431        ctx (repository_ctx): A repository_ctx.
432        iso_date (str): The date of the tool (or None, if the version is a specific version).
433        target_triple (struct): The Rust-style target that this compiler runs on.
434        version (str): The version of the tool among \"nightly\", \"beta\", or an exact version.
435
436    Returns:
437        Tuple[str, str]: The BUILD file contents for Clippy and the sha256 of it's artifact
438    """
439    sha256 = load_arbitrary_tool(
440        ctx,
441        iso_date = iso_date,
442        target_triple = target_triple,
443        tool_name = "clippy",
444        tool_subdirectories = ["clippy-preview"],
445        version = version,
446    )
447
448    return BUILD_for_clippy(target_triple), sha256
449
450def load_cargo(ctx, iso_date, target_triple, version):
451    """Loads Cargo and yields corresponding BUILD for it
452
453    Args:
454        ctx (repository_ctx): A repository_ctx.
455        iso_date (str): The date of the tool (or None, if the version is a specific version).
456        target_triple (struct): The Rust-style target that this compiler runs on.
457        version (str): The version of the tool among \"nightly\", \"beta\", or an exact version.
458
459    Returns:
460        Tuple[str, Dict[str, str]]: The BUILD file contents for Cargo and the sha256 of its artifact.
461    """
462
463    sha256 = load_arbitrary_tool(
464        ctx,
465        iso_date = iso_date,
466        target_triple = target_triple,
467        tool_name = "cargo",
468        tool_subdirectories = ["cargo"],
469        version = version,
470    )
471
472    return BUILD_for_cargo(target_triple), sha256
473
474def includes_rust_analyzer_proc_macro_srv(version, iso_date):
475    """Determine whether or not the rust_analyzer_proc_macro_srv binary in available in the given version of Rust.
476
477    Args:
478        version (str): The version of the tool among \"nightly\", \"beta\", or an exact version.
479        iso_date (str): The date of the tool (or None, if the version is a specific version).
480
481    Returns:
482        bool: Whether or not the binary is expected to be included
483    """
484
485    if version == "nightly":
486        return iso_date >= "2022-09-21"
487    elif version == "beta":
488        return False
489    elif version >= "1.64.0":
490        return True
491
492    return False
493
494def load_rust_src(ctx, iso_date, version, sha256 = None):
495    """Loads the rust source code. Used by the rust-analyzer rust-project.json generator.
496
497    Args:
498        ctx (ctx): A repository_ctx.
499        version (str): The version of the tool among "nightly", "beta', or an exact version.
500        iso_date (str): The date of the tool (or None, if the version is a specific version).
501        sha256 (str): The sha256 value for the `rust-src` artifact
502
503    Returns:
504        Dict[str, str]: A mapping of the artifact name to sha256
505    """
506    tool_suburl = produce_tool_suburl("rust-src", None, version, iso_date)
507    url = ctx.attr.urls[0].format(tool_suburl)
508
509    tool_path = produce_tool_path("rust-src", version, None)
510    archive_path = tool_path + _get_tool_extension(getattr(ctx.attr, "urls", None))
511
512    is_reproducible = True
513    if sha256 == None:
514        sha256s = getattr(ctx.attr, "sha256s", {})
515        sha256 = sha256s.get(archive_path, None) or FILE_KEY_TO_SHA.get(archive_path, None)
516        if not sha256:
517            sha256 = ""
518            is_reproducible = False
519
520    result = ctx.download_and_extract(
521        url,
522        output = "lib/rustlib/src",
523        sha256 = sha256,
524        auth = _make_auth_dict(ctx, [url]),
525        stripPrefix = "{}/rust-src/lib/rustlib/src/rust".format(tool_path),
526    )
527    ctx.file(
528        "lib/rustlib/src/BUILD.bazel",
529        """\
530filegroup(
531    name = "rustc_srcs",
532    srcs = glob(["**/*"]),
533    visibility = ["//visibility:public"],
534)""",
535    )
536
537    if is_reproducible:
538        return {}
539
540    return {archive_path: result.sha256}
541
542_build_file_for_rust_analyzer_toolchain_template = """\
543load("@rules_rust//rust:toolchain.bzl", "rust_analyzer_toolchain")
544
545rust_analyzer_toolchain(
546    name = "{name}",
547    proc_macro_srv = {proc_macro_srv},
548    rustc = "{rustc}",
549    rustc_srcs = "//lib/rustlib/src:rustc_srcs",
550    visibility = ["//visibility:public"],
551)
552"""
553
554def BUILD_for_rust_analyzer_toolchain(name, rustc, proc_macro_srv):
555    return _build_file_for_rust_analyzer_toolchain_template.format(
556        name = name,
557        rustc = rustc,
558        proc_macro_srv = repr(proc_macro_srv),
559    )
560
561_build_file_for_rustfmt_toolchain_template = """\
562load("@rules_rust//rust:toolchain.bzl", "rustfmt_toolchain")
563
564rustfmt_toolchain(
565    name = "{name}",
566    rustfmt = "{rustfmt}",
567    rustc = "{rustc}",
568    rustc_lib = "{rustc_lib}",
569    visibility = ["//visibility:public"],
570)
571"""
572
573def BUILD_for_rustfmt_toolchain(name, rustfmt, rustc, rustc_lib):
574    return _build_file_for_rustfmt_toolchain_template.format(
575        name = name,
576        rustfmt = rustfmt,
577        rustc = rustc,
578        rustc_lib = rustc_lib,
579    )
580
581def load_rust_stdlib(ctx, target_triple, version, iso_date = None):
582    """Loads a rust standard library and yields corresponding BUILD for it
583
584    Args:
585        ctx (repository_ctx): A repository_ctx.
586        target_triple (struct): The rust-style target triple of the tool
587        version (str): The version of the tool among \"nightly\", \"beta\", or an exact version.
588        iso_date (str): The iso_date to use with \"nightly\" or \"beta\" versions.
589
590    Returns:
591        Tuple[str, Dict[str, str]]: The BUILD file contents for this stdlib and the sha256 of the artifact.
592    """
593
594    sha256 = load_arbitrary_tool(
595        ctx,
596        iso_date = iso_date,
597        target_triple = target_triple,
598        tool_name = "rust-std",
599        tool_subdirectories = ["rust-std-{}".format(target_triple.str)],
600        version = version,
601    )
602
603    return BUILD_for_stdlib(target_triple), sha256
604
605def load_rustc_dev_nightly(ctx, target_triple, version, iso_date = None):
606    """Loads the nightly rustc dev component
607
608    Args:
609        ctx: A repository_ctx.
610        target_triple: The rust-style target triple of the tool
611        version (str): The version of the tool among \"nightly\", \"beta\", or an exact version.
612        iso_date (str): The iso_date to use with \"nightly\" or \"beta\" versions.
613
614    Returns:
615        Dict[str, str]: The sha256 value of the rustc-dev artifact.
616    """
617
618    subdir_name = "rustc-dev"
619    if iso_date and iso_date < "2020-12-24":
620        subdir_name = "rustc-dev-{}".format(target_triple)
621
622    sha256 = load_arbitrary_tool(
623        ctx,
624        iso_date = iso_date,
625        target_triple = target_triple,
626        tool_name = "rustc-dev",
627        tool_subdirectories = [subdir_name],
628        version = version,
629    )
630
631    return sha256
632
633def load_llvm_tools(ctx, target_triple, version, iso_date = None):
634    """Loads the llvm tools
635
636    Args:
637        ctx (repository_ctx): A repository_ctx.
638        target_triple (struct): The rust-style target triple of the tool
639        version (str): The version of the tool among \"nightly\", \"beta\", or an exact version.
640        iso_date (str): The iso_date to use with \"nightly\" or \"beta\" versions.
641
642    Returns:
643        Tuple[str, Dict[str, str]]: The BUILD.bazel content and sha256 value of the llvm tools artifact.
644    """
645    sha256 = load_arbitrary_tool(
646        ctx,
647        iso_date = iso_date,
648        target_triple = target_triple,
649        tool_name = "llvm-tools",
650        tool_subdirectories = ["llvm-tools-preview"],
651        version = version,
652    )
653
654    return BUILD_for_llvm_tools(target_triple), sha256
655
656def check_version_valid(version, iso_date, param_prefix = ""):
657    """Verifies that the provided rust version and iso_date make sense.
658
659    Args:
660        version (str): The rustc version
661        iso_date (str): The rustc nightly version's iso date
662        param_prefix (str, optional): The name of the tool who's version is being checked.
663    """
664
665    if not version and iso_date:
666        fail("{param_prefix}iso_date must be paired with a {param_prefix}version".format(param_prefix = param_prefix))
667
668    if version in ("beta", "nightly") and not iso_date:
669        fail("{param_prefix}iso_date must be specified if version is 'beta' or 'nightly'".format(param_prefix = param_prefix))
670
671def produce_tool_suburl(tool_name, target_triple, version, iso_date = None):
672    """Produces a fully qualified Rust tool name for URL
673
674    Args:
675        tool_name (str): The name of the tool per `static.rust-lang.org`.
676        target_triple (struct): The rust-style target triple of the tool.
677        version (str): The version of the tool among "nightly", "beta', or an exact version.
678        iso_date (str): The date of the tool (or None, if the version is a specific version).
679
680    Returns:
681        str: The fully qualified url path for the specified tool.
682    """
683    path = produce_tool_path(tool_name, version, target_triple)
684    return iso_date + "/" + path if (iso_date and version in ("beta", "nightly")) else path
685
686def produce_tool_path(tool_name, version, target_triple = None):
687    """Produces a qualified Rust tool name
688
689    Args:
690        tool_name (str): The name of the tool per static.rust-lang.org
691        version (str): The version of the tool among "nightly", "beta', or an exact version.
692        target_triple (struct, optional): The rust-style target triple of the tool
693
694    Returns:
695        str: The qualified path for the specified tool.
696    """
697    if not tool_name:
698        fail("No tool name was provided")
699    if not version:
700        fail("No tool version was provided")
701
702    # Not all tools require a triple. E.g. `rustc_src` (Rust source files for rust-analyzer).
703    platform_triple = None
704    if target_triple:
705        platform_triple = target_triple.str
706
707    return "-".join([e for e in [tool_name, version, platform_triple] if e])
708
709def lookup_tool_sha256(
710        repository_ctx,
711        tool_name,
712        target_triple,
713        version,
714        iso_date):
715    """Looks up the sha256 hash of a specific tool archive.
716
717    The lookup order is:
718
719    1. The sha256s dict in the context attributes;
720    2. The list of sha256 hashes populated in `//rust:known_shas.bzl`;
721
722    Args:
723        repository_ctx (repository_ctx): A repository_ctx (no attrs required).
724        tool_name (str): The name of the given tool per the archive naming.
725        target_triple (struct): The rust-style target triple of the tool.
726        version (str): The version of the tool among "nightly", "beta', or an exact version.
727        iso_date (str): The date of the tool (ignored if the version is a specific version).
728
729    Returns:
730        str: The sha256 of the tool archive, or an empty string if the hash could not be found.
731    """
732    tool_suburl = produce_tool_suburl(tool_name, target_triple, version, iso_date)
733    urls = getattr(repository_ctx.attr, "urls", None)
734    archive_path = tool_suburl + _get_tool_extension(urls)
735    sha256s = getattr(repository_ctx.attr, "sha256s", dict())
736
737    from_attr = sha256s.get(archive_path, None)
738    if from_attr:
739        return archive_path, from_attr
740
741    from_builtin = FILE_KEY_TO_SHA.get(archive_path, None)
742    if from_builtin:
743        return archive_path, from_builtin
744
745    return archive_path, ""
746
747def load_arbitrary_tool(
748        ctx,
749        tool_name,
750        tool_subdirectories,
751        version,
752        iso_date,
753        target_triple,
754        sha256 = None):
755    """Loads a Rust tool, downloads, and extracts into the common workspace.
756
757    This function sources the tool from the Rust-lang static file server. The index is available at:
758    - https://static.rust-lang.org/dist/channel-rust-stable.toml
759    - https://static.rust-lang.org/dist/channel-rust-beta.toml
760    - https://static.rust-lang.org/dist/channel-rust-nightly.toml
761
762    Args:
763        ctx (repository_ctx): A repository_ctx (no attrs required).
764        tool_name (str): The name of the given tool per the archive naming.
765        tool_subdirectories (str): The subdirectories of the tool files (at a level below the root directory of
766            the archive). The root directory of the archive is expected to match
767            $TOOL_NAME-$VERSION-$TARGET_TRIPLE.
768            Example:
769            tool_name
770            |    version
771            |    |      target_triple
772            v    v      v
773            rust-1.39.0-x86_64-unknown-linux-gnu/clippy-preview
774                                             .../rustc
775                                             .../etc
776            tool_subdirectories = ["clippy-preview", "rustc"]
777        version (str): The version of the tool among "nightly", "beta', or an exact version.
778        iso_date (str): The date of the tool (ignored if the version is a specific version).
779        target_triple (struct): The rust-style target triple of the tool.
780        sha256 (str, optional): The expected hash of hash of the Rust tool. Defaults to "".
781
782    Returns:
783        Dict[str, str]: A mapping of the tool name to it's sha256 value if the requested tool does not have
784            enough information in the repository_ctx to be reproducible.
785    """
786    check_version_valid(version, iso_date, param_prefix = tool_name + "_")
787
788    # View the indices mentioned in the docstring to find the tool_suburl for a given
789    # tool.
790    tool_suburl = produce_tool_suburl(tool_name, target_triple, version, iso_date)
791    urls = []
792
793    for url in getattr(ctx.attr, "urls", DEFAULT_STATIC_RUST_URL_TEMPLATES):
794        new_url = url.format(tool_suburl)
795        if new_url not in urls:
796            urls.append(new_url)
797
798    tool_path = produce_tool_path(tool_name, version, target_triple)
799
800    archive_path, ctx_sha256 = lookup_tool_sha256(ctx, tool_name, target_triple, version, iso_date)
801
802    is_reproducible = True
803    if sha256 == None:
804        sha256 = ctx_sha256
805        is_reproducible = bool(ctx_sha256)
806
807    for subdirectory in tool_subdirectories:
808        # As long as the sha256 value is consistent accross calls here the
809        # cost of downloading an artifact is negated as by Bazel's caching.
810        result = ctx.download_and_extract(
811            urls,
812            sha256 = sha256,
813            auth = _make_auth_dict(ctx, urls),
814            stripPrefix = "{}/{}".format(tool_path, subdirectory),
815        )
816
817        # In the event no sha256 was provided, set it to the value of the first
818        # downloaded item so subsequent downloads use a cached artifact.
819        if not sha256:
820            sha256 = result.sha256
821
822    # If the artifact is reproducibly downloadable then return an
823    # empty dict to inform consumers no attributes require updating.
824    if is_reproducible:
825        return {}
826
827    return {archive_path: sha256}
828
829# The function is copied from the main branch of bazel_tools.
830# It should become available there from version 7.1.0,
831# We should remove this function when we upgrade minimum supported
832# version to 7.1.0.
833# https://github.com/bazelbuild/bazel/blob/d37762b494a4e122d46a5a71e3a8cc77fa15aa25/tools/build_defs/repo/utils.bzl#L424-L446
834def _get_auth(ctx, urls):
835    """Utility function to obtain the correct auth dict for a list of urls from .netrc file.
836
837    Support optional netrc and auth_patterns attributes if available.
838
839    Args:
840      ctx: The repository context of the repository rule calling this utility
841        function.
842      urls: the list of urls to read
843
844    Returns:
845      the auth dict which can be passed to repository_ctx.download
846    """
847    if hasattr(ctx.attr, "netrc") and ctx.attr.netrc:
848        netrc = read_netrc(ctx, ctx.attr.netrc)
849    elif "NETRC" in ctx.os.environ:
850        netrc = read_netrc(ctx, ctx.os.environ["NETRC"])
851    else:
852        netrc = read_user_netrc(ctx)
853    auth_patterns = {}
854    if hasattr(ctx.attr, "auth_patterns") and ctx.attr.auth_patterns:
855        auth_patterns = ctx.attr.auth_patterns
856    return use_netrc(netrc, urls, auth_patterns)
857
858def _make_auth_dict(ctx, urls):
859    auth = getattr(ctx.attr, "auth", {})
860    if not auth:
861        return _get_auth(ctx, urls)
862    ret = {}
863    for url in urls:
864        ret[url] = auth
865    return ret
866
867def _get_tool_extension(urls = None):
868    if urls == None:
869        urls = DEFAULT_STATIC_RUST_URL_TEMPLATES
870    if urls[0][-7:] == ".tar.gz":
871        return ".tar.gz"
872    elif urls[0][-7:] == ".tar.xz":
873        return ".tar.xz"
874    else:
875        return ""
876
877def select_rust_version(versions):
878    """Select the highest priorty version for a list of Rust versions
879
880    Priority order: `stable > nightly > beta`
881
882    Note that duplicate channels are unexpected in `versions`.
883
884    Args:
885        versions (list): A list of Rust versions. E.g. [`1.66.0`, `nightly/2022-12-15`]
886
887    Returns:
888        str: The highest ranking value from `versions`
889    """
890    if not versions:
891        fail("No versions were provided")
892
893    current = versions[0]
894
895    for ver in versions:
896        if ver.startswith("beta"):
897            if current[0].isdigit() or current.startswith("nightly"):
898                continue
899            if current.startswith("beta") and ver > current:
900                current = ver
901                continue
902
903            current = ver
904        elif ver.startswith("nightly"):
905            if current[0].isdigit():
906                continue
907            if current.startswith("nightly") and ver > current:
908                current = ver
909                continue
910
911            current = ver
912
913        else:
914            current = ver
915
916    return current
917
918_build_file_for_toolchain_hub_template = """
919toolchain(
920    name = "{name}",
921    exec_compatible_with = {exec_constraint_sets_serialized},
922    target_compatible_with = {target_constraint_sets_serialized},
923    target_settings = {target_settings_serialized},
924    toolchain = "{toolchain}",
925    toolchain_type = "{toolchain_type}",
926    visibility = ["//visibility:public"],
927)
928"""
929
930def BUILD_for_toolchain_hub(
931        toolchain_names,
932        toolchain_labels,
933        toolchain_types,
934        target_settings,
935        target_compatible_with,
936        exec_compatible_with):
937    return "\n".join([_build_file_for_toolchain_hub_template.format(
938        name = toolchain_name,
939        exec_constraint_sets_serialized = json.encode(exec_compatible_with[toolchain_name]),
940        target_constraint_sets_serialized = json.encode(target_compatible_with[toolchain_name]),
941        target_settings_serialized = json.encode(target_settings[toolchain_name]) if toolchain_name in target_settings else "None",
942        toolchain = toolchain_labels[toolchain_name],
943        toolchain_type = toolchain_types[toolchain_name],
944    ) for toolchain_name in toolchain_names])
945
946def _toolchain_repository_hub_impl(repository_ctx):
947    repository_ctx.file("WORKSPACE.bazel", """workspace(name = "{}")""".format(
948        repository_ctx.name,
949    ))
950
951    repository_ctx.file("BUILD.bazel", BUILD_for_toolchain_hub(
952        toolchain_names = repository_ctx.attr.toolchain_names,
953        toolchain_labels = repository_ctx.attr.toolchain_labels,
954        toolchain_types = repository_ctx.attr.toolchain_types,
955        target_settings = repository_ctx.attr.target_settings,
956        target_compatible_with = repository_ctx.attr.target_compatible_with,
957        exec_compatible_with = repository_ctx.attr.exec_compatible_with,
958    ))
959
960toolchain_repository_hub = repository_rule(
961    doc = (
962        "Generates a toolchain-bearing repository that declares a set of other toolchains from other " +
963        "repositories. This exists to allow registering a set of toolchains in one go with the `:all` target."
964    ),
965    attrs = {
966        "exec_compatible_with": attr.string_list_dict(
967            doc = "A list of constraints for the execution platform for this toolchain, keyed by toolchain name.",
968            mandatory = True,
969        ),
970        "target_compatible_with": attr.string_list_dict(
971            doc = "A list of constraints for the target platform for this toolchain, keyed by toolchain name.",
972            mandatory = True,
973        ),
974        "target_settings": attr.string_list_dict(
975            doc = "A list of config_settings that must be satisfied by the target configuration in order for this toolchain to be selected during toolchain resolution.",
976            mandatory = True,
977        ),
978        "toolchain_labels": attr.string_dict(
979            doc = "The name of the toolchain implementation target, keyed by toolchain name.",
980            mandatory = True,
981        ),
982        "toolchain_names": attr.string_list(
983            mandatory = True,
984        ),
985        "toolchain_types": attr.string_dict(
986            doc = "The toolchain type of the toolchain to declare, keyed by toolchain name.",
987            mandatory = True,
988        ),
989    },
990    implementation = _toolchain_repository_hub_impl,
991)