9

I want to write a custom build.rs script that generates some diagrams to accompany the documentation for a crate I'm working on. I want this script to run only when I run cargo doc, not the other profiles (cargo build, cargo test, ...). What would be the best way to do that?

I was hoping that cargo would pass this info to build.rs in the PROFILE env variable, but that seems to only contain "debug" or "release".

adam
  • 791
  • 6
  • 21
  • I don't think the `build.rs` scripts are meant to handle this kind of logic. They are meant to provide everything needed to build the library/binary I don't think they are even meant for tests/benches (use another crate + `build.rs` for that). What exactly do you want to do only for `cargo doc`? – oli_obk Apr 06 '16 at 13:33
  • I want to generate some images that contains some schematics (UML diagrams basically) and include them in the docs. The images are generated from other format and I'd like it to happen automatically on `cargo doc`. I don't insist on using build.rs if there is a better way to do it. – adam Apr 06 '16 at 15:43
  • `build.rs` is intended for `cargo build` and `cargo doc` runs `cargo build` before generating documentation. `cargo build` needs to generate the same thing whoever calls it. That means `build.rs` won't be able to do exactly what you want, it will only be able to always generate the diagrams even if `cargo doc` will not be run afterwards. AFAIK there is nothing in cargo currently that can do what you want, perhaps file an issue with a feature request for a `doc.rs`? – Jan Zerebecki Feb 24 '17 at 16:12
  • 1
    Possible duplicate of [How to generate the documentation of an FFI crate when the native library is not present?](https://stackoverflow.com/questions/57158261/how-to-generate-the-documentation-of-an-ffi-crate-when-the-native-library-is-not) – Stargateur Jul 31 '19 at 19:00

1 Answers1

4

This is not possible as of Rust 1.47. Cargo issue #4001 tracks the possibility of supporting this in some fashion.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366