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

Made pg_config path configurable per-target. #18

Closed
wants to merge 2 commits 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
61 changes: 52 additions & 9 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -21,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")]
Expand Down Expand Up @@ -63,8 +58,56 @@ fn configured_by_vcpkg() -> bool {
false
}

fn static_or_not() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method name seems odd to me. It has side effects but implies it returns a boolean. Can we have it return a value instead? Perhaps a 3 state enum?

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");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've noticed that I need to include the following when linking diesel statically:

[dependencies]
# This is needed to make sure that Cargo statically links against
# `libssl`. This should happen automatically, but it doesn't.
openssl-sys = "0.9"

Is there an underlying issue here with ps-sys or openssl-sys when statically linking that we should deal with? Or am I just messing up? I have a very small test program here that might help figure this out.

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("-", "_"));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This requires us adding a new

println!("cargo:rerun-if-env-changed={}", pg_config_for_target);

...line somewhere in the script. Also, you probably mean pq_lib_static_for_target here. :-)

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;

if let Ok(target) = env::var("TARGET") {
let pg_config_for_target = &format!("PG_CONFIG_{}", target.to_ascii_uppercase().replace("-", "_"));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also requires something like:

println!("cargo:rerun-if-env-changed={}", pg_config_for_target);

It might be best to wrap env::var_os in a wrapper that always generates this println!, and then refactor all of build.rs to use it.

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<String> {
Command::new("pg_config")

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✂️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀

Command::new(pg_config_path())
.arg(command)
.output()
.ok()
Expand Down