1 use std::path::PathBuf;
4 use rustc_session::Session;
5 use rustc_target::spec::LinkerFlavor;
7 /// Tries to infer the path of a binary for the target toolchain from the linker name.
8 pub(crate) fn get_toolchain_binary(sess: &Session, tool: &str) -> PathBuf {
9 let (mut linker, _linker_flavor) = linker_and_flavor(sess);
10 let linker_file_name = linker
12 .and_then(|name| name.to_str())
13 .unwrap_or_else(|| sess.fatal("couldn't extract file name from specified linker"));
15 if linker_file_name == "ld.lld" {
17 linker.set_file_name(tool)
20 let tool_file_name = linker_file_name
23 .replace("clang", tool)
26 linker.set_file_name(tool_file_name)
32 // Adapted from https://github.com/rust-lang/rust/blob/5db778affee7c6600c8e7a177c48282dab3f6292/src/librustc_codegen_ssa/back/link.rs#L848-L931
33 fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
36 linker: Option<PathBuf>,
37 flavor: Option<LinkerFlavor>,
38 ) -> Option<(PathBuf, LinkerFlavor)> {
39 match (linker, flavor) {
40 (Some(linker), Some(flavor)) => Some((linker, flavor)),
41 // only the linker flavor is known; use the default linker for the selected flavor
42 (None, Some(flavor)) => Some((
43 PathBuf::from(match flavor {
51 LinkerFlavor::Gcc => {
52 if cfg!(any(target_os = "solaris", target_os = "illumos")) {
53 // On historical Solaris systems, "cc" may have
54 // been Sun Studio, which is not flag-compatible
55 // with "gcc". This history casts a long shadow,
56 // and many modern illumos distributions today
57 // ship GCC as "gcc" without also making it
64 LinkerFlavor::Ld => "ld",
65 LinkerFlavor::Msvc => "link.exe",
66 LinkerFlavor::Lld(_) => "lld",
67 LinkerFlavor::PtxLinker => "rust-ptx-linker",
71 (Some(linker), None) => {
74 .and_then(|stem| stem.to_str())
76 sess.fatal("couldn't extract file stem from specified linker")
79 let flavor = if stem == "emcc" {
81 } else if stem == "gcc"
82 || stem.ends_with("-gcc")
84 || stem.ends_with("-clang")
87 } else if stem == "ld" || stem == "ld.lld" || stem.ends_with("-ld") {
89 } else if stem == "link" || stem == "lld-link" {
91 } else if stem == "lld" || stem == "rust-lld" {
92 LinkerFlavor::Lld(sess.target.target.options.lld_flavor)
94 // fall back to the value in the target spec
95 sess.target.target.linker_flavor
98 Some((linker, flavor))
100 (None, None) => None,
104 // linker and linker flavor specified via command line have precedence over what the target
105 // specification specifies
106 if let Some(ret) = infer_from(
108 sess.opts.cg.linker.clone(),
109 sess.opts.cg.linker_flavor,
114 if let Some(ret) = infer_from(
116 sess.target.target.options.linker.clone().map(PathBuf::from),
117 Some(sess.target.target.linker_flavor),
122 bug!("Not enough information provided to determine how to invoke the linker");