]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/build_system/rustc_info.rs
Rollup merge of #105727 - estebank:use-impl-trait, r=oli-obk
[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_cargo_path() -> PathBuf {
27     let cargo_path = Command::new("rustup")
28         .stderr(Stdio::inherit())
29         .args(&["which", "cargo"])
30         .output()
31         .unwrap()
32         .stdout;
33     Path::new(String::from_utf8(cargo_path).unwrap().trim()).to_owned()
34 }
35
36 pub(crate) fn get_rustc_path() -> PathBuf {
37     let rustc_path = Command::new("rustup")
38         .stderr(Stdio::inherit())
39         .args(&["which", "rustc"])
40         .output()
41         .unwrap()
42         .stdout;
43     Path::new(String::from_utf8(rustc_path).unwrap().trim()).to_owned()
44 }
45
46 pub(crate) fn get_rustdoc_path() -> PathBuf {
47     let rustc_path = Command::new("rustup")
48         .stderr(Stdio::inherit())
49         .args(&["which", "rustdoc"])
50         .output()
51         .unwrap()
52         .stdout;
53     Path::new(String::from_utf8(rustc_path).unwrap().trim()).to_owned()
54 }
55
56 pub(crate) fn get_default_sysroot() -> PathBuf {
57     let default_sysroot = Command::new("rustc")
58         .stderr(Stdio::inherit())
59         .args(&["--print", "sysroot"])
60         .output()
61         .unwrap()
62         .stdout;
63     Path::new(String::from_utf8(default_sysroot).unwrap().trim()).to_owned()
64 }
65
66 pub(crate) fn get_file_name(crate_name: &str, crate_type: &str) -> String {
67     let file_name = Command::new("rustc")
68         .stderr(Stdio::inherit())
69         .args(&[
70             "--crate-name",
71             crate_name,
72             "--crate-type",
73             crate_type,
74             "--print",
75             "file-names",
76             "-",
77         ])
78         .output()
79         .unwrap()
80         .stdout;
81     let file_name = String::from_utf8(file_name).unwrap().trim().to_owned();
82     assert!(!file_name.contains('\n'));
83     assert!(file_name.contains(crate_name));
84     file_name
85 }
86
87 /// Similar to `get_file_name`, but converts any dashes (`-`) in the `crate_name` to
88 /// underscores (`_`). This is specially made for the rustc and cargo wrappers
89 /// which have a dash in the name, and that is not allowed in a crate name.
90 pub(crate) fn get_wrapper_file_name(crate_name: &str, crate_type: &str) -> String {
91     let crate_name = crate_name.replace('-', "_");
92     let wrapper_name = get_file_name(&crate_name, crate_type);
93     wrapper_name.replace('_', "-")
94 }