22
33use std:: path:: PathBuf ;
44
5- use rustc_middle :: bug ;
5+ use rustc_codegen_ssa :: back :: link :: linker_and_flavor ;
66use rustc_session:: Session ;
7- use rustc_target:: spec:: LinkerFlavor ;
87
98/// Tries to infer the path of a binary for the target toolchain from the linker name.
109pub ( crate ) fn get_toolchain_binary ( sess : & Session , tool : & str ) -> PathBuf {
@@ -30,90 +29,3 @@ pub(crate) fn get_toolchain_binary(sess: &Session, tool: &str) -> PathBuf {
3029
3130 linker
3231}
33-
34- // Adapted from https://github.com/rust-lang/rust/blob/5db778affee7c6600c8e7a177c48282dab3f6292/src/librustc_codegen_ssa/back/link.rs#L848-L931
35- fn linker_and_flavor ( sess : & Session ) -> ( PathBuf , LinkerFlavor ) {
36- fn infer_from (
37- sess : & Session ,
38- linker : Option < PathBuf > ,
39- flavor : Option < LinkerFlavor > ,
40- ) -> Option < ( PathBuf , LinkerFlavor ) > {
41- match ( linker, flavor) {
42- ( Some ( linker) , Some ( flavor) ) => Some ( ( linker, flavor) ) ,
43- // only the linker flavor is known; use the default linker for the selected flavor
44- ( None , Some ( flavor) ) => Some ( (
45- PathBuf :: from ( match flavor {
46- LinkerFlavor :: Em => {
47- if cfg ! ( windows) {
48- "emcc.bat"
49- } else {
50- "emcc"
51- }
52- }
53- LinkerFlavor :: Gcc => {
54- if cfg ! ( any( target_os = "solaris" , target_os = "illumos" ) ) {
55- // On historical Solaris systems, "cc" may have
56- // been Sun Studio, which is not flag-compatible
57- // with "gcc". This history casts a long shadow,
58- // and many modern illumos distributions today
59- // ship GCC as "gcc" without also making it
60- // available as "cc".
61- "gcc"
62- } else {
63- "cc"
64- }
65- }
66- LinkerFlavor :: Ld => "ld" ,
67- LinkerFlavor :: Msvc => "link.exe" ,
68- LinkerFlavor :: Lld ( _) => "lld" ,
69- LinkerFlavor :: PtxLinker => "rust-ptx-linker" ,
70- LinkerFlavor :: BpfLinker => "bpf-linker" ,
71- } ) ,
72- flavor,
73- ) ) ,
74- ( Some ( linker) , None ) => {
75- let stem = linker. file_stem ( ) . and_then ( |stem| stem. to_str ( ) ) . unwrap_or_else ( || {
76- sess. fatal ( "couldn't extract file stem from specified linker" )
77- } ) ;
78-
79- let flavor = if stem == "emcc" {
80- LinkerFlavor :: Em
81- } else if stem == "gcc"
82- || stem. ends_with ( "-gcc" )
83- || stem == "clang"
84- || stem. ends_with ( "-clang" )
85- {
86- LinkerFlavor :: Gcc
87- } else if stem == "ld" || stem == "ld.lld" || stem. ends_with ( "-ld" ) {
88- LinkerFlavor :: Ld
89- } else if stem == "link" || stem == "lld-link" {
90- LinkerFlavor :: Msvc
91- } else if stem == "lld" || stem == "rust-lld" {
92- LinkerFlavor :: Lld ( sess. target . lld_flavor )
93- } else {
94- // fall back to the value in the target spec
95- sess. target . linker_flavor
96- } ;
97-
98- Some ( ( linker, flavor) )
99- }
100- ( None , None ) => None ,
101- }
102- }
103-
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 ( sess, sess. opts . cg . linker . clone ( ) , sess. opts . cg . linker_flavor ) {
107- return ret;
108- }
109-
110- if let Some ( ret) = infer_from (
111- sess,
112- sess. target . linker . clone ( ) . map ( PathBuf :: from) ,
113- Some ( sess. target . linker_flavor ) ,
114- ) {
115- return ret;
116- }
117-
118- bug ! ( "Not enough information provided to determine how to invoke the linker" ) ;
119- }
0 commit comments