Description
The #12432 issue adopted the build-rs
crate, but I haven't seen discussion on how its API should look like.
The API of the already-released versions happens to be a very direct mapping of functions to the existing printed cargo:…
directives. This has an advantage of being immediately familiar to Rust/Cargo users, but because the API keeps using global state and emitting Cargo directives as a side effect, it remains difficult to coordinate interaction with Cargo across build-time dependencies.
-
Build-time dependencies have a dilemma whether they should emit
cargo:
directives automatically to have a convenient robust API, or not, and let the caller decide what to emit.cc
andpkg-config
crates have an option to disable printing ofcargo:
directives, but this is all-or-nothing, and it's not easy for callers to customize and filter the directives. -
Build-time dependencies can't emit
cargo:error
fromResult
-based APIs, because the caller of the function could recover from the error. However, this prevents libraries from emitting well-formatted Cargo errors when their callers uselibrary.call().unwrap()
. -
Build-time dependencies can't emit
rerun-if-changed
by default, because presence of any such directive disables Cargo's standard heuristics. This needs to be configured on case-by-case basis for each build dependency, because there's no standard way for build scripts to specify whether they want the standard heuristics or not. -
The global stateless functions don't have ability to be customized in the future, unless the configuration would be global as well. However, global configuration doesn't compose well, so build-time dependencies would struggle to use it without causing issues for other parts of the build script.
-
Cargo doesn't have first-class support for handling build scripts failing via a panic. Cargo just gets usual rustc output as an opaque text, which isn't formatted like a native Cargo error (with color output, etc.), and includes a panic stack trace that is irrelevant for most build scripts, especially sys crates.
Some of these issues could be solved by build dependencies themselves offering richer APIs, but I think there's an opportunity for the build-rs
crate to improve Cargo's interface and offer a standard way of composing build dependencies.