Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check PostgreSQL major version and add linking flags if needed #28

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::env;
use std::path::PathBuf;
use std::fmt::{self, Display};

#[derive(Eq, PartialEq)]
enum LinkType {
Static,
Dynamic
Expand Down Expand Up @@ -81,6 +82,12 @@ fn main() {
println!("cargo:rerun-if-env-changed=PQ_LIB_STATIC");
println!("cargo:rerun-if-env-changed=TARGET");

// If major version of PostgreSQL is not known, use 12 as default,
// as version 12 introduced new required linking flags for static builds.
// Those flags are normally optional, but required when version is >= 12
// and the build type is static.
let mut major_version = 12;

if let Ok(lib_dir) = env::var("PQ_LIB_DIR") {
println!("cargo:rustc-link-search=native={}", lib_dir);
} else if configured_by_pkg_config() {
Expand All @@ -90,8 +97,20 @@ fn main() {
} else if let Some(path) = pg_config_output("--libdir") {
let path = replace_homebrew_path_on_mac(path);
println!("cargo:rustc-link-search=native={}", path);

let pg_ver = pg_config_output("--version").expect("pg_config was already shown to work");
major_version = pg_ver.split_whitespace().nth(1)
.and_then(|s| s.split('.').nth(0))
.and_then(|s| s.parse::<u32>().ok()).expect("invalid version number schema");
}

let link_opts = LinkingOptions::from_env();
println!("cargo:rustc-link-lib={}", link_opts);

if major_version >= 12 && link_opts.linking_type == Some(LinkType::Static) {
println!("cargo:rustc-link-lib=static=pgcommon");
println!("cargo:rustc-link-lib=static=pgport");
}
println!("cargo:rustc-link-lib={}", LinkingOptions::from_env());
}

#[cfg(feature = "pkg-config")]
Expand Down