Repository: mesonbuild/meson

Issue:
Regression in Ninja link flags generation in 0.52.0
Meson version: **0.52.0**  
OS: **Linux**  
Compiler toolchain: **LLVM/Clang**

I believe the change introduced in 0.52.0 wrt link flag generation caused a regression. Not sure what the minimal working example would look like but in my case the error appears when using Boost with `BOOST_ROOT` defined.

After successfully running `meson` followed by `ninja` the linker errors out with:

```
[2/2] Linking target test_app.
FAILED: test_app
clang++  -o test_app ... /opt/boost/lib -L ...
ld.lld: error: cannot open /opt/boost/lib: Is a directory
```
Ninja generator puts `-L` after the path instead of the other way around.

I've found a probable cause:

In 0.51.2:

https://github.com/mesonbuild/meson/blob/6857936c592d6f9608add5a74a51ee405aaddc0d/mesonbuild/compilers/clike.py#L270-L271


In 0.52.0:

https://github.com/mesonbuild/meson/blob/2779603c689c6347809255a5aa3bd59b46b18b81/mesonbuild/linkers.py#L414-L415

Here:

https://github.com/mesonbuild/meson/blob/ba1367b588388ebe9e37057cee9639e2ea2046fb/mesonbuild/backend/ninjabackend.py#L2517

`extend_preserving_lflags` receives an argument that looks like this:

```python
# In 0.52.0
linker.get_dependency_link_args(dep)
# ['-L', '/boost/root/lib']

# In 0.51.2
dep.get_link_args()
# ['-L/boost/root/lib'] 
```
And `extend_preserving_lflags` reorders the args here:

https://github.com/mesonbuild/meson/blob/ba1367b588388ebe9e37057cee9639e2ea2046fb/mesonbuild/compilers/compilers.py#L589-L598

`-L` goes into `lflags` and `/opt/boost/lib` goes into `normal_flags` then `normal_flags` gets appended first resulting in `[ ..., '/opt/boost/lib', '-L', ...]`.


Discussion:

Resolving patch:
diff --git a/mesonbuild/linkers.py b/mesonbuild/linkers.py
--- a/mesonbuild/linkers.py
+++ b/mesonbuild/linkers.py
@@ -412,7 +412,7 @@ def get_std_shared_lib_args(self) -> typing.List[str]:
         return ['-shared']
 
     def get_search_args(self, dirname: str) -> typing.List[str]:
-        return ['-L', dirname]
+        return ['-L' + dirname]
 
 
 class GnuLikeDynamicLinkerMixin:
@@ -930,7 +930,7 @@ def __init__(self, for_machine: mesonlib.MachineChoice,
     def get_allow_undefined_args(self) -> typing.List[str]:
         return []
 
-class CudaLinker(DynamicLinker):
+class CudaLinker(PosixDynamicLinkerMixin, DynamicLinker):
     """Cuda linker (nvlink)"""
     @staticmethod
     def parse_version():
@@ -964,12 +964,6 @@ def get_lib_prefix(self) -> str:
         from .compilers import CudaCompiler
         return CudaCompiler.LINKER_PREFIX
 
-    def get_output_args(self, outname: str) -> typing.List[str]:
-        return ['-o', outname]
-
-    def get_search_args(self, dirname: str) -> typing.List[str]:
-        return ['-L', dirname]
-
     def fatal_warnings(self) -> typing.List[str]:
         return ['--warning-as-error']
 
@@ -980,6 +974,3 @@ def get_soname_args(self, env: 'Environment', prefix: str, shlib_name: str,
                         suffix: str, soversion: str, darwin_versions: typing.Tuple[str, str],
                         is_shared_module: bool) -> typing.List[str]:
         return []
-
-    def get_std_shared_lib_args(self) -> typing.List[str]:
-        return ['-shared']


Test patch: