fork/rules_rust

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

UebelAndreMoved `cargo_dep_env` into it's own file. (#2823)64502533

cleanup
4.1 KiB107 linesraw
1"""Rules for Cargo build scripts (`build.rs` files)"""
2
3load("//rust:rust_common.bzl", "BuildInfo", "DepInfo")
4
5# buildifier: disable=bzl-visibility
6load("//rust/private:utils.bzl", "dedent")
7
8def _cargo_dep_env_impl(ctx):
9    empty_file = ctx.actions.declare_file(ctx.label.name + ".empty_file")
10    empty_dir = ctx.actions.declare_directory(ctx.label.name + ".empty_dir")
11    ctx.actions.write(
12        output = empty_file,
13        content = "",
14    )
15    ctx.actions.run(
16        outputs = [empty_dir],
17        executable = "true",
18    )
19
20    build_infos = []
21    out_dir = ctx.file.out_dir
22    if out_dir:
23        if not out_dir.is_directory:
24            fail("out_dir must be a directory artifact")
25
26        # BuildInfos in this list are collected up for all transitive cargo_build_script
27        # dependencies. This is important for any flags set in `dep_env` which reference this
28        # `out_dir`.
29        #
30        # TLDR: This BuildInfo propagates up build script dependencies.
31        build_infos.append(BuildInfo(
32            dep_env = empty_file,
33            flags = empty_file,
34            linker_flags = empty_file,
35            link_search_paths = empty_file,
36            out_dir = out_dir,
37            rustc_env = empty_file,
38            compile_data = depset([]),
39        ))
40    return [
41        DefaultInfo(files = depset(ctx.files.src)),
42        # Parts of this BuildInfo is used when building all transitive dependencies
43        # (cargo_build_script and otherwise), alongside the DepInfo. This is how other rules
44        # identify this one as a valid dependency, but we don't otherwise have a use for it.
45        #
46        # TLDR: This BuildInfo propagates up normal (non build script) depenencies.
47        #
48        # In the future, we could consider setting rustc_env here, and also propagating dep_dir
49        # so files in it can be referenced there.
50        BuildInfo(
51            dep_env = empty_file,
52            flags = empty_file,
53            linker_flags = empty_file,
54            link_search_paths = empty_file,
55            out_dir = None,
56            rustc_env = empty_file,
57            compile_data = depset([]),
58        ),
59        # Information here is used directly by dependencies, and it is an error to have more than
60        # one dependency which sets this. This is the main way to specify information from build
61        # scripts, which is what we're looking to do.
62        DepInfo(
63            dep_env = ctx.file.src,
64            direct_crates = depset(),
65            link_search_path_files = depset(),
66            transitive_build_infos = depset(direct = build_infos),
67            transitive_crate_outputs = depset(),
68            transitive_crates = depset(),
69            transitive_noncrates = depset(),
70        ),
71    ]
72
73cargo_dep_env = rule(
74    implementation = _cargo_dep_env_impl,
75    doc = (
76        "A rule for generating variables for dependent `cargo_build_script`s " +
77        "without a build script. This is useful for using Bazel rules instead " +
78        "of a build script, while also generating configuration information " +
79        "for build scripts which depend on this crate."
80    ),
81    attrs = {
82        "out_dir": attr.label(
83            doc = dedent("""\
84                Folder containing additional inputs when building all direct dependencies.
85
86                This has the same effect as a `cargo_build_script` which prints
87                puts files into `$OUT_DIR`, but without requiring a build script.
88            """),
89            allow_single_file = True,
90            mandatory = False,
91        ),
92        "src": attr.label(
93            doc = dedent("""\
94                File containing additional environment variables to set for build scripts of direct dependencies.
95
96                This has the same effect as a `cargo_build_script` which prints
97                `cargo:VAR=VALUE` lines, but without requiring a build script.
98
99                This files should  contain a single variable per line, of format
100                `NAME=value`, and newlines may be included in a value by ending a
101                line with a trailing back-slash (`\\\\`).
102            """),
103            allow_single_file = True,
104            mandatory = True,
105        ),
106    },
107)