问题
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