]> git.lizzy.rs Git - rust.git/blob - src/toolchain.rs
Rollup merge of #86016 - luqmana:infer-linker-flavor, r=petrochenkov
[rust.git] / src / toolchain.rs
1 //! Locating various executables part of a C toolchain.
2
3 use std::path::PathBuf;
4
5 use rustc_codegen_ssa::back::link::linker_and_flavor;
6 use rustc_session::Session;
7
8 /// Tries to infer the path of a binary for the target toolchain from the linker name.
9 pub(crate) fn get_toolchain_binary(sess: &Session, tool: &str) -> PathBuf {
10     let (mut linker, _linker_flavor) = linker_and_flavor(sess);
11     let linker_file_name = linker
12         .file_name()
13         .and_then(|name| name.to_str())
14         .unwrap_or_else(|| sess.fatal("couldn't extract file name from specified linker"));
15
16     if linker_file_name == "ld.lld" {
17         if tool != "ld" {
18             linker.set_file_name(tool)
19         }
20     } else {
21         let tool_file_name = linker_file_name
22             .replace("ld", tool)
23             .replace("gcc", tool)
24             .replace("clang", tool)
25             .replace("cc", tool);
26
27         linker.set_file_name(tool_file_name)
28     }
29
30     linker
31 }