Skip to content

Only use metadata for current target #675

@jonhoo

Description

@jonhoo

Unlike most other cargo tools, cargo metadata counter-intuitively defaults to downloading all dependencies for all targets unless the argument --filter-platform is passed. This means that with the current logic, cbindgen ends up downloading potentially a very large number of dependencies whenever this is invoked:
https://github.com/eqrion/cbindgen/blob/93eed3aaf3a6ed94ae5bdf59e5df4c508fe05a5b/src/bindgen/cargo/cargo_metadata.rs#L189-L194

Since cbindgen doesn't actually need the dependencies themselves (I don't think), I believe cbindgen should probably always specify --filter-platform, and just specify the host platform. Cargo finds the host platform by running rustc -Vv and extracing the host: line, which cbindgen could probably also do pretty easily:
https://github.com/rust-lang/cargo/blob/fde8ee3ba0cba4eef75f197d42586ef4664f057b/src/cargo/util/rustc.rs#L48-L68

This is also what rust-analyzer recently switch to doing: rust-lang/rust-analyzer#6912

There's also precedent for relying on rustc to determine these kinds of things, such as using it to find the sysroot:
https://github.com/rust-analyzer/rust-analyzer/blob/48802e54d1669c68fd27b007533dd6aa5221d089/crates/project_model/src/sysroot.rs#L126-L127

One way to make this nicer would be if cargo metadata supported a "special" value for --filter-platform called host. I'll look at submitting that as a PR upstream, but the more immediate fix (and also one that would continue to work for older compiler releases) would be to just use rustc -Vv.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions