Merge pull request #13139 from aaronjg/openfortivpn/ppp-netifd-fix
[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 PYTHONOPTIMIZE="" \
46 _python_sysroot="$(STAGING_DIR)" \
47 _python_prefix="/usr" \
48 _python_exec_prefix="/usr"
49
50 # $(1) => directory of python script
51 # $(2) => python script and its arguments
52 # $(3) => additional variables
53 define Python3/Run
54 cd "$(if $(strip $(1)),$(strip $(1)),.)" && \
55 $(PYTHON3_VARS) \
56 $(3) \
57 $(HOST_PYTHON3_BIN) $(2)
58 endef
59
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)
65 $(call Python3/Run, \
66 $(PKG_BUILD_DIR)/$(strip $(1)), \
67 setup.py $(2), \
68 $(3))
69 endef
70
71 define Python3/FixShebang
72 $(SED) "1"'!'"b;s,^#"'!'".*python.*,#"'!'"/usr/bin/python3," -i --follow-symlinks $(1)
73 endef
74
75 # default max recursion is 10
76 PYTHON3_COMPILEALL_MAX_RECURSION_LEVEL:=20
77
78 # $(1) => directory of python source files to compile
79 #
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.
86 #
87 # Setting a fixed hash seed value is less secure than using
88 # random seed values, but is necessary for reproducible builds
89 # (for now).
90 #
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)")
97 )
98 endef
99
100 # $(1) => target directory
101 define Python3/DeleteSourceFiles
102 $(FIND) $(1) -type f -name '*.py' -delete
103 endef
104
105 # $(1) => target directory
106 define Python3/DeleteNonSourceFiles
107 $(FIND) $(1) -not -type d -not -name '*.py' -delete
108 endef
109
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
113 endef
114
115
116 # Py3Package
117
118 define Py3Package/filespec/Default
119 +|$(PYTHON3_PKG_DIR)
120 endef
121
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)"
129 endef
130
131 define Py3Package
132 define Package/$(1)-src
133 $(call Package/$(1))
134 DEPENDS:=
135 CONFLICTS:=
136 PROVIDES:=
137 EXTRA_DEPENDS:=
138 TITLE+= (sources)
139 USERID:=
140 MENU:=
141 endef
142
143 define Package/$(1)-src/description
144 $$(call Package/$(1)/description)
145
146 This package contains the Python source files for $(1).
147 endef
148
149 define Package/$(1)-src/config
150 depends on PACKAGE_$(1)
151 endef
152
153 # Add default PyPackage filespec none defined
154 ifeq ($(origin Py3Package/$(1)/filespec),undefined)
155 Py3Package/$(1)/filespec=$$(Py3Package/filespec/Default)
156 endif
157
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/ ; \
163 fi
164 endef
165 endif
166
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/*) ; \
177 fi
178 endef
179
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))
185 endef
186 endif # Package/$(1)/install
187 endef
188
189
190 # Py3Build
191
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 ?=
196
197 PYTHON3_PKG_HOST_PIP_INSTALL_ARGS = \
198 $(foreach req,$(HOST_PYTHON3_PACKAGE_BUILD_DEPENDS), \
199 --requirement \
200 $(if $(findstring /,$(req)),$(req),$(python3_mk_path)host-pip-requirements/$(req).txt) \
201 )
202
203 define Py3Build/FindStdlibDepends
204 $(SHELL) $(python3_mk_path)python3-find-stdlib-depends.sh -n "$(PKG_NAME)" "$(PKG_BUILD_DIR)"
205 endef
206
207 define Py3Build/Compile/Default
208 $(if $(PYTHON3_PKG_HOST_PIP_INSTALL_ARGS), \
209 $(call HostPython3/PipInstall,$(PYTHON3_PKG_HOST_PIP_INSTALL_ARGS)) \
210 )
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) \
217 )
218 endef
219
220 Py3Build/Configure=$(Py3Build/Configure/Default)
221 Py3Build/Compile=$(Py3Build/Compile/Default)
222
223 PYTHON3_PKG_BUILD ?= 1
224
225 ifeq ($(strip $(PYTHON3_PKG_BUILD)),1)
226 ifeq ($(PY3),stdlib)
227 Hooks/Configure/Post+=Py3Build/FindStdlibDepends
228 endif
229 Build/Compile=$(Py3Build/Compile)
230 endif