diff --git a/README.md b/README.md index 3ef868f..e510b7a 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,9 @@ installed on the system. The build script of crate will attempt to find the lib path of libpq using the following methods: -* First, if the environment variable `PQ_LIB_DIR` is set, it will use its value +* First it will look for an environment variable in the format of `PQ_LIB_DIR_{TARGET}` +where `{TARGET}` gets replaced by the Target environment variable set for cross-compilation +* Second, if the environment variable `PQ_LIB_DIR` is set, it will use its value * If the environment variable isn't set, it tries to use pkg-config to locate it. All the config options, such as `PKG_CONFIG_ALLOW_CROSS`, `PKG_CONFIG_ALL_STATIC` etc., of the crate [pkg-config](http://alexcrichton.com/pkg-config-rs/pkg_config/index.html) diff --git a/build.rs b/build.rs index aeb0e7e..93b4f0f 100644 --- a/build.rs +++ b/build.rs @@ -77,11 +77,19 @@ impl Display for LinkingOptions { } fn main() { - println!("cargo:rerun-if-env-changed=PQ_LIB_DIR"); println!("cargo:rerun-if-env-changed=PQ_LIB_STATIC"); println!("cargo:rerun-if-env-changed=TARGET"); - if let Ok(lib_dir) = env::var("PQ_LIB_DIR") { + // if target is specified the more concrete pq_lib_dir overwrites a more general one + let lib_dir = if let Ok(target) = env::var("TARGET") { + let pq_lib_dir_for_target = format!("PQ_LIB_DIR_{}", target.to_ascii_uppercase().replace("-", "_")); + check_and_use_lib_dir(&pq_lib_dir_for_target).or_else(|_| check_and_use_lib_dir("PQ_LIB_DIR")) + } + else{ + check_and_use_lib_dir("PQ_LIB_DIR") + }; + + if let Ok(lib_dir) = lib_dir { println!("cargo:rustc-link-search=native={}", lib_dir); } else if configured_by_pkg_config() { return // pkg_config does everything for us, including output for cargo @@ -127,6 +135,20 @@ fn configured_by_vcpkg() -> bool { false } +fn check_and_use_lib_dir(var_name: &str) -> Result{ + println!("cargo:rerun-if-env-changed={:?}", var_name); + println!("{:?} = {:?}", var_name , env::var(var_name)); + + let pq_lib_dir = env::var(var_name); + if let Ok(pg_lib_path) = pq_lib_dir.clone() { + let path = PathBuf::from(&pg_lib_path); + if !path.exists() { + panic!("Folder {:?} doesn't exist in the configured path: {:?}", var_name, path); + } + } + pq_lib_dir +} + fn pg_config_path() -> PathBuf { if let Ok(target) = env::var("TARGET") { let pg_config_for_target = &format!("PG_CONFIG_{}", target.to_ascii_uppercase().replace("-", "_"));