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 \
46 _python_sysroot
="$(STAGING_DIR)" \
47 _python_prefix
="/usr" \
48 _python_exec_prefix
="/usr"
50 # $(1) => directory of python script
51 # $(2) => python script and its arguments
52 # $(3) => additional variables
54 cd
"$(if $(strip $(1)),$(strip $(1)),.)" && \
57 $(HOST_PYTHON3_BIN
) $(2)
60 # $(1) => build subdir
61 # $(2) => additional arguments to setup.py
62 # $(3) => additional variables
63 define Python3
/ModSetup
64 $(INSTALL_DIR
) $(PKG_INSTALL_DIR
)/$(PYTHON3_PKG_DIR
)
66 $(PKG_BUILD_DIR
)/$(strip $(1)), \
71 define Python3
/FixShebang
72 $(SED
) "1"'!'"b;s,^#"'!'".*python.*,#"'!'"/usr/bin/python3," -i
--follow-symlinks
$(1)
75 # default max recursion is 10
76 PYTHON3_COMPILEALL_MAX_RECURSION_LEVEL
:=20
78 # $(1) => directory of python source files to compile
80 # XXX [So that you won't goof as I did]
81 # Note: Yes, I tried to use the -O & -OO flags here.
82 # However the generated byte-codes were not portable.
83 # So, we just stuck to un-optimized byte-codes,
84 # which is still way better/faster than running
85 # Python sources all the time.
87 # Setting a fixed hash seed value is less secure than using
88 # random seed values, but is necessary for reproducible builds
91 # Should revisit this when https://bugs.python.org/issue37596
92 # (and other related reproducibility issues) are fixed.
93 define Python3
/CompileAll
94 $(call Python3
/Run
,, \
95 -m compileall
-r
"$(PYTHON3_COMPILEALL_MAX_RECURSION_LEVEL)" -b
-d
'/' $(1),
96 $(if
$(SOURCE_DATE_EPOCH
),PYTHONHASHSEED
="$(SOURCE_DATE_EPOCH)")
100 # $(1) => target directory
101 define Python3
/DeleteSourceFiles
102 $(FIND
) $(1) -type f
-name
'*.py' -delete
105 # $(1) => target directory
106 define Python3
/DeleteNonSourceFiles
107 $(FIND
) $(1) -not
-type d
-not
-name
'*.py' -delete
110 # $(1) => target directory
111 define Python3
/DeleteEmptyDirs
112 $(FIND
) $(1) -mindepth
1 -empty
-type d
-not
-path
'$(1)/CONTROL' -not
-path
'$(1)/CONTROL/*' -delete
118 define Py3Package
/filespec
/Default
122 # $(1) => package name
123 # $(2) => src directory
124 # $(3) => dest directory
125 define Py3Package
/ProcessFilespec
126 $(eval
$(call shexport
,Py3Package
/$(1)/filespec
))
127 $(SHELL
) $(python3_mk_path
)python-package-install.sh \
128 "$(2)" "$(3)" "$$$$$(call shvar,Py3Package/$(1)/filespec)"
132 define Package
/$(1)-src
143 define Package
/$(1)-src
/description
144 $$(call Package
/$(1)/description
)
146 This package contains the Python source files for
$(1).
149 define Package
/$(1)-src
/config
150 depends on PACKAGE_
$(1)
153 # Add default PyPackage filespec none defined
154 ifeq ($(origin Py3Package
/$(1)/filespec
),undefined
)
155 Py3Package
/$(1)/filespec
=$$(Py3Package
/filespec
/Default
)
158 ifndef Py3Package
/$(1)/install
159 define Py3Package
/$(1)/install
160 if
[ -d
$(PKG_INSTALL_DIR
)/usr
/bin
]; then \
161 $(INSTALL_DIR
) $$(1)/usr
/bin
; \
162 $(CP
) $(PKG_INSTALL_DIR
)/usr
/bin
/* $$(1)/usr
/bin
/ ; \
167 ifndef Package
/$(1)/install
168 define Package
/$(1)/install
169 $$(call Py3Package
/$(1)/install,$$(1))
170 $$(call Py3Package
/ProcessFilespec
,$(1),$(PKG_INSTALL_DIR
),$$(1))
171 $(FIND
) $$(1) -name
'*.exe' -delete
172 $$(call Python3
/CompileAll
,$$(1))
173 $$(call Python3
/DeleteSourceFiles
,$$(1))
174 $$(call Python3
/DeleteEmptyDirs
,$$(1))
175 if
[ -d
"$$(1)/usr/bin" ]; then \
176 $$(call Python3
/FixShebang
,$$(1)/usr
/bin
/*) ; \
180 define Package
/$(1)-src
/install
181 $$(call Py3Package
/$(1)/install,$$(1))
182 $$(call Py3Package
/ProcessFilespec
,$(1),$(PKG_INSTALL_DIR
),$$(1))
183 $$(call Python3
/DeleteNonSourceFiles
,$$(1))
184 $$(call Python3
/DeleteEmptyDirs
,$$(1))
186 endif # Package/$(1)/install
192 PYTHON3_PKG_SETUP_DIR ?
=
193 PYTHON3_PKG_SETUP_GLOBAL_ARGS ?
=
194 PYTHON3_PKG_SETUP_ARGS ?
= --single-version-externally-managed
195 PYTHON3_PKG_SETUP_VARS ?
=
197 PYTHON3_PKG_HOST_PIP_INSTALL_ARGS
= \
198 $(foreach req
,$(HOST_PYTHON3_PACKAGE_BUILD_DEPENDS
), \
200 $(if
$(findstring /,$(req
)),$(req
),$(python3_mk_path
)host-pip-requirements
/$(req
).txt
) \
203 define Py3Build
/FindStdlibDepends
204 $(SHELL
) $(python3_mk_path
)python3-find-stdlib-depends.sh
-n
"$(PKG_NAME)" "$(PKG_BUILD_DIR)"
207 define Py3Build
/Compile
/Default
208 $(if
$(PYTHON3_PKG_HOST_PIP_INSTALL_ARGS
), \
209 $(call HostPython3
/PipInstall
,$(PYTHON3_PKG_HOST_PIP_INSTALL_ARGS
)) \
211 $(call Python3
/ModSetup
, \
212 $(PYTHON3_PKG_SETUP_DIR
), \
213 $(PYTHON3_PKG_SETUP_GLOBAL_ARGS
) \
214 install --prefix="/usr" --root
="$(PKG_INSTALL_DIR)" \
215 $(PYTHON3_PKG_SETUP_ARGS
), \
216 $(PYTHON3_PKG_SETUP_VARS
) \
220 Py3Build
/Configure
=$(Py3Build
/Configure
/Default
)
221 Py3Build
/Compile
=$(Py3Build
/Compile
/Default
)
223 PYTHON3_PKG_BUILD ?
= 1
225 ifeq ($(strip $(PYTHON3_PKG_BUILD
)),1)
227 Hooks
/Configure
/Post
+=Py3Build
/FindStdlibDepends
229 Build
/Compile
=$(Py3Build
/Compile
)