Skip to content

build-rs crate API design #14816

Open
Open
@kornelski

Description

@kornelski

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 and pkg-config crates have an option to disable printing of cargo: 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 from Result-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 use library.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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-build-scriptsArea: build.rs scriptsS-needs-designStatus: Needs someone to work further on the design for the feature or fix. NOT YET accepted.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions