Skip to content

Commit dd5417d

Browse files
committed
More recipe documentation
1 parent 03c676a commit dd5417d

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

doc/source/recipes.rst

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,74 @@ create a generic mechanism for all Python modules.
238238
Using a CythonRecipe
239239
--------------------
240240

241+
If your recipe is to install a Python module that uses Cython, you
242+
should use a CythonRecipe. This overrides ``build_arch`` to both build
243+
the cython components and to install the Python module just like a
244+
normal PythonRecipe.
245+
246+
For instance, the following is all that's necessary to make a recipe
247+
for Kivy (in this case, depending on Pygame rather than SDL2)::
248+
249+
class KivyRecipe(CythonRecipe):
250+
version = 'stable'
251+
url = 'https://github.com/kivy/kivy/archive/{version}.zip'
252+
name = 'kivy'
253+
254+
depends = ['pygame', 'pyjnius', 'android']
255+
256+
recipe = KivyRecipe()
257+
258+
For reference, the code that accomplishes this is the following::
259+
260+
def build_arch(self, arch):
261+
Recipe.build_arch(self, arch) # a hack to avoid calling
262+
# PythonRecipe.build_arch
263+
self.build_cython_components(arch)
264+
self.install_python_package() # this is the same as in a PythonRecipe
265+
266+
def build_cython_components(self, arch):
267+
env = self.get_recipe_env(arch)
268+
with current_directory(self.get_build_dir(arch.arch)):
269+
hostpython = sh.Command(self.ctx.hostpython)
270+
271+
# This first attempt *will* fail, because cython isn't
272+
# installed in the hostpython
273+
try:
274+
shprint(hostpython, 'setup.py', 'build_ext', _env=env)
275+
except sh.ErrorReturnCode_1:
276+
pass
277+
278+
# ...so we manually run cython from the user's system
279+
shprint(sh.find, self.get_build_dir('armeabi'), '-iname', '*.pyx', '-exec',
280+
self.ctx.cython, '{}', ';', _env=env)
281+
282+
# now cython has already been run so the build works
283+
shprint(hostpython, 'setup.py', 'build_ext', '-v', _env=env)
284+
285+
# stripping debug symbols lowers the file size a lot
286+
build_lib = glob.glob('./build/lib*')
287+
shprint(sh.find, build_lib[0], '-name', '*.o', '-exec',
288+
env['STRIP'], '{}', ';', _env=env)
289+
290+
The failing build and manual cythonisation is necessary, first to
291+
make sure that any .pyx files have been generated by setup.py, and
292+
second because cython isn't installed in the hostpython build.
293+
294+
Other than this, these methods follow the techniques in the above
295+
documentation to make a generic recipe for most cython based modules.
296+
241297
Using a CompiledComponentsPythonRecipe
242298
--------------------------------------
243299

300+
This is similar to a CythonRecipe but is intended for modules like
301+
numpy which include compiled but non-cython components. It uses a
302+
similar mechanism to compile with the right environment.
303+
304+
This isn't documented yet because it will probably be changed so that
305+
CythonRecipe inherits from it (to avoid code duplication).
306+
307+
Using an NDKRecipe
308+
------------------
244309

245310
.. _recipe_class:
246311

0 commit comments

Comments
 (0)