[libcamera-devel] py: Use Meson python module
diff mbox series

Message ID 20220515170253.53075-1-aklhfex@gmail.com
State New
Headers show
Series
  • [libcamera-devel] py: Use Meson python module
Related show

Commit Message

Chris Mayo May 15, 2022, 5:02 p.m. UTC
Detection with dependency('python3') can fail because not all
distributions install python-3.pc. Installation is invalid if
site-packages is not below get_option('libdir').

Signed-off-by: Chris Mayo <aklhfex@gmail.com>
---
 src/py/libcamera/meson.build | 27 ++++++++++++---------------
 1 file changed, 12 insertions(+), 15 deletions(-)

Comments

Tomi Valkeinen May 16, 2022, 2:43 p.m. UTC | #1
Hi,

On 15/05/2022 20:02, Chris Mayo via libcamera-devel wrote:
> Detection with dependency('python3') can fail because not all
> distributions install python-3.pc. Installation is invalid if
> site-packages is not below get_option('libdir').

I don't understand the comment about libdir. Do you mean that this 
installs to the wrong dir if the platform's python site-packages are not 
located under get_option('libdir')?

> Signed-off-by: Chris Mayo <aklhfex@gmail.com>
> ---
>   src/py/libcamera/meson.build | 27 ++++++++++++---------------
>   1 file changed, 12 insertions(+), 15 deletions(-)
> 
> diff --git a/src/py/libcamera/meson.build b/src/py/libcamera/meson.build
> index 0cd7c75b..130c1be6 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'))
> +pymod = import('python')
> +py3 = pymod.find_installation('python3')
> +py3_dep = py3.dependency(required : get_option('pycamera'))
>   
>   if not py3_dep.found()
>       pycamera_enabled = false
> @@ -44,21 +46,16 @@ pycamera_args = [
>       '-DLIBCAMERA_BASE_PRIVATE',
>   ]
>   
> -destdir = get_option('libdir') / ('python' + py3_dep.version()) / 'site-packages' / 'libcamera'
> +py3.extension_module('_libcamera',
> +                     pycamera_sources,
> +                     install : true,
> +                     dependencies : pycamera_deps,
> +                     cpp_args : pycamera_args,
> +                     subdir : 'libcamera')

The problem is that this doesn't work with cross-compiling. When I 
compile for ARM, I get "_libcamera.cpython-310-x86_64-linux-gnu.so".

If I remember right, I discussed the use of py.extension_module() on 
meson chat, but the conclusion was that it doesn't work for 
cross-compiling. This may have changed, but at least here it doesn't work.

> -pycamera = shared_module('_libcamera',
> -                         pycamera_sources,
> -                         install : true,
> -                         install_dir : destdir,
> -                         name_prefix : '',
> -                         dependencies : pycamera_deps,
> -                         cpp_args : pycamera_args)
> -
> -run_command('ln', '-fsT', '../../../../src/py/libcamera/__init__.py',
> -            meson.current_build_dir() / '__init__.py',
> -            check: true)

Was there a reason to remove this symlink? I want to be able to use the 
bindings without installing. That's the purpose of the symlink.

  Tomi
Chris Mayo May 16, 2022, 7:01 p.m. UTC | #2
> Hi,
>
> On 15/05/2022 20:02, Chris Mayo via libcamera-devel wrote:
> > Detection with dependency('python3') can fail because not all
> > distributions install python-3.pc. Installation is invalid if
> > site-packages is not below get_option('libdir').
>
> I don't understand the comment about libdir. Do you mean that this
> installs to the wrong dir if the platform's python site-packages are not
> located under get_option('libdir')?

Yes. On Gentoo Linux:

/usr/lib/python3.10/site-packages/libcamera

and:

/usr/lib64/libcamera.so

> > Signed-off-by: Chris Mayo <aklhfex@gmail.com>
> > ---
> >   src/py/libcamera/meson.build | 27 ++++++++++++---------------
> >   1 file changed, 12 insertions(+), 15 deletions(-)
> >
> > diff --git a/src/py/libcamera/meson.build b/src/py/libcamera/meson.build
> > index 0cd7c75b..130c1be6 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'))
> > +pymod = import('python')
> > +py3 = pymod.find_installation('python3')
> > +py3_dep = py3.dependency(required : get_option('pycamera'))
> >
> >   if not py3_dep.found()
> >       pycamera_enabled = false
> > @@ -44,21 +46,16 @@ pycamera_args = [
> >       '-DLIBCAMERA_BASE_PRIVATE',
> >   ]
> >
> > -destdir = get_option('libdir') / ('python' + py3_dep.version()) / 'site-packages' / 'libcamera'
> > +py3.extension_module('_libcamera',
> > +                     pycamera_sources,
> > +                     install : true,
> > +                     dependencies : pycamera_deps,
> > +                     cpp_args : pycamera_args,
> > +                     subdir : 'libcamera')
>
> The problem is that this doesn't work with cross-compiling. When I
> compile for ARM, I get "_libcamera.cpython-310-x86_64-linux-gnu.so".
>
> If I remember right, I discussed the use of py.extension_module() on
> meson chat, but the conclusion was that it doesn't work for
> cross-compiling. This may have changed, but at least here it doesn't work.

OK. Instead of using py.extension_module() this works for me:

destdir = py3.get_install_dir() / 'libcamera'

Still using the Meson python module as before.

> > -pycamera = shared_module('_libcamera',
> > -                         pycamera_sources,
> > -                         install : true,
> > -                         install_dir : destdir,
> > -                         name_prefix : '',
> > -                         dependencies : pycamera_deps,
> > -                         cpp_args : pycamera_args)
> > -
> > -run_command('ln', '-fsT', '../../../../src/py/libcamera/__init__.py',
> > -            meson.current_build_dir() / '__init__.py',
> > -            check: true)
>
> Was there a reason to remove this symlink? I want to be able to use the
> bindings without installing. That's the purpose of the symlink.

No, that's fine it doesn't cause any problems.

>   Tomi


On Mon, 16 May 2022 at 15:43, Tomi Valkeinen
<tomi.valkeinen@ideasonboard.com> wrote:
>
> Hi,
>
> On 15/05/2022 20:02, Chris Mayo via libcamera-devel wrote:
> > Detection with dependency('python3') can fail because not all
> > distributions install python-3.pc. Installation is invalid if
> > site-packages is not below get_option('libdir').
>
> I don't understand the comment about libdir. Do you mean that this
> installs to the wrong dir if the platform's python site-packages are not
> located under get_option('libdir')?
>
> > Signed-off-by: Chris Mayo <aklhfex@gmail.com>
> > ---
> >   src/py/libcamera/meson.build | 27 ++++++++++++---------------
> >   1 file changed, 12 insertions(+), 15 deletions(-)
> >
> > diff --git a/src/py/libcamera/meson.build b/src/py/libcamera/meson.build
> > index 0cd7c75b..130c1be6 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'))
> > +pymod = import('python')
> > +py3 = pymod.find_installation('python3')
> > +py3_dep = py3.dependency(required : get_option('pycamera'))
> >
> >   if not py3_dep.found()
> >       pycamera_enabled = false
> > @@ -44,21 +46,16 @@ pycamera_args = [
> >       '-DLIBCAMERA_BASE_PRIVATE',
> >   ]
> >
> > -destdir = get_option('libdir') / ('python' + py3_dep.version()) / 'site-packages' / 'libcamera'
> > +py3.extension_module('_libcamera',
> > +                     pycamera_sources,
> > +                     install : true,
> > +                     dependencies : pycamera_deps,
> > +                     cpp_args : pycamera_args,
> > +                     subdir : 'libcamera')
>
> The problem is that this doesn't work with cross-compiling. When I
> compile for ARM, I get "_libcamera.cpython-310-x86_64-linux-gnu.so".
>
> If I remember right, I discussed the use of py.extension_module() on
> meson chat, but the conclusion was that it doesn't work for
> cross-compiling. This may have changed, but at least here it doesn't work.
>
> > -pycamera = shared_module('_libcamera',
> > -                         pycamera_sources,
> > -                         install : true,
> > -                         install_dir : destdir,
> > -                         name_prefix : '',
> > -                         dependencies : pycamera_deps,
> > -                         cpp_args : pycamera_args)
> > -
> > -run_command('ln', '-fsT', '../../../../src/py/libcamera/__init__.py',
> > -            meson.current_build_dir() / '__init__.py',
> > -            check: true)
>
> Was there a reason to remove this symlink? I want to be able to use the
> bindings without installing. That's the purpose of the symlink.
>
>   Tomi
Tomi Valkeinen May 17, 2022, 6:53 a.m. UTC | #3
On 16/05/2022 22:01, Chris Mayo wrote:
>> Hi,
>>
>> On 15/05/2022 20:02, Chris Mayo via libcamera-devel wrote:
>>> Detection with dependency('python3') can fail because not all
>>> distributions install python-3.pc. Installation is invalid if
>>> site-packages is not below get_option('libdir').
>>
>> I don't understand the comment about libdir. Do you mean that this
>> installs to the wrong dir if the platform's python site-packages are not
>> located under get_option('libdir')?
> 
> Yes. On Gentoo Linux:
> 
> /usr/lib/python3.10/site-packages/libcamera
> 
> and:
> 
> /usr/lib64/libcamera.so
> 
>>> Signed-off-by: Chris Mayo <aklhfex@gmail.com>
>>> ---
>>>    src/py/libcamera/meson.build | 27 ++++++++++++---------------
>>>    1 file changed, 12 insertions(+), 15 deletions(-)
>>>
>>> diff --git a/src/py/libcamera/meson.build b/src/py/libcamera/meson.build
>>> index 0cd7c75b..130c1be6 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'))
>>> +pymod = import('python')
>>> +py3 = pymod.find_installation('python3')
>>> +py3_dep = py3.dependency(required : get_option('pycamera'))
>>>
>>>    if not py3_dep.found()
>>>        pycamera_enabled = false
>>> @@ -44,21 +46,16 @@ pycamera_args = [
>>>        '-DLIBCAMERA_BASE_PRIVATE',
>>>    ]
>>>
>>> -destdir = get_option('libdir') / ('python' + py3_dep.version()) / 'site-packages' / 'libcamera'
>>> +py3.extension_module('_libcamera',
>>> +                     pycamera_sources,
>>> +                     install : true,
>>> +                     dependencies : pycamera_deps,
>>> +                     cpp_args : pycamera_args,
>>> +                     subdir : 'libcamera')
>>
>> The problem is that this doesn't work with cross-compiling. When I
>> compile for ARM, I get "_libcamera.cpython-310-x86_64-linux-gnu.so".
>>
>> If I remember right, I discussed the use of py.extension_module() on
>> meson chat, but the conclusion was that it doesn't work for
>> cross-compiling. This may have changed, but at least here it doesn't work.
> 
> OK. Instead of using py.extension_module() this works for me:
> 
> destdir = py3.get_install_dir() / 'libcamera'

For me, when not cross-compiling, py3.get_install_dir() returns 
"/usr/local/lib/python3/dist-packages/". Afaics, that is not correct, as 
"python3 -m site" doesn't contain that dir, but does contain, e.g. :

     '/usr/local/lib/python3.10/dist-packages',
     '/usr/lib/python3/dist-packages',

When I cross-compile, py3.get_install_dir() again returns the same path 
hinting that the py3 is really the host python (as also indicated by the 
module name). On my ARM device, "python3 -m site" returns 
'/usr/lib/python3.10/site-packages'.

So... the meson python module just seems to be totally broken. Or I'm 
somehow confused. I admit I know quite little about Python installations.

  Tomi

Patch
diff mbox series

diff --git a/src/py/libcamera/meson.build b/src/py/libcamera/meson.build
index 0cd7c75b..130c1be6 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'))
+pymod = import('python')
+py3 = pymod.find_installation('python3')
+py3_dep = py3.dependency(required : get_option('pycamera'))
 
 if not py3_dep.found()
     pycamera_enabled = false
@@ -44,21 +46,16 @@  pycamera_args = [
     '-DLIBCAMERA_BASE_PRIVATE',
 ]
 
-destdir = get_option('libdir') / ('python' + py3_dep.version()) / 'site-packages' / 'libcamera'
+py3.extension_module('_libcamera',
+                     pycamera_sources,
+                     install : true,
+                     dependencies : pycamera_deps,
+                     cpp_args : pycamera_args,
+                     subdir : 'libcamera')
 
-pycamera = shared_module('_libcamera',
-                         pycamera_sources,
-                         install : true,
-                         install_dir : destdir,
-                         name_prefix : '',
-                         dependencies : pycamera_deps,
-                         cpp_args : pycamera_args)
-
-run_command('ln', '-fsT', '../../../../src/py/libcamera/__init__.py',
-            meson.current_build_dir() / '__init__.py',
-            check: true)
-
-install_data(['__init__.py'], install_dir : destdir)
+py3.install_sources('__init__.py',
+                    pure : false,
+                    subdir : 'libcamera')
 
 # \todo Generate stubs when building. See https://peps.python.org/pep-0484/#stub-files
 # Note: Depends on pybind11-stubgen. To generate pylibcamera stubs: