extern crate pkg_config; extern crate gcc; use std::env; use std::path::PathBuf; fn main() { let target = env::var("TARGET").unwrap(); // libressl_pnacl_sys links the libs needed. if target.ends_with("nacl") { return; } let lib_dir = env::var("OPENSSL_LIB_DIR").ok(); let include_dir = env::var("OPENSSL_INCLUDE_DIR").ok(); if lib_dir.is_none() && include_dir.is_none() { if let Ok(info) = pkg_config::find_library("openssl") { build_old_openssl_shim(&info.include_paths); return; } } let libs_env = env::var("OPENSSL_LIBS").ok(); let libs = match libs_env { Some(ref v) => v.split(":").collect(), None => if target.contains("windows") { if let Some(mingw_paths) = check_mingw_path() { for path in mingw_paths { println!("cargo:rustc-flags=-L native={}", path); } vec!("eay32", "ssleay32") } else { vec!("eay32", "ssl32") } } else { vec!("crypto", "ssl") } }; let mode = if env::var_os("OPENSSL_STATIC").is_some() { "static" } else { "dylib" }; if let Some(lib_dir) = lib_dir { println!("cargo:rustc-flags=-L native={}", lib_dir); } let libs_arg = libs.iter().fold(String::new(), |args, lib| args + &format!(" -l {0}={1}", mode, lib)); println!("cargo:rustc-flags={0}", libs_arg); let mut include_dirs = vec![]; if let Some(include_dir) = include_dir { include_dirs.push(PathBuf::from(&include_dir)); } build_old_openssl_shim(&include_dirs); } fn build_old_openssl_shim(include_paths: &[PathBuf]) { let mut config = gcc::Config::new(); for path in include_paths { config.include(path); } config.file("src/old_openssl_shim.c") .compile("libold_openssl_shim.a"); } fn check_mingw_path() -> Option> { use std::ascii::AsciiExt; let mut paths = vec![]; if let Some(env_path) = env::var("PATH").ok() { for path in env::split_paths(&env_path) { if let Some(path_str) = path.to_str() { if path_str.to_ascii_lowercase().contains("mingw") { paths.push(path_str.to_string()); } } } } if paths.len() > 0 { Option::Some(paths) } else { Option::None } }