adding-packages-python.txt 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // -*- mode:doc; -*-
  2. // vim: set syntax=asciidoc:
  3. Infrastructure for Python packages
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. This infrastructure applies to Python packages that use the standard
  6. Python setuptools mechanism as their build system, generally
  7. recognizable by the usage of a +setup.py+ script.
  8. [[python-package-tutorial]]
  9. +python-package+ tutorial
  10. ^^^^^^^^^^^^^^^^^^^^^^^^^
  11. First, let's see how to write a +.mk+ file for a Python package,
  12. with an example :
  13. ------------------------
  14. 01: ################################################################################
  15. 02: #
  16. 03: # python-foo
  17. 04: #
  18. 05: ################################################################################
  19. 06:
  20. 07: PYTHON_FOO_VERSION = 1.0
  21. 08: PYTHON_FOO_SOURCE = python-foo-$(LIBFOO_VERSION).tar.xz
  22. 09: PYTHON_FOO_SITE = http://www.foosoftware.org/download
  23. 10: PYTHON_FOO_LICENSE = BSD-3c
  24. 11: PYTHON_FOO_LICENSE_FILES = LICENSE
  25. 12: PYTHON_FOO_ENV = SOME_VAR=1
  26. 13: PYTHON_FOO_DEPENDENCIES = libmad
  27. 14: PYTHON_FOO_SETUP_TYPE = distutils
  28. 15:
  29. 16: $(eval $(python-package))
  30. ------------------------
  31. On line 7, we declare the version of the package.
  32. On line 8 and 9, we declare the name of the tarball (xz-ed tarball
  33. recommended) and the location of the tarball on the Web. Buildroot
  34. will automatically download the tarball from this location.
  35. On line 10 and 11, we give licensing details about the package (its
  36. license on line 10, and the file containing the license text on line
  37. 11).
  38. On line 12, we tell Buildroot to pass custom options to the Python
  39. +setup.py+ script when it is configuring the package.
  40. On line 13, we declare our dependencies, so that they are built
  41. before the build process of our package starts.
  42. On line 14, we declare the specific Python build system being used. In
  43. this case the +distutils+ Python build system is used. The two
  44. supported ones are +distutils+ and +setuptools+.
  45. Finally, on line 16, we invoke the +python-package+ macro that
  46. generates all the Makefile rules that actually allow the package to be
  47. built.
  48. [[python-package-reference]]
  49. +python-package+ reference
  50. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  51. As a policy, packages that merely provide Python modules should all be
  52. named +python-<something>+ in Buildroot. Other packages that use the
  53. Python build system, but are not Python modules, can freely choose
  54. their name (existing examples in Buildroot are +scons+ and
  55. +supervisor+).
  56. In their +Config.in+ file, they should depend on +BR2_PACKAGE_PYTHON+
  57. so that when Buildroot will enable Python 3 usage for modules, we will
  58. be able to enable Python modules progressively on Python 3.
  59. The main macro of the Python package infrastructure is
  60. +python-package+. It is similar to the +generic-package+ macro. It is
  61. also possible to create Python host packages with the
  62. +host-python-package+ macro.
  63. Just like the generic infrastructure, the Python infrastructure works
  64. by defining a number of variables before calling the +python-package+
  65. or +host-python-package+ macros.
  66. All the package metadata information variables that exist in the
  67. xref:generic-package-reference[generic package infrastructure] also
  68. exist in the Python infrastructure: +PYTHON_FOO_VERSION+,
  69. +PYTHON_FOO_SOURCE+, +PYTHON_FOO_PATCH+, +PYTHON_FOO_SITE+,
  70. +PYTHON_FOO_SUBDIR+, +PYTHON_FOO_DEPENDENCIES+, +PYTHON_FOO_LICENSE+,
  71. +PYTHON_FOO_LICENSE_FILES+, etc.
  72. Note that:
  73. * Setting +PYTHON_FOO_INSTALL_STAGING+ to +YES+ has no effect (unless
  74. a +PYTHON_FOO_INSTALL_STAGING_CMDS+ variable is defined), since
  75. Python modules generally don't need to be installed to the
  76. +staging+ directory.
  77. * It is not necessary to add +python+ or +host-python+ in the
  78. +PYTHON_FOO_DEPENDENCIES+ variable of a package, since these basic
  79. dependencies are automatically added as needed by the Python
  80. package infrastructure.
  81. * Similarly, it is not needed to add +host-setuptools+ and/or
  82. +host-distutilscross+ dependencies to +PYTHON_FOO_DEPENDENCIES+ for
  83. setuptools-based packages, since these are automatically added by
  84. the Python infrastructure as needed.
  85. One variable specific to the Python infrastructure is mandatory:
  86. * +PYTHON_FOO_SETUP_TYPE+, to define which Python build system is used
  87. by the package. The two supported values are +distutils+ and
  88. +setuptools+. If you don't know which one is used in your package,
  89. look at the +setup.py+ file in your package source code, and see
  90. whether it imports things from the +distutils+ module or the
  91. +setuptools+ module.
  92. A few additional variables, specific to the Python infrastructure, can
  93. optionally be defined, depending on the package's needs. Many of them
  94. are only useful in very specific cases, typical packages will
  95. therefore only use a few of them, or none.
  96. * +PYTHON_FOO_ENV+, to specify additional environment variables to
  97. pass to the Python +setup.py+ script (for both the build and install
  98. steps). Note that the infrastructure is automatically passing
  99. several standard variables, defined in +PKG_PYTHON_DISTUTILS_ENV+
  100. (for distutils target packages), +HOST_PKG_PYTHON_DISTUTILS_ENV+
  101. (for distutils host packages), +PKG_PYTHON_SETUPTOOLS_ENV+ (for
  102. setuptools target packages) and +HOST_PKG_PYTHON_SETUPTOOLS_ENV+
  103. (for setuptools host packages).
  104. * +PYTHON_FOO_BUILD_OPT+, to specify additional options to pass to the
  105. Python +setup.py+ script during the build step. For target distutils
  106. packages, the +PKG_PYTHON_DISTUTILS_BUILD_OPT+ options are already
  107. passed automatically by the infrastructure.
  108. * +PYTHON_FOO_INSTALL_OPT+, to specify additional options to pass to
  109. the Python +setup.py+ script during the installation step. Note that
  110. the infrastructure is automatically passing some options, defined in
  111. +PKG_PYTHON_DISTUTILS_INSTALL_OPT+ (for target distutils packages),
  112. +HOST_PKG_PYTHON_DISTUTILS_INSTALL_OPT+ (for host distutils
  113. packages), +PKG_PYTHON_SETUPTOOLS_INSTALL_OPT+ (for target
  114. setuptools packages) and +HOST_PKG_PYTHON_SETUPTOOLS_INSTALL_OPT+
  115. (for host setuptools packages).
  116. * +HOST_PYTHON_FOO_NEEDS_HOST_PYTHON+, to define the host python
  117. interpreter. The usage of this variable is limited to host
  118. packages. The two supported value are +python2+ and +python3+. It
  119. will ensures the right host python package is available and will
  120. invoke it for the build. If some build steps are overloaded, the
  121. right python interpreter must be explicitly called in the commands.
  122. With the Python infrastructure, all the steps required to build and
  123. install the packages are already defined, and they generally work well
  124. for most Python-based packages. However, when required, it is still
  125. possible to customize what is done in any particular step:
  126. * By adding a post-operation hook (after extract, patch, configure,
  127. build or install). See xref:hooks[] for details.
  128. * By overriding one of the steps. For example, even if the Python
  129. infrastructure is used, if the package +.mk+ file defines its own
  130. +PYTHON_FOO_BUILD_CMDS+ variable, it will be used instead of the
  131. default Python one. However, using this method should be restricted
  132. to very specific cases. Do not use it in the general case.