1 ################################################################################
2 # Python package infrastructure
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.
8 # See the Buildroot documentation for details on the usage of this
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.
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.
21 ################################################################################
23 # Target distutils-based packages
24 PKG_PYTHON_DISTUTILS_ENV = \
25 PATH="$(TARGET_PATH)" \
27 CFLAGS="$(TARGET_CFLAGS)" \
28 LDFLAGS="$(TARGET_LDFLAGS)" \
29 LDSHARED="$(TARGET_CROSS)gcc -shared" \
30 PYTHONPATH="$(if $(BR2_PACKAGE_PYTHON3),$(PYTHON3_PATH),$(PYTHON_PATH))" \
31 _python_sysroot=$(STAGING_DIR) \
33 _python_exec_prefix=/usr
35 PKG_PYTHON_DISTUTILS_BUILD_OPT = \
36 --executable=/usr/bin/python
38 PKG_PYTHON_DISTUTILS_INSTALL_OPT = \
39 --prefix=$(TARGET_DIR)/usr
41 # Host distutils-based packages
42 HOST_PKG_PYTHON_DISTUTILS_ENV = \
45 HOST_PKG_PYTHON_DISTUTILS_INSTALL_OPT = \
46 --prefix=$(HOST_DIR)/usr
48 # Target setuptools-based packages
49 PKG_PYTHON_SETUPTOOLS_ENV = \
50 PATH="$(TARGET_PATH)" \
51 PYTHONPATH="$(if $(BR2_PACKAGE_PYTHON3),$(PYTHON3_PATH),$(PYTHON_PATH))" \
52 _python_sysroot=$(STAGING_DIR) \
54 _python_exec_prefix=/usr
56 PKG_PYTHON_SETUPTOOLS_INSTALL_OPT = \
57 --prefix=$(TARGET_DIR)/usr \
58 --executable=/usr/bin/python \
59 --single-version-externally-managed \
62 # Host setuptools-based packages
63 HOST_PKG_PYTHON_SETUPTOOLS_ENV = \
66 HOST_PKG_PYTHON_SETUPTOOLS_INSTALL_OPT = \
67 --prefix=$(HOST_DIR)/usr
69 ################################################################################
70 # inner-python-package -- defines how the configuration, compilation
71 # and installation of a Python package should be done, implements a
72 # few hooks to tune the build process and calls the generic package
73 # infrastructure to generate the necessary make targets
75 # argument 1 is the lowercase package name
76 # argument 2 is the uppercase package name, including an HOST_ prefix
78 # argument 3 is the uppercase package name, without the HOST_ prefix
80 # argument 4 is the type (target or host)
81 ################################################################################
83 define inner-python-package
85 $(2)_SRCDIR = $$($(2)_DIR)/$($(2)_SUBDIR)
86 $(2)_BUILDDIR = $$($(2)_SRCDIR)
92 ifndef $(2)_SETUP_TYPE
94 $(2)_SETUP_TYPE = $($(3)_SETUP_TYPE)
96 $$(error "$(2)_SETUP_TYPE must be set")
101 ifeq ($$($(2)_SETUP_TYPE),distutils)
103 $(2)_BASE_ENV = $$(PKG_PYTHON_DISTUTILS_ENV)
104 $(2)_BASE_BUILD_TGT = build
105 $(2)_BASE_BUILD_OPT = $$(PKG_PYTHON_DISTUTILS_BUILD_OPT)
106 $(2)_BASE_INSTALL_OPT = $$(PKG_PYTHON_DISTUTILS_INSTALL_OPT)
108 $(2)_BASE_ENV = $$(HOST_PKG_PYTHON_DISTUTILS_ENV)
109 $(2)_BASE_BUILD_TGT = build
110 $(2)_BASE_BUILD_OPT =
111 $(2)_BASE_INSTALL_OPT = $$(HOST_PKG_PYTHON_DISTUTILS_INSTALL_OPT)
114 else ifeq ($$($(2)_SETUP_TYPE),setuptools)
116 $(2)_BASE_ENV = $$(PKG_PYTHON_SETUPTOOLS_ENV)
117 $(2)_BASE_BUILD_TGT = build
118 $(2)_BASE_BUILD_OPT =
119 $(2)_BASE_INSTALL_OPT = $$(PKG_PYTHON_SETUPTOOLS_INSTALL_OPT)
121 $(2)_BASE_ENV = $$(HOST_PKG_PYTHON_SETUPTOOLS_ENV)
122 $(2)_BASE_BUILD_TGT = build
123 $(2)_BASE_BUILD_OPT =
124 $(2)_BASE_INSTALL_OPT = $$(HOST_PKG_PYTHON_SETUPTOOLS_INSTALL_OPT)
127 $$(error "Invalid $(2)_SETUP_TYPE. Valid options are 'distutils' or 'setuptools'")
130 # The below statement intends to calculate the dependencies of host
131 # packages by derivating them from the dependencies of the
132 # corresponding target package, after adding the 'host-' prefix in
133 # front of the dependencies.
135 # However it must be repeated from inner-generic-package, as we need
136 # to exclude the python, host-python and host-python-setuptools
137 # packages, which are added below in the list of dependencies
138 # depending on the package characteristics, and shouldn't be derived
139 # automatically from the dependencies of the corresponding target
141 $(2)_DEPENDENCIES ?= $(filter-out host-python host-python3 host-python-setuptools host-toolchain $(1),$(patsubst host-host-%,host-%,$(addprefix host-,$($(3)_DEPENDENCIES))))
143 # Target packages need both the python interpreter on the target (for
144 # runtime) and the python interpreter on the host (for
145 # compilation). However, host packages only need the python
146 # interpreter on the host.
148 $(2)_DEPENDENCIES += $(if $(BR2_PACKAGE_PYTHON3),host-python3 python3,host-python python)
150 $(2)_DEPENDENCIES += $(if $(BR2_PACKAGE_PYTHON3),host-python3,host-python)
153 # Setuptools based packages will need host-python-setuptools (both
154 # host and target). We need to have a special exclusion for the
155 # host-setuptools package itself: it is setuptools-based, but
156 # shouldn't depend on host-setuptools (because it would otherwise
157 # depend on itself!).
158 ifeq ($$($(2)_SETUP_TYPE),setuptools)
159 ifneq ($(2),HOST_PYTHON_SETUPTOOLS)
160 $(2)_DEPENDENCIES += host-python-setuptools
165 # Build step. Only define it if not already defined by the package .mk
168 ifndef $(2)_BUILD_CMDS
169 define $(2)_BUILD_CMDS
170 (cd $$($$(PKG)_BUILDDIR)/; \
171 $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
172 $(HOST_DIR)/usr/bin/python setup.py \
173 $$($$(PKG)_BASE_BUILD_TGT) \
174 $$($$(PKG)_BASE_BUILD_OPT) $$($$(PKG)_BUILD_OPT))
179 # Host installation step. Only define it if not already defined by the
182 ifndef $(2)_INSTALL_CMDS
183 define $(2)_INSTALL_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))
192 # Target installation step. Only define it if not already defined by
193 # the package .mk file.
195 ifndef $(2)_INSTALL_TARGET_CMDS
196 define $(2)_INSTALL_TARGET_CMDS
197 (cd $$($$(PKG)_BUILDDIR)/; \
198 $$($$(PKG)_BASE_ENV) $$($$(PKG)_ENV) \
199 $(HOST_DIR)/usr/bin/python setup.py install \
200 $$($$(PKG)_BASE_INSTALL_OPT) $$($$(PKG)_INSTALL_OPT))
204 # Call the generic package infrastructure to generate the necessary
206 $(call inner-generic-package,$(1),$(2),$(3),$(4))
210 ################################################################################
211 # python-package -- the target generator macro for Python packages
212 ################################################################################
214 python-package = $(call inner-python-package,$(pkgname),$(call UPPERCASE,$(pkgname)),$(call UPPERCASE,$(pkgname)),target)
215 host-python-package = $(call inner-python-package,host-$(pkgname),$(call UPPERCASE,host-$(pkgname)),$(call UPPERCASE,$(pkgname)),host)