2 # Copyright (C) 2007-2016 OpenWrt.org
4 # This is free software, licensed under the GNU General Public License v2.
5 # See /LICENSE for more information.
8 # Note: include this file after `include $(TOPDIR)/rules.mk in your package Makefile
10 python3_mk_path
:=$(dir $(lastword
$(MAKEFILE_LIST
)))
11 include $(python3_mk_path
)python3-host.mk
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
)
17 PYTHON3_PKG_DIR
:=/usr
/lib
/python
$(PYTHON3_VERSION
)/site-packages
19 PYTHON3
:=python
$(PYTHON3_VERSION
)
21 PYTHON3PATH
:=$(PYTHON3_LIB_DIR
):$(STAGING_DIR
)/$(PYTHON3_PKG_DIR
):$(PKG_INSTALL_DIR
)/$(PYTHON3_PKG_DIR
)
23 -include $(PYTHON3_LIB_DIR
)/config-
$(PYTHON3_VERSION
)/Makefile-vars
25 # These configure args are needed in detection of path to Python header files
28 _python_sysroot
="$(STAGING_DIR)" \
29 _python_prefix
="/usr" \
30 _python_exec_prefix
="/usr"
34 CCSHARED
="$(TARGET_CC) $(FPIC)" \
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"
49 # $(1) => directory of python script
50 # $(2) => python script and its arguments
51 # $(3) => additional variables
53 cd
"$(if $(strip $(1)),$(strip $(1)),.)" && \
56 $(HOST_PYTHON3_BIN
) $(2)
59 define Python3
/FixShebang
60 $(SED
) "1"'!'"b;s,^#"'!'".*python.*,#"'!'"/usr/bin/python3," -i
--follow-symlinks
$(1)
63 # default max recursion is 10
64 PYTHON3_COMPILEALL_MAX_RECURSION_LEVEL
:=20
66 # $(1) => directory of python source files to compile
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.
75 # Setting a fixed hash seed value is less secure than using
76 # random seed values, but is necessary for reproducible builds
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)")
88 # $(1) => target directory
89 define Python3
/DeleteSourceFiles
90 $(FIND
) $(1) -type f
-name
'*.py' -delete
93 # $(1) => target directory
94 define Python3
/DeleteNonSourceFiles
95 $(FIND
) $(1) -not
-type d
-not
-name
'*.py' -delete
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
106 define Py3Package
/filespec
/Default
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)"
120 define Package
/$(1)-src
131 define Package
/$(1)-src
/description
132 $$(call Package
/$(1)/description
)
134 This package contains the Python source files for
$(1).
137 define Package
/$(1)-src
/config
138 depends on PACKAGE_
$(1)
141 # Add default PyPackage filespec none defined
142 ifeq ($(origin Py3Package
/$(1)/filespec
),undefined
)
143 Py3Package
/$(1)/filespec
=$$(Py3Package
/filespec
/Default
)
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
/ ; \
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
/*) ; \
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))
174 endif # Package/$(1)/install
181 PYTHON3_PKG_FORCE_DISTUTILS_SETUP?
=
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?
=
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
)
193 PYTHON3_PKG_INSTALL_VARS?
=
195 PYTHON3_PKG_WHEEL_NAME?
=$(subst -,_
,$(if
$(PYPI_SOURCE_NAME
),$(PYPI_SOURCE_NAME
),$(PKG_NAME
)))
196 PYTHON3_PKG_WHEEL_VERSION?
=$(PKG_VERSION
)
198 PYTHON3_PKG_BUILD_DIR?
=$(PKG_BUILD_DIR
)/$(PYTHON3_PKG_BUILD_PATH
)
201 PYTHON3_PKG_HOST_PIP_INSTALL_ARGS
= \
202 $(foreach req
,$(HOST_PYTHON3_PACKAGE_BUILD_DEPENDS
), \
204 $(if
$(findstring /,$(req
)),$(req
),$(python3_mk_path
)host-pip-requirements
/$(req
).txt
) \
207 define Py3Build
/FindStdlibDepends
208 $(SHELL
) $(python3_mk_path
)python3-find-stdlib-depends.sh
-n
"$(PKG_NAME)" "$(PKG_BUILD_DIR)";
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" ; \
225 define Py3Build
/InstallBuildDepends
226 $(if
$(PYTHON3_PKG_HOST_PIP_INSTALL_ARGS
), \
227 $(call HostPython3
/PipInstall
,$(PYTHON3_PKG_HOST_PIP_INSTALL_ARGS
)) \
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
)), \
237 $(PYTHON3_PKG_SETUP_GLOBAL_ARGS
) \
240 --root
="$(PKG_INSTALL_DIR)" \
241 $(PYTHON3_PKG_SETUP_ARGS
) \
243 $(PYTHON3_PKG_SETUP_VARS
) \
247 define Py3Build
/Compile
/Default
248 $(call Py3Build
/InstallBuildDepends
)
249 $(call Python3
/Run
, \
253 --outdir
"$(PYTHON3_PKG_BUILD_DIR)"/openwrt-build \
255 $(foreach setting
,$(PYTHON3_PKG_BUILD_CONFIG_SETTINGS
),--config-setting
=$(setting
)) \
256 $(PYTHON3_PKG_BUILD_ARGS
) \
257 "$(PYTHON3_PKG_BUILD_DIR)" \
259 $(PYTHON3_PKG_BUILD_VARS
) \
263 define Py3Build
/Install
/Default
264 $(call Python3
/Run
, \
267 --destdir
"$(PKG_INSTALL_DIR)" \
268 --no-compile-bytecode \
270 "$(PYTHON3_PKG_BUILD_DIR)"/openwrt-build
/$(PYTHON3_PKG_WHEEL_NAME
)-$(PYTHON3_PKG_WHEEL_VERSION
)-*.whl \
272 $(PYTHON3_PKG_INSTALL_VARS
) \
276 Py3Build
/Compile
=$(Py3Build
/Compile
/Default
)
277 Py3Build
/Install
=$(Py3Build
/Install
/Default
)
279 ifeq ($(strip $(PYTHON3_PKG_FORCE_DISTUTILS_SETUP
)),1)
280 Py3Build
/Compile
=$(Py3Build
/Compile
/Distutils
)
284 ifeq ($(strip $(PYTHON3_PKG_BUILD
)),1)
286 Hooks
/Configure
/Post
+=Py3Build
/FindStdlibDepends
288 Hooks
/Configure
/Post
+=Py3Build
/CheckHostPipVersionMatch
289 Build
/Compile
=$(Py3Build
/Compile
)
290 Build
/Install
=$(Py3Build
/Install
)