From bcd9aa2c7d6f9a66ee8f9c4d3e759882823e2eaf Mon Sep 17 00:00:00 2001 From: Kontio Pyry Date: Mon, 21 Aug 2017 18:41:50 +0900 Subject: [PATCH 1/2] Made pg_config path configurable per-target. --- build.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/build.rs b/build.rs index 4ffb2fd..04c9c50 100644 --- a/build.rs +++ b/build.rs @@ -6,6 +6,8 @@ extern crate vcpkg; use std::process::Command; use std::env; +use std::path::PathBuf; + fn main() { println!("cargo:rerun-if-env-changed=PQ_LIB_DIR"); @@ -63,8 +65,28 @@ fn configured_by_vcpkg() -> bool { false } +fn pg_config_path() -> PathBuf { + use std::ascii::AsciiExt; + + if let Ok(target) = env::var("TARGET") { + let pg_config_for_target = &format!("PG_CONFIG_{}", target.to_ascii_uppercase().replace("-", "_")); + if let Some(pg_config_path) = env::var_os(pg_config_for_target) { + + let path = PathBuf::from(&pg_config_path); + + if !path.exists() { + panic!("pg_config doesn't exist in the configured path: {:?}", path); + } + + return path; + } + } + return PathBuf::from("pg_config"); +} + fn pg_config_output(command: &str) -> Option { - Command::new("pg_config") + + Command::new(pg_config_path()) .arg(command) .output() .ok() From e1d26acf6eabdd40d071d5ce8c9d52b2199960b8 Mon Sep 17 00:00:00 2001 From: Kontio Pyry Date: Tue, 22 Aug 2017 14:03:38 +0900 Subject: [PATCH 2/2] Now the static linking setting is also target-dependent. --- build.rs | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/build.rs b/build.rs index 04c9c50..1dd89c0 100644 --- a/build.rs +++ b/build.rs @@ -23,14 +23,7 @@ fn main() { let path = replace_homebrew_path_on_mac(path); println!("cargo:rustc-link-search=native={}", path); } - - if cfg!(all(windows, target_env="msvc")) { - println!("cargo:rustc-link-lib=dylib=libpq"); - } else if env::var_os("PQ_LIB_STATIC").is_some() { - println!("cargo:rustc-link-lib=static=pq"); - } else { - println!("cargo:rustc-link-lib=pq"); - } + static_or_not(); } #[cfg(feature = "pkg-config")] @@ -65,6 +58,34 @@ fn configured_by_vcpkg() -> bool { false } +fn static_or_not() { + use std::ascii::AsciiExt; + + // On Windows-MSVC, always link dynamically + if cfg!(all(windows, target_env="msvc")) { + println!("cargo:rustc-link-lib=dylib=libpq"); + return; + } + + // Link unconditionally statically + if env::var_os("PQ_LIB_STATIC").is_some() { + println!("cargo:rustc-link-lib=static=pq"); + return; + } + + // Examine the per-target env vars + if let Ok(target) = env::var("TARGET") { + let pg_config_for_target = format!("PQ_LIB_STATIC_{}", target.to_ascii_uppercase().replace("-", "_")); + if env::var_os(&pg_config_for_target).is_some() { + println!("cargo:rustc-link-lib=static=pq"); + return; + } + } + + // Otherwise, don't specify + println!("cargo:rustc-link-lib=pq"); +} + fn pg_config_path() -> PathBuf { use std::ascii::AsciiExt;