[libcamera-devel] [PATCH] py: libcamera: Improve python binding installation

William Vinnicombe william.vinnicombe at raspberrypi.com
Wed Oct 25 11:36:49 CEST 2023


The existing meson.build file installs the bindings to an architecture
specific libdir (eg /usr/local/lib/aarch64-linux-gnu/), which is not
picked up by default python which only looks in the non architecture
specific libdir (eg /usr/local/lib/python3.11/). It also will always
build using the system python, rather than building using the same
python as meson is using. This prevents a user being able to build the
bindings for a different version of python, without changing their
system python to that version.

Modify the build process to use the meson Python module to build the
python bindings targets, so it installs them to the correct directories
for python, and builds them for the version of python that meson is
running with.

Signed-off-by: William Vinnicombe <william.vinnicombe at raspberrypi.com>
---
 src/py/libcamera/meson.build | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/src/py/libcamera/meson.build b/src/py/libcamera/meson.build
index f58c7198..e9e3f915 100644
--- a/src/py/libcamera/meson.build
+++ b/src/py/libcamera/meson.build
@@ -1,6 +1,8 @@
 # SPDX-License-Identifier: CC0-1.0
 
-py3_dep = dependency('python3', required : get_option('pycamera'))
+
+py = import('python').find_installation('python3', required : get_option('pycamera'))
+py3_dep = py.dependency(required : get_option('pycamera'))
 
 if not py3_dep.found()
     pycamera_enabled = false
@@ -78,15 +80,12 @@ pycamera_args = [
     '-DPYBIND11_USE_SMART_HOLDER_AS_DEFAULT',
 ]
 
-destdir = get_option('libdir') / ('python' + py3_dep.version()) / 'site-packages' / 'libcamera'
-
-pycamera = shared_module('_libcamera',
-                         pycamera_sources,
-                         install : true,
-                         install_dir : destdir,
-                         name_prefix : '',
-                         dependencies : pycamera_deps,
-                         cpp_args : pycamera_args)
+pycamera = py.extension_module('_libcamera',
+                               pycamera_sources,
+                               install : true,
+                               subdir : 'libcamera',
+                               dependencies : pycamera_deps,
+                               cpp_args : pycamera_args)
 
 # Create symlinks from the build dir to the source dir so that we can use the
 # Python module directly from the build dir.
@@ -99,7 +98,9 @@ run_command('ln', '-fsrT', meson.current_source_dir() / 'utils',
             meson.current_build_dir() / 'utils',
             check : true)
 
-install_data(['__init__.py'], install_dir : destdir)
+py.install_sources(['__init__.py'],
+                   subdir : 'libcamera',
+                   pure : false)
 
 # \todo Generate stubs when building. See https://peps.python.org/pep-0484/#stub-files
 # Note: Depends on pybind11-stubgen. To generate pylibcamera stubs:
-- 
2.39.2



More information about the libcamera-devel mailing list