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

旧城冷巷雨未停 提交于 2019-12-11 07:46:17

问题


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?


回答1:


The about page of docs.rs gives more information about that. You can actually enable a specific feature (among other things) during the docs.rs doc generation. An example explains well how to achieve that:

Cargo.toml:

[features]
docs-rs = []

[package.metadata.docs.rs]
features = [ "docs-rs" ] # This feature will be enabled during the docs.rs build

And then, the linking can be disabled in build.rs:

#[cfg(feature = "docs-rs")]
fn main() {} // Skip the script when the doc is building

#[cfg(not(feature = "docs-rs"))]
fn main() {
    println!("cargo:rustc-link-lib=dylib=foo-1.0");
}


来源:https://stackoverflow.com/questions/57158261/how-to-build-the-docs-rs-documentation-of-an-ffi-crate-when-the-native-library-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!