Skip to content

Commit

Permalink
Merge pull request #86 from kornelski/autocross
Browse files Browse the repository at this point in the history
Allow cross-compilation if pkg-config is customized
  • Loading branch information
sdroege committed Jul 25, 2019
2 parents 3c8b14e + c320067 commit 69d261f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/lib.rs
Expand Up @@ -64,6 +64,7 @@
#![doc(html_root_url = "https://docs.rs/pkg-config/0.3")]

#[allow(unused_imports)] // Required for Rust <1.23
#[allow(deprecated)]
use std::ascii::AsciiExt;
use std::collections::HashMap;
use std::env;
Expand All @@ -76,15 +77,6 @@ use std::path::{PathBuf, Path};
use std::process::{Command, Output};
use std::str;

pub fn target_supported() -> bool {
let target = env::var("TARGET").unwrap_or_else(|_| String::new());
let host = env::var("HOST").unwrap_or_else(|_| String::new());

// Only use pkg-config in host == target situations by default (allowing an
// override).
(host == target || env::var_os("PKG_CONFIG_ALLOW_CROSS").is_some())
}

#[derive(Clone)]
pub struct Config {
statik: Option<bool>,
Expand Down Expand Up @@ -310,7 +302,7 @@ impl Config {
let abort_var_name = format!("{}_NO_PKG_CONFIG", envify(name));
if self.env_var_os(&abort_var_name).is_some() {
return Err(Error::EnvNoPkgConfig(abort_var_name))
} else if !target_supported() {
} else if !self.target_supported() {
return Err(Error::CrossCompilation);
}

Expand All @@ -325,6 +317,31 @@ impl Config {
Ok(library)
}

pub fn target_supported(&self) -> bool {
let target = env::var("TARGET").unwrap_or_default();
let host = env::var("HOST").unwrap_or_default();

// Only use pkg-config in host == target situations by default (allowing an
// override).
if host == target {
return true;
}

// pkg-config may not be aware of cross-compilation, and require
// a wrapper script that sets up platform-specific prefixes.
match self.targetted_env_var("PKG_CONFIG_ALLOW_CROSS") {
// don't use pkg-config if explicitly disabled
Ok(ref val) if val == "0" => false,
Ok(_) => true,
Err(_) => {
// if not disabled, and pkg-config is customized,
// then assume it's prepared for cross-compilation
self.targetted_env_var("PKG_CONFIG").is_ok() ||
self.targetted_env_var("PKG_CONFIG_SYSROOT_DIR").is_ok()
},
}
}

/// Deprecated in favor of the top level `get_variable` function
#[doc(hidden)]
pub fn get_variable(package: &str, variable: &str) -> Result<String, String> {
Expand Down
25 changes: 25 additions & 0 deletions tests/test.rs
Expand Up @@ -16,6 +16,7 @@ fn reset() {
if k.contains("DYNAMIC") ||
k.contains("STATIC") ||
k.contains("PKG_CONFIG_ALLOW_CROSS") ||
k.contains("PKG_CONFIG_SYSROOT_DIR") ||
k.contains("FOO_NO_PKG_CONFIG") {
env::remove_var(&k);
}
Expand Down Expand Up @@ -51,6 +52,30 @@ fn cross_enabled() {
find("foo").unwrap();
}

#[test]
fn cross_enabled_if_customized() {
let _g = LOCK.lock();
reset();
env::set_var("TARGET", "foo");
env::set_var("HOST", "bar");
env::set_var("PKG_CONFIG_SYSROOT_DIR", "/tmp/cross-test");
find("foo").unwrap();
}

#[test]
fn cross_disabled_if_customized() {
let _g = LOCK.lock();
reset();
env::set_var("TARGET", "foo");
env::set_var("HOST", "bar");
env::set_var("PKG_CONFIG_ALLOW_CROSS", "0");
env::set_var("PKG_CONFIG_SYSROOT_DIR", "/tmp/cross-test");
match find("foo") {
Err(Error::CrossCompilation) => {},
_ => panic!("expected CrossCompilation failure"),
}
}

#[test]
fn package_disabled() {
let _g = LOCK.lock();
Expand Down

0 comments on commit 69d261f

Please sign in to comment.