Skip to content

Commit

Permalink
gdal-sys build for detected version (pkg-config)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdroenner committed Sep 25, 2020
1 parent 3ed5656 commit 87c59f5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
4 changes: 4 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ fn main() {
detected_version.major, detected_version.minor, detected_version.patch
);

println!("cargo:rustc-cfg=major_is_{}", detected_version.major);
println!("cargo:rustc-cfg=minor_is_{}", detected_version.minor);
println!("cargo:rustc-cfg=patch_is_{}", detected_version.patch);

// we only support GDAL >= 2.0.
for major in 2..=detected_version.major {
println!("cargo:rustc-cfg=major_ge_{}", major);
Expand Down
11 changes: 1 addition & 10 deletions gdal-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,10 @@ repository = "https://github.com/georust/gdal"
authors = ["Johannes Drönner <[email protected]>"]
edition = "2018"

[features]
default = ["min_gdal_version_1_11"]
min_gdal_version_1_11 = []
min_gdal_version_2_0 = []
min_gdal_version_2_1 = []
min_gdal_version_2_2 = []
min_gdal_version_2_3 = []
min_gdal_version_2_4 = []
min_gdal_version_3_0 = []

[dependencies]
libc = "0.2"

[build-dependencies]
bindgen = { version = "0.54", optional = true }
pkg-config = "0.3"
semver = "0.11"
45 changes: 26 additions & 19 deletions gdal-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(feature = "bindgen")]
use bindgen;
use pkg_config;
use semver::Version;

use pkg_config::Config;
use std::env;
Expand Down Expand Up @@ -59,9 +59,11 @@ fn find_gdal_dll(lib_dir: &Path) -> io::Result<Option<String>> {

fn main() {
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");
println!("cargo:rerun-if-env-changed=GDAL_LIB_DIR");
println!("cargo:rerun-if-env-changed=GDAL_HOME");
println!("cargo:rerun-if-env-changed=GDAL_VERSION");

let mut need_metadata = true;
let mut lib_name = String::from("gdal");
Expand All @@ -72,6 +74,9 @@ fn main() {
let mut include_dir = env_dir("GDAL_INCLUDE_DIR");
let mut lib_dir = env_dir("GDAL_LIB_DIR");
let home_dir = env_dir("GDAL_HOME");
let mut version = env::var_os("GDAL_VERSION")
.map(|vs| vs.to_string_lossy().to_string())
.and_then(|vs| Version::parse(&vs).ok());

let mut found = false;
if cfg!(windows) {
Expand Down Expand Up @@ -166,30 +171,32 @@ fn main() {
for dir in gdal.include_paths {
include_paths.push(dir.to_str().unwrap().to_string());
}
if version.is_none() {
version = Version::parse(&gdal.version).ok();
}
}

#[cfg(feature = "bindgen")]
write_bindings(include_paths, &out_path);

#[cfg(not(feature = "bindgen"))]
{
let prebuilt_paths = &[
#[cfg(feature = "min_gdal_version_1_11")]
"prebuilt-bindings/gdal_1.11.rs",
#[cfg(feature = "min_gdal_version_2_0")]
"prebuilt-bindings/gdal_2.0.rs",
#[cfg(feature = "min_gdal_version_2_1")]
"prebuilt-bindings/gdal_2.1.rs",
#[cfg(feature = "min_gdal_version_2_2")]
"prebuilt-bindings/gdal_2.2.rs",
#[cfg(feature = "min_gdal_version_2_3")]
"prebuilt-bindings/gdal_2.3.rs",
#[cfg(feature = "min_gdal_version_2_4")]
"prebuilt-bindings/gdal_2.4.rs",
#[cfg(feature = "min_gdal_version_3_0")]
"prebuilt-bindings/gdal_3.0.rs",
];
std::fs::copy(&prebuilt_paths[prebuilt_paths.len() - 1], &out_path)
.expect("Can't copy bindings to output directory");
if let Some(version) = version {
println!(
"cargo:rustc-cfg=gdal_sys_{}_{}_{}",
version.major, version.minor, version.patch
);

let binding_path = PathBuf::from(format!(
"prebuilt-bindings/gdal_{}.{}.rs",
version.major, version.minor
));
if !binding_path.exists() {
panic!("No pre-build binding available for this GDAl version.");
}

std::fs::copy(&binding_path, &out_path)
.expect("Can't copy bindings to output directory");
}
}
}

0 comments on commit 87c59f5

Please sign in to comment.