Add rule to materialize generated files into a `OUT_DIR`

Open
#4,196 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
45/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Quiet
Tech stack
rust
Domain
build-system

Research direction

Start by reading the existing BuildInfo.out_dir and cargo_dep_env(out_dir = ...) entry points, then compare how rust_bindgen exposes generated files. Done means a public general-purpose rule can map declared individual files into a Cargo-style OUT_DIR layout and expose that directory to a consuming Rust target, including nested paths.

Written by the indexing model from the issue text.

Description

Is your feature request related to a problem? Please describe.

Cargo crates sometimes expect generated files to exist under OUT_DIR, for example:

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

This is common for crates that normally use a build.rs script, including FFI/sys crates. In Bazel, those generated files may already be produced by other rules, such as rust_bindgen, genrules, protobuf/schema generators, or checked-in generated files.

Today rules_rust can expose an existing directory artifact as OUT_DIR via BuildInfo.out_dir / cargo_dep_env(out_dir = ...), but there does not seem to be a general public helper for taking individual Bazel-produced files and materializing them into the directory layout expected by Cargo-style Rust code.

Describe the solution you'd like

Add a small general-purpose rule that materializes declared files into a Cargo-style OUT_DIR directory and exposes that directory to a consuming Rust target.

For example:

cargo_out_dir(
    name = "bindings_out_dir",
    files = {
        "bindings.rs": ":bindings",
        "nested/config.txt": ":config",
    },
)

rust_library(
    name = "my_sys_crate",
    srcs = ["src/lib.rs"],
    deps = [":bindings_out_dir"],
)

The consuming crate could then use:

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

This would provide a general solution for crates that need an OUT_DIR layout without requiring a dummy build.rs.

This could also supersede #3184. That issue asks for rust_bindgen to place generated files into OUT_DIR, but the underlying problem is not specific to bindgen. A general rule would allow rust_bindgen output to be mapped to bindings.rs, while also supporting other generators and files.

Describe alternatives you've considered

One alternative is to add a bindgen-specific option such as use_out_dir to rust_bindgen. I think a general rule would be cleaner because OUT_DIR is a convention of the consuming Rust crate, not of bindgen specifically.

Another alternative is to use cargo_dep_env(out_dir = ...) directly. That works if the user already has a directory artifact with the exact expected layout, but it does not solve the common case where the user has individual generated files that need to be assembled into that layout.

A third alternative is to run a dummy build.rs just to copy files into OUT_DIR, but that adds unnecessary build-script machinery when Bazel has already produced the files explicitly.

Dominant language
Starlark
Stars
857
Forks
701
Avg merge
4d 7h
Merged PRs (30d)
46

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from bazelbuild/rules_rust

All issues in bazelbuild/rules_rust

Similar issues

More Build System issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.