Skip to content

Commit

Permalink
add support of vcpkg with msvc toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent PRELAT committed Mar 3, 2021
1 parent 77ecb97 commit b74dd9b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 22 deletions.
4 changes: 4 additions & 0 deletions proj-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ cmake = "0.1"
flate2 = "1.0.14"
tar = "0.4.26"

[target.'cfg(target_env = "msvc")'.build-dependencies]
vcpkg = "0.2.11"
winapi-build = "0.1.1"

[features]
nobuild = []
bundled_proj = []
Expand Down
76 changes: 54 additions & 22 deletions proj-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,66 @@ use cmake;
use flate2::read::GzDecoder;
use std::fs::File;

#[cfg(target_env = "msvc")]
use vcpkg;
#[cfg(target_env = "msvc")]
use build;
use pkg_config;
use std::env;
use std::path::PathBuf;
use tar::Archive;

const MINIMUM_PROJ_VERSION: &str = "7.2.1";

#[cfg(target_env = "msvc")]
fn try_vcpkg() -> Result<std::path::PathBuf, Box<dyn std::error::Error>> {
build::link("shell32", true);
build::link("ole32", true);
let lib = vcpkg::Config::new()
.emit_includes(true)
.find_package("proj");

if let Err(_e) = lib {
return try_pkg_config()
}

let include_path = lib.unwrap()
.include_paths[0]
.clone();

Ok(include_path)
}

#[cfg(not(target_env = "msvc"))]
fn try_vcpkg() -> Result<std::path::PathBuf, Box<dyn std::error::Error>> {
try_pkg_config()
}

fn try_pkg_config() -> Result<std::path::PathBuf, Box<dyn std::error::Error>> {
pkg_config::Config::new()
.atleast_version(MINIMUM_PROJ_VERSION)
.probe("proj")
.and_then(|pk| {
eprintln!("found acceptable libproj already installed at: {:?}", pk.link_paths[0]);
if let Ok(val) = &env::var("_PROJ_SYS_TEST_EXPECT_BUILD_FROM_SRC") {
if val != "0" {
panic!("for testing purposes: existing package was found, but should not have been");
}
}

// Tell cargo to tell rustc to link the system proj
// shared library.
println!("cargo:rustc-link-search=native={:?}", pk.link_paths[0]);
println!("cargo:rustc-link-lib=proj");

Ok(pk.include_paths[0].clone())
})
.or_else(|err| {
eprintln!("pkg-config unable to find existing libproj installation: {}", err);
build_from_source()
})
}

#[cfg(feature = "nobuild")]
fn main() {} // Skip the build script on docs.rs

Expand All @@ -19,28 +72,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
eprintln!("feature flags specified source build");
build_from_source()?
} else {
pkg_config::Config::new()
.atleast_version(MINIMUM_PROJ_VERSION)
.probe("proj")
.and_then(|pk| {
eprintln!("found acceptable libproj already installed at: {:?}", pk.link_paths[0]);
if let Ok(val) = &env::var("_PROJ_SYS_TEST_EXPECT_BUILD_FROM_SRC") {
if val != "0" {
panic!("for testing purposes: existing package was found, but should not have been");
}
}

// Tell cargo to tell rustc to link the system proj
// shared library.
println!("cargo:rustc-link-search=native={:?}", pk.link_paths[0]);
println!("cargo:rustc-link-lib=proj");

Ok(pk.include_paths[0].clone())
})
.or_else(|err| {
eprintln!("pkg-config unable to find existing libproj installation: {}", err);
build_from_source()
})?
try_vcpkg()?
};

// The bindgen::Builder is the main entry point
Expand Down

0 comments on commit b74dd9b

Please sign in to comment.