[v3,2/3] py: libcamera: Get dependency from meson python module unconditionally
diff mbox series

Message ID 20250819012402.8395-3-laurent.pinchart@ideasonboard.com
State Accepted
Commit a2b21d60c7bf1d4539da89aaca49b5575e8a4549
Headers show
Series
  • py: Improve native and cross-compilation of Python bindings
Related show

Commit Message

Laurent Pinchart Aug. 19, 2025, 1:24 a.m. UTC
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>
---
 src/py/libcamera/meson.build |  4 ++--
 src/py/meson.build           | 16 +++-------------
 2 files changed, 5 insertions(+), 15 deletions(-)

Patch
diff mbox series

diff --git a/src/py/libcamera/meson.build b/src/py/libcamera/meson.build
index fe22ffd2f70b..d5433fbc1e3a 100644
--- a/src/py/libcamera/meson.build
+++ b/src/py/libcamera/meson.build
@@ -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,
diff --git a/src/py/meson.build b/src/py/meson.build
index 24102f83d2e1..88ad8b7ec26e 100644
--- a/src/py/meson.build
+++ b/src/py/meson.build
@@ -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