I have a custom build script in a sys crate that optionally builds a static library or links to an existing library, depending on a --features flag. I don't provide a default because I am following @kornel 's advice from Using C libraries in Rust: make a sys crate i.e. "Don't put any of them as Cargo's default feature, because it's too hard to unset defaults in Cargo."
So, that means by build.rs script fails if neither the static nor the dynamic feature is set. The problem is that docs.rs build doesn't set any features, even if you cargo publish --features .... So the docs build fails and no docs get published on docs.rs.
So, how do I detect that it's building documentation in the build.rs script so I can disable the C build steps?
What you said stands to reason, but sadly it doesn't appear to work. For debugging purposes, I outputted all environment vars from the build script and none of them appear to indicate that it's running from RustDoc.
I believe RustDoc builds the crate in two passes, and the first pass is an ordinary debug build without the cfg set. Perhaps to expand macros. (just my guess) Then the pass with #[cfg(doc)] set appears to come next.
But the linked issue there implies that you only don't get --cfg doc when it's being built as a dependency? But then it talks about cargo rustdoc vs cargo doc behavior and I've now gotten very confused.