@@ -164,6 +164,22 @@ pub fn get_or_default_sysroot() -> Result<PathBuf, String> {
164
164
fix_windows_verbatim_for_gcc ( & path)
165
165
}
166
166
167
+ // Use env::current_exe() to get the path of the executable following
168
+ // symlinks/canonicalizing components.
169
+ #[ cfg( target_os = "linux" ) ]
170
+ fn default_from_current_exe ( ) -> Result < PathBuf , io:: Error > {
171
+ match env:: current_exe ( ) {
172
+ Ok ( exe) => {
173
+ let mut p = canonicalize ( exe) ?;
174
+ p. pop ( ) ;
175
+ p. pop ( ) ;
176
+
177
+ Ok ( p)
178
+ }
179
+ Err ( e) => Err ( e) ,
180
+ }
181
+ }
182
+
167
183
fn default_from_rustc_driver_dll ( ) -> Result < PathBuf , String > {
168
184
let dll = current_dll_path ( ) . map ( |s| canonicalize ( s) ) ?;
169
185
@@ -193,6 +209,18 @@ pub fn get_or_default_sysroot() -> Result<PathBuf, String> {
193
209
}
194
210
}
195
211
212
+ // Select a default path based on the platform this Rust binary is currently running on
213
+ fn default_from_current_platform ( ) -> Result < PathBuf , String > {
214
+ #[ cfg( target_os = "linux" ) ]
215
+ {
216
+ if let Ok ( p) = default_from_current_exe ( ) {
217
+ return Ok ( p) ;
218
+ }
219
+ }
220
+
221
+ default_from_rustc_driver_dll ( )
222
+ }
223
+
196
224
// Use env::args().next() to get the path of the executable without
197
225
// following symlinks/canonicalizing any component. This makes the rustc
198
226
// binary able to locate Rust libraries in systems using content-addressable
@@ -223,5 +251,5 @@ pub fn get_or_default_sysroot() -> Result<PathBuf, String> {
223
251
}
224
252
}
225
253
226
- Ok ( from_env_args_next ( ) . unwrap_or ( default_from_rustc_driver_dll ( ) ?) )
254
+ Ok ( from_env_args_next ( ) . unwrap_or ( default_from_current_platform ( ) ?) )
227
255
}
0 commit comments