summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_codegen_cranelift/src/toolchain.rs
blob: f86236ef3eafc6eee6c6a476bb19d2fed1f13ce4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//! Locating various executables part of a C toolchain.

use std::path::PathBuf;

use rustc_codegen_ssa::back::link::linker_and_flavor;
use rustc_session::Session;

/// Tries to infer the path of a binary for the target toolchain from the linker name.
pub(crate) fn get_toolchain_binary(sess: &Session, tool: &str) -> PathBuf {
    let (mut linker, _linker_flavor) = linker_and_flavor(sess);
    let linker_file_name = linker
        .file_name()
        .and_then(|name| name.to_str())
        .unwrap_or_else(|| sess.fatal("couldn't extract file name from specified linker"));

    if linker_file_name == "ld.lld" {
        if tool != "ld" {
            linker.set_file_name(tool)
        }
    } else {
        let tool_file_name = linker_file_name
            .replace("ld", tool)
            .replace("gcc", tool)
            .replace("clang", tool)
            .replace("cc", tool);

        linker.set_file_name(tool_file_name)
    }

    linker
}