]> git.lizzy.rs Git - rust.git/blob - build_system/rustc_info.rs
Improve windows support
[rust.git] / build_system / rustc_info.rs
1 use std::path::{Path, PathBuf};
2 use std::process::{Command, Stdio};
3
4 pub(crate) fn get_host_triple() -> String {
5     let version_info =
6         Command::new("rustc").stderr(Stdio::inherit()).args(&["-vV"]).output().unwrap().stdout;
7     String::from_utf8(version_info)
8         .unwrap()
9         .lines()
10         .to_owned()
11         .find(|line| line.starts_with("host"))
12         .unwrap()
13         .split(":")
14         .nth(1)
15         .unwrap()
16         .trim()
17         .to_owned()
18 }
19
20 pub(crate) fn get_rustc_path() -> PathBuf {
21     let rustc_path = Command::new("rustup")
22         .stderr(Stdio::inherit())
23         .args(&["which", "rustc"])
24         .output()
25         .unwrap()
26         .stdout;
27     Path::new(String::from_utf8(rustc_path).unwrap().trim()).to_owned()
28 }
29
30 pub(crate) fn get_default_sysroot() -> PathBuf {
31     let default_sysroot = Command::new("rustc")
32         .stderr(Stdio::inherit())
33         .args(&["--print", "sysroot"])
34         .output()
35         .unwrap()
36         .stdout;
37     Path::new(String::from_utf8(default_sysroot).unwrap().trim()).to_owned()
38 }
39
40 pub(crate) fn get_file_name(crate_name: &str, crate_type: &str) -> String {
41     let file_name = Command::new("rustc")
42         .stderr(Stdio::inherit())
43         .args(&[
44             "--crate-name",
45             crate_name,
46             "--crate-type",
47             crate_type,
48             "--print",
49             "file-names",
50             "-",
51         ])
52         .output()
53         .unwrap()
54         .stdout;
55     let file_name = String::from_utf8(file_name).unwrap().trim().to_owned();
56     assert!(!file_name.contains('\n'));
57     assert!(file_name.contains(crate_name));
58     file_name
59 }