Merge pull request #18400 from nmav/tmp-tang-self-contained
[feed/packages.git] / lang / python / python3-package.mk
1 #
2 # Copyright (C) 2007-2016 OpenWrt.org
3 #
4 # This is free software, licensed under the GNU General Public License v2.
5 # See /LICENSE for more information.
6 #
7
8 # Note: include this file after `include $(TOPDIR)/rules.mk in your package Makefile
9
10 python3_mk_path:=$(dir $(lastword $(MAKEFILE_LIST)))
11 include $(python3_mk_path)python3-host.mk
12
13 PYTHON3_DIR:=$(STAGING_DIR)/usr
14 PYTHON3_INC_DIR:=$(PYTHON3_DIR)/include/python$(PYTHON3_VERSION)
15 PYTHON3_LIB_DIR:=$(PYTHON3_DIR)/lib/python$(PYTHON3_VERSION)
16
17 PYTHON3_PKG_DIR:=/usr/lib/python$(PYTHON3_VERSION)/site-packages
18
19 PYTHON3:=python$(PYTHON3_VERSION)
20
21 PYTHON3PATH:=$(PYTHON3_LIB_DIR):$(STAGING_DIR)/$(PYTHON3_PKG_DIR):$(PKG_INSTALL_DIR)/$(PYTHON3_PKG_DIR)
22
23 -include $(PYTHON3_LIB_DIR)/config-$(PYTHON3_VERSION)/Makefile-vars
24
25 # These configure args are needed in detection of path to Python header files
26 # using autotools.
27 CONFIGURE_ARGS += \
28 _python_sysroot="$(STAGING_DIR)" \
29 _python_prefix="/usr" \
30 _python_exec_prefix="/usr"
31
32 PYTHON3_VARS = \
33 CC="$(TARGET_CC)" \
34 CCSHARED="$(TARGET_CC) $(FPIC)" \
35 CXX="$(TARGET_CXX)" \
36 LD="$(TARGET_CC)" \
37 LDSHARED="$(TARGET_CC) -shared" \
38 CFLAGS="$(TARGET_CFLAGS)" \
39 CPPFLAGS="$(TARGET_CPPFLAGS) -I$(PYTHON3_INC_DIR)" \
40 LDFLAGS="$(TARGET_LDFLAGS) -lpython$(PYTHON3_VERSION)" \
41 _PYTHON_HOST_PLATFORM="$(_PYTHON_HOST_PLATFORM)" \
42 __PYVENV_LAUNCHER__="/usr/bin/$(PYTHON3)" \
43 PYTHONPATH="$(PYTHON3PATH)" \
44 PYTHONDONTWRITEBYTECODE=1 \
45 _python_sysroot="$(STAGING_DIR)" \
46 _python_prefix="/usr" \
47 _python_exec_prefix="/usr"
48
49 # $(1) => directory of python script
50 # $(2) => python script and its arguments
51 # $(3) => additional variables
52 define Python3/Run
53 cd "$(if $(strip $(1)),$(strip $(1)),.)" && \
54 $(PYTHON3_VARS) \
55 $(3) \
56 $(HOST_PYTHON3_BIN) $(2)
57 endef
58
59 define Python3/FixShebang
60 $(SED) "1"'!'"b;s,^#"'!'".*python.*,#"'!'"/usr/bin/python3," -i --follow-symlinks $(1)
61 endef
62
63 # default max recursion is 10
64 PYTHON3_COMPILEALL_MAX_RECURSION_LEVEL:=20
65
66 # $(1) => directory of python source files to compile
67 #
68 # XXX [So that you won't goof as I did]
69 # Note: Yes, I tried to use the -O & -OO flags here.
70 # However the generated byte-codes were not portable.
71 # So, we just stuck to un-optimized byte-codes,
72 # which is still way better/faster than running
73 # Python sources all the time.
74 #
75 # Setting a fixed hash seed value is less secure than using
76 # random seed values, but is necessary for reproducible builds
77 # (for now).
78 #
79 # Should revisit this when https://bugs.python.org/issue37596
80 # (and other related reproducibility issues) are fixed.
81 define Python3/CompileAll
82 $(call Python3/Run,, \
83 -m compileall -r "$(PYTHON3_COMPILEALL_MAX_RECURSION_LEVEL)" -b -d '/' $(1),
84 $(if $(SOURCE_DATE_EPOCH),PYTHONHASHSEED="$(SOURCE_DATE_EPOCH)")
85 )
86 endef
87
88 # $(1) => target directory
89 define Python3/DeleteSourceFiles
90 $(FIND) $(1) -type f -name '*.py' -delete
91 endef
92
93 # $(1) => target directory
94 define Python3/DeleteNonSourceFiles
95 $(FIND) $(1) -not -type d -not -name '*.py' -delete
96 endef
97
98 # $(1) => target directory
99 define Python3/DeleteEmptyDirs
100 $(FIND) $(1) -mindepth 1 -empty -type d -not -path '$(1)/CONTROL' -not -path '$(1)/CONTROL/*' -delete
101 endef
102
103
104 # Py3Package
105
106 define Py3Package/filespec/Default
107 +|$(PYTHON3_PKG_DIR)
108 endef
109
110 # $(1) => package name
111 # $(2) => src directory
112 # $(3) => dest directory
113 define Py3Package/ProcessFilespec
114 $(eval $(call shexport,Py3Package/$(1)/filespec))
115 $(SHELL) $(python3_mk_path)python-package-install.sh \
116 "$(2)" "$(3)" "$$$$$(call shvar,Py3Package/$(1)/filespec)"
117 endef
118
119 define Py3Package
120 define Package/$(1)-src
121 $(call Package/$(1))
122 DEPENDS:=
123 CONFLICTS:=
124 PROVIDES:=
125 EXTRA_DEPENDS:=
126 TITLE+= (sources)
127 USERID:=
128 MENU:=
129 endef
130
131 define Package/$(1)-src/description
132 $$(call Package/$(1)/description)
133
134 This package contains the Python source files for $(1).
135 endef
136
137 define Package/$(1)-src/config
138 depends on PACKAGE_$(1)
139 endef
140
141 # Add default PyPackage filespec none defined
142 ifeq ($(origin Py3Package/$(1)/filespec),undefined)
143 Py3Package/$(1)/filespec=$$(Py3Package/filespec/Default)
144 endif
145
146 ifndef Py3Package/$(1)/install
147 define Py3Package/$(1)/install
148 if [ -d $(PKG_INSTALL_DIR)/usr/bin ]; then \
149 $(INSTALL_DIR) $$(1)/usr/bin ; \
150 $(CP) $(PKG_INSTALL_DIR)/usr/bin/* $$(1)/usr/bin/ ; \
151 fi
152 endef
153 endif
154
155 ifndef Package/$(1)/install
156 define Package/$(1)/install
157 $$(call Py3Package/$(1)/install,$$(1))
158 $$(call Py3Package/ProcessFilespec,$(1),$(PKG_INSTALL_DIR),$$(1))
159 $(FIND) $$(1) -name '*.exe' -delete
160 $$(call Python3/CompileAll,$$(1))
161 $$(call Python3/DeleteSourceFiles,$$(1))
162 $$(call Python3/DeleteEmptyDirs,$$(1))
163 if [ -d "$$(1)/usr/bin" ]; then \
164 $$(call Python3/FixShebang,$$(1)/usr/bin/*) ; \
165 fi
166 endef
167
168 define Package/$(1)-src/install
169 $$(call Py3Package/$(1)/install,$$(1))
170 $$(call Py3Package/ProcessFilespec,$(1),$(PKG_INSTALL_DIR),$$(1))
171 $$(call Python3/DeleteNonSourceFiles,$$(1))
172 $$(call Python3/DeleteEmptyDirs,$$(1))
173 endef
174 endif # Package/$(1)/install
175 endef
176
177
178 # Py3Build
179
180 PYTHON3_PKG_BUILD?=1
181 PYTHON3_PKG_FORCE_DISTUTILS_SETUP?=
182
183 PYTHON3_PKG_SETUP_DIR?=
184 PYTHON3_PKG_SETUP_GLOBAL_ARGS?=
185 PYTHON3_PKG_SETUP_ARGS?=--single-version-externally-managed
186 PYTHON3_PKG_SETUP_VARS?=
187
188 PYTHON3_PKG_BUILD_CONFIG_SETTINGS?=
189 PYTHON3_PKG_BUILD_VARS?=$(PYTHON3_PKG_SETUP_VARS)
190 PYTHON3_PKG_BUILD_ARGS?=
191 PYTHON3_PKG_BUILD_PATH?=$(PYTHON3_PKG_SETUP_DIR)
192
193 PYTHON3_PKG_INSTALL_VARS?=
194
195 PYTHON3_PKG_WHEEL_NAME?=$(subst -,_,$(if $(PYPI_SOURCE_NAME),$(PYPI_SOURCE_NAME),$(PKG_NAME)))
196 PYTHON3_PKG_WHEEL_VERSION?=$(PKG_VERSION)
197
198 PYTHON3_PKG_BUILD_DIR?=$(PKG_BUILD_DIR)/$(PYTHON3_PKG_BUILD_PATH)
199
200
201 PYTHON3_PKG_HOST_PIP_INSTALL_ARGS = \
202 $(foreach req,$(HOST_PYTHON3_PACKAGE_BUILD_DEPENDS), \
203 --requirement \
204 $(if $(findstring /,$(req)),$(req),$(python3_mk_path)host-pip-requirements/$(req).txt) \
205 )
206
207 define Py3Build/FindStdlibDepends
208 $(SHELL) $(python3_mk_path)python3-find-stdlib-depends.sh -n "$(PKG_NAME)" "$(PKG_BUILD_DIR)";
209 endef
210
211 ifneq ($(strip $(PYPI_NAME)),)
212 define Py3Build/CheckHostPipVersionMatch
213 if [ -d "$(python3_mk_path)host-pip-requirements" ] && \
214 [ -n "$$$$($(FIND) $(python3_mk_path)host-pip-requirements -maxdepth 1 -mindepth 1 -name '*.txt' -print -quit 2>/dev/null)" ]; then \
215 if grep -q "$(PYPI_NAME)==" $(python3_mk_path)host-pip-requirements/*.txt ; then \
216 if ! grep -q "$(PYPI_NAME)==$(PKG_VERSION)" $(python3_mk_path)host-pip-requirements/*.txt ; then \
217 printf "\nPlease update version of $(PYPI_NAME) to $(PKG_VERSION) in 'host-pip-requirements'/\n\n" ; \
218 exit 1 ; \
219 fi \
220 fi \
221 fi
222 endef
223 endif
224
225 define Py3Build/InstallBuildDepends
226 $(if $(PYTHON3_PKG_HOST_PIP_INSTALL_ARGS), \
227 $(call HostPython3/PipInstall,$(PYTHON3_PKG_HOST_PIP_INSTALL_ARGS)) \
228 )
229 endef
230
231 define Py3Build/Compile/Distutils
232 $(call Py3Build/InstallBuildDepends)
233 $(INSTALL_DIR) $(PKG_INSTALL_DIR)/$(PYTHON3_PKG_DIR)
234 $(call Python3/Run, \
235 $(PKG_BUILD_DIR)/$(strip $(PYTHON3_PKG_SETUP_DIR)), \
236 setup.py \
237 $(PYTHON3_PKG_SETUP_GLOBAL_ARGS) \
238 install \
239 --prefix="/usr" \
240 --root="$(PKG_INSTALL_DIR)" \
241 $(PYTHON3_PKG_SETUP_ARGS) \
242 , \
243 $(PYTHON3_PKG_SETUP_VARS) \
244 )
245 endef
246
247 define Py3Build/Compile/Default
248 $(call Py3Build/InstallBuildDepends)
249 $(call Python3/Run, \
250 $(PKG_BUILD_DIR), \
251 -m build \
252 --no-isolation \
253 --outdir "$(PYTHON3_PKG_BUILD_DIR)"/openwrt-build \
254 --wheel \
255 $(foreach setting,$(PYTHON3_PKG_BUILD_CONFIG_SETTINGS),--config-setting=$(setting)) \
256 $(PYTHON3_PKG_BUILD_ARGS) \
257 "$(PYTHON3_PKG_BUILD_DIR)" \
258 , \
259 $(PYTHON3_PKG_BUILD_VARS) \
260 )
261 endef
262
263 define Py3Build/Install/Default
264 $(call Python3/Run, \
265 $(PKG_BUILD_DIR), \
266 -m installer \
267 --destdir "$(PKG_INSTALL_DIR)" \
268 --no-compile-bytecode \
269 --prefix /usr \
270 "$(PYTHON3_PKG_BUILD_DIR)"/openwrt-build/$(PYTHON3_PKG_WHEEL_NAME)-$(PYTHON3_PKG_WHEEL_VERSION)-*.whl \
271 , \
272 $(PYTHON3_PKG_INSTALL_VARS) \
273 )
274 endef
275
276 Py3Build/Compile=$(Py3Build/Compile/Default)
277 Py3Build/Install=$(Py3Build/Install/Default)
278
279 ifeq ($(strip $(PYTHON3_PKG_FORCE_DISTUTILS_SETUP)),1)
280 Py3Build/Compile=$(Py3Build/Compile/Distutils)
281 Py3Build/Install:=:
282 endif
283
284 ifeq ($(strip $(PYTHON3_PKG_BUILD)),1)
285 ifeq ($(PY3),stdlib)
286 Hooks/Configure/Post+=Py3Build/FindStdlibDepends
287 endif
288 Hooks/Configure/Post+=Py3Build/CheckHostPipVersionMatch
289 Build/Compile=$(Py3Build/Compile)
290 Build/Install=$(Py3Build/Install)
291 endif