fork/rules_rust

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

scentiniDon't propagate `compatible_with` to the underlying `cargo_build_script` `rust_binary` target (#1754)941c7cca

rust-analyzer
5.2 KiB147 linesraw
1"""Rules for Cargo build scripts (`build.rs` files)"""
2
3load(
4    "//cargo/private:cargo_build_script.bzl",
5    "name_to_crate_name",
6    "name_to_pkg_name",
7    _build_script_run = "cargo_build_script",
8)
9load("//rust:defs.bzl", "rust_binary")
10
11def cargo_build_script(
12        name,
13        crate_features = [],
14        version = None,
15        deps = [],
16        build_script_env = {},
17        data = [],
18        tools = [],
19        links = None,
20        rustc_env = {},
21        rustc_flags = [],
22        visibility = None,
23        tags = None,
24        **kwargs):
25    """Compile and execute a rust build script to generate build attributes
26
27    This rules take the same arguments as rust_binary.
28
29    Example:
30
31    Suppose you have a crate with a cargo build script `build.rs`:
32
33    ```output
34    [workspace]/
35        hello_lib/
36            BUILD
37            build.rs
38            src/
39                lib.rs
40    ```
41
42    Then you want to use the build script in the following:
43
44    `hello_lib/BUILD`:
45    ```python
46    package(default_visibility = ["//visibility:public"])
47
48    load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library")
49    load("@rules_rust//cargo:defs.bzl", "cargo_build_script")
50
51    # This will run the build script from the root of the workspace, and
52    # collect the outputs.
53    cargo_build_script(
54        name = "build_script",
55        srcs = ["build.rs"],
56        # Optional environment variables passed during build.rs compilation
57        rustc_env = {
58           "CARGO_PKG_VERSION": "0.1.2",
59        },
60        # Optional environment variables passed during build.rs execution.
61        # Note that as the build script's working directory is not execroot,
62        # execpath/location will return an absolute path, instead of a relative
63        # one.
64        build_script_env = {
65            "SOME_TOOL_OR_FILE": "$(execpath @tool//:binary)"
66        }
67        # Optional data/tool dependencies
68        data = ["@tool//:binary"],
69    )
70
71    rust_library(
72        name = "hello_lib",
73        srcs = [
74            "src/lib.rs",
75        ],
76        deps = [":build_script"],
77    )
78    ```
79
80    The `hello_lib` target will be build with the flags and the environment variables declared by the \
81    build script in addition to the file generated by it.
82
83    Args:
84        name (str): The name for the underlying rule. This should be the name of the package
85            being compiled, optionally with a suffix of `_build_script`.
86        crate_features (list, optional): A list of features to enable for the build script.
87        version (str, optional): The semantic version (semver) of the crate.
88        deps (list, optional): The dependencies of the crate.
89        build_script_env (dict, optional): Environment variables for build scripts.
90        data (list, optional): Files needed by the build script.
91        tools (list, optional): Tools (executables) needed by the build script.
92        links (str, optional): Name of the native library this crate links against.
93        rustc_env (dict, optional): Environment variables to set in rustc when compiling the build script.
94        rustc_flags (list, optional): List of compiler flags passed to `rustc`.
95        visibility (list of label, optional): Visibility to apply to the generated build script output.
96        tags: (list of str, optional): Tags to apply to the generated build script output.
97        **kwargs: Forwards to the underlying `rust_binary` rule. An exception is the `compatible_with`
98            attribute, which shouldn't be forwarded to the `rust_binary`, as the `rust_binary` is only
99            built and used in `exec` mode. We propagate the `compatible_with` attribute to the `_build_scirpt_run`
100            target.
101    """
102
103    # This duplicates the code in _cargo_build_script_impl because we need to make these
104    # available both when we invoke rustc (this code) and when we run the compiled build
105    # script (_cargo_build_script_impl). https://github.com/bazelbuild/rules_rust/issues/661
106    # will hopefully remove this duplication.
107    rustc_env = dict(rustc_env)
108    if "CARGO_PKG_NAME" not in rustc_env:
109        rustc_env["CARGO_PKG_NAME"] = name_to_pkg_name(name)
110    if "CARGO_CRATE_NAME" not in rustc_env:
111        rustc_env["CARGO_CRATE_NAME"] = name_to_crate_name(name_to_pkg_name(name))
112
113    binary_tags = [tag for tag in tags or []]
114    if "manual" not in binary_tags:
115        binary_tags.append("manual")
116    build_script_kwargs = {}
117    binary_kwargs = kwargs
118    if "compatible_with" in kwargs:
119        build_script_kwargs["compatible_with"] = kwargs["compatible_with"]
120        binary_kwargs.pop("compatible_with")
121
122    rust_binary(
123        name = name + "_",
124        crate_features = crate_features,
125        version = version,
126        deps = deps,
127        data = data,
128        rustc_env = rustc_env,
129        rustc_flags = rustc_flags,
130        tags = binary_tags,
131        **binary_kwargs
132    )
133    _build_script_run(
134        name = name,
135        script = ":{}_".format(name),
136        crate_features = crate_features,
137        version = version,
138        build_script_env = build_script_env,
139        links = links,
140        deps = deps,
141        data = data,
142        tools = tools,
143        rustc_flags = rustc_flags,
144        visibility = visibility,
145        tags = tags,
146        **build_script_kwargs
147    )