1
1
use std:: env;
2
- use std:: path:: PathBuf ;
2
+ use std:: path:: { Path , PathBuf } ;
3
3
use std:: process:: Command ;
4
4
5
5
use failure:: Error ;
@@ -89,7 +89,7 @@ fn collect_possible_paths() -> Result<Vec<PathBuf>, Error> {
89
89
cargo_path. pop ( ) ;
90
90
91
91
if let Some ( toolchain) = cargo_path. file_name ( ) {
92
- let arch = extract_arch ( toolchain. to_str ( ) . unwrap ( ) ) ;
92
+ let arch = env :: var ( "HOST" ) . unwrap_or_else ( |_| extract_arch ( toolchain. to_str ( ) . unwrap ( ) ) ) ;
93
93
94
94
paths. push (
95
95
cargo_path
@@ -101,6 +101,21 @@ fn collect_possible_paths() -> Result<Vec<PathBuf>, Error> {
101
101
}
102
102
}
103
103
104
+ if let Some ( rustc) = find_rustc ( ) {
105
+ if let Ok ( output) = Command :: new ( & rustc) . args ( & [ "--print" , "sysroot" ] ) . output ( ) {
106
+ let mut sysroot = PathBuf :: from ( String :: from_utf8_lossy ( & output. stdout ) . trim ( ) ) ;
107
+ if let Some ( arch) = find_arch ( & rustc, & sysroot) {
108
+ paths. push (
109
+ sysroot
110
+ . join ( "lib" )
111
+ . join ( "rustlib" )
112
+ . join ( arch)
113
+ . join ( "codegen-backends" ) ,
114
+ ) ;
115
+ }
116
+ }
117
+ }
118
+
104
119
if let Ok ( output) = Command :: new ( "rustup" ) . args ( & [ "which" , "rustc" ] ) . output ( ) {
105
120
let mut rustc_path = PathBuf :: from ( String :: from_utf8_lossy ( & output. stdout ) . trim ( ) ) ;
106
121
rustc_path. pop ( ) ;
@@ -122,6 +137,8 @@ fn collect_possible_paths() -> Result<Vec<PathBuf>, Error> {
122
137
Ok ( paths)
123
138
}
124
139
140
+ // Fails if using nightly build from a specific date
141
+ // e.g. nightly-2018-11-30-x86_64-unknown-linux-gnu
125
142
fn extract_arch ( toolchain : & str ) -> String {
126
143
toolchain
127
144
. split ( '-' )
@@ -135,3 +152,30 @@ fn extract_arch(toolchain: &str) -> String {
135
152
. collect :: < Vec < _ > > ( )
136
153
. join ( "-" )
137
154
}
155
+
156
+ fn find_rustc ( ) -> Option < PathBuf > {
157
+ if let Some ( path) = env:: var_os ( "RUSTC" ) {
158
+ Some ( path. into ( ) )
159
+ } else if let Ok ( output) = Command :: new ( "rustup" ) . args ( & [ "which" , "rustc" ] ) . output ( ) {
160
+ Some ( String :: from_utf8_lossy ( & output. stdout ) . trim ( ) . into ( ) )
161
+ } else {
162
+ None
163
+ }
164
+ }
165
+
166
+ fn find_arch ( rustc : & Path , sysroot : & Path ) -> Option < String > {
167
+ if let Ok ( path) = env:: var ( "HOST" ) {
168
+ Some ( path)
169
+ } else if let Ok ( output) = Command :: new ( & rustc) . args ( & [ "-vV" ] ) . output ( ) {
170
+ for line in String :: from_utf8_lossy ( & output. stdout ) . lines ( ) {
171
+ if line. starts_with ( "host" ) {
172
+ return Some ( line. trim_start_matches ( "host:" ) . trim ( ) . to_string ( ) ) ;
173
+ }
174
+ }
175
+ None
176
+ } else if let Some ( toolchain) = sysroot. file_name ( ) {
177
+ Some ( extract_arch ( toolchain. to_str ( ) . unwrap ( ) ) )
178
+ } else {
179
+ None
180
+ }
181
+ }
0 commit comments