Repository: mesonbuild/meson

Issue:
Meson misdetects lld as "APPLE ld unknown version" with the llvm-mingw cross toolchain
Here's the toolchain in question: https://github.com/mstorsjo/llvm-mingw

Linker invocation:
```
 % bin/x86_64-w64-mingw32-c11 -o test.exe test.c -v
clang version 9.0.0 (trunk 356111) (llvm/trunk 356109)
Target: x86_64-w64-windows-gnu
Thread model: posix
InstalledDir: /data/llvm-mingw/bin
 "/data/llvm-mingw/bin/clang-9" -cc1 -triple x86_64-w64-windows-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name test.c -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -v -resource-dir /data/llvm-mingw/lib/clang/9.0.0 -internal-isystem /data/llvm-mingw/lib/clang/9.0.0/include -internal-isystem /data/llvm-mingw/x86_64-w64-mingw32/include -internal-isystem /data/llvm-mingw/include -fdebug-compilation-dir /data/llvm-mingw -ferror-limit 19 -fmessage-length 239 -fobjc-runtime=gcc -fseh-exceptions -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/test-ae289e.o -x c test.c -faddrsig
clang -cc1 version 9.0.0 based upon LLVM 9.0.0svn default target x86_64-unknown-linux-gnu
#include "..." search starts here:
#include <...> search starts here:
 /data/llvm-mingw/lib/clang/9.0.0/include
 /data/llvm-mingw/x86_64-w64-mingw32/include
 /data/llvm-mingw/include
End of search list.
test.c:1:1: warning: variable named 'main' with external linkage has undefined behavior [-Wmain]
int main;
^
1 warning generated.
 "/data/llvm-mingw/bin/ld.lld" -m i386pep -Bdynamic -o test.exe /data/llvm-mingw/x86_64-w64-mingw32/lib/crt2.o /data/llvm-mingw/x86_64-w64-mingw32/lib/crtbegin.o -L/data/llvm-mingw/x86_64-w64-mingw32/lib -L/data/llvm-mingw/lib -L/data/llvm-mingw/x86_64-w64-mingw32/sys-root/mingw/lib /tmp/test-ae289e.o -lmingw32 /data/llvm-mingw/lib/clang/9.0.0/lib/windows/libclang_rt.builtins-x86_64.a -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 /data/llvm-mingw/lib/clang/9.0.0/lib/windows/libclang_rt.builtins-x86_64.a -lmoldname -lmingwex -lmsvcrt /data/llvm-mingw/x86_64-w64-mingw32/lib/crtend.o
```

Used to work just fine before the linker separation landed.


Discussion:

Resolving patch:
diff --git a/mesonbuild/environment.py b/mesonbuild/environment.py
--- a/mesonbuild/environment.py
+++ b/mesonbuild/environment.py
@@ -14,6 +14,7 @@
 
 import os, platform, re, sys, shutil, subprocess, typing
 import tempfile
+import shlex
 
 from . import coredata
 from .linkers import ArLinker, ArmarLinker, VisualStudioLinker, DLinker, CcrxLinker, IntelVisualStudioLinker
@@ -708,6 +709,22 @@ def _guess_nix_linker(compiler: typing.List[str], for_machine: MachineChoice,
         v = search_version(o)
         if o.startswith('LLD'):
             linker = LLVMDynamicLinker(compiler, for_machine, 'lld', prefix, version=v)  # type: DynamicLinker
+        elif e.startswith('lld-link: '):
+            # Toolchain wrapper got in the way; this happens with e.g. https://github.com/mstorsjo/llvm-mingw
+            # Let's try to extract the linker invocation command to grab the version.
+
+            _, o, e = Popen_safe(compiler + check_args + ['-v'])
+
+            try:
+                linker_cmd = re.match(r'.*\n(.*?)\nlld-link: ', e, re.DOTALL).group(1)
+                linker_cmd = shlex.split(linker_cmd)[0]
+            except (AttributeError, IndexError, ValueError):
+                pass
+            else:
+                _, o, e = Popen_safe([linker_cmd, '--version'])
+                v = search_version(o)
+
+            linker = LLVMDynamicLinker(compiler, for_machine, 'lld', prefix, version=v)
         # first is for apple clang, second is for real gcc
         elif e.endswith('(use -v to see invocation)\n') or 'macosx_version' in e:
             if isinstance(prefix, str):


Test patch: