adding-packages-python.adoc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // -*- mode:doc; -*-
  2. // vim: set syntax=asciidoc:
  3. === Infrastructure for Python packages
  4. This infrastructure applies to Python packages that use the standard
  5. Python setuptools or pep517 mechanisms as their build system, generally
  6. recognizable by the usage of a +setup.py+ script or +pyproject.toml+
  7. file.
  8. [[python-package-tutorial]]
  9. ==== +python-package+ tutorial
  10. First, let's see how to write a +.mk+ file for a Python package,
  11. with an example :
  12. ------------------------
  13. 01: ################################################################################
  14. 02: #
  15. 03: # python-foo
  16. 04: #
  17. 05: ################################################################################
  18. 06:
  19. 07: PYTHON_FOO_VERSION = 1.0
  20. 08: PYTHON_FOO_SOURCE = python-foo-$(PYTHON_FOO_VERSION).tar.xz
  21. 09: PYTHON_FOO_SITE = http://www.foosoftware.org/download
  22. 10: PYTHON_FOO_LICENSE = BSD-3-Clause
  23. 11: PYTHON_FOO_LICENSE_FILES = LICENSE
  24. 12: PYTHON_FOO_ENV = SOME_VAR=1
  25. 13: PYTHON_FOO_DEPENDENCIES = libmad
  26. 14: PYTHON_FOO_SETUP_TYPE = distutils
  27. 15:
  28. 16: $(eval $(python-package))
  29. ------------------------
  30. On line 7, we declare the version of the package.
  31. On line 8 and 9, we declare the name of the tarball (xz-ed tarball
  32. recommended) and the location of the tarball on the Web. Buildroot
  33. will automatically download the tarball from this location.
  34. On line 10 and 11, we give licensing details about the package (its
  35. license on line 10, and the file containing the license text on line
  36. 11).
  37. On line 12, we tell Buildroot to pass custom options to the Python
  38. +setup.py+ script when it is configuring the package.
  39. On line 13, we declare our dependencies, so that they are built
  40. before the build process of our package starts.
  41. On line 14, we declare the specific Python build system being used. In
  42. this case the +distutils+ Python build system is used. The four
  43. supported ones are +distutils+, +flit+, +pep517+ and +setuptools+.
  44. Finally, on line 16, we invoke the +python-package+ macro that
  45. generates all the Makefile rules that actually allow the package to be
  46. built.
  47. [[python-package-reference]]
  48. ==== +python-package+ reference
  49. As a policy, packages that merely provide Python modules should all be
  50. named +python-<something>+ in Buildroot. Other packages that use the
  51. Python build system, but are not Python modules, can freely choose
  52. their name (existing examples in Buildroot are +scons+ and
  53. +supervisor+).
  54. The main macro of the Python package infrastructure is
  55. +python-package+. It is similar to the +generic-package+ macro. It is
  56. also possible to create Python host packages with the
  57. +host-python-package+ macro.
  58. Just like the generic infrastructure, the Python infrastructure works
  59. by defining a number of variables before calling the +python-package+
  60. or +host-python-package+ macros.
  61. All the package metadata information variables that exist in the
  62. xref:generic-package-reference[generic package infrastructure] also
  63. exist in the Python infrastructure: +PYTHON_FOO_VERSION+,
  64. +PYTHON_FOO_SOURCE+, +PYTHON_FOO_PATCH+, +PYTHON_FOO_SITE+,
  65. +PYTHON_FOO_SUBDIR+, +PYTHON_FOO_DEPENDENCIES+, +PYTHON_FOO_LICENSE+,
  66. +PYTHON_FOO_LICENSE_FILES+, +PYTHON_FOO_INSTALL_STAGING+, etc.
  67. Note that:
  68. * It is not necessary to add +python+ or +host-python+ in the
  69. +PYTHON_FOO_DEPENDENCIES+ variable of a package, since these basic
  70. dependencies are automatically added as needed by the Python
  71. package infrastructure.
  72. * Similarly, it is not needed to add +host-setuptools+ to
  73. +PYTHON_FOO_DEPENDENCIES+ for setuptools-based packages, since it's
  74. automatically added by the Python infrastructure as needed.
  75. One variable specific to the Python infrastructure is mandatory:
  76. * +PYTHON_FOO_SETUP_TYPE+, to define which Python build system is used
  77. by the package. The four supported values are +distutils+, +flit+,
  78. +pep517+ and +setuptools+. If you don't know which one is used in
  79. your package, look at the +setup.py+ or +pyproject.toml+ file in your
  80. package source code, and see whether it imports things from the
  81. +distutils+, +flit+ module or the +setuptools+ module. If the package
  82. is using a +pyproject.toml+ file without any build-system requires
  83. and with a local in-tree backend-path one should use +pep517+.
  84. A few additional variables, specific to the Python infrastructure, can
  85. optionally be defined, depending on the package's needs. Many of them
  86. are only useful in very specific cases, typical packages will
  87. therefore only use a few of them, or none.
  88. * +PYTHON_FOO_SUBDIR+ may contain the name of a subdirectory inside the
  89. package that contains the main +setup.py+ or +pyproject.toml+ file.
  90. This is useful, if for example, the main +setup.py+ or +pyproject.toml+
  91. file is not at the root of the tree extracted by the tarball. If
  92. +HOST_PYTHON_FOO_SUBDIR+ is not specified, it defaults to
  93. +PYTHON_FOO_SUBDIR+.
  94. * +PYTHON_FOO_ENV+, to specify additional environment variables to
  95. pass to the Python +setup.py+ script (for distutils/setuptools
  96. packages) or the +support/scripts/pyinstaller.py+ script (for
  97. flit/pep517 packages) for both the build and install steps. Note
  98. that the infrastructure is automatically passing several standard
  99. variables, defined in +PKG_PYTHON_DISTUTILS_ENV+ (for distutils
  100. target packages), +HOST_PKG_PYTHON_DISTUTILS_ENV+ (for distutils
  101. host packages), +PKG_PYTHON_SETUPTOOLS_ENV+ (for setuptools target
  102. packages), +HOST_PKG_PYTHON_SETUPTOOLS_ENV+ (for setuptools host
  103. packages), +PKG_PYTHON_PEP517_ENV+ (for flit/pep517 target packages)
  104. and +HOST_PKG_PYTHON_PEP517_ENV+ (for flit/pep517 host packages).
  105. * +PYTHON_FOO_BUILD_OPTS+, to specify additional options to pass to the
  106. Python +setup.py+ script during the build step, this generally only
  107. makes sense to use for distutils/setuptools based packages as
  108. flit/pep517 based packages do not pass these options to a +setup.py+
  109. script but instead pass them to +support/scripts/pyinstaller.py+.
  110. * +PYTHON_FOO_INSTALL_TARGET_OPTS+, +PYTHON_FOO_INSTALL_STAGING_OPTS+,
  111. +HOST_PYTHON_FOO_INSTALL_OPTS+ to specify additional options to pass
  112. to the Python +setup.py+ script (for distutils/setuptools packages)
  113. or +support/scripts/pyinstaller.py+ (for flit/pep517 packages) during
  114. the target installation step, the staging installation step or the
  115. host installation, respectively.
  116. With the Python infrastructure, all the steps required to build and
  117. install the packages are already defined, and they generally work well
  118. for most Python-based packages. However, when required, it is still
  119. possible to customize what is done in any particular step:
  120. * By adding a post-operation hook (after extract, patch, configure,
  121. build or install). See xref:hooks[] for details.
  122. * By overriding one of the steps. For example, even if the Python
  123. infrastructure is used, if the package +.mk+ file defines its own
  124. +PYTHON_FOO_BUILD_CMDS+ variable, it will be used instead of the
  125. default Python one. However, using this method should be restricted
  126. to very specific cases. Do not use it in the general case.
  127. [[scanpypi]]
  128. ==== Generating a +python-package+ from a PyPI repository
  129. If the Python package for which you would like to create a Buildroot
  130. package is available on PyPI, you may want to use the +scanpypi+ tool
  131. located in +utils/+ to automate the process.
  132. You can find the list of existing PyPI packages
  133. https://pypi.python.org[here].
  134. +scanpypi+ requires Python's +setuptools+ package to be installed on
  135. your host.
  136. When at the root of your buildroot directory just do :
  137. -----------------------
  138. utils/scanpypi foo bar -o package
  139. -----------------------
  140. This will generate packages +python-foo+ and +python-bar+ in the package
  141. folder if they exist on https://pypi.python.org.
  142. Find the +external python modules+ menu and insert your package inside.
  143. Keep in mind that the items inside a menu should be in alphabetical order.
  144. Please keep in mind that you'll most likely have to manually check the
  145. package for any mistakes as there are things that cannot be guessed by
  146. the generator (e.g. dependencies on any of the python core modules
  147. such as BR2_PACKAGE_PYTHON_ZLIB). Also, please take note that the
  148. license and license files are guessed and must be checked. You also
  149. need to manually add the package to the +package/Config.in+ file.
  150. If your Buildroot package is not in the official Buildroot tree but in
  151. a br2-external tree, use the -o flag as follows:
  152. -----------------------
  153. utils/scanpypi foo bar -o other_package_dir
  154. -----------------------
  155. This will generate packages +python-foo+ and +python-bar+ in the
  156. +other_package_directory+ instead of +package+.
  157. Option +-h+ will list the available options:
  158. -----------------------
  159. utils/scanpypi -h
  160. -----------------------
  161. [[python-package-cffi-backend]]
  162. ==== +python-package+ CFFI backend
  163. C Foreign Function Interface for Python (CFFI) provides a convenient
  164. and reliable way to call compiled C code from Python using interface
  165. declarations written in C. Python packages relying on this backend can
  166. be identified by the appearance of a +cffi+ dependency in the
  167. +install_requires+ field of their +setup.py+ file.
  168. Such a package should:
  169. * add +python-cffi+ as a runtime dependency in order to install the
  170. compiled C library wrapper on the target. This is achieved by adding
  171. +select BR2_PACKAGE_PYTHON_CFFI+ to the package +Config.in+.
  172. ------------------------
  173. config BR2_PACKAGE_PYTHON_FOO
  174. bool "python-foo"
  175. select BR2_PACKAGE_PYTHON_CFFI # runtime
  176. ------------------------
  177. * add +host-python-cffi+ as a build-time dependency in order to
  178. cross-compile the C wrapper. This is achieved by adding
  179. +host-python-cffi+ to the +PYTHON_FOO_DEPENDENCIES+ variable.
  180. ------------------------
  181. ################################################################################
  182. #
  183. # python-foo
  184. #
  185. ################################################################################
  186. ...
  187. PYTHON_FOO_DEPENDENCIES = host-python-cffi
  188. $(eval $(python-package))
  189. ------------------------