]> git.lizzy.rs Git - rust.git/blob - src/build_helper/lib.rs
Auto merge of #38443 - frewsxcv:file-docs, r=brson
[rust.git] / src / build_helper / lib.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 #![deny(warnings)]
12
13 use std::process::{Command, Stdio};
14 use std::path::{Path, PathBuf};
15
16 pub fn run(cmd: &mut Command) {
17     println!("running: {:?}", cmd);
18     run_silent(cmd);
19 }
20
21 pub fn run_silent(cmd: &mut Command) {
22     let status = match cmd.status() {
23         Ok(status) => status,
24         Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}",
25                                 cmd, e)),
26     };
27     if !status.success() {
28         fail(&format!("command did not execute successfully: {:?}\n\
29                        expected success, got: {}",
30                       cmd,
31                       status));
32     }
33 }
34
35 pub fn gnu_target(target: &str) -> String {
36     match target {
37         "i686-pc-windows-msvc" => "i686-pc-win32".to_string(),
38         "x86_64-pc-windows-msvc" => "x86_64-pc-win32".to_string(),
39         "i686-pc-windows-gnu" => "i686-w64-mingw32".to_string(),
40         "x86_64-pc-windows-gnu" => "x86_64-w64-mingw32".to_string(),
41         s => s.to_string(),
42     }
43 }
44
45 pub fn cc2ar(cc: &Path, target: &str) -> Option<PathBuf> {
46     if target.contains("msvc") {
47         None
48     } else if target.contains("musl") {
49         Some(PathBuf::from("ar"))
50     } else if target.contains("openbsd") {
51         Some(PathBuf::from("ar"))
52     } else {
53         let parent = cc.parent().unwrap();
54         let file = cc.file_name().unwrap().to_str().unwrap();
55         for suffix in &["gcc", "cc", "clang"] {
56             if let Some(idx) = file.rfind(suffix) {
57                 let mut file = file[..idx].to_owned();
58                 file.push_str("ar");
59                 return Some(parent.join(&file));
60             }
61         }
62         Some(parent.join(file))
63     }
64 }
65
66 pub fn make(host: &str) -> PathBuf {
67     if host.contains("bitrig") || host.contains("dragonfly") ||
68         host.contains("freebsd") || host.contains("netbsd") ||
69         host.contains("openbsd") {
70         PathBuf::from("gmake")
71     } else {
72         PathBuf::from("make")
73     }
74 }
75
76 pub fn output(cmd: &mut Command) -> String {
77     let output = match cmd.stderr(Stdio::inherit()).output() {
78         Ok(status) => status,
79         Err(e) => fail(&format!("failed to execute command: {:?}\nerror: {}",
80                                 cmd, e)),
81     };
82     if !output.status.success() {
83         panic!("command did not execute successfully: {:?}\n\
84                 expected success, got: {}",
85                cmd,
86                output.status);
87     }
88     String::from_utf8(output.stdout).unwrap()
89 }
90
91 fn fail(s: &str) -> ! {
92     println!("\n\n{}\n\n", s);
93     std::process::exit(1);
94 }