Fix crate build on docs.rs
Use dynamic code-gen to build-around non-availabiltiy of some packages.
MODIFY
build.rs
build.rs
use std::str::FromStr;
-#[cfg(docsrs)]
-pub fn gdal_version_info(_key: &str) -> String {
- "3020000".to_string()
-}
-
-#[cfg(not(docsrs))]
-pub fn gdal_version_info(key: &str) -> String {
- let c_key = std::ffi::CString::new(key.as_bytes()).unwrap();
-
- unsafe {
- let res_ptr = gdal_sys::GDALVersionInfo(c_key.as_ptr());
- let c_res = std::ffi::CStr::from_ptr(res_ptr);
- c_res.to_string_lossy().into_owned()
- }
+fn gdal_version_info() -> String {
+ gdal_sys::gdal_version_docs_rs_wrapper()
}
fn main() {
- let gdal_version_string = gdal_version_info("VERSION_NUM");
+ let gdal_version_string = gdal_version_info();
println!("GDAL version string: \"{}\"", gdal_version_string);
// this version string is the result of:

MODIFY
gdal-sys/build.rs
gdal-sys/build.rs
@@ -57,7 +57,31 @@ fn find_gdal_dll(lib_dir: &Path) -> io::Result<Option<String>> {
Ok(None)
}
+fn add_docs_rs_helper() {
+ let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("docs_rs_helper.rs");
+ let docs_helper_code = if std::env::var_os("DOCS_RS").is_some() {
+ r#"
+ pub fn gdal_version_docs_rs_wrapper() -> String {
+ "3020000".to_string()
+ }"#
+ } else {
+ r#"
+ pub fn gdal_version_docs_rs_wrapper() -> String {
+ let key = "VERSION_NUM";
+ let c_key = std::ffi::CString::new(key.as_bytes()).unwrap();
+
+ unsafe {
+ let res_ptr = crate::GDALVersionInfo(c_key.as_ptr());
+ let c_res = std::ffi::CStr::from_ptr(res_ptr);
+ c_res.to_string_lossy().into_owned()
+ }
+ }"#
+ };
+ std::fs::write(&out_path, docs_helper_code.as_bytes()).unwrap();
+}
+
fn main() {
+ add_docs_rs_helper();
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("bindings.rs");
// Hardcode a prebuilt binding version while generating docs.
@@ -234,4 +258,5 @@ fn main() {
panic!("No GDAL version detected");
}
}
+
}

MODIFY
gdal-sys/src/lib.rs
gdal-sys/src/lib.rs
#![allow(clippy::upper_case_acronyms)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
+include!(concat!(env!("OUT_DIR"), "/docs_rs_helper.rs"));

Fix crate build on docs.rs

Use dynamic code-gen to build-around non-availabiltiy of some packages.