pkg-python.mk 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. CROSS_COMPILING=yes \
  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="$(TARGET_DIR)/usr/lib/python$(PYTHON_VERSION_MAJOR)/site-packages" \
  46. PYTHONXCPREFIX="$(STAGING_DIR)/usr/" \
  47. CROSS_COMPILING=yes \
  48. _python_sysroot=$(STAGING_DIR) \
  49. _python_prefix=/usr \
  50. _python_exec_prefix=/usr
  51. PKG_PYTHON_SETUPTOOLS_INSTALL_OPT = \
  52. --prefix=$(TARGET_DIR)/usr \
  53. --executable=/usr/bin/python \
  54. --single-version-externally-managed \
  55. --root=/
  56. # Host setuptools-based packages
  57. HOST_PKG_PYTHON_SETUPTOOLS_ENV = \
  58. PATH="$(HOST_PATH)" \
  59. PYTHONXCPREFIX="$(HOST_DIR)/usr/"
  60. HOST_PKG_PYTHON_SETUPTOOLS_INSTALL_OPT = \
  61. --prefix=$(HOST_DIR)/usr
  62. ################################################################################
  63. # inner-python-package -- defines how the configuration, compilation
  64. # and installation of a Python package should be done, implements a
  65. # few hooks to tune the build process and calls the generic package
  66. # infrastructure to generate the necessary make targets
  67. #
  68. # argument 1 is the lowercase package name
  69. # argument 2 is the uppercase package name, including an HOST_ prefix
  70. # for host packages
  71. # argument 3 is the uppercase package name, without the HOST_ prefix
  72. # for host packages
  73. # argument 4 is the type (target or host)
  74. ################################################################################
  75. define inner-python-package
  76. $(2)_SRCDIR = $$($(2)_DIR)/$($(2)_SUBDIR)
  77. $(2)_BUILDDIR = $$($(2)_SRCDIR)
  78. $(2)_ENV ?=
  79. $(2)_BUILD_OPT ?=
  80. $(2)_INSTALL_OPT ?=
  81. ifndef $(2)_SETUP_TYPE
  82. ifdef $(3)_SETUP_TYPE
  83. $(2)_SETUP_TYPE = $($(3)_SETUP_TYPE)
  84. else
  85. $$(error "$(2)_SETUP_TYPE must be set")
  86. endif
  87. endif
  88. # Distutils
  89. ifeq ($$($(2)_SETUP_TYPE),distutils)
  90. ifeq ($(4),target)
  91. $(2)_BASE_ENV = $$(PKG_PYTHON_DISTUTILS_ENV)
  92. $(2)_BASE_BUILD_TGT = build
  93. $(2)_BASE_BUILD_OPT = $$(PKG_PYTHON_DISTUTILS_BUILD_OPT)
  94. $(2)_BASE_INSTALL_OPT = $$(PKG_PYTHON_DISTUTILS_INSTALL_OPT)
  95. else
  96. $(2)_BASE_ENV = $$(HOST_PKG_PYTHON_DISTUTILS_ENV)
  97. $(2)_BASE_BUILD_TGT = build
  98. $(2)_BASE_BUILD_OPT =
  99. $(2)_BASE_INSTALL_OPT = $$(HOST_PKG_PYTHON_DISTUTILS_INSTALL_OPT)
  100. endif
  101. # Setuptools
  102. else ifeq ($$($(2)_SETUP_TYPE),setuptools)
  103. ifeq ($(4),target)
  104. $(2)_BASE_ENV = $$(PKG_PYTHON_SETUPTOOLS_ENV)
  105. $(2)_BASE_BUILD_TGT = build -x
  106. $(2)_BASE_BUILD_OPT =
  107. $(2)_BASE_INSTALL_OPT = $$(PKG_PYTHON_SETUPTOOLS_INSTALL_OPT)
  108. else
  109. $(2)_BASE_ENV = $$(HOST_PKG_PYTHON_SETUPTOOLS_ENV)
  110. $(2)_BASE_BUILD_TGT = build
  111. $(2)_BASE_BUILD_OPT =
  112. $(2)_BASE_INSTALL_OPT = $$(HOST_PKG_PYTHON_SETUPTOOLS_INSTALL_OPT)
  113. endif
  114. else
  115. $$(error "Invalid $(2)_SETUP_TYPE. Valid options are 'distutils' or 'setuptools'")
  116. endif
  117. # The below statement intends to calculate the dependencies of host
  118. # packages by derivating them from the dependencies of the
  119. # corresponding target package, after adding the 'host-' prefix in
  120. # front of the dependencies.
  121. #
  122. # However it must be repeated from inner-generic-package, as we need
  123. # to exclude the python, host-python, host-python-setuptools and
  124. # host-distutilscross packages, which are added below in the list of
  125. # dependencies depending on the package characteristics, and shouldn't
  126. # be derived automatically from the dependencies of the corresponding
  127. # target package. For example, target packages need
  128. # host-python-distutilscross, but not host packages.
  129. $(2)_DEPENDENCIES ?= $(filter-out host-python host-python-setuptools host-python-distutilscross host-toolchain $(1),$(patsubst host-host-%,host-%,$(addprefix host-,$($(3)_DEPENDENCIES))))
  130. # Target packages need both the python interpreter on the target (for
  131. # runtime) and the python interpreter on the host (for
  132. # compilation). However, host packages only need the python
  133. # interpreter on the host.
  134. ifeq ($(4),target)
  135. $(2)_DEPENDENCIES += host-python python
  136. else
  137. $(2)_DEPENDENCIES += host-python
  138. endif
  139. # Setuptools based packages will need host-python-setuptools (both
  140. # host and target) and host-python-distutilscross (only target
  141. # packages). We need to have a special exclusion for the
  142. # host-setuptools package itself: it is setuptools-based, but
  143. # shouldn't depend on host-setuptools (because it would otherwise
  144. # depend on itself!).
  145. ifeq ($$($(2)_SETUP_TYPE),setuptools)
  146. ifneq ($(2),HOST_PYTHON_SETUPTOOLS)
  147. $(2)_DEPENDENCIES += host-python-setuptools
  148. ifeq ($(4),target)
  149. $(2)_DEPENDENCIES += host-python-distutilscross
  150. endif
  151. endif
  152. endif
  153. #
  154. # Build step. Only define it if not already defined by the package .mk
  155. # file.
  156. #
  157. ifndef $(2)_BUILD_CMDS
  158. define $(2)_BUILD_CMDS
  159. (cd $$($$(PKG)_BUILDDIR)/; \
  160. $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
  161. $(HOST_DIR)/usr/bin/python setup.py \
  162. $$($$(PKG)_BASE_BUILD_TGT) \
  163. $$($$(PKG)_BASE_BUILD_OPT) $$($$(PKG)_BUILD_OPT))
  164. endef
  165. endif
  166. #
  167. # Host installation step. Only define it if not already defined by the
  168. # package .mk file.
  169. #
  170. ifndef $(2)_INSTALL_CMDS
  171. define $(2)_INSTALL_CMDS
  172. (cd $$($$(PKG)_BUILDDIR)/; \
  173. $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
  174. $(HOST_DIR)/usr/bin/python setup.py install \
  175. $$($$(PKG)_BASE_INSTALL_OPT) $$($$(PKG)_INSTALL_OPT))
  176. endef
  177. endif
  178. #
  179. # Target installation step. Only define it if not already defined by
  180. # the package .mk file.
  181. #
  182. ifndef $(2)_INSTALL_TARGET_CMDS
  183. define $(2)_INSTALL_TARGET_CMDS
  184. (cd $$($$(PKG)_BUILDDIR)/; \
  185. $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
  186. $(HOST_DIR)/usr/bin/python setup.py install \
  187. $$($$(PKG)_BASE_INSTALL_OPT) $$($$(PKG)_INSTALL_OPT))
  188. endef
  189. endif
  190. # Call the generic package infrastructure to generate the necessary
  191. # make targets
  192. $(call inner-generic-package,$(1),$(2),$(3),$(4))
  193. endef
  194. ################################################################################
  195. # python-package -- the target generator macro for Python packages
  196. ################################################################################
  197. python-package = $(call inner-python-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
  198. host-python-package = $(call inner-python-package,host-$(pkgname),$(call UPPERCASE,host-$(pkgname)),$(call UPPERCASE,$(pkgname)),host)