Repository: mesonbuild/meson

Issue:
Meson and PyPy(3) and incorrect libdir
I was trying to build pygobject for pypy3 on Gentoo and ran into a problem. The problem was that meson consistently failed to get the dependency for it. However it did not fail on ``python.find_installation() ``. I first opened a report on pygobject and after some discussion and me poking at meson I found what was causing the problem for me on Gentoo. For reference see pygobject issue on  [1].

I tracked it down to [self.clib_compiler.find_library(libname, environment, libdirs)](https://github.com/mesonbuild/meson/blob/3997649139b76a15dfd6d5a2ce406b86c7728748/mesonbuild/modules/python.py#L151) returning None. I was wondering what ``libdirs`` was and it looked wrong as it appends ``bin``. On Gentoo it meant is ended up looking in ``/usr/lib64/pypy3/bin`` which does not exist. Dropping the ``bin`` part makes it work, see diff below [2].

I am not familiar enough with this module so dropping the ``bin`` may break other targets. @lazka suggest to use ``distutils.sysconfig.get_config_var('LIBDIR')``.

[1] https://gitlab.gnome.org/GNOME/pygobject/issues/299
[2]
```diff
diff --git a/mesonbuild/modules/python.py b/mesonbuild/modules/python.py
index 1d41165b..03e98a7b 100644
--- a/mesonbuild/modules/python.py
+++ b/mesonbuild/modules/python.py
@@ -138,7 +138,7 @@ class PythonDependency(ExternalDependency):
                 libname = 'pypy3-c'
             else:
                 libname = 'pypy-c'
-            libdir = os.path.join(self.variables.get('base'), 'bin')
+            libdir = os.path.join(self.variables.get('base'))
             libdirs = [libdir]
         else:
             libname = 'python{}'.format(self.version)
```


Discussion:
Now that I got it to build I ran into another problem. It looks like meson tries to install the pypy files in the wrong directory, ``/usr/lib/python3.5/site-packages/`` instead of ``/usr/lib64/pypy3/site-packages/``. See below a few example lines from the build log. I am done for today, I may look into this later this week.

```
[0/1] /usr/lib64/python-exec/python3.7/meson install --no-rebuild
Installing gi/_gi.pypy3-60-x86_64-linux-gnu.so to /var/tmp/portage/dev-python/pygobject-3.30.4/image//usr/lib/python3.5/site-packages/gi
Installing gi/_gi_cairo.pypy3-60-x86_64-linux-gnu.so to /var/tmp/portage/dev-python/pygobject-3.30.4/image//usr/lib/python3.5/site-packages/gi
```


> It looks like meson tries to install the pypy files in the wrong directory, `/usr/lib/python3.5/site-packages/` instead of `/usr/lib64/pypy3/site-packages/`.

I think this was fixed by https://bitbucket.org/pypy/pypy/commits/5ca68e43950be83 and will be in pypy7
> I am not familiar enough with this module so dropping the `bin` may break other targets. @lazka suggest to use `distutils.sysconfig.get_config_var('LIBDIR')`.

Turns out this works with the system pypy but not the portable one, there is not path pointing to the lib afaics. I think we should get the lib dirs from the build_ext distutils command (none in the pypy case) and if we are not linking against libpython then we should just continue even if no lib has been found.
> > It looks like meson tries to install the pypy files in the wrong directory, `/usr/lib/python3.5/site-packages/` instead of `/usr/lib64/pypy3/site-packages/`.
> 
> I think this was fixed by https://bitbucket.org/pypy/pypy/commits/5ca68e43950be83 and will be in pypy7

Yeah, that looks like what I am seeing, thanks!

edit: Better but still in the wrong dir, pypy3.5/site-packages instead of pypy3/site-packages

edit2: For some reason Gentoo chose to use pypy3/sitepackages and not follow convention of pypymajor.minor/site-packages. I'll open a bug on Gentoo for this.

Resolving patch:
diff --git a/mesonbuild/modules/python.py b/mesonbuild/modules/python.py
--- a/mesonbuild/modules/python.py
+++ b/mesonbuild/modules/python.py
@@ -60,6 +60,7 @@ def __init__(self, python_holder, environment, kwargs):
         self.pkgdep = None
         self.variables = python_holder.variables
         self.paths = python_holder.paths
+        self.link_libpython = python_holder.link_libpython
         if mesonlib.version_compare(self.version, '>= 3.0'):
             self.major_version = 3
         else:
@@ -149,11 +150,11 @@ def _find_libpy(self, python_holder, environment):
             libdirs = []
 
         largs = self.clib_compiler.find_library(libname, environment, libdirs)
-
-        self.is_found = largs is not None
-        if self.is_found:
+        if largs is not None:
             self.link_args = largs
 
+        self.is_found = largs is not None or not self.link_libpython
+
         inc_paths = mesonlib.OrderedSet([
             self.variables.get('INCLUDEPY'),
             self.paths.get('include'),


Test patch: