覆盖依赖项的构建脚本

Override build script for a dependency

我正在构建一个 Rust 板条箱,它对一些 *-sys 包装本机库的板条箱具有传递依赖性。 *-sys crates 使用 build.rs 通过 cmake 构建本机库,这在我的环境中不受支持。

我已经在我的项目树的其他地方预先构建了这些本地库。我想 override the build scripts 而不是 运行,而是使用现有的本机库。

If a manifest contains a links key, then Cargo supports overriding the build script specified with a custom library. The purpose of this functionality is to prevent running the build script in question altogether and instead supply the metadata ahead of time.

To override a build script, place the following configuration in any acceptable Cargo configuration location.

[target.x86_64-unknown-linux-gnu.foo]
rustc-link-search = ["/path/to/foo"]
rustc-link-lib = ["foo"]
root = "/path/to/foo"
key = "value"

来源:Cargo Reference > Build Scripts

根据该文档,我最初的猜测是我只需要在声明依赖项时添加 rustc-link-lib,但这似乎不起作用。

[package]
# ...

[dependencies]
# ...
harfbuzz-sys = { version = "0.3", rustc-link-lib = ["harfbuzz"] }
# ...

Cargo 仍然尝试调用 build.rs 但失败了。

是否有正确的方法来为我的项目中的所有传递依赖覆盖 harfbuzz-sysbuild.rs

您需要将覆盖信息放入 Cargo configuration files 之一。例如,对于 harfbuzz-sys,您可以将其放入工作区内的 .cargo/config

[target.machine-vendor-os.harfbuzz]
rustc-link-search = ["/path/to/staging/usr/lib"]
rustc-link-lib = ["harfbuzz"]

注意第一行:

  • machine-vendor-os 必须与您使用 --target 选项赋予货物的价值相同。
  • harfbuzz 必须与 your dependency's Cargo.toml.
  • 中定义的 links 键相同

而在第二行,/path/to/staging/usr/lib是你的预编译依赖在构建系统中的路径。