当本机库不存在时,如何构建FFI板条箱的docs.rs文档? [英] How to build the docs.rs documentation of an FFI crate when the native library is not present?
问题描述
我有一个静态链接到库的 sys板条箱:
I have a "sys" crate that links statically to a library:
Cargo.toml:
Cargo.toml:
[package]
links = "foo-1.0"
build.rs:
fn main() {
println!("cargo:rustc-link-lib=dylib=foo-1.0");
}
发布软件包时,docs.rs无法生成文档,因为libfoo是未安装:
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?
推荐答案
关于docs.rs 页的信息提供了有关此信息的更多信息。实际上,您可以在docs.rs文档生成期间启用特定功能(除其他外)。一个示例很好地说明了如何实现:
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
期间启用然后,可以在 build.rs
中禁用链接:
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");
}
这篇关于当本机库不存在时,如何构建FFI板条箱的docs.rs文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!