python module: implicitly add python dep to extensions

If there isn't a preexisting dependency on python, append one. It's
almost assuredly needed, so just do the right thing out of the box.
pull/10542/head
Eli Schwartz 2 years ago committed by Jussi Pakkanen
parent 43c60318fd
commit 22dcb692ad
  1. 6
      docs/markdown/Python-module.md
  2. 13
      docs/markdown/snippets/python-extension-module-implicit-dependency.md
  3. 28
      mesonbuild/modules/python.py
  4. 1
      test cases/python/2 extmodule/ext/meson.build
  5. 1
      test cases/python/2 extmodule/ext/nested/meson.build
  6. 1
      test cases/python/2 extmodule/ext/wrongdir/meson.build

@ -99,9 +99,9 @@ Additionally, the following diverge from [[shared_module]]'s default behavior:
of Python that support this (the python headers define `PyMODINIT_FUNC` has
default visibility).
`extension_module` does not add any dependencies to the library so
user may need to add `dependencies : py_installation.dependency()`,
see [[dependency]].
*since 0.63.0* `extension_module` automatically adds a dependency to the library
if one is not explicitly provided. To support older versions, the user may need to
add `dependencies : py_installation.dependency()`, see [[dependency]].
**Returns**: a [[@build_tgt]] object

@ -0,0 +1,13 @@
## Python extension modules now depend on the python library by default
Python extension modules are usually expected to link to the python library
and/or its headers in order to build correctly (via the default `embed: false`,
which may not actually link to the library itself). This means that every
single use of `.extension_module()` needed to include the `dependencies:
py_installation.dependency()` kwarg explicitly.
In the interest of doing the right thing out of the box, this is now the
default for extension modules that don't already include a dependency on
python. This is not expected to break anything, because it should always be
needed. Nevertheless, `py_installation.dependency().partial_dependency()` will
be detected as already included while providing no compile/link args.

@ -516,16 +516,26 @@ class PythonInstallation(ExternalProgramHolder):
kwargs['install_dir'] = os.path.join(self.platlib_install_path, subdir)
# 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:
new_deps = []
for dep in mesonlib.extract_as_list(kwargs, 'dependencies'):
if isinstance(dep, _PythonDependencyBase):
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)
kwargs['dependencies'] = new_deps
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
# msys2's python3 has "-cpython-36m.dll", we have to be clever
# FIXME: explain what the specific cleverness is here

@ -1,6 +1,5 @@
pylib = py.extension_module('tachyon',
'tachyon_module.c',
dependencies : py_dep,
c_args: '-DMESON_MODULENAME="tachyon"',
install: true,
)

@ -1,6 +1,5 @@
py.extension_module('tachyon',
'../tachyon_module.c',
dependencies : py_dep,
c_args: '-DMESON_MODULENAME="nested.tachyon"',
install: true,
subdir: 'nested'

@ -1,6 +1,5 @@
py.extension_module('tachyon',
'../tachyon_module.c',
dependencies : py_dep,
c_args: '-DMESON_MODULENAME="tachyon"',
install: true,
install_dir: get_option('libdir')

Loading…
Cancel
Save