fork/rules_rust

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

UebelAndreAdded support for compact windows repository names, avoiding MAX_PATH (#3023)8e962a16

main
12.2 KiB353 linesraw
1"""The `cargo_bootstrap` rule is used for bootstrapping cargo binaries in a repository rule."""
2
3load("//cargo/private:cargo_utils.bzl", "get_rust_tools")
4load("//rust:defs.bzl", "rust_common")
5load("//rust/platform:triple.bzl", "get_host_triple")
6
7_CARGO_BUILD_MODES = [
8    "release",
9    "debug",
10]
11
12_FAIL_MESSAGE = """\
13Process exited with code '{code}'
14# ARGV ########################################################################
15{argv}
16
17# STDOUT ######################################################################
18{stdout}
19
20# STDERR ######################################################################
21{stderr}
22"""
23
24def cargo_bootstrap(
25        repository_ctx,
26        cargo_bin,
27        rustc_bin,
28        binary,
29        cargo_manifest,
30        cargo_config = None,
31        environment = {},
32        quiet = False,
33        build_mode = "release",
34        target_dir = None,
35        timeout = 600):
36    """A function for bootstrapping a cargo binary within a repository rule
37
38    Args:
39        repository_ctx (repository_ctx): The rule's context object.
40        cargo_bin (path): The path to a Cargo binary.
41        rustc_bin (path): The path to a Rustc binary.
42        binary (str): The binary to build (the `--bin` parameter for Cargo).
43        cargo_manifest (path): The path to a Cargo manifest (Cargo.toml file).
44        cargo_config (path, optional): The path to a Cargo configuration file (Config.toml) to use.
45        environment (dict): Environment variables to use during execution.
46        quiet (bool, optional): Whether or not to print output from the Cargo command.
47        build_mode (str, optional): The build mode to use
48        target_dir (path, optional): The directory in which to produce build outputs
49            (Cargo's --target-dir argument).
50        timeout (int, optional): Maximum duration of the Cargo build command in seconds,
51
52    Returns:
53        path: The path of the built binary within the target directory
54    """
55
56    if not target_dir:
57        target_dir = repository_ctx.path(".")
58
59    if cargo_config:
60        repository_ctx.symlink(cargo_config, repository_ctx.path(".cargo/config.toml"))
61
62    args = [
63        cargo_bin,
64        "build",
65        "--bin",
66        binary,
67        "--locked",
68        "--target-dir",
69        target_dir,
70        "--manifest-path",
71        cargo_manifest,
72    ]
73
74    if build_mode not in _CARGO_BUILD_MODES:
75        fail("'{}' is not a supported build mode. Use one of {}".format(build_mode, _CARGO_BUILD_MODES))
76
77    if build_mode == "release":
78        args.append("--release")
79
80    env = dict({
81        "RUSTC": str(rustc_bin),
82    }.items() + environment.items())
83
84    repository_ctx.report_progress("Cargo Bootstrapping {}".format(binary))
85    result = repository_ctx.execute(
86        args,
87        environment = env,
88        quiet = quiet,
89        timeout = timeout,
90    )
91
92    if result.return_code != 0:
93        fail(_FAIL_MESSAGE.format(
94            code = result.return_code,
95            argv = args,
96            stdout = result.stdout,
97            stderr = result.stderr,
98        ))
99
100    extension = ""
101    if "win" in repository_ctx.os.name:
102        extension = ".exe"
103
104    binary_path = "{}/{}{}".format(
105        build_mode,
106        binary,
107        extension,
108    )
109
110    if not repository_ctx.path(binary_path).exists:
111        fail("Failed to produce binary at {}".format(binary_path))
112
113    return binary_path
114
115_BUILD_FILE_CONTENT = """\
116load("@rules_rust//rust:defs.bzl", "rust_binary")
117
118package(default_visibility = ["//visibility:public"])
119
120exports_files([
121    "{binary_name}",
122    "{binary}"
123])
124
125alias(
126    name = "binary",
127    actual = "{binary}",
128)
129
130rust_binary(
131    name = "install",
132    rustc_env = {{
133        "RULES_RUST_CARGO_BOOTSTRAP_BINARY": "$(rootpath {binary})"
134    }},
135    data = [
136        "{binary}",
137    ],
138    srcs = [
139        "@rules_rust//cargo/bootstrap:bootstrap_installer.rs"
140    ],
141)
142"""
143
144def _collect_environ(repository_ctx, host_triple):
145    """Gather environment varialbes to use from the current rule context
146
147    Args:
148        repository_ctx (repository_ctx): The rule's context object.
149        host_triple (str): A string of the current host triple
150
151    Returns:
152        dict: A map of environment variables
153    """
154    env_vars = dict(json.decode(repository_ctx.attr.env.get(host_triple, "{}")))
155
156    # Gather the path for each label and ensure it exists
157    env_labels = dict(json.decode(repository_ctx.attr.env_label.get(host_triple, "{}")))
158    env_labels = {key: repository_ctx.path(Label(value)) for (key, value) in env_labels.items()}
159    for key in env_labels:
160        if not env_labels[key].exists:
161            fail("File for key '{}' does not exist: {}", key, env_labels[key])
162    env_labels = {key: str(value) for (key, value) in env_labels.items()}
163
164    return dict(env_vars.items() + env_labels.items())
165
166def _detect_changes(repository_ctx):
167    """Inspect files that are considered inputs to the build for changes
168
169    Args:
170        repository_ctx (repository_ctx): The rule's context object.
171    """
172    # Simply generating a `path` object consideres the file as 'tracked' or
173    # 'consumed' which means changes to it will trigger rebuilds
174
175    for src in repository_ctx.attr.srcs:
176        repository_ctx.path(src)
177
178    repository_ctx.path(repository_ctx.attr.cargo_lockfile)
179    repository_ctx.path(repository_ctx.attr.cargo_toml)
180
181    if repository_ctx.attr.cargo_config:
182        repository_ctx.path(repository_ctx.attr.cargo_config)
183
184def _cargo_bootstrap_repository_impl(repository_ctx):
185    # Pretend to Bazel that this rule's input files have been used, so that it will re-run the rule if they change.
186    _detect_changes(repository_ctx)
187
188    # Expects something like `1.56.0`, or `nightly/2021-09-08`.
189    version, _, iso_date = repository_ctx.attr.version.partition("/")
190    if iso_date:
191        channel = version
192        version = iso_date
193    else:
194        channel = "stable"
195
196    host_triple = get_host_triple(repository_ctx)
197    cargo_template = repository_ctx.attr.rust_toolchain_cargo_template
198    rustc_template = repository_ctx.attr.rust_toolchain_rustc_template
199    compress_windows_names = repository_ctx.attr.compressed_windows_toolchain_names
200
201    tools = get_rust_tools(
202        cargo_template = cargo_template,
203        rustc_template = rustc_template,
204        host_triple = host_triple,
205        channel = channel,
206        version = version,
207        compress_windows_names = compress_windows_names,
208    )
209
210    binary_name = repository_ctx.attr.binary or repository_ctx.name
211
212    # In addition to platform specific environment variables, a common set (indicated by `*`) will always
213    # be gathered.
214    environment = dict(_collect_environ(repository_ctx, "*").items() + _collect_environ(repository_ctx, host_triple.str).items())
215
216    cargo_config = None
217    if repository_ctx.attr.cargo_config:
218        cargo_config = repository_ctx.path(repository_ctx.attr.cargo_config)
219
220    built_binary = cargo_bootstrap(
221        repository_ctx = repository_ctx,
222        cargo_bin = repository_ctx.path(tools.cargo),
223        rustc_bin = repository_ctx.path(tools.rustc),
224        cargo_config = cargo_config,
225        binary = binary_name,
226        cargo_manifest = repository_ctx.path(repository_ctx.attr.cargo_toml),
227        build_mode = repository_ctx.attr.build_mode,
228        environment = environment,
229        timeout = repository_ctx.attr.timeout,
230    )
231
232    # Create a symlink so that the binary can be accesed via it's target name
233    repository_ctx.symlink(built_binary, binary_name)
234
235    repository_ctx.file("BUILD.bazel", _BUILD_FILE_CONTENT.format(
236        binary_name = binary_name,
237        binary = built_binary,
238    ))
239
240cargo_bootstrap_repository = repository_rule(
241    doc = "A rule for bootstrapping a Rust binary using [Cargo](https://doc.rust-lang.org/cargo/)",
242    implementation = _cargo_bootstrap_repository_impl,
243    attrs = {
244        "binary": attr.string(
245            doc = "The binary to build (the `--bin` parameter for Cargo). If left empty, the repository name will be used.",
246        ),
247        "build_mode": attr.string(
248            doc = "The build mode the binary should be built with",
249            values = [
250                "debug",
251                "release",
252            ],
253            default = "release",
254        ),
255        "cargo_config": attr.label(
256            doc = "The path of the Cargo configuration (`Config.toml`) file.",
257            allow_single_file = True,
258        ),
259        "cargo_lockfile": attr.label(
260            doc = "The lockfile of the crate_universe resolver",
261            allow_single_file = ["Cargo.lock"],
262            mandatory = True,
263        ),
264        "cargo_toml": attr.label(
265            doc = "The path of the `Cargo.toml` file.",
266            allow_single_file = ["Cargo.toml"],
267            mandatory = True,
268        ),
269        "compressed_windows_toolchain_names": attr.bool(
270            doc = "Wether or not the toolchain names of windows toolchains are expected to be in a `compressed` format.",
271            default = True,
272        ),
273        "env": attr.string_dict(
274            doc = (
275                "A mapping of platform triple to a set of environment variables. See " +
276                "[cargo_env](#cargo_env) for usage details. Additionally, the platform triple `*` applies to all platforms."
277            ),
278        ),
279        "env_label": attr.string_dict(
280            doc = (
281                "A mapping of platform triple to a set of environment variables. This " +
282                "attribute differs from `env` in that all variables passed here must be " +
283                "fully qualified labels of files. See [cargo_env](#cargo_env) for usage details. " +
284                "Additionally, the platform triple `*` applies to all platforms."
285            ),
286        ),
287        "rust_toolchain_cargo_template": attr.string(
288            doc = (
289                "The template to use for finding the host `cargo` binary. `{version}` (eg. '1.53.0'), " +
290                "`{triple}` (eg. 'x86_64-unknown-linux-gnu'), `{arch}` (eg. 'aarch64'), `{vendor}` (eg. 'unknown'), " +
291                "`{system}` (eg. 'darwin'), `{channel}` (eg. 'stable'), and `{tool}` (eg. 'rustc.exe') will be " +
292                "replaced in the string if present."
293            ),
294            default = "@rust_{system}_{arch}__{triple}__{channel}_tools//:bin/{tool}",
295        ),
296        "rust_toolchain_rustc_template": attr.string(
297            doc = (
298                "The template to use for finding the host `rustc` binary. `{version}` (eg. '1.53.0'), " +
299                "`{triple}` (eg. 'x86_64-unknown-linux-gnu'), `{arch}` (eg. 'aarch64'), `{vendor}` (eg. 'unknown'), " +
300                "`{system}` (eg. 'darwin'), `{channel}` (eg. 'stable'), and `{tool}` (eg. 'rustc.exe') will be " +
301                "replaced in the string if present."
302            ),
303            default = "@rust_{system}_{arch}__{triple}__{channel}_tools//:bin/{tool}",
304        ),
305        "srcs": attr.label_list(
306            doc = "Souce files of the crate to build. Passing source files here can be used to trigger rebuilds when changes are made",
307            allow_files = True,
308        ),
309        "timeout": attr.int(
310            doc = "Maximum duration of the Cargo build command in seconds",
311            default = 600,
312        ),
313        "version": attr.string(
314            doc = "The version of Rust the currently registered toolchain is using. Eg. `1.56.0`, or `nightly/2021-09-08`",
315            default = rust_common.default_version,
316        ),
317    },
318)
319
320def cargo_env(env):
321    """A helper for generating platform specific environment variables
322
323    ```python
324    load("@rules_rust//rust:defs.bzl", "rust_common")
325    load("@rules_rust//cargo:defs.bzl", "cargo_bootstrap_repository", "cargo_env")
326
327    cargo_bootstrap_repository(
328        name = "bootstrapped_bin",
329        cargo_lockfile = "//:Cargo.lock",
330        cargo_toml = "//:Cargo.toml",
331        srcs = ["//:resolver_srcs"],
332        version = rust_common.default_version,
333        binary = "my-crate-binary",
334        env = {
335            "x86_64-unknown-linux-gnu": cargo_env({
336                "FOO": "BAR",
337            }),
338        },
339        env_label = {
340            "aarch64-unknown-linux-musl": cargo_env({
341                "DOC": "//:README.md",
342            }),
343        }
344    )
345    ```
346
347    Args:
348        env (dict): A map of environment variables
349
350    Returns:
351        str: A json encoded string of the environment variables
352    """
353    return json.encode(dict(env))