fork/rules_rust

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

UebelAndreAdded `rust_analyzer_toolchain` and repository rules for creating one (#1455)0e86b9dd

0.8.0
20.7 KiB649 linesraw
1"""Utility macros for use in rules_rust repository rules"""
2
3load("//rust:known_shas.bzl", "FILE_KEY_TO_SHA")
4load(
5    "//rust/platform:triple_mappings.bzl",
6    "system_to_binary_ext",
7    "system_to_dylib_ext",
8    "system_to_staticlib_ext",
9    "system_to_stdlib_linkflags",
10    "triple_to_system",
11)
12
13DEFAULT_TOOLCHAIN_NAME_PREFIX = "toolchain_for"
14DEFAULT_STATIC_RUST_URL_TEMPLATES = ["https://static.rust-lang.org/dist/{}.tar.gz"]
15
16_build_file_for_compiler_template = """\
17filegroup(
18    name = "rustc",
19    srcs = ["bin/rustc{binary_ext}"],
20    visibility = ["//visibility:public"],
21)
22
23filegroup(
24    name = "rustc_lib",
25    srcs = glob(
26        [
27            "bin/*{dylib_ext}",
28            "lib/*{dylib_ext}",
29            "lib/rustlib/{target_triple}/codegen-backends/*{dylib_ext}",
30            "lib/rustlib/{target_triple}/bin/rust-lld{binary_ext}",
31            "lib/rustlib/{target_triple}/lib/*{dylib_ext}",
32        ],
33        allow_empty = True,
34    ),
35    visibility = ["//visibility:public"],
36)
37
38filegroup(
39    name = "rustdoc",
40    srcs = ["bin/rustdoc{binary_ext}"],
41    visibility = ["//visibility:public"],
42)
43"""
44
45def BUILD_for_compiler(target_triple):
46    """Emits a BUILD file the compiler archive.
47
48    Args:
49        target_triple (str): The triple of the target platform
50
51    Returns:
52        str: The contents of a BUILD file
53    """
54    system = triple_to_system(target_triple)
55    return _build_file_for_compiler_template.format(
56        binary_ext = system_to_binary_ext(system),
57        staticlib_ext = system_to_staticlib_ext(system),
58        dylib_ext = system_to_dylib_ext(system),
59        target_triple = target_triple,
60    )
61
62_build_file_for_cargo_template = """\
63filegroup(
64    name = "cargo",
65    srcs = ["bin/cargo{binary_ext}"],
66    visibility = ["//visibility:public"],
67)"""
68
69def BUILD_for_cargo(target_triple):
70    """Emits a BUILD file the cargo archive.
71
72    Args:
73        target_triple (str): The triple of the target platform
74
75    Returns:
76        str: The contents of a BUILD file
77    """
78    system = triple_to_system(target_triple)
79    return _build_file_for_cargo_template.format(
80        binary_ext = system_to_binary_ext(system),
81    )
82
83_build_file_for_rustfmt_template = """\
84filegroup(
85    name = "rustfmt_bin",
86    srcs = ["bin/rustfmt{binary_ext}"],
87    visibility = ["//visibility:public"],
88)
89
90sh_binary(
91    name = "rustfmt",
92    srcs = [":rustfmt_bin"],
93    visibility = ["//visibility:public"],
94)
95"""
96
97def BUILD_for_rustfmt(target_triple):
98    """Emits a BUILD file the rustfmt archive.
99
100    Args:
101        target_triple (str): The triple of the target platform
102
103    Returns:
104        str: The contents of a BUILD file
105    """
106    system = triple_to_system(target_triple)
107    return _build_file_for_rustfmt_template.format(
108        binary_ext = system_to_binary_ext(system),
109    )
110
111_build_file_for_clippy_template = """\
112filegroup(
113    name = "clippy_driver_bin",
114    srcs = ["bin/clippy-driver{binary_ext}"],
115    visibility = ["//visibility:public"],
116)
117"""
118
119def BUILD_for_clippy(target_triple):
120    """Emits a BUILD file the clippy archive.
121
122    Args:
123        target_triple (str): The triple of the target platform
124
125    Returns:
126        str: The contents of a BUILD file
127    """
128    system = triple_to_system(target_triple)
129    return _build_file_for_clippy_template.format(binary_ext = system_to_binary_ext(system))
130
131_build_file_for_llvm_tools = """\
132filegroup(
133    name = "llvm_cov_bin",
134    srcs = ["lib/rustlib/{target_triple}/bin/llvm-cov{binary_ext}"],
135    visibility = ["//visibility:public"],
136)
137
138filegroup(
139    name = "llvm_profdata_bin",
140    srcs = ["lib/rustlib/{target_triple}/bin/llvm-profdata{binary_ext}"],
141    visibility = ["//visibility:public"],
142)
143"""
144
145def BUILD_for_llvm_tools(target_triple):
146    """Emits a BUILD file the llvm-tools binaries.
147
148    Args:
149        target_triple (str): The triple of the target platform
150
151    Returns:
152        str: The contents of a BUILD file
153    """
154    system = triple_to_system(target_triple)
155    return _build_file_for_llvm_tools.format(
156        binary_ext = system_to_binary_ext(system),
157        target_triple = target_triple,
158    )
159
160_build_file_for_stdlib_template = """\
161load("@rules_rust//rust:toolchain.bzl", "rust_stdlib_filegroup")
162
163rust_stdlib_filegroup(
164    name = "rust_std-{target_triple}",
165    srcs = glob(
166        [
167            "lib/rustlib/{target_triple}/lib/*.rlib",
168            "lib/rustlib/{target_triple}/lib/*{dylib_ext}",
169            "lib/rustlib/{target_triple}/lib/*{staticlib_ext}",
170            "lib/rustlib/{target_triple}/lib/self-contained/**",
171        ],
172        # Some patterns (e.g. `lib/*.a`) don't match anything, see https://github.com/bazelbuild/rules_rust/pull/245
173        allow_empty = True,
174    ),
175    visibility = ["//visibility:public"],
176)
177
178# For legacy support
179alias(
180    name = "rust_lib-{target_triple}",
181    actual = "rust_std-{target_triple}",
182    visibility = ["//visibility:public"],
183)
184"""
185
186def BUILD_for_stdlib(target_triple):
187    """Emits a BUILD file the stdlib archive.
188
189    Args:
190        target_triple (str): The triple of the target platform
191
192    Returns:
193        str: The contents of a BUILD file
194    """
195    system = triple_to_system(target_triple)
196    return _build_file_for_stdlib_template.format(
197        binary_ext = system_to_binary_ext(system),
198        staticlib_ext = system_to_staticlib_ext(system),
199        dylib_ext = system_to_dylib_ext(system),
200        target_triple = target_triple,
201    )
202
203_build_file_for_rust_toolchain_template = """\
204load("@rules_rust//rust:toolchain.bzl", "rust_toolchain")
205
206rust_toolchain(
207    name = "{toolchain_name}",
208    rust_doc = "@{workspace_name}//:rustdoc",
209    rust_std = "@{workspace_name}//:rust_std-{target_triple}",
210    rustc = "@{workspace_name}//:rustc",
211    rustfmt = {rustfmt_label},
212    cargo = "@{workspace_name}//:cargo",
213    clippy_driver = "@{workspace_name}//:clippy_driver_bin",
214    llvm_cov = {llvm_cov_label},
215    llvm_profdata = {llvm_profdata_label},
216    rustc_lib = "@{workspace_name}//:rustc_lib",
217    rustc_srcs = {rustc_srcs},
218    binary_ext = "{binary_ext}",
219    staticlib_ext = "{staticlib_ext}",
220    dylib_ext = "{dylib_ext}",
221    stdlib_linkflags = [{stdlib_linkflags}],
222    os = "{system}",
223    default_edition = "{default_edition}",
224    exec_triple = "{exec_triple}",
225    target_triple = "{target_triple}",
226    visibility = ["//visibility:public"],
227)
228"""
229
230def BUILD_for_rust_toolchain(
231        workspace_name,
232        name,
233        exec_triple,
234        target_triple,
235        include_rustc_srcs,
236        default_edition,
237        include_rustfmt,
238        include_llvm_tools,
239        stdlib_linkflags = None):
240    """Emits a toolchain declaration to match an existing compiler and stdlib.
241
242    Args:
243        workspace_name (str): The name of the workspace that this toolchain resides in
244        name (str): The name of the toolchain declaration
245        exec_triple (str): The rust-style target that this compiler runs on
246        target_triple (str): The rust-style target triple of the tool
247        include_rustc_srcs (bool, optional): Whether to download rustc's src code. This is required in order to use rust-analyzer support. Defaults to False.
248        default_edition (str): Default Rust edition.
249        include_rustfmt (bool): Whether rustfmt is present in the toolchain.
250        include_llvm_tools (bool): Whether llvm-tools are present in the toolchain.
251        stdlib_linkflags (list, optional): Overriden flags needed for linking to rust
252                                           stdlib, akin to BAZEL_LINKLIBS. Defaults to
253                                           None.
254
255
256    Returns:
257        str: A rendered template of a `rust_toolchain` declaration
258    """
259    system = triple_to_system(target_triple)
260    if stdlib_linkflags == None:
261        stdlib_linkflags = ", ".join(['"%s"' % x for x in system_to_stdlib_linkflags(system)])
262
263    rustc_srcs = "None"
264    if include_rustc_srcs:
265        rustc_srcs = "\"@{workspace_name}//lib/rustlib/src:rustc_srcs\"".format(workspace_name = workspace_name)
266    rustfmt_label = "None"
267    if include_rustfmt:
268        rustfmt_label = "\"@{workspace_name}//:rustfmt_bin\"".format(workspace_name = workspace_name)
269    llvm_cov_label = "None"
270    llvm_profdata_label = "None"
271    if include_llvm_tools:
272        llvm_cov_label = "\"@{workspace_name}//:llvm_cov_bin\"".format(workspace_name = workspace_name)
273        llvm_profdata_label = "\"@{workspace_name}//:llvm_profdata_bin\"".format(workspace_name = workspace_name)
274
275    return _build_file_for_rust_toolchain_template.format(
276        toolchain_name = name,
277        workspace_name = workspace_name,
278        binary_ext = system_to_binary_ext(system),
279        staticlib_ext = system_to_staticlib_ext(system),
280        dylib_ext = system_to_dylib_ext(system),
281        rustc_srcs = rustc_srcs,
282        stdlib_linkflags = stdlib_linkflags,
283        system = system,
284        default_edition = default_edition,
285        exec_triple = exec_triple,
286        target_triple = target_triple,
287        rustfmt_label = rustfmt_label,
288        llvm_cov_label = llvm_cov_label,
289        llvm_profdata_label = llvm_profdata_label,
290    )
291
292_build_file_for_toolchain_template = """\
293toolchain(
294    name = "{name}",
295    exec_compatible_with = {exec_constraint_sets_serialized},
296    target_compatible_with = {target_constraint_sets_serialized},
297    toolchain = "{toolchain}",
298    toolchain_type = "{toolchain_type}",
299)
300"""
301
302def BUILD_for_toolchain(
303        name,
304        toolchain,
305        toolchain_type,
306        target_compatible_with,
307        exec_compatible_with):
308    return _build_file_for_toolchain_template.format(
309        name = name,
310        exec_constraint_sets_serialized = exec_compatible_with,
311        target_constraint_sets_serialized = target_compatible_with,
312        toolchain = toolchain,
313        toolchain_type = toolchain_type,
314    )
315
316def load_rustfmt(ctx):
317    """Loads a rustfmt binary and yields corresponding BUILD for it
318
319    Args:
320        ctx (repository_ctx): The repository rule's context object
321
322    Returns:
323        str: The BUILD file contents for this rustfmt binary
324    """
325    target_triple = ctx.attr.exec_triple
326
327    load_arbitrary_tool(
328        ctx,
329        iso_date = ctx.attr.iso_date,
330        target_triple = target_triple,
331        tool_name = "rustfmt",
332        tool_subdirectories = ["rustfmt-preview"],
333        version = ctx.attr.rustfmt_version,
334    )
335
336    return BUILD_for_rustfmt(target_triple)
337
338def load_rust_compiler(ctx):
339    """Loads a rust compiler and yields corresponding BUILD for it
340
341    Args:
342        ctx (repository_ctx): A repository_ctx.
343
344    Returns:
345        str: The BUILD file contents for this compiler and compiler library
346    """
347
348    target_triple = ctx.attr.exec_triple
349    load_arbitrary_tool(
350        ctx,
351        iso_date = ctx.attr.iso_date,
352        target_triple = target_triple,
353        tool_name = "rustc",
354        tool_subdirectories = ["rustc"],
355        version = ctx.attr.version,
356    )
357
358    return BUILD_for_compiler(target_triple)
359
360def load_clippy(ctx):
361    """Loads Clippy and yields corresponding BUILD for it
362
363    Args:
364        ctx (repository_ctx): A repository_ctx.
365
366    Returns:
367        str: The BUILD file contents for Clippy
368    """
369
370    target_triple = ctx.attr.exec_triple
371    load_arbitrary_tool(
372        ctx,
373        iso_date = ctx.attr.iso_date,
374        target_triple = target_triple,
375        tool_name = "clippy",
376        tool_subdirectories = ["clippy-preview"],
377        version = ctx.attr.version,
378    )
379
380    return BUILD_for_clippy(target_triple)
381
382def load_cargo(ctx):
383    """Loads Cargo and yields corresponding BUILD for it
384
385    Args:
386        ctx (repository_ctx): A repository_ctx.
387
388    Returns:
389        str: The BUILD file contents for Cargo
390    """
391
392    target_triple = ctx.attr.exec_triple
393    load_arbitrary_tool(
394        ctx,
395        iso_date = ctx.attr.iso_date,
396        target_triple = target_triple,
397        tool_name = "cargo",
398        tool_subdirectories = ["cargo"],
399        version = ctx.attr.version,
400    )
401
402    return BUILD_for_cargo(target_triple)
403
404def should_include_rustc_srcs(repository_ctx):
405    """Determing whether or not to include rustc sources in the toolchain.
406
407    Args:
408        repository_ctx (repository_ctx): The repository rule's context object
409
410    Returns:
411        bool: Whether or not to include rustc source files in a `rustc_toolchain`
412    """
413
414    # The environment variable will always take precedence over the attribute.
415    include_rustc_srcs_env = repository_ctx.os.environ.get("RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS")
416    if include_rustc_srcs_env != None:
417        return include_rustc_srcs_env.lower() in ["true", "1"]
418
419    return getattr(repository_ctx.attr, "include_rustc_srcs", False)
420
421def load_rust_src(ctx, sha256 = ""):
422    """Loads the rust source code. Used by the rust-analyzer rust-project.json generator.
423
424    Args:
425        ctx (ctx): A repository_ctx.
426        sha256 (str): The sha256 value for the `rust-src` artifact
427    """
428    tool_suburl = produce_tool_suburl("rust-src", None, ctx.attr.version, ctx.attr.iso_date)
429    url = ctx.attr.urls[0].format(tool_suburl)
430
431    tool_path = produce_tool_path("rust-src", None, ctx.attr.version)
432    archive_path = tool_path + _get_tool_extension(ctx)
433    sha256 = sha256 or getattr(ctx.attr, "sha256s", {}).get(archive_path) or FILE_KEY_TO_SHA.get(archive_path) or ""
434    ctx.download_and_extract(
435        url,
436        output = "lib/rustlib/src",
437        sha256 = sha256,
438        auth = _make_auth_dict(ctx, [url]),
439        stripPrefix = "{}/rust-src/lib/rustlib/src/rust".format(tool_path),
440    )
441    ctx.file(
442        "lib/rustlib/src/BUILD.bazel",
443        """\
444filegroup(
445    name = "rustc_srcs",
446    srcs = glob(["**/*"]),
447    visibility = ["//visibility:public"],
448)""",
449    )
450
451_build_file_for_rust_analyzer_toolchain_template = """\
452load("@rules_rust//rust:toolchain.bzl", "rust_analyzer_toolchain")
453
454rust_analyzer_toolchain(
455    name = "{name}",
456    rustc_srcs = "//lib/rustlib/src:rustc_srcs",
457    visibility = ["//visibility:public"],
458)
459"""
460
461def BUILD_for_rust_analyzer_toolchain(name):
462    return _build_file_for_rust_analyzer_toolchain_template.format(
463        name = name,
464    )
465
466def load_rust_stdlib(ctx, target_triple):
467    """Loads a rust standard library and yields corresponding BUILD for it
468
469    Args:
470        ctx (repository_ctx): A repository_ctx.
471        target_triple (str): The rust-style target triple of the tool
472
473    Returns:
474        str: The BUILD file contents for this stdlib
475    """
476
477    load_arbitrary_tool(
478        ctx,
479        iso_date = ctx.attr.iso_date,
480        target_triple = target_triple,
481        tool_name = "rust-std",
482        tool_subdirectories = ["rust-std-{}".format(target_triple)],
483        version = ctx.attr.version,
484    )
485
486    return BUILD_for_stdlib(target_triple)
487
488def load_rustc_dev_nightly(ctx, target_triple):
489    """Loads the nightly rustc dev component
490
491    Args:
492        ctx: A repository_ctx.
493        target_triple: The rust-style target triple of the tool
494    """
495
496    subdir_name = "rustc-dev"
497    if ctx.attr.iso_date < "2020-12-24":
498        subdir_name = "rustc-dev-{}".format(target_triple)
499
500    load_arbitrary_tool(
501        ctx,
502        iso_date = ctx.attr.iso_date,
503        target_triple = target_triple,
504        tool_name = "rustc-dev",
505        tool_subdirectories = [subdir_name],
506        version = ctx.attr.version,
507    )
508
509def load_llvm_tools(ctx, target_triple):
510    """Loads the llvm tools
511
512    Args:
513        ctx: A repository_ctx.
514        target_triple: The rust-style target triple of the tool
515    """
516    load_arbitrary_tool(
517        ctx,
518        iso_date = ctx.attr.iso_date,
519        target_triple = target_triple,
520        tool_name = "llvm-tools",
521        tool_subdirectories = ["llvm-tools-preview"],
522        version = ctx.attr.version,
523    )
524
525    return BUILD_for_llvm_tools(target_triple)
526
527def check_version_valid(version, iso_date, param_prefix = ""):
528    """Verifies that the provided rust version and iso_date make sense.
529
530    Args:
531        version (str): The rustc version
532        iso_date (str): The rustc nightly version's iso date
533        param_prefix (str, optional): The name of the tool who's version is being checked.
534    """
535
536    if not version and iso_date:
537        fail("{param_prefix}iso_date must be paired with a {param_prefix}version".format(param_prefix = param_prefix))
538
539    if version in ("beta", "nightly") and not iso_date:
540        fail("{param_prefix}iso_date must be specified if version is 'beta' or 'nightly'".format(param_prefix = param_prefix))
541
542def produce_tool_suburl(tool_name, target_triple, version, iso_date = None):
543    """Produces a fully qualified Rust tool name for URL
544
545    Args:
546        tool_name: The name of the tool per static.rust-lang.org
547        target_triple: The rust-style target triple of the tool
548        version: The version of the tool among "nightly", "beta', or an exact version.
549        iso_date: The date of the tool (or None, if the version is a specific version).
550
551    Returns:
552        str: The fully qualified url path for the specified tool.
553    """
554    path = produce_tool_path(tool_name, target_triple, version)
555    return iso_date + "/" + path if (iso_date and version in ("beta", "nightly")) else path
556
557def produce_tool_path(tool_name, target_triple, version):
558    """Produces a qualified Rust tool name
559
560    Args:
561        tool_name: The name of the tool per static.rust-lang.org
562        target_triple: The rust-style target triple of the tool
563        version: The version of the tool among "nightly", "beta', or an exact version.
564
565    Returns:
566        str: The qualified path for the specified tool.
567    """
568    if not tool_name:
569        fail("No tool name was provided")
570    if not version:
571        fail("No tool version was provided")
572    return "-".join([e for e in [tool_name, version, target_triple] if e])
573
574def load_arbitrary_tool(ctx, tool_name, tool_subdirectories, version, iso_date, target_triple, sha256 = ""):
575    """Loads a Rust tool, downloads, and extracts into the common workspace.
576
577    This function sources the tool from the Rust-lang static file server. The index is available at:
578    - https://static.rust-lang.org/dist/channel-rust-stable.toml
579    - https://static.rust-lang.org/dist/channel-rust-beta.toml
580    - https://static.rust-lang.org/dist/channel-rust-nightly.toml
581
582    Args:
583        ctx (repository_ctx): A repository_ctx (no attrs required).
584        tool_name (str): The name of the given tool per the archive naming.
585        tool_subdirectories (str): The subdirectories of the tool files (at a level below the root directory of
586            the archive). The root directory of the archive is expected to match
587            $TOOL_NAME-$VERSION-$TARGET_TRIPLE.
588            Example:
589            tool_name
590            |    version
591            |    |      target_triple
592            v    v      v
593            rust-1.39.0-x86_64-unknown-linux-gnu/clippy-preview
594                                             .../rustc
595                                             .../etc
596            tool_subdirectories = ["clippy-preview", "rustc"]
597        version (str): The version of the tool among "nightly", "beta', or an exact version.
598        iso_date (str): The date of the tool (ignored if the version is a specific version).
599        target_triple (str): The rust-style target triple of the tool
600        sha256 (str, optional): The expected hash of hash of the Rust tool. Defaults to "".
601    """
602    check_version_valid(version, iso_date, param_prefix = tool_name + "_")
603
604    # View the indices mentioned in the docstring to find the tool_suburl for a given
605    # tool.
606    tool_suburl = produce_tool_suburl(tool_name, target_triple, version, iso_date)
607    urls = []
608
609    for url in getattr(ctx.attr, "urls", DEFAULT_STATIC_RUST_URL_TEMPLATES):
610        new_url = url.format(tool_suburl)
611        if new_url not in urls:
612            urls.append(new_url)
613
614    tool_path = produce_tool_path(tool_name, target_triple, version)
615    archive_path = tool_path + _get_tool_extension(ctx)
616    sha256 = getattr(ctx.attr, "sha256s", dict()).get(archive_path) or FILE_KEY_TO_SHA.get(archive_path) or sha256
617
618    for subdirectory in tool_subdirectories:
619        # As long as the sha256 value is consistent accross calls here the
620        # cost of downloading an artifact is negated as by Bazel's caching.
621        result = ctx.download_and_extract(
622            urls,
623            sha256 = sha256,
624            auth = _make_auth_dict(ctx, urls),
625            stripPrefix = "{}/{}".format(tool_path, subdirectory),
626        )
627
628        # In the event no sha256 was provided, set it to the value of the first
629        # downloaded item so subsequent downloads use a cached artifact.
630        if not sha256:
631            sha256 = result.sha256
632
633def _make_auth_dict(ctx, urls):
634    auth = getattr(ctx.attr, "auth", {})
635    if not auth:
636        return {}
637    ret = {}
638    for url in urls:
639        ret[url] = auth
640    return ret
641
642def _get_tool_extension(ctx):
643    urls = getattr(ctx.attr, "urls", DEFAULT_STATIC_RUST_URL_TEMPLATES)
644    if urls[0][-7:] == ".tar.gz":
645        return ".tar.gz"
646    elif urls[0][-7:] == ".tar.xz":
647        return ".tar.xz"
648    else:
649        return ""