fork/rules_rust

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

Wren TurkalRemove doc about STATIC_RUST_URL env var. (#1306)1cab691d

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