We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
ffi.h: No such file or directory (solutions included)
# Problem summary
On Gentoo Linux `cffi` recipe host install fails with the error below:
```
c/_cffi_backend.c:13:17: fatal error: ffi.h: No such file or directory
```

# Detailed description
This is because `ffi.h` is installed in `/usr/lib64/libffi-3.2.1/include/ffi.h` which is not part of gcc default include paths.
Here is where `ffi.h` is installed (`equery f` is the Gentoo equivalent to Debian-like `dpkg -L`):
```
$ equery f dev-libs/libffi | grep ffi.h
/usr/lib64/libffi-3.2.1/include/ffi.h
```
Or with `dpkg-config`:
```
$ pkg-config --cflags libffi
-I/usr/lib64/libffi-3.2.1/include
```
And here are the gcc default include paths:
```
$ echo | gcc -E -Wp,-v -
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-pc-linux-gnu/5.4.0/include
 /usr/lib/gcc/x86_64-pc-linux-gnu/5.4.0/include-fixed
 /usr/include
End of search list.
```

# Complete error output
Below is the detailed `buildozer android debug` (cffi) output log:
```
[INFO]:    Installing cffi into site-packages                                                                                                                                                                                                                     [66/1768]
[INFO]:    -> directory context /home/andre/Progz/PyWallet/.buildozer/android/platform/build/build/other_builds/cffi-python2/armeabi-v7a/cffi                                                                                                                              
[INFO]:    -> running python.host setup.py install -O2                                                                                                                                                                                                                     
[INFO]:    Rebuilding compiled components in cffi                                                                                                                                                                                                                          
[INFO]:    -> running hostpython setup.py clean --all                                                                                
[INFO]:    -> running hostpython setup.py build_ext -v                                                                               
Exception in thread background thread for pid 19037:                                                                                 
Traceback (most recent call last):                                                                                                   
  File "/usr/lib64/python3.4/threading.py", line 911, in _bootstrap_inner                                                            
    self.run()                                                                                                                       
  File "/usr/lib64/python3.4/threading.py", line 859, in run                                                                         
    self._target(*self._args, **self._kwargs)                                                                                        
  File "/home/andre/.local/lib64/python3.4/site-packages/sh.py", line 2170, in background_thread                                     
    handle_exit_code(exit_code)                                                                                                      
  File "/home/andre/.local/lib64/python3.4/site-packages/sh.py", line 1929, in fn                                                    
    return self.command.handle_command_exit_code(exit_code)                                                                          
  File "/home/andre/.local/lib64/python3.4/site-packages/sh.py", line 672, in handle_command_exit_code                               
    raise exc                                                                                                                        
sh.ErrorReturnCode_1:                                                                                                                
                                                                                                                                     
  RAN: /home/andre/Progz/PyWallet/.buildozer/android/platform/build/build/other_builds/hostpython2/desktop/hostpython2/hostpython setup.py build_ext -v
                                                                                                                                     
  STDOUT:                                                                                                                            
running build_ext                                                                                                                    
building '_cffi_backend' extension
creating build
creating build/temp.linux-x86_64-2.7
creating build/temp.linux-x86_64-2.7/c
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -DUSE__THREAD -I/home/andre/Progz/PyWallet/.buildozer/android/platform/build/build/other_builds/hostpython2/desktop/hostpython2/Include -I/home/andre/Progz/PyWallet/.buildozer/android/platform/build/build/other_builds/hostpython2/desktop/hostpython2 -c c/_cffi_backend.c -o build/temp.linux-x86_64-2.7/c/_cffi_backend.o
c/_cffi_backend.c:13:17: fatal error: ffi.h: No such file or directory
compilation terminated.
```

# Solutions / workarounds
Below are few solutions/workarounds I had in mind.

## Quick'n dirty
Symlink libffi includes to gcc default include path:
```
ln -sfn /usr/lib64/libffi-3.2.1/include/ffi.h /usr/include/
ln -sfn /usr/lib64/libffi-3.2.1/include/ffitarget.h /usr/include/
```
While it works it's an unsatisfying solution.

## `dpkg-config` when possible (i.e. host)
We could update current [disable-pkg-config.patch](https://github.com/kivy/python-for-android/blob/0.5.3/pythonforandroid/recipes/cffi/disable-pkg-config.patch) to `try` to run, but `except` silently. That way if `dpkg-config` is available on the host, we can use it, but it won't fail if not.

## `dpkg-config` recipe
Since `cffi` is not the first package, nor the last needing `dpkg-config`, we could create a recipe for it to make it available for the target architecture.
https://autotools.io/pkgconfig/cross-compiling.html

## Install `libffi` recipe for the host
I'm not too sure about that one, but the idea would be to update the `libffi` recipe so it also makes the library/headers available for the host so we could point to it. But it could be a mess.

# Final word
I have a preference for the "`dpkg-config` when possible (i.e. host)" solution. It seems easy enough to implement and it would fix the issue. For the long run I prefer if we had `dpkg-config` available for the target, but I'm not sure how achievable it is.
I would be glad to contribute more to python-for-android, so please let me know which solution you prefer and I could work on a pull request when I get time.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pythonforandroid/recipes/cffi/__init__.py`
Content:
```
1 import os
2 from pythonforandroid.recipe import CompiledComponentsPythonRecipe
3 
4 
5 class CffiRecipe(CompiledComponentsPythonRecipe):
6     name = 'cffi'
7     version = '1.4.2'
8     url = 'https://pypi.python.org/packages/source/c/cffi/cffi-{version}.tar.gz'
9 
10     depends = [('python2', 'python3crystax'), 'setuptools', 'pycparser', 'libffi']
11 
12     patches = ['disable-pkg-config.patch']
13 
14     # call_hostpython_via_targetpython = False
15     install_in_hostpython = True
16 
17     def get_recipe_env(self, arch=None):
18         env = super(CffiRecipe, self).get_recipe_env(arch)
19         # sets linker to use the correct gcc (cross compiler)
20         env['LDSHARED'] = env['CC'] + ' -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions'
21         libffi = self.get_recipe('libffi', self.ctx)
22         includes = libffi.get_include_dirs(arch)
23         env['CFLAGS'] = ' -I'.join([env.get('CFLAGS', '')] + includes)
24         env['LDFLAGS'] = (env.get('CFLAGS', '') + ' -L' +
25                           self.ctx.get_libs_dir(arch.arch))
26         env['LDFLAGS'] += ' -L{}'.format(os.path.join(self.ctx.bootstrap.build_dir, 'libs', arch.arch))
27         # required for libc and libdl
28         ndk_dir = self.ctx.ndk_platform
29         ndk_lib_dir = os.path.join(ndk_dir, 'usr', 'lib')
30         env['LDFLAGS'] += ' -L{}'.format(ndk_lib_dir)
31         env['LDFLAGS'] += " --sysroot={}".format(self.ctx.ndk_platform)
32         env['PYTHONPATH'] = ':'.join([
33             self.ctx.get_site_packages_dir(),
34             env['BUILDLIB_PATH'],
35         ])
36         if self.ctx.ndk == 'crystax':
37             # only keeps major.minor (discards patch)
38             python_version = self.ctx.python_recipe.version[0:3]
39             ndk_dir_python = os.path.join(self.ctx.ndk_dir, 'sources/python/', python_version)
40             env['LDFLAGS'] += ' -L{}'.format(os.path.join(ndk_dir_python, 'libs', arch.arch))
41             env['LDFLAGS'] += ' -lpython{}m'.format(python_version)
42             # until `pythonforandroid/archs.py` gets merged upstream:
43             # https://github.com/kivy/python-for-android/pull/1250/files#diff-569e13021e33ced8b54385f55b49cbe6
44             env['CFLAGS'] += ' -I{}/include/python/'.format(ndk_dir_python)
45         return env
46 
47 
48 recipe = CffiRecipe()
49
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/pythonforandroid/recipes/cffi/__init__.py b/pythonforandroid/recipes/cffi/__init__.py
--- a/pythonforandroid/recipes/cffi/__init__.py
+++ b/pythonforandroid/recipes/cffi/__init__.py
@@ -14,6 +14,14 @@
     # call_hostpython_via_targetpython = False
     install_in_hostpython = True
 
+    def get_hostrecipe_env(self, arch=None):
+        # fixes missing ffi.h on some host systems (e.g. gentoo)
+        env = super(CffiRecipe, self).get_hostrecipe_env(arch)
+        libffi = self.get_recipe('libffi', self.ctx)
+        includes = libffi.get_include_dirs(arch)
+        env['FFI_INC'] = ",".join(includes)
+        return env
+
     def get_recipe_env(self, arch=None):
         env = super(CffiRecipe, self).get_recipe_env(arch)
         # sets linker to use the correct gcc (cross compiler)

{"golden_diff": "diff --git a/pythonforandroid/recipes/cffi/__init__.py b/pythonforandroid/recipes/cffi/__init__.py\n--- a/pythonforandroid/recipes/cffi/__init__.py\n+++ b/pythonforandroid/recipes/cffi/__init__.py\n@@ -14,6 +14,14 @@\n     # call_hostpython_via_targetpython = False\n     install_in_hostpython = True\n \n+    def get_hostrecipe_env(self, arch=None):\n+        # fixes missing ffi.h on some host systems (e.g. gentoo)\n+        env = super(CffiRecipe, self).get_hostrecipe_env(arch)\n+        libffi = self.get_recipe('libffi', self.ctx)\n+        includes = libffi.get_include_dirs(arch)\n+        env['FFI_INC'] = \",\".join(includes)\n+        return env\n+\n     def get_recipe_env(self, arch=None):\n         env = super(CffiRecipe, self).get_recipe_env(arch)\n         # sets linker to use the correct gcc (cross compiler)\n", "issue": "ffi.h: No such file or directory (solutions included)\n# Problem summary\r\nOn Gentoo Linux `cffi` recipe host install fails with the error below:\r\n```\r\nc/_cffi_backend.c:13:17: fatal error: ffi.h: No such file or directory\r\n```\r\n\r\n# Detailed description\r\nThis is because `ffi.h` is installed in `/usr/lib64/libffi-3.2.1/include/ffi.h` which is not part of gcc default include paths.\r\nHere is where `ffi.h` is installed (`equery f` is the Gentoo equivalent to Debian-like `dpkg -L`):\r\n```\r\n$ equery f dev-libs/libffi | grep ffi.h\r\n/usr/lib64/libffi-3.2.1/include/ffi.h\r\n```\r\nOr with `dpkg-config`:\r\n```\r\n$ pkg-config --cflags libffi\r\n-I/usr/lib64/libffi-3.2.1/include\r\n```\r\nAnd here are the gcc default include paths:\r\n```\r\n$ echo | gcc -E -Wp,-v -\r\n#include \"...\" search starts here:\r\n#include <...> search starts here:\r\n /usr/lib/gcc/x86_64-pc-linux-gnu/5.4.0/include\r\n /usr/lib/gcc/x86_64-pc-linux-gnu/5.4.0/include-fixed\r\n /usr/include\r\nEnd of search list.\r\n```\r\n\r\n# Complete error output\r\nBelow is the detailed `buildozer android debug` (cffi) output log:\r\n```\r\n[INFO]:    Installing cffi into site-packages                                                                                                                                                                                                                     [66/1768]\r\n[INFO]:    -> directory context /home/andre/Progz/PyWallet/.buildozer/android/platform/build/build/other_builds/cffi-python2/armeabi-v7a/cffi                                                                                                                              \r\n[INFO]:    -> running python.host setup.py install -O2                                                                                                                                                                                                                     \r\n[INFO]:    Rebuilding compiled components in cffi                                                                                                                                                                                                                          \r\n[INFO]:    -> running hostpython setup.py clean --all                                                                                \r\n[INFO]:    -> running hostpython setup.py build_ext -v                                                                               \r\nException in thread background thread for pid 19037:                                                                                 \r\nTraceback (most recent call last):                                                                                                   \r\n  File \"/usr/lib64/python3.4/threading.py\", line 911, in _bootstrap_inner                                                            \r\n    self.run()                                                                                                                       \r\n  File \"/usr/lib64/python3.4/threading.py\", line 859, in run                                                                         \r\n    self._target(*self._args, **self._kwargs)                                                                                        \r\n  File \"/home/andre/.local/lib64/python3.4/site-packages/sh.py\", line 2170, in background_thread                                     \r\n    handle_exit_code(exit_code)                                                                                                      \r\n  File \"/home/andre/.local/lib64/python3.4/site-packages/sh.py\", line 1929, in fn                                                    \r\n    return self.command.handle_command_exit_code(exit_code)                                                                          \r\n  File \"/home/andre/.local/lib64/python3.4/site-packages/sh.py\", line 672, in handle_command_exit_code                               \r\n    raise exc                                                                                                                        \r\nsh.ErrorReturnCode_1:                                                                                                                \r\n                                                                                                                                     \r\n  RAN: /home/andre/Progz/PyWallet/.buildozer/android/platform/build/build/other_builds/hostpython2/desktop/hostpython2/hostpython setup.py build_ext -v\r\n                                                                                                                                     \r\n  STDOUT:                                                                                                                            \r\nrunning build_ext                                                                                                                    \r\nbuilding '_cffi_backend' extension\r\ncreating build\r\ncreating build/temp.linux-x86_64-2.7\r\ncreating build/temp.linux-x86_64-2.7/c\r\ngcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -DUSE__THREAD -I/home/andre/Progz/PyWallet/.buildozer/android/platform/build/build/other_builds/hostpython2/desktop/hostpython2/Include -I/home/andre/Progz/PyWallet/.buildozer/android/platform/build/build/other_builds/hostpython2/desktop/hostpython2 -c c/_cffi_backend.c -o build/temp.linux-x86_64-2.7/c/_cffi_backend.o\r\nc/_cffi_backend.c:13:17: fatal error: ffi.h: No such file or directory\r\ncompilation terminated.\r\n```\r\n\r\n# Solutions / workarounds\r\nBelow are few solutions/workarounds I had in mind.\r\n\r\n## Quick'n dirty\r\nSymlink libffi includes to gcc default include path:\r\n```\r\nln -sfn /usr/lib64/libffi-3.2.1/include/ffi.h /usr/include/\r\nln -sfn /usr/lib64/libffi-3.2.1/include/ffitarget.h /usr/include/\r\n```\r\nWhile it works it's an unsatisfying solution.\r\n\r\n## `dpkg-config` when possible (i.e. host)\r\nWe could update current [disable-pkg-config.patch](https://github.com/kivy/python-for-android/blob/0.5.3/pythonforandroid/recipes/cffi/disable-pkg-config.patch) to `try` to run, but `except` silently. That way if `dpkg-config` is available on the host, we can use it, but it won't fail if not.\r\n\r\n## `dpkg-config` recipe\r\nSince `cffi` is not the first package, nor the last needing `dpkg-config`, we could create a recipe for it to make it available for the target architecture.\r\nhttps://autotools.io/pkgconfig/cross-compiling.html\r\n\r\n## Install `libffi` recipe for the host\r\nI'm not too sure about that one, but the idea would be to update the `libffi` recipe so it also makes the library/headers available for the host so we could point to it. But it could be a mess.\r\n\r\n# Final word\r\nI have a preference for the \"`dpkg-config` when possible (i.e. host)\" solution. It seems easy enough to implement and it would fix the issue. For the long run I prefer if we had `dpkg-config` available for the target, but I'm not sure how achievable it is.\r\nI would be glad to contribute more to python-for-android, so please let me know which solution you prefer and I could work on a pull request when I get time.\n", "before_files": [{"content": "import os\nfrom pythonforandroid.recipe import CompiledComponentsPythonRecipe\n\n\nclass CffiRecipe(CompiledComponentsPythonRecipe):\n    name = 'cffi'\n    version = '1.4.2'\n    url = 'https://pypi.python.org/packages/source/c/cffi/cffi-{version}.tar.gz'\n\n    depends = [('python2', 'python3crystax'), 'setuptools', 'pycparser', 'libffi']\n\n    patches = ['disable-pkg-config.patch']\n\n    # call_hostpython_via_targetpython = False\n    install_in_hostpython = True\n\n    def get_recipe_env(self, arch=None):\n        env = super(CffiRecipe, self).get_recipe_env(arch)\n        # sets linker to use the correct gcc (cross compiler)\n        env['LDSHARED'] = env['CC'] + ' -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions'\n        libffi = self.get_recipe('libffi', self.ctx)\n        includes = libffi.get_include_dirs(arch)\n        env['CFLAGS'] = ' -I'.join([env.get('CFLAGS', '')] + includes)\n        env['LDFLAGS'] = (env.get('CFLAGS', '') + ' -L' +\n                          self.ctx.get_libs_dir(arch.arch))\n        env['LDFLAGS'] += ' -L{}'.format(os.path.join(self.ctx.bootstrap.build_dir, 'libs', arch.arch))\n        # required for libc and libdl\n        ndk_dir = self.ctx.ndk_platform\n        ndk_lib_dir = os.path.join(ndk_dir, 'usr', 'lib')\n        env['LDFLAGS'] += ' -L{}'.format(ndk_lib_dir)\n        env['LDFLAGS'] += \" --sysroot={}\".format(self.ctx.ndk_platform)\n        env['PYTHONPATH'] = ':'.join([\n            self.ctx.get_site_packages_dir(),\n            env['BUILDLIB_PATH'],\n        ])\n        if self.ctx.ndk == 'crystax':\n            # only keeps major.minor (discards patch)\n            python_version = self.ctx.python_recipe.version[0:3]\n            ndk_dir_python = os.path.join(self.ctx.ndk_dir, 'sources/python/', python_version)\n            env['LDFLAGS'] += ' -L{}'.format(os.path.join(ndk_dir_python, 'libs', arch.arch))\n            env['LDFLAGS'] += ' -lpython{}m'.format(python_version)\n            # until `pythonforandroid/archs.py` gets merged upstream:\n            # https://github.com/kivy/python-for-android/pull/1250/files#diff-569e13021e33ced8b54385f55b49cbe6\n            env['CFLAGS'] += ' -I{}/include/python/'.format(ndk_dir_python)\n        return env\n\n\nrecipe = CffiRecipe()\n", "path": "pythonforandroid/recipes/cffi/__init__.py"}], "after_files": [{"content": "import os\nfrom pythonforandroid.recipe import CompiledComponentsPythonRecipe\n\n\nclass CffiRecipe(CompiledComponentsPythonRecipe):\n    name = 'cffi'\n    version = '1.4.2'\n    url = 'https://pypi.python.org/packages/source/c/cffi/cffi-{version}.tar.gz'\n\n    depends = [('python2', 'python3crystax'), 'setuptools', 'pycparser', 'libffi']\n\n    patches = ['disable-pkg-config.patch']\n\n    # call_hostpython_via_targetpython = False\n    install_in_hostpython = True\n\n    def get_hostrecipe_env(self, arch=None):\n        # fixes missing ffi.h on some host systems (e.g. gentoo)\n        env = super(CffiRecipe, self).get_hostrecipe_env(arch)\n        libffi = self.get_recipe('libffi', self.ctx)\n        includes = libffi.get_include_dirs(arch)\n        env['FFI_INC'] = \",\".join(includes)\n        return env\n\n    def get_recipe_env(self, arch=None):\n        env = super(CffiRecipe, self).get_recipe_env(arch)\n        # sets linker to use the correct gcc (cross compiler)\n        env['LDSHARED'] = env['CC'] + ' -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions'\n        libffi = self.get_recipe('libffi', self.ctx)\n        includes = libffi.get_include_dirs(arch)\n        env['CFLAGS'] = ' -I'.join([env.get('CFLAGS', '')] + includes)\n        env['LDFLAGS'] = (env.get('CFLAGS', '') + ' -L' +\n                          self.ctx.get_libs_dir(arch.arch))\n        env['LDFLAGS'] += ' -L{}'.format(os.path.join(self.ctx.bootstrap.build_dir, 'libs', arch.arch))\n        # required for libc and libdl\n        ndk_dir = self.ctx.ndk_platform\n        ndk_lib_dir = os.path.join(ndk_dir, 'usr', 'lib')\n        env['LDFLAGS'] += ' -L{}'.format(ndk_lib_dir)\n        env['LDFLAGS'] += \" --sysroot={}\".format(self.ctx.ndk_platform)\n        env['PYTHONPATH'] = ':'.join([\n            self.ctx.get_site_packages_dir(),\n            env['BUILDLIB_PATH'],\n        ])\n        if self.ctx.ndk == 'crystax':\n            # only keeps major.minor (discards patch)\n            python_version = self.ctx.python_recipe.version[0:3]\n            ndk_dir_python = os.path.join(self.ctx.ndk_dir, 'sources/python/', python_version)\n            env['LDFLAGS'] += ' -L{}'.format(os.path.join(ndk_dir_python, 'libs', arch.arch))\n            env['LDFLAGS'] += ' -lpython{}m'.format(python_version)\n            # until `pythonforandroid/archs.py` gets merged upstream:\n            # https://github.com/kivy/python-for-android/pull/1250/files#diff-569e13021e33ced8b54385f55b49cbe6\n            env['CFLAGS'] += ' -I{}/include/python/'.format(ndk_dir_python)\n        return env\n\n\nrecipe = CffiRecipe()\n", "path": "pythonforandroid/recipes/cffi/__init__.py"}]}