Repository: mesonbuild/meson

Issue:
`compiler.find_library` cannot find libraries in paths specified with -L under MSVC
**Describe the bug**
`compiler.find_library` cannot find libraries in paths specified with `-L` under MSVC. This is caused by the `-L` flag being transformed and then passed to `compiler.find_library` in the wrong order:

```
Running compile:
Working directory:  E:\src\ext_ffmpeg-build\meson-private\tmp0pjhhm6m
Command line:  C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.34.31933/bin/Hostx64/x64/cl.exe /LIBPATH:E:/i//lib -IE:/i//include E:\src\ext_ffmpeg-build\meson-private\tmp0pjhhm6m\testfile.c /FeE:\src\ext_ffmpeg-build\meson-private\tmp0pjhhm6m\output.exe /nologo /showIncludes /utf-8 /MD -guard:cf /nologo /showIncludes /utf-8 /Od /Oi- theoraenc.lib /link /nologo /release /nologo -GUARD:CF 

Code:
 int main(void) { return 0; }

Compiler stdout:
 testfile.c
LINK : fatal error LNK1181: no se puede abrir el archivo de entrada 'theoraenc.lib'

Compiler stderr:
 cl : L¡nea de comandos warning D9002 : se omite la opci¢n desconocida '/LIBPATH:E:/i//lib'


..\ext_ffmpeg\meson.build:1646:10: ERROR: C shared or static library 'theoraenc' not found
```

**To Reproduce**

Build and install libtheora to a `prefix` you have access to.

Set a native file up with the following contents: 

```
[constants]
common_args = ['-I<prefix>/include']
common_link_args = ['-L<prefix>/lib']

[properties]
c_args = common_args + ['-guard:cf']
cpp_args = common_args +  ['-guard:cf']
c_link_args = common_link_args + ['-GUARD:CF']
cpp_link_args = common_link_args + ['-GUARD:CF']
```

Then issue `meson setup build --prefix=<your prefix> --libdir=lib` against the below project:

```
project(test, c)

cc = meson.get_compiler('c')

dep = cc.find_library('theoraenc', required: true)
```

**Expected behavior**

Project is configured successfully.

**system parameters**
* Is this a [cross build](https://mesonbuild.com/Cross-compilation.html) or just a plain native build (for the same computer)? **no**
* what operating system (e.g. MacOS Catalina, Windows 10, CentOS 8.0, Ubuntu 18.04, etc.) **Windows 10 21H2**
* what Python version are you using e.g. 3.8.0 **3.10.1**
* what `meson --version` **0.63.0.rc1**
* what `ninja --version` if it's a Ninja build **1.10.2**



Discussion:

Resolving patch:
diff --git a/mesonbuild/compilers/mixins/clike.py b/mesonbuild/compilers/mixins/clike.py
--- a/mesonbuild/compilers/mixins/clike.py
+++ b/mesonbuild/compilers/mixins/clike.py
@@ -446,14 +446,20 @@ def build_wrapper_args(self, env: 'Environment',
 
         ca, la = self._get_basic_compiler_args(env, mode)
         cargs += ca
-        largs += la
 
         cargs += self.get_compiler_check_args(mode)
 
         # on MSVC compiler and linker flags must be separated by the "/link" argument
         # at this point, the '/link' argument may already be part of extra_args, otherwise, it is added here
-        if self.linker_to_compiler_args([]) == ['/link'] and largs != [] and '/link' not in extra_args:
-            extra_args += ['/link']
+        largs += [l for l in self.linker_to_compiler_args(la) if l != '/link']
+
+        if self.linker_to_compiler_args([]) == ['/link']:
+            if largs != [] and '/link' not in extra_args:
+                extra_args += ['/link']
+            # all linker flags must be converted now, otherwise the reordering
+            # of arglist will apply and -L flags will be reordered into
+            # breaking form. See arglist._should_prepend
+            largs = self.unix_args_to_native(largs)
 
         args = cargs + extra_args + largs
         return args


Test patch:
diff --git a/test cases/windows/22 msvc library argument order/lib/.gitignore b/test cases/windows/22 msvc library argument order/lib/.gitignore
new file mode 100644
--- /dev/null
+++ b/test cases/windows/22 msvc library argument order/lib/.gitignore	
@@ -0,0 +1,2 @@
+*.obj
+*.lib
diff --git a/test cases/windows/22 msvc library argument order/meson.build b/test cases/windows/22 msvc library argument order/meson.build
new file mode 100644
--- /dev/null
+++ b/test cases/windows/22 msvc library argument order/meson.build	
@@ -0,0 +1,33 @@
+project('can-this-find-an-external-library', 'c')
+
+cc = meson.get_compiler('c')
+
+if cc.get_argument_syntax() != 'msvc'
+  error('MESON_SKIP_TEST: test is only relevant for msvc and clang-cl')
+endif
+
+# We need to conjure a static library for the current architecture
+# Generate an object file manually.
+run_command(
+  [
+    meson.get_compiler('c').cmd_array().get(-1),
+    '/nologo',
+    '/MDd',
+    '/Fo@0@'.format(meson.current_source_dir() / 'lib' / 'source.obj'),
+    '/c',
+    files('source.c'),
+  ],
+  check: true
+)
+# Turn it into a library.
+run_command(
+  [
+    find_program('LIB'),
+    '/OUT:@0@'.format(meson.current_source_dir() / 'lib' / 'conjured.lib'),
+    meson.current_source_dir() / 'lib' / 'source.obj',
+  ],
+  check: true
+)
+
+# Ensure this library can be found
+dep = cc.find_library('conjured', required: true)
diff --git a/test cases/windows/22 msvc library argument order/nativefile.ini.in b/test cases/windows/22 msvc library argument order/nativefile.ini.in
new file mode 100644
--- /dev/null
+++ b/test cases/windows/22 msvc library argument order/nativefile.ini.in	
@@ -0,0 +1,9 @@
+[constants]
+common_args = ['-I@MESON_TEST_ROOT@/include']
+common_link_args = ['-LIBPATH:@MESON_TEST_ROOT@/lib']
+
+[properties]
+c_args = common_args
+cpp_args = common_args
+c_link_args = common_link_args
+cpp_link_args = common_link_args
diff --git a/test cases/windows/22 msvc library argument order/source.c b/test cases/windows/22 msvc library argument order/source.c
new file mode 100644
--- /dev/null
+++ b/test cases/windows/22 msvc library argument order/source.c	
@@ -0,0 +1,3 @@
+int func1_in_obj(void) {
+    return 0;
+}