Skip to content

Commit

Permalink
copy dlls to OUT_DIR
Browse files Browse the repository at this point in the history
work around new cargo runtime path changes rust-lang/cargo#3651
  • Loading branch information
mcgoo committed Apr 28, 2017
1 parent 3c1b6f1 commit fdab8ec
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions vcpkg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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};
Expand All @@ -83,6 +83,9 @@ pub struct Config {

/// libs that must be be found for probing to be considered successful
required_libs: Vec<LibNames>,

/// should DLLs be copies to OUT_DIR?
copy_dlls: bool,
}

#[derive(Debug)]
Expand Down Expand Up @@ -255,6 +258,7 @@ impl Config {
statik: None,
cargo_metadata: true,
required_libs: Vec::new(),
copy_dlls: true,
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit fdab8ec

Please sign in to comment.