Add rule to materialize generated files into a `OUT_DIR`

未关闭
#4,196 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
45/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
冷清
技术栈
rust
领域
build-system

调研方向

首先阅读现有的入口点 BuildInfo.out_dir 和 cargo_dep_env(out_dir = ...),然后比较 rust_bindgen 如何提供生成的文件。完成的标准是:一个公开的通用规则能够将声明的单个文件映射到 Cargo 风格的 OUT_DIR 布局中,并将该目录提供给使用它的 Rust 目标,包括嵌套路径。

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Starlark
星标
857
派生
701
平均合并
4 天 7 小时
30 天内合并 PR
46

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

bazelbuild/rules_rust 的其他 Issue

查看 bazelbuild/rules_rust 的全部 Issue

相似的 Issue

更多 Build System Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。