]> git.proxmox.com Git - rustc.git/blob - src/bootstrap/build/util.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / bootstrap / build / util.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::env;
12 use std::path::{Path, PathBuf};
13 use std::fs;
14 use std::process::Command;
15
16 use bootstrap::{dylib_path, dylib_path_var};
17 use filetime::FileTime;
18
19 pub fn staticlib(name: &str, target: &str) -> String {
20 if target.contains("windows-msvc") {
21 format!("{}.lib", name)
22 } else {
23 format!("lib{}.a", name)
24 }
25 }
26
27 pub fn mtime(path: &Path) -> FileTime {
28 fs::metadata(path).map(|f| {
29 FileTime::from_last_modification_time(&f)
30 }).unwrap_or(FileTime::zero())
31 }
32
33 pub fn copy(src: &Path, dst: &Path) {
34 let res = fs::hard_link(src, dst);
35 let res = res.or_else(|_| fs::copy(src, dst).map(|_| ()));
36 if let Err(e) = res {
37 panic!("failed to copy `{}` to `{}`: {}", src.display(),
38 dst.display(), e)
39 }
40 }
41
42 pub fn cp_r(src: &Path, dst: &Path) {
43 for f in t!(fs::read_dir(src)) {
44 let f = t!(f);
45 let path = f.path();
46 let name = path.file_name().unwrap();
47 let dst = dst.join(name);
48 if t!(f.file_type()).is_dir() {
49 let _ = fs::remove_dir_all(&dst);
50 t!(fs::create_dir(&dst));
51 cp_r(&path, &dst);
52 } else {
53 let _ = fs::remove_file(&dst);
54 copy(&path, &dst);
55 }
56 }
57 }
58
59 /// Given an executable called `name`, return the filename for the
60 /// executable for a particular target.
61 pub fn exe(name: &str, target: &str) -> String {
62 if target.contains("windows") {
63 format!("{}.exe", name)
64 } else {
65 name.to_string()
66 }
67 }
68
69 pub fn is_dylib(name: &str) -> bool {
70 name.ends_with(".dylib") || name.ends_with(".so") || name.ends_with(".dll")
71 }
72
73 pub fn libdir(target: &str) -> &'static str {
74 if target.contains("windows") {"bin"} else {"lib"}
75 }
76
77 pub fn add_lib_path(path: Vec<PathBuf>, cmd: &mut Command) {
78 let mut list = dylib_path();
79 for path in path {
80 list.insert(0, path);
81 }
82 cmd.env(dylib_path_var(), t!(env::join_paths(list)));
83 }
84
85 #[allow(dead_code)] // this will be used soon
86 pub fn up_to_date(src: &Path, dst: &Path) -> bool {
87 let threshold = mtime(dst);
88 let meta = t!(fs::metadata(src));
89 if meta.is_dir() {
90 dir_up_to_date(src, &threshold)
91 } else {
92 FileTime::from_last_modification_time(&meta) <= threshold
93 }
94 }
95
96 fn dir_up_to_date(src: &Path, threshold: &FileTime) -> bool {
97 t!(fs::read_dir(src)).map(|e| t!(e)).all(|e| {
98 let meta = t!(e.metadata());
99 if meta.is_dir() {
100 dir_up_to_date(&e.path(), threshold)
101 } else {
102 FileTime::from_last_modification_time(&meta) < *threshold
103 }
104 })
105 }