Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crate build on docs.rs #281

Merged
merged 4 commits into from Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 3 additions & 15 deletions build.rs
@@ -1,23 +1,11 @@
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:
Expand Down
30 changes: 29 additions & 1 deletion gdal-sys/build.rs
Expand Up @@ -57,13 +57,40 @@ fn find_gdal_dll(lib_dir: &Path) -> io::Result<Option<String>> {
Ok(None)
}

fn add_docs_rs_helper(version: Option<&str>) {
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("docs_rs_helper.rs");
let docs_helper_code = if let Some(version) = version {
format!(
r#"
pub fn gdal_version_docs_rs_wrapper() -> String {{
"{version}".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()
}
}"#
.to_string()
};
std::fs::write(&out_path, docs_helper_code.as_bytes()).unwrap();
}

fn main() {
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("bindings.rs");

// Hardcode a prebuilt binding version while generating docs.
// Otherwise docs.rs will explode due to not actually having libgdal installed.
if std::env::var("DOCS_RS").is_ok() {
let version = Version::parse("3.2.0").expect("invalid version for docs.rs");
let version = Version::parse("3.5.0").expect("invalid version for docs.rs");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: this seems a bit buried here, what about pulling the version out as global variables at the top, to make them easier to maintain as new versions are released?

At top:

// Set the following to the latest released version of GDAL for use by docs.rs
const DOCS_RS_GDAL_VERSION: &str = "3.5.0";
const DOCS_RS_GDAL_VERSION_NUM: &str = "3050000";

Here

let version = Version::parse(DOCS_RS_GDAL_VERSION).expect(...);
add_docs_rs_helper(Some(DOCS_RS_GDAL_VERSION_NUM));

Also - is there a way to programmatically derive the "3050000" value from "3.5.0", or does it vary somewhat independently? (presumably the major / minor parts of it follow the semver components). If the latter, might be good to include a comment on where to find this value; I didn't find it in the prebuilt bindings for 3.5, so it doesn't seem easily obvious for future versions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The number is defined like so in gdal source:

/* GDAL_COMPUTE_VERSION macro introduced in GDAL 1.10 */
/* Must be used ONLY to compare with version numbers for GDAL >= 1.10 */
#ifndef GDAL_COMPUTE_VERSION
#define GDAL_COMPUTE_VERSION(maj,min,rev) ((maj)*1000000+(min)*10000+(rev)*100)
#endif

We'll probably refactor the version handling soon-ish as the recent gdal versions are not strictly semver (like 3.5.0.2). The semver crate seems to fail on these at present.

add_docs_rs_helper(Some("3050000"));
println!(
"cargo:rustc-cfg=gdal_sys_{}_{}_{}",
version.major, version.minor, version.patch
Expand All @@ -83,6 +110,7 @@ fn main() {
return;
}

add_docs_rs_helper(None);
println!("cargo:rerun-if-env-changed=GDAL_STATIC");
println!("cargo:rerun-if-env-changed=GDAL_DYNAMIC");
println!("cargo:rerun-if-env-changed=GDAL_INCLUDE_DIR");
Expand Down
1 change: 1 addition & 0 deletions gdal-sys/src/lib.rs
Expand Up @@ -4,3 +4,4 @@
#![allow(clippy::upper_case_acronyms)]

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
include!(concat!(env!("OUT_DIR"), "/docs_rs_helper.rs"));