Skip to content

python.extension_module() linking with libpython when it should not #11097

@dnicolodi

Description

@dnicolodi

Since version 0.63.0 Meson automatically adds a dependency on libpython when needed compiling extension modules. To determine whether to link to libpython Meson consults distutils. On Python 3.7 on Linux, distutils says that libpython should not be linked:

>>> import distutils.core
>>> cmd = distutils.core.Distribution().get_command_obj('build_ext')
>>> cmd.ensure_finalized()
>>> cmd.get_libraries(distutils.core.Extension('dummy', []))
[]

However, the pkg-config file for python contains these linker arguments

$ pkg-config python3 --libs
-L/opt/_internal/cpython-3.7.15/lib -lpython3.7m

This code

new_deps = []
has_pydep = False
for dep in mesonlib.extract_as_list(kwargs, 'dependencies'):
if isinstance(dep, _PythonDependencyBase):
has_pydep = True
# On macOS and some Linux distros (Debian) distutils doesn't link
# extensions against libpython. We call into distutils and mirror its
# behavior. See https://github.com/mesonbuild/meson/issues/4117
if not self.link_libpython:
dep = dep.get_partial_dependency(compile_args=True)
new_deps.append(dep)
if not has_pydep:
pydep = self._dependency_method_impl({})
if not pydep.found():
raise mesonlib.MesonException('Python dependency not found')
new_deps.append(pydep)
FeatureNew.single_use('python_installation.extension_module with implicit dependency on python',
'0.63.0', self.subproject, 'use python_installation.dependency()',
self.current_node)
kwargs['dependencies'] = new_deps

deals with adding the libpython dependency as need. In the case the libpython dependency is explicitly requested, only the compile arguments are used, however, in the case when it is not explicitly requested, all the arguments are added, including the link arguments found via pkg-config. I think that only the compile arguments should be included in this case too when the extension module should not be linked with libpython.

I'll prepare a PR, but I would like to check with @eli-schwartz if he agrees on the diagnosis.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions