用货物构建子模块二进制箱

Building submodule binary crates with cargo

我有以下 crate 结构:

|── proj/
    └── src/
        └── bin/
            └── foo-bin-rs/
                └── src/
                    └── main.rs
                └── Cargo.toml
        └── main.rs
    └── Cargo.toml
    └── build.rs

foo-bin-rs 是一个子模块。我想找到一种干净的方式来发布构建 通过将构建 foo-bin-rs 作为构建的一部分的货物进行命令 用于 proj 的命令。我没有找到任何使用 src/bin 包含二进制文件的目录,它们是自己单独的箱子,只是 单个文件。我的第一个想法是有一个 build.rs 发布自己的 cargo 命令,但我找不到允许通过的货物标志 用作 root 的目录。对此的规范解决方案是什么?

My first thought was to have a build.rs that issued its own cargo command, but I couldn't find a flag for cargo that allowed passing a directory to use as root.

有命令行参数,但不需要目录;相反,它需要 Cargo.toml 文件的完整路径。该参数名为 --manifest-path,它可用于许多子命令,例如 buildrun。是这样使用的(注意相对路径也是有效的):

$ cargo build --manifest-path=/path/to/proj/src/bin/foo-bin-rs/Cargo.toml

如果您需要 运行 构建脚本中的可执行文件,您可以像往常一样简单地使用 cargo run 构建和 运行 一次性完成。