pkg-python.mk 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. ################################################################################
  2. # Python package infrastructure
  3. #
  4. # This file implements an infrastructure that eases development of
  5. # package .mk files for Python packages. It should be used for all
  6. # packages that use Python setup.py/setuptools as their build system.
  7. #
  8. # See the Buildroot documentation for details on the usage of this
  9. # infrastructure
  10. #
  11. # In terms of implementation, this Python infrastructure requires the
  12. # .mk file to only specify metadata informations about the package:
  13. # name, version, download URL, etc.
  14. #
  15. # We still allow the package .mk file to override what the different
  16. # steps are doing, if needed. For example, if <PKG>_BUILD_CMDS is
  17. # already defined, it is used as the list of commands to perform to
  18. # build the package, instead of the default Python behaviour. The
  19. # package can also define some post operation hooks.
  20. #
  21. ################################################################################
  22. # Target distutils-based packages
  23. PKG_PYTHON_DISTUTILS_ENV = \
  24. PATH="$(TARGET_PATH)" \
  25. CC="$(TARGET_CC)" \
  26. CFLAGS="$(TARGET_CFLAGS)" \
  27. LDFLAGS="$(TARGET_LDFLAGS)" \
  28. LDSHARED="$(TARGET_CROSS)gcc -shared" \
  29. PYTHONPATH="$(PYTHON_PATH)" \
  30. _python_sysroot=$(STAGING_DIR) \
  31. _python_prefix=/usr \
  32. _python_exec_prefix=/usr
  33. PKG_PYTHON_DISTUTILS_BUILD_OPT = \
  34. --executable=/usr/bin/python
  35. PKG_PYTHON_DISTUTILS_INSTALL_OPT = \
  36. --prefix=$(TARGET_DIR)/usr
  37. # Host distutils-based packages
  38. HOST_PKG_PYTHON_DISTUTILS_ENV = \
  39. PATH="$(HOST_PATH)"
  40. HOST_PKG_PYTHON_DISTUTILS_INSTALL_OPT = \
  41. --prefix=$(HOST_DIR)/usr
  42. # Target setuptools-based packages
  43. PKG_PYTHON_SETUPTOOLS_ENV = \
  44. PATH="$(TARGET_PATH)" \
  45. PYTHONPATH="$(PYTHON_PATH)" \
  46. _python_sysroot=$(STAGING_DIR) \
  47. _python_prefix=/usr \
  48. _python_exec_prefix=/usr
  49. PKG_PYTHON_SETUPTOOLS_INSTALL_OPT = \
  50. --prefix=$(TARGET_DIR)/usr \
  51. --executable=/usr/bin/python \
  52. --single-version-externally-managed \
  53. --root=/
  54. # Host setuptools-based packages
  55. HOST_PKG_PYTHON_SETUPTOOLS_ENV = \
  56. PATH="$(HOST_PATH)"
  57. HOST_PKG_PYTHON_SETUPTOOLS_INSTALL_OPT = \
  58. --prefix=$(HOST_DIR)/usr
  59. ################################################################################
  60. # inner-python-package -- defines how the configuration, compilation
  61. # and installation of a Python package should be done, implements a
  62. # few hooks to tune the build process and calls the generic package
  63. # infrastructure to generate the necessary make targets
  64. #
  65. # argument 1 is the lowercase package name
  66. # argument 2 is the uppercase package name, including an HOST_ prefix
  67. # for host packages
  68. # argument 3 is the uppercase package name, without the HOST_ prefix
  69. # for host packages
  70. # argument 4 is the type (target or host)
  71. ################################################################################
  72. define inner-python-package
  73. $(2)_SRCDIR = $$($(2)_DIR)/$($(2)_SUBDIR)
  74. $(2)_BUILDDIR = $$($(2)_SRCDIR)
  75. $(2)_ENV ?=
  76. $(2)_BUILD_OPT ?=
  77. $(2)_INSTALL_OPT ?=
  78. ifndef $(2)_SETUP_TYPE
  79. ifdef $(3)_SETUP_TYPE
  80. $(2)_SETUP_TYPE = $($(3)_SETUP_TYPE)
  81. else
  82. $$(error "$(2)_SETUP_TYPE must be set")
  83. endif
  84. endif
  85. # Distutils
  86. ifeq ($$($(2)_SETUP_TYPE),distutils)
  87. ifeq ($(4),target)
  88. $(2)_BASE_ENV = $$(PKG_PYTHON_DISTUTILS_ENV)
  89. $(2)_BASE_BUILD_TGT = build
  90. $(2)_BASE_BUILD_OPT = $$(PKG_PYTHON_DISTUTILS_BUILD_OPT)
  91. $(2)_BASE_INSTALL_OPT = $$(PKG_PYTHON_DISTUTILS_INSTALL_OPT)
  92. else
  93. $(2)_BASE_ENV = $$(HOST_PKG_PYTHON_DISTUTILS_ENV)
  94. $(2)_BASE_BUILD_TGT = build
  95. $(2)_BASE_BUILD_OPT =
  96. $(2)_BASE_INSTALL_OPT = $$(HOST_PKG_PYTHON_DISTUTILS_INSTALL_OPT)
  97. endif
  98. # Setuptools
  99. else ifeq ($$($(2)_SETUP_TYPE),setuptools)
  100. ifeq ($(4),target)
  101. $(2)_BASE_ENV = $$(PKG_PYTHON_SETUPTOOLS_ENV)
  102. $(2)_BASE_BUILD_TGT = build
  103. $(2)_BASE_BUILD_OPT =
  104. $(2)_BASE_INSTALL_OPT = $$(PKG_PYTHON_SETUPTOOLS_INSTALL_OPT)
  105. else
  106. $(2)_BASE_ENV = $$(HOST_PKG_PYTHON_SETUPTOOLS_ENV)
  107. $(2)_BASE_BUILD_TGT = build
  108. $(2)_BASE_BUILD_OPT =
  109. $(2)_BASE_INSTALL_OPT = $$(HOST_PKG_PYTHON_SETUPTOOLS_INSTALL_OPT)
  110. endif
  111. else
  112. $$(error "Invalid $(2)_SETUP_TYPE. Valid options are 'distutils' or 'setuptools'")
  113. endif
  114. # The below statement intends to calculate the dependencies of host
  115. # packages by derivating them from the dependencies of the
  116. # corresponding target package, after adding the 'host-' prefix in
  117. # front of the dependencies.
  118. #
  119. # However it must be repeated from inner-generic-package, as we need
  120. # to exclude the python, host-python and host-python-setuptools
  121. # packages, which are added below in the list of dependencies
  122. # depending on the package characteristics, and shouldn't be derived
  123. # automatically from the dependencies of the corresponding target
  124. # package.
  125. $(2)_DEPENDENCIES ?= $(filter-out host-python host-python-setuptools host-toolchain $(1),$(patsubst host-host-%,host-%,$(addprefix host-,$($(3)_DEPENDENCIES))))
  126. # Target packages need both the python interpreter on the target (for
  127. # runtime) and the python interpreter on the host (for
  128. # compilation). However, host packages only need the python
  129. # interpreter on the host.
  130. ifeq ($(4),target)
  131. $(2)_DEPENDENCIES += host-python python
  132. else
  133. $(2)_DEPENDENCIES += host-python
  134. endif
  135. # Setuptools based packages will need host-python-setuptools (both
  136. # host and target). We need to have a special exclusion for the
  137. # host-setuptools package itself: it is setuptools-based, but
  138. # shouldn't depend on host-setuptools (because it would otherwise
  139. # depend on itself!).
  140. ifeq ($$($(2)_SETUP_TYPE),setuptools)
  141. ifneq ($(2),HOST_PYTHON_SETUPTOOLS)
  142. $(2)_DEPENDENCIES += host-python-setuptools
  143. endif
  144. endif
  145. #
  146. # Build step. Only define it if not already defined by the package .mk
  147. # file.
  148. #
  149. ifndef $(2)_BUILD_CMDS
  150. define $(2)_BUILD_CMDS
  151. (cd $$($$(PKG)_BUILDDIR)/; \
  152. $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
  153. $(HOST_DIR)/usr/bin/python setup.py \
  154. $$($$(PKG)_BASE_BUILD_TGT) \
  155. $$($$(PKG)_BASE_BUILD_OPT) $$($$(PKG)_BUILD_OPT))
  156. endef
  157. endif
  158. #
  159. # Host installation step. Only define it if not already defined by the
  160. # package .mk file.
  161. #
  162. ifndef $(2)_INSTALL_CMDS
  163. define $(2)_INSTALL_CMDS
  164. (cd $$($$(PKG)_BUILDDIR)/; \
  165. $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
  166. $(HOST_DIR)/usr/bin/python setup.py install \
  167. $$($$(PKG)_BASE_INSTALL_OPT) $$($$(PKG)_INSTALL_OPT))
  168. endef
  169. endif
  170. #
  171. # Target installation step. Only define it if not already defined by
  172. # the package .mk file.
  173. #
  174. ifndef $(2)_INSTALL_TARGET_CMDS
  175. define $(2)_INSTALL_TARGET_CMDS
  176. (cd $$($$(PKG)_BUILDDIR)/; \
  177. $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
  178. $(HOST_DIR)/usr/bin/python setup.py install \
  179. $$($$(PKG)_BASE_INSTALL_OPT) $$($$(PKG)_INSTALL_OPT))
  180. endef
  181. endif
  182. # Call the generic package infrastructure to generate the necessary
  183. # make targets
  184. $(call inner-generic-package,$(1),$(2),$(3),$(4))
  185. endef
  186. ################################################################################
  187. # python-package -- the target generator macro for Python packages
  188. ################################################################################
  189. python-package = $(call inner-python-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
  190. host-python-package = $(call inner-python-package,host-$(pkgname),$(call UPPERCASE,host-$(pkgname)),$(call UPPERCASE,$(pkgname)),host)