Skip to content

Commit

Permalink
Merge #281
Browse files Browse the repository at this point in the history
281: Fix crate build on docs.rs r=rmanoka a=rmanoka

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

- [X] I agree to follow the project's [code of conduct](https://github.com/georust/gdal/blob/master/CODE_OF_CONDUCT.md).
- [X] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users.
---



Co-authored-by: Rajsekar Manokaran <[email protected]>
  • Loading branch information
bors[bot] and rmanoka authored Aug 24, 2022
2 parents 921f2ac + 377a3fa commit 2093998
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 19 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

- Add prebuild bindings for GDAL 3.5
- <https://github.com/georust/gdal/pull/277>

- **Breaking**: Add `gdal::vector::OwnedLayer`, `gdal::vector::LayerAccess` and `gdal::vector::layer::OwnedFeatureIterator`. This requires importing `gdal::vector::LayerAccess` for using most vector layer methods.

- https://github.com/georust/gdal/pull/238
Expand Down
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,3 @@ trybuild = "1.0"

[workspace]
members = ["gdal-sys"]

[package.metadata.docs.rs]
rustc-args = ["--cfg", "docsrs"]
18 changes: 3 additions & 15 deletions build.rs
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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");
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
Original file line number Diff line number Diff line change
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"));

0 comments on commit 2093998

Please sign in to comment.