Repository: kivy/python-for-android

Issue:
How to add webp support to pillow?
<!--
The issue tracker is a tool to address bugs NOT a support platform.
Please use the Discord community or Stack Overflow for support questions,
more information at https://github.com/kivy/python-for-android#support
-->

### Checklist

- [ ] the issue is indeed a bug and not a support request
- [ x] issue doesn't already exist: https://github.com/kivy/python-for-android/issues
- [ ] I have a short, runnable example that reproduces the issue
- [ ] I reproduced the problem with the latest development version (`p4a.branch = develop`)
- [ ] I used the grave accent (aka backticks) to format code or logs when appropriated

### Versions

- Python: 3.8.3
- OS: Manjaro Budgie
- Kivy:1.11.1
- Cython:
- OpenJDK: 8

### Description
I am trying to make an app that works with webp files and converts them to png. The problem is that it seems to me that the Pillow recipe for python4android does not add webp support. How can I get the web support added with the recipe?





Discussion:

Resolving patch:
diff --git a/pythonforandroid/recipes/Pillow/__init__.py b/pythonforandroid/recipes/Pillow/__init__.py
--- a/pythonforandroid/recipes/Pillow/__init__.py
+++ b/pythonforandroid/recipes/Pillow/__init__.py
@@ -1,13 +1,33 @@
-from pythonforandroid.recipe import CompiledComponentsPythonRecipe
 from os.path import join
 
+from pythonforandroid.recipe import CompiledComponentsPythonRecipe
+
 
 class PillowRecipe(CompiledComponentsPythonRecipe):
+    """
+    A recipe for Pillow (previously known as Pil).
+
+    This recipe allow us to build the Pillow recipe with support for different
+    types of images and fonts. But you should be aware, that in order to  use
+    some of the features of  Pillow, we must build some libraries. By default
+    we automatically trigger the build of below libraries::
+
+        - freetype: rendering fonts support.
+        - harfbuzz: a text shaping library.
+        - jpeg: reading and writing JPEG image files.
+        - png: support for PNG images.
+
+    But you also could enable the build of some extra image types by requesting
+    the build of some libraries via argument `requirements`::
+
+        - libwebp: library to encode and decode images in WebP format.
+    """
 
     version = '7.0.0'
     url = 'https://github.com/python-pillow/Pillow/archive/{version}.tar.gz'
     site_packages_name = 'Pillow'
     depends = ['png', 'jpeg', 'freetype', 'setuptools']
+    opt_depends = ['libwebp']
     patches = [join('patches', 'fix-setup.patch')]
 
     call_hostpython_via_targetpython = False
@@ -36,20 +56,40 @@ def get_recipe_env(self, arch=None, with_flags_in_cc=True):
         harf_lib_dir = join(harfbuzz.get_build_dir(arch.arch), 'src', '.libs')
         harf_inc_dir = harfbuzz.get_build_dir(arch.arch)
 
-        env['JPEG_ROOT'] = '{}|{}'.format(jpeg_lib_dir, jpeg_inc_dir)
-        env['FREETYPE_ROOT'] = '{}|{}'.format(free_lib_dir, free_inc_dir)
-        env['ZLIB_ROOT'] = '{}|{}'.format(ndk_lib_dir, ndk_include_dir)
-
-        cflags = ' -I{}'.format(png_inc_dir)
-        cflags += ' -I{} -I{}'.format(harf_inc_dir, join(harf_inc_dir, 'src'))
-        cflags += ' -I{}'.format(free_inc_dir)
-        cflags += ' -I{}'.format(jpeg_inc_dir)
-        cflags += ' -I{}'.format(ndk_include_dir)
-
+        # these below env variables are defined at Pillow's `setup.py`
+        env['JPEG_ROOT'] = f'{jpeg_lib_dir}|{jpeg_inc_dir}'
+        env['FREETYPE_ROOT'] = f'{free_lib_dir}|{free_inc_dir}'
+        env['ZLIB_ROOT'] = f'{ndk_lib_dir}|{ndk_include_dir}'
+
+        # libwebp is an optional dependency, so we add the
+        # flags if we have it in our `ctx.recipe_build_order`
+        build_with_webp_support = 'libwebp' in self.ctx.recipe_build_order
+        if build_with_webp_support:
+            webp = self.get_recipe('libwebp', self.ctx)
+            webp_install = join(
+                webp.get_build_dir(arch.arch), 'installation'
+            )
+
+        # Add libraries includes to CFLAGS
+        cflags = f' -I{png_inc_dir}'
+        cflags += f' -I{harf_inc_dir} -I{join(harf_inc_dir, "src")}'
+        cflags += f' -I{free_inc_dir}'
+        cflags += f' -I{jpeg_inc_dir}'
+        if build_with_webp_support:
+            cflags += f' -I{join(webp_install, "include")}'
+        cflags += f' -I{ndk_include_dir}'
+
+        # Link the basic Pillow libraries...no need to add webp's libraries
+        # since it seems that the linkage is properly made without it :)
         env['LIBS'] = ' -lpng -lfreetype -lharfbuzz -ljpeg -lturbojpeg'
 
-        env['LDFLAGS'] += ' -L{} -L{} -L{} -L{}'.format(
-            png_lib_dir, harf_lib_dir, jpeg_lib_dir, ndk_lib_dir)
+        # Add libraries locations to LDFLAGS
+        env['LDFLAGS'] += f' -L{png_lib_dir}'
+        env['LDFLAGS'] += f' -L{harf_lib_dir}'
+        env['LDFLAGS'] += f' -L{jpeg_lib_dir}'
+        if build_with_webp_support:
+            env['LDFLAGS'] += f' -L{join(webp_install, "lib")}'
+        env['LDFLAGS'] += f' -L{ndk_lib_dir}'
         if cflags not in env['CFLAGS']:
             env['CFLAGS'] += cflags
         return env


Test patch: