@@ -87,6 +87,7 @@ impl AnalysisLoader for CargoAnalysisLoader {
87
87
. join ( "deps" )
88
88
. join ( "save-analysis" ) ;
89
89
// FIXME sys_root_path allows to break out of 'sandbox' - is that Ok?
90
+ // FIXME libs_path and src_path both assume the default `libdir = "lib"`.
90
91
let sys_root_path = sys_root_path ( ) ;
91
92
let target_triple = extract_target_triple ( sys_root_path. as_path ( ) ) ;
92
93
let libs_path = sys_root_path
@@ -108,9 +109,35 @@ impl AnalysisLoader for CargoAnalysisLoader {
108
109
}
109
110
}
110
111
112
+ fn extract_target_triple ( sys_root_path : & Path ) -> String {
113
+ // First try to get the triple from the rustc version output,
114
+ // otherwise fall back on the rustup-style toolchain path.
115
+ // FIXME: Both methods assume that the target is the host triple,
116
+ // which isn't the case for cross-compilation (rust-lang/rls#309).
117
+ extract_rustc_host_triple ( )
118
+ . unwrap_or_else ( || extract_rustup_target_triple ( sys_root_path) )
119
+ }
120
+
121
+ fn extract_rustc_host_triple ( ) -> Option < String > {
122
+ let rustc = env:: var ( "RUSTC" ) . unwrap_or ( String :: from ( "rustc" ) ) ;
123
+ let verbose_version = Command :: new ( rustc)
124
+ . arg ( "--verbose" )
125
+ . arg ( "--version" )
126
+ . output ( )
127
+ . ok ( )
128
+ . and_then ( |out| String :: from_utf8 ( out. stdout ) . ok ( ) ) ?;
129
+
130
+ // Extracts the triple from a line like `host: x86_64-unknown-linux-gnu`
131
+ verbose_version
132
+ . lines ( )
133
+ . find ( |line| line. starts_with ( "host: " ) )
134
+ . and_then ( |host| host. split_whitespace ( ) . nth ( 1 ) )
135
+ . map ( String :: from)
136
+ }
137
+
111
138
// FIXME: This can fail when using a custom toolchain in rustup (often linked to
112
139
// `/$rust_repo/build/$target/stage2`)
113
- fn extract_target_triple ( sys_root_path : & Path ) -> String {
140
+ fn extract_rustup_target_triple ( sys_root_path : & Path ) -> String {
114
141
// Extracts nightly-x86_64-pc-windows-msvc from
115
142
// $HOME/.rustup/toolchains/nightly-x86_64-pc-windows-msvc
116
143
let toolchain = sys_root_path
@@ -169,7 +196,7 @@ mod tests {
169
196
r#"C:\Users\user\.rustup\toolchains\nightly-x86_64-pc-windows-msvc"# ,
170
197
) ;
171
198
assert_eq ! (
172
- extract_target_triple ( path) ,
199
+ extract_rustup_target_triple ( path) ,
173
200
String :: from( "x86_64-pc-windows-msvc" )
174
201
) ;
175
202
}
@@ -180,8 +207,19 @@ mod tests {
180
207
"/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu" ,
181
208
) ;
182
209
assert_eq ! (
183
- extract_target_triple ( path) ,
210
+ extract_rustup_target_triple ( path) ,
184
211
String :: from( "x86_64-unknown-linux-gnu" )
185
212
) ;
186
213
}
214
+
215
+ #[ test]
216
+ fn target_triple ( ) {
217
+ let sys_root_path = sys_root_path ( ) ;
218
+ let target_triple = extract_target_triple ( & sys_root_path) ;
219
+ let target_path = sys_root_path
220
+ . join ( "lib" )
221
+ . join ( "rustlib" )
222
+ . join ( & target_triple) ;
223
+ assert ! ( target_path. is_dir( ) , "{:?} is not a directory!" , target_path) ;
224
+ }
187
225
}
0 commit comments