'How to build the docs.rs documentation of an FFI crate when the native library is not present?

I have a "sys" crate that links statically to a library:

Cargo.toml:

[package]
links = "foo-1.0"

build.rs:

fn main() {
    println!("cargo:rustc-link-lib=dylib=foo-1.0");
}

When I publish the package, docs.rs cannot generate the documentation because libfoo is not installed:

error: failed to run custom build command for `foo-sys v0.0.1`

Caused by:
  process didn't exit successfully: `/home/cratesfyi/cratesfyi/debug/build/foo-sys-f4bd3ee95677500b/build-script-build` (exit code: 1)
--- stderr
`"pkg-config" "--libs" "--cflags" "foo-1.0 >= 1.0"` did not exit successfully: exit code: 1
--- stderr

How can I configure my crate so that the doc is generated without the library being installed?



Solution 1:[1]

The about/build page of docs.rs gives more information about that. docs.rs will set an environment variable DOCS_RS.

The linking can be disabled in build.rs:

fn main() {
    if std::env::var("DOCS_RS").is_ok() {
        println!("cargo:rustc-link-lib=dylib=foo-1.0");
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stargateur