fork/rules_rust

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

Keith SmileyAdd test coverage support (#1324)7465c1aa

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