@@ -58,21 +58,26 @@ def __init__(self, env, langinfo, command, found, version_output):
5858 arflags_name = arinfo .var ('flags' ).lower ()
5959 arflags = shell .split (env .getvar (arinfo .var ('flags' ), 'cr' ))
6060
61- # macOS's ld doesn't support --version, but we can still try it out and
62- # grab the command line.
6361 ld_command = None
6462 try :
65- stdout , stderr = env .execute (
66- command + ldflags + ['-v' , '-Wl,--version' ],
67- stdout = shell .Mode .pipe , stderr = shell .Mode .pipe ,
63+ # Pass a sentinel flag to the linker so we can examine the verbose
64+ # compiler output and try to determine which linker we're using.
65+ output = env .execute (
66+ command + ldflags + ['-v' , '-Wl,-v' , '-Wl,--not-a-real-flag' ],
67+ stdout = shell .Mode .pipe , stderr = shell .Mode .stdout ,
6868 returncode = 'any'
6969 )
7070
71- for line in stderr .split ('\n ' ):
72- if '--version' in line :
73- ld_command = shell .split (line )[0 :1 ]
74- if os .path .basename (ld_command [0 ]) != 'collect2' :
75- break
71+ for line in output .split ('\n ' ):
72+ if '--not-a-real-flag' in line :
73+ args = shell .split (line )
74+ if os .path .basename (args [0 ]) != 'collect2' :
75+ try :
76+ shell .which (args [0 ])
77+ ld_command = args [0 :1 ]
78+ break
79+ except FileNotFoundError :
80+ pass
7681 except (OSError , shell .CalledProcessError ):
7782 pass
7883
@@ -96,7 +101,7 @@ def __init__(self, env, langinfo, command, found, version_output):
96101 ),
97102 }
98103 if ld_command :
99- self ._linkers ['raw' ] = LdLinker (self , env , ld_command , stdout )
104+ self ._linkers ['raw' ] = LdLinker (self , env , ld_command )
100105
101106 self .packages = CcPackageResolver (self , env , command , ldflags )
102107 self .runner = None
0 commit comments