4
votes

I am building a Rust crate that has transitive dependencies on a few *-sys crates wrapping native libraries. The *-sys crates use build.rs to build the native libraries with cmake, which is not supported in my environment.

I already have those native libraries prebuilt elsewhere in my project tree. I would like to override the build scripts to not run, and use the existing native libraries instead.

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"

Source: Cargo Reference > Build Scripts

My initial guess based on that documentation is that I would just need to add rustc-link-lib when declaring the dependency, but this doesn't seem to work.

[package]
# ...

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

Cargo still tries to invoke build.rs and fails.

Is there a correct way to override harfbuzz-sys's build.rs for all of its transitive dependents in my project?

1

1 Answers

5
votes

You need to put the override information into one of the Cargo configuration files. For example for harfbuzz-sys, you could put this into .cargo/config inside your workspace:

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

Note that on the first line:

  • machine-vendor-os must be the same as the value that you give to cargo with the --target option.
  • harfbuzz must be the same as the links key defined in your dependency's Cargo.toml.

And on the second line, /path/to/staging/usr/lib is the path where your pre-compiled dependency is located on the build system.