]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/build_system/rustc_info.rs
Auto merge of #2607 - RalfJung:rustup, r=RalfJung
[rust.git] / compiler / rustc_codegen_cranelift / build_system / rustc_info.rs
1 use std::path::{Path, PathBuf};
2 use std::process::{Command, Stdio};
3
4 pub(crate) fn get_rustc_version() -> String {
5     let version_info =
6         Command::new("rustc").stderr(Stdio::inherit()).args(&["-V"]).output().unwrap().stdout;
7     String::from_utf8(version_info).unwrap()
8 }
9
10 pub(crate) fn get_host_triple() -> String {
11     let version_info =
12         Command::new("rustc").stderr(Stdio::inherit()).args(&["-vV"]).output().unwrap().stdout;
13     String::from_utf8(version_info)
14         .unwrap()
15         .lines()
16         .to_owned()
17         .find(|line| line.starts_with("host"))
18         .unwrap()
19         .split(":")
20         .nth(1)
21         .unwrap()
22         .trim()
23         .to_owned()
24 }
25
26 pub(crate) fn get_rustc_path() -> PathBuf {
27     let rustc_path = Command::new("rustup")
28         .stderr(Stdio::inherit())
29         .args(&["which", "rustc"])
30         .output()
31         .unwrap()
32         .stdout;
33     Path::new(String::from_utf8(rustc_path).unwrap().trim()).to_owned()
34 }
35
36 pub(crate) fn get_default_sysroot() -> PathBuf {
37     let default_sysroot = Command::new("rustc")
38         .stderr(Stdio::inherit())
39         .args(&["--print", "sysroot"])
40         .output()
41         .unwrap()
42         .stdout;
43     Path::new(String::from_utf8(default_sysroot).unwrap().trim()).to_owned()
44 }
45
46 pub(crate) fn get_file_name(crate_name: &str, crate_type: &str) -> String {
47     let file_name = Command::new("rustc")
48         .stderr(Stdio::inherit())
49         .args(&[
50             "--crate-name",
51             crate_name,
52             "--crate-type",
53             crate_type,
54             "--print",
55             "file-names",
56             "-",
57         ])
58         .output()
59         .unwrap()
60         .stdout;
61     let file_name = String::from_utf8(file_name).unwrap().trim().to_owned();
62     assert!(!file_name.contains('\n'));
63     assert!(file_name.contains(crate_name));
64     file_name
65 }
66
67 /// Similar to `get_file_name`, but converts any dashes (`-`) in the `crate_name` to
68 /// underscores (`_`). This is specially made for the rustc and cargo wrappers
69 /// which have a dash in the name, and that is not allowed in a crate name.
70 pub(crate) fn get_wrapper_file_name(crate_name: &str, crate_type: &str) -> String {
71     let crate_name = crate_name.replace('-', "_");
72     let wrapper_name = get_file_name(&crate_name, crate_type);
73     wrapper_name.replace('_', "-")
74 }