]> rtime.felk.cvut.cz Git - coffee/buildroot.git/blob - package/pkg-python.mk
package: add python3 support in the package infrastructure
[coffee/buildroot.git] / package / pkg-python.mk
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
23 # Target distutils-based packages
24 PKG_PYTHON_DISTUTILS_ENV = \
25         PATH="$(TARGET_PATH)" \
26         CC="$(TARGET_CC)" \
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) \
32         _python_prefix=/usr \
33         _python_exec_prefix=/usr
34
35 PKG_PYTHON_DISTUTILS_BUILD_OPT = \
36         --executable=/usr/bin/python
37
38 PKG_PYTHON_DISTUTILS_INSTALL_OPT = \
39         --prefix=$(TARGET_DIR)/usr
40
41 # Host distutils-based packages
42 HOST_PKG_PYTHON_DISTUTILS_ENV = \
43         PATH="$(HOST_PATH)"
44
45 HOST_PKG_PYTHON_DISTUTILS_INSTALL_OPT = \
46         --prefix=$(HOST_DIR)/usr
47
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) \
53         _python_prefix=/usr \
54         _python_exec_prefix=/usr
55
56 PKG_PYTHON_SETUPTOOLS_INSTALL_OPT = \
57         --prefix=$(TARGET_DIR)/usr \
58         --executable=/usr/bin/python \
59         --single-version-externally-managed \
60         --root=/
61
62 # Host setuptools-based packages
63 HOST_PKG_PYTHON_SETUPTOOLS_ENV = \
64         PATH="$(HOST_PATH)"
65
66 HOST_PKG_PYTHON_SETUPTOOLS_INSTALL_OPT = \
67         --prefix=$(HOST_DIR)/usr
68
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
74 #
75 #  argument 1 is the lowercase package name
76 #  argument 2 is the uppercase package name, including an HOST_ prefix
77 #             for host packages
78 #  argument 3 is the uppercase package name, without the HOST_ prefix
79 #             for host packages
80 #  argument 4 is the type (target or host)
81 ################################################################################
82
83 define inner-python-package
84
85 $(2)_SRCDIR     = $$($(2)_DIR)/$($(2)_SUBDIR)
86 $(2)_BUILDDIR   = $$($(2)_SRCDIR)
87
88 $(2)_ENV         ?=
89 $(2)_BUILD_OPT   ?=
90 $(2)_INSTALL_OPT ?=
91
92 ifndef $(2)_SETUP_TYPE
93  ifdef $(3)_SETUP_TYPE
94   $(2)_SETUP_TYPE = $($(3)_SETUP_TYPE)
95  else
96   $$(error "$(2)_SETUP_TYPE must be set")
97  endif
98 endif
99
100 # Distutils
101 ifeq ($$($(2)_SETUP_TYPE),distutils)
102 ifeq ($(4),target)
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)
107 else
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)
112 endif
113 # Setuptools
114 else ifeq ($$($(2)_SETUP_TYPE),setuptools)
115 ifeq ($(4),target)
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)
120 else
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)
125 endif
126 else
127 $$(error "Invalid $(2)_SETUP_TYPE. Valid options are 'distutils' or 'setuptools'")
128 endif
129
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.
134 #
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
140 # package.
141 $(2)_DEPENDENCIES ?= $(filter-out host-python host-python3 host-python-setuptools host-toolchain $(1),$(patsubst host-host-%,host-%,$(addprefix host-,$($(3)_DEPENDENCIES))))
142
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.
147 ifeq ($(4),target)
148 $(2)_DEPENDENCIES += $(if $(BR2_PACKAGE_PYTHON3),host-python3 python3,host-python python)
149 else
150 $(2)_DEPENDENCIES += $(if $(BR2_PACKAGE_PYTHON3),host-python3,host-python)
151 endif
152
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
161 endif
162 endif
163
164 #
165 # Build step. Only define it if not already defined by the package .mk
166 # file.
167 #
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))
175 endef
176 endif
177
178 #
179 # Host installation step. Only define it if not already defined by the
180 # package .mk file.
181 #
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))
188 endef
189 endif
190
191 #
192 # Target installation step. Only define it if not already defined by
193 # the package .mk file.
194 #
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))
201 endef
202 endif
203
204 # Call the generic package infrastructure to generate the necessary
205 # make targets
206 $(call inner-generic-package,$(1),$(2),$(3),$(4))
207
208 endef
209
210 ################################################################################
211 # python-package -- the target generator macro for Python packages
212 ################################################################################
213
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)