From fdab8ec5f6725c8d0e02efed89a557f0f31f7f38 Mon Sep 17 00:00:00 2001 From: Jim McGrath Date: Fri, 28 Apr 2017 14:38:02 -0500 Subject: [PATCH] copy dlls to OUT_DIR work around new cargo runtime path changes https://github.com/rust-lang/cargo/pull/3651 --- vcpkg/src/lib.rs | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/vcpkg/src/lib.rs b/vcpkg/src/lib.rs index c4c5c7e..fdc1803 100644 --- a/vcpkg/src/lib.rs +++ b/vcpkg/src/lib.rs @@ -4,7 +4,7 @@ //! The simplest possible usage for a library whose vcpkg port name matches the //! name of the lib and DLL that are being looked for looks like this :- //! ```rust -//! vcpkg::probe_library("libgit2").unwrap(); +//! vcpkg::probe_library("libssh2").unwrap(); //! ``` //! //! In practice the .lib and .dll often differ in name from the package itself, @@ -65,7 +65,7 @@ use std::ascii::AsciiExt; use std::env; use std::error; -use std::fs::File; +use std::fs::{self, File}; use std::fmt; use std::io::{BufRead, BufReader}; use std::path::{PathBuf, Path}; @@ -83,6 +83,9 @@ pub struct Config { /// libs that must be be found for probing to be considered successful required_libs: Vec, + + /// should DLLs be copies to OUT_DIR? + copy_dlls: bool, } #[derive(Debug)] @@ -255,6 +258,7 @@ impl Config { statik: None, cargo_metadata: true, required_libs: Vec::new(), + copy_dlls: true, } } @@ -304,6 +308,13 @@ impl Config { self } + /// Should DLLs be copied to OUT_DIR? + /// Defaults to `true`. + pub fn copy_dlls(&mut self, copy_dlls: bool) -> &mut Config { + self.copy_dlls = copy_dlls; + self + } + /// Find the library `port_name` in a vcpkg tree. /// /// This will use all configuration previously set to select the @@ -391,6 +402,27 @@ impl Config { } } + if self.copy_dlls { + if let Some(target_dir) = env::var_os("OUT_DIR") { + for file in &lib.found_dlls { + let mut dest_path = Path::new(target_dir.as_os_str()).to_path_buf(); + dest_path.push(Path::new(file.file_name().unwrap())); + fs::copy(file, &dest_path) + .map_err(|_| { + Error::LibNotFound(format!("Can't copy file {} to {}", + file.to_string_lossy(), + dest_path.to_string_lossy())) + })?; + // + println!("warning: copied {} to {}", + file.to_string_lossy(), + dest_path.to_string_lossy()); + } + } else { + return Err(Error::LibNotFound("Can't copy file".to_owned())); // TODO: + } + } + if self.cargo_metadata { for line in &lib.cargo_metadata { println!("{}", line);