Repository: mesonbuild/meson

Issue:
meson does not support -libpath: in LDFLAGS environment variable
meson seems to modify `LDFLAGS` environment varibable if it contains some `-LIBPATH:whatever`, and it makes the build fail:
```
Appending LDFLAGS from environment: ['-LIBPATH:C:\\Users\\appveyor\\.conan\\data\\libffi\\3.2.1\\_\\_\\package\\f27dcd2d5e453cb01f9324a959461903ce8116a0\\lib', '-LIBPATH:C:\\Users\\appveyor\\.conan\\data\\pcre\\8.41\\_\\_\\package\\799aa7257b99a1d9c85e265ab0b7ddc6342e7064\\lib', '-LIBPATH:C:\\Users\\appveyor\\.conan\\data\\libelf\\0.8.13\\_\\_\\package\\f27dcd2d5e453cb01f9324a959461903ce8116a0\\lib', '-LIBPATH:C:\\Users\\appveyor\\.conan\\data\\gettext\\0.20.1\\bincrafters\\stable\\package\\2b9b20f61d3ea15628d5e59640e45d270d4e1baa\\lib', '-LIBPATH:C:\\Users\\appveyor\\.conan\\data\\bzip2\\1.0.8\\_\\_\\package\\b553e9981f9078ff331b4fd4c3b7f9c44a5514ff\\lib', '-LIBPATH:C:\\Users\\appveyor\\.conan\\data\\libxml2\\2.9.9\\bincrafters\\stable\\package\\164a9ccb60ddea0ae0d23eacd120c5b18d4082e1\\lib', '-LIBPATH:C:\\Users\\appveyor\\.conan\\data\\zlib\\1.2.11\\_\\_\\package\\f27dcd2d5e453cb01f9324a959461903ce8116a0\\lib', '-LIBPATH:C:\\.conan\\c517c1\\1\\lib']
(...)
Traceback (most recent call last):
  File "C:\Users\appveyor\.conan\data\meson\0.52.0\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\bin\mesonbuild\mesonmain.py", line 129, in run
    return options.run_func(options)
  File "C:\Users\appveyor\.conan\data\meson\0.52.0\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\bin\mesonbuild\msetup.py", line 245, in run
    app.generate()
  File "C:\Users\appveyor\.conan\data\meson\0.52.0\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\bin\mesonbuild\msetup.py", line 159, in generate
    self._generate(env)
  File "C:\Users\appveyor\.conan\data\meson\0.52.0\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\bin\mesonbuild\msetup.py", line 215, in _generate
    intr.backend.generate(intr)
  File "C:\Users\appveyor\.conan\data\meson\0.52.0\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\bin\mesonbuild\backend\ninjabackend.py", line 314, in generate
    self.generate_target(t)
  File "C:\Users\appveyor\.conan\data\meson\0.52.0\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\bin\mesonbuild\backend\ninjabackend.py", line 632, in generate_target
    elem = self.generate_link(target, outname, obj_list, linker, pch_objects, stdlib_args=stdlib_args)
  File "C:\Users\appveyor\.conan\data\meson\0.52.0\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\bin\mesonbuild\backend\ninjabackend.py", line 2534, in generate_link
    dep_targets.extend(self.guess_external_link_dependencies(linker, target, commands, internal))
  File "C:\Users\appveyor\.conan\data\meson\0.52.0\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\bin\mesonbuild\backend\ninjabackend.py", line 2428, in guess_external_link_dependencies
    search_dirs, static_patterns)
  File "C:\Users\appveyor\.conan\data\meson\0.52.0\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\bin\mesonbuild\backend\ninjabackend.py", line 2369, in guess_library_absolute_path
    trial = CCompiler._get_file_from_list(self.environment, trial)
  File "C:\Users\appveyor\.conan\data\meson\0.52.0\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\bin\mesonbuild\compilers\mixins\clike.py", line 916, in _get_file_from_list
    if f.is_file():
  File "C:\Python37\lib\pathlib.py", line 1372, in is_file
    return S_ISREG(self.stat().st_mode)
  File "C:\Python37\lib\pathlib.py", line 1168, in stat
    return self._accessor.stat(self)
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:\\.conan\\01ae82\\1\\build_subfolder\\IBPATH:C:\\Users\\appveyor\\.conan\\data\\libffi\\3.2.1\\_\\_\\package\\f27dcd2d5e453cb01f9324a959461903ce8116a0\\lib\\libglib\\libglib-2.0.a.a'
```
(more detail https://ci.appveyor.com/project/bincrafters/conan-glib/builds/28354777/job/nexbj5fnd0vx3yq9#L906)

this issue is very similar to https://github.com/mesonbuild/meson/issues/5811 (with solution proposed in https://github.com/mesonbuild/meson/pull/5812), but it does not involve pkg-config


Discussion:
I'm sitting on a trivial patch that fixes this that I originally considered including in #5727, but decided to wait until we have a confirmed valid use case. Could you elaborate on why you are using the `-LIBPATH:` spelling instead of `/LIBPATH:`?
it's how conan package manager generates compiler flags (https://github.com/conan-io/conan/blob/8a6df42e34f3e8b4b3c4e8d2e115ab87ab272072/conans/client/build/compiler_flags.py#L194)
The reason is at the top of the file:
```
    # Visual Studio cl options reference:
    #   https://msdn.microsoft.com/en-us/library/610ecb4h.aspx
    #       "Options are specified by either a forward slash (/) or a dash (–)."
    #   Here we use "-" better than "/" that produces invalid escaped chars using AutoTools.
    #   -LIBPATH, -D, -I, -ZI and so on.
```
Ha, I didn't realize MSVC actually allows both. I'll submit a PR with a fix later today. Thanks for clarifying!

Resolving patch:
diff --git a/mesonbuild/compilers/mixins/visualstudio.py b/mesonbuild/compilers/mixins/visualstudio.py
--- a/mesonbuild/compilers/mixins/visualstudio.py
+++ b/mesonbuild/compilers/mixins/visualstudio.py
@@ -217,7 +217,9 @@ def unix_args_to_native(cls, args: typing.List[str]) -> typing.List[str]:
             # -pthread is only valid for GCC
             if i in ('-mms-bitfields', '-pthread'):
                 continue
-            if i.startswith('-L'):
+            if i.startswith('-LIBPATH:'):
+                i = '/LIBPATH:' + i[9:]
+            elif i.startswith('-L'):
                 i = '/LIBPATH:' + i[2:]
             # Translate GNU-style -lfoo library name to the import library
             elif i.startswith('-l'):
@@ -250,7 +252,7 @@ def unix_args_to_native(cls, args: typing.List[str]) -> typing.List[str]:
     def native_args_to_unix(cls, args: typing.List[str]) -> typing.List[str]:
         result = []
         for arg in args:
-            if arg.startswith('/LIBPATH:'):
+            if arg.startswith(('/LIBPATH:', '-LIBPATH:')):
                 result.append('-L' + arg[9:])
             elif arg.endswith(('.a', '.lib')) and not os.path.isabs(arg):
                 result.append('-l' + arg)
diff --git a/run_unittests.py b/run_unittests.py
--- a/run_unittests.py
+++ b/run_unittests.py
@@ -3162,7 +3162,7 @@ def test_guessed_linker_dependencies(self):
         env = get_fake_env(testdirlib, self.builddir, self.prefix)
         if env.detect_c_compiler(MachineChoice.HOST).get_id() in {'msvc', 'clang-cl', 'intel-cl'}:
             # msvc-like compiler, also test it with msvc-specific flags
-            libdir_flags += ['/LIBPATH:']
+            libdir_flags += ['/LIBPATH:', '-LIBPATH:']
         else:
             # static libraries are not linkable with -l with msvc because meson installs them
             # as .a files which unix_args_to_native will not know as it expects libraries to use
@@ -3176,6 +3176,7 @@ def test_guessed_linker_dependencies(self):
 
         for libdir_flag in libdir_flags:
             # build library
+            self.new_builddir()
             self.init(testdirlib, extra_args=extra_args)
             self.build()
             self.install()


Test patch: