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

Make cross-compilation possible #40

Merged
merged 4 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 24 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -127,6 +135,20 @@ fn configured_by_vcpkg() -> bool {
false
}

fn check_and_use_lib_dir(var_name: &str) -> Result<String, VarError>{
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("-", "_"));
Expand Down