Repository: mesonbuild/meson

Issue:
CCompiler.get_library_dirs assumes en locale and fails on es_ES (and probably other locales)
This is the output of `cc --print-search-dirs` on en_US:

```
install: /usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/
programs: =/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/:/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/:/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../x86_64-pc-linux-gnu/bin/x86_64-pc-linux-gnu/7.1.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../x86_64-pc-linux-gnu/bin/
libraries: =/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../x86_64-pc-linux-gnu/lib/x86_64-pc-linux-gnu/7.1.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../x86_64-pc-linux-gnu/lib/../lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/../../../x86_64-pc-linux-gnu/7.1.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../lib/:/lib/x86_64-pc-linux-gnu/7.1.1/:/lib/../lib/:/usr/lib/x86_64-pc-linux-gnu/7.1.1/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../x86_64-pc-linux-gnu/lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/../../../:/lib/:/usr/lib/
```

And on es_ES:

```
instalar: /usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/
programas: =/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/:/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/:/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../x86_64-pc-linux-gnu/bin/x86_64-pc-linux-gnu/7.1.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../x86_64-pc-linux-gnu/bin/
bibliotecas: =/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../x86_64-pc-linux-gnu/lib/x86_64-pc-linux-gnu/7.1.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../x86_64-pc-linux-gnu/lib/../lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/../../../x86_64-pc-linux-gnu/7.1.1/:/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../lib/:/lib/x86_64-pc-linux-gnu/7.1.1/:/lib/../lib/:/usr/lib/x86_64-pc-linux-gnu/7.1.1/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/../../../../x86_64-pc-linux-gnu/lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/7.1.1/../../../:/lib/:/usr/lib/
```

We look for `libraries:`, but maybe we should pick the last line instead. Because of this, `cc.find_library()` fails to find libraries in the search path (f.ex. static libraries that need symbols from other libraries at link time).

Reported on IRC by `chuckdaniels`.


Discussion:
This has been present for several releases, but we should try to get this into 0.41.1 anyway since it affects a huge number of people, but they haven't hit it yet because most libraries can be found by `CCompiler.links()` and manual searching is not needed.
Can you export `LANG=C` (or `C.utf8`) before running it?
Maybe, is it possible to build a system without the `C.utf8` or `C` locales?

Perhaps I'm wrong, but I have a feeling that trying to solve a locale issue with more locale magic when it can all be avoided might lead to increased yak-shaving in the future.
`LANG=C` is basically "use what's in the code" not what's in the translation file; it's also [required by POSIX](https://superuser.com/a/219953).
Can we assume that output with `LANG=C` can be decoded as UTF-8?
If we are precise, the output is directory names that are raw bytestreams and not guaranteed to conform to any encoding. So no. But in practice probably yes.
So I guess we can set an env object on `Popen_safe` that sets the `LANG`. We probably want to do this at all places where we parse tool output? At least for GNU and GNU-compatible tools.

Resolving patch:
diff --git a/mesonbuild/compilers/c.py b/mesonbuild/compilers/c.py
--- a/mesonbuild/compilers/c.py
+++ b/mesonbuild/compilers/c.py
@@ -156,7 +156,9 @@ def get_std_shared_lib_link_args(self):
         return ['-shared']
 
     def get_library_dirs(self):
-        stdo = Popen_safe(self.exelist + ['--print-search-dirs'])[1]
+        env = os.environ.copy()
+        env['LC_ALL'] = 'C'
+        stdo = Popen_safe(self.exelist + ['--print-search-dirs'], env=env)[1]
         for line in stdo.split('\n'):
             if line.startswith('libraries:'):
                 libstr = line.split('=', 1)[1]


Test patch: