py: libcamera: Get dependency from meson python module unconditionally

libcamera uses the meson python module to handle native compilation of
Python extension modules.

When cross-compiling, the module uses the build machine suffix instead
of the host machine suffix in some enviroments (for instance naming the
shared object file _libcamera.cpython-313-x86_64-linux-gnu.so instead of
_libcamera.cpython-313-aarch64-linux-gnu.so when cross-compiling from
x86_64 to aarch64). This prevents using the python module in that case,
and libcamera uses the normal dependency() function to locate the Python
libraries, and the shared_module() function to build the module.

Not using the meson python module to get the Python dependency prevents
selecting a specific Python interpreter, the same way as it does for
native builds. While having multiple Python interpreter versions in a
cross-build environment is likely less common, different behaviours and
features between native and cross-compilation are still not optimal.

Improve this situation by getting the dependency from the python module
for cross-compilation as well. This also prepares for usage of
py.extension_module() once the file suffix issue will be fixed in meson.

The user will need to ensure that the Python interpreter for the build
machine matches the version of the interpreter in the cross-compilation
environment for the host machine. Otherwise, meson will fail to find the
Python dependency. Cross-compilation environment provided by Linux
distributions (such as Debian multi-arch support) should work out of the
box, but compiling libcamera manually against a cross-compilation
environment provided by Buildroot or Yocto may require manual
configuration.

When the interpreters versions do not match, meson needs to be pointed
to the build ùachine interpreter from the cross-compilation environment
using the cross file. For instance, assuming a 'br_host_dir' variable
pointing to the host directory from Buildroot, the cross file should
contain

[binaries]
python = br_host_dir / 'bin/python3'

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com>
This commit is contained in:
Laurent Pinchart
2025-08-18 23:19:48 +03:00
parent 91365356bd
commit a2b21d60c7
2 changed files with 5 additions and 15 deletions

View File

@@ -47,7 +47,7 @@ pycamera_sources += custom_target('py_gen_formats',
pycamera_deps = [
libcamera_private,
py3_dep,
py3.dependency(),
pybind11_dep,
]
@@ -62,7 +62,7 @@ if meson.is_cross_build()
# module. There's work in progress to fix this, based on PEP 739
# (https://github.com/mesonbuild/meson/pull/14657). While waiting for this
# to become available, work around the issue by using shared_module().
destdir = get_option('libdir') / ('python' + py3_dep.version()) / 'site-packages' / 'libcamera'
destdir = get_option('libdir') / ('python' + py3.language_version()) / 'site-packages' / 'libcamera'
pycamera = shared_module('_libcamera',
pycamera_sources,

View File

@@ -1,20 +1,10 @@
# SPDX-License-Identifier: CC0-1.0
if meson.is_cross_build()
py3_dep = dependency('python3', required : get_option('pycamera'))
else
py3 = import('python').find_installation('python3', required : get_option('pycamera'))
if not py3.found()
pycamera_enabled = false
subdir_done()
endif
py3_dep = py3.dependency(required : get_option('pycamera'))
endif
py_mod = import('python')
py3 = py_mod.find_installation('python3', required : get_option('pycamera'))
pybind11_dep = dependency('pybind11', required : get_option('pycamera'))
pycamera_enabled = py3_dep.found() and pybind11_dep.found()
pycamera_enabled = py3.found() and pybind11_dep.found()
if not pycamera_enabled
subdir_done()
endif