You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
6.0 KiB

  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. # Note: include this file after `include $(TOPDIR)/rules.mk in your package Makefile
  8. python3_mk_path:=$(dir $(lastword $(MAKEFILE_LIST)))
  9. include $(python3_mk_path)python3-host.mk
  10. PYTHON3_DIR:=$(STAGING_DIR)/usr
  11. PYTHON3_INC_DIR:=$(PYTHON3_DIR)/include/python$(PYTHON3_VERSION)
  12. PYTHON3_LIB_DIR:=$(PYTHON3_DIR)/lib/python$(PYTHON3_VERSION)
  13. PYTHON3_PKG_DIR:=/usr/lib/python$(PYTHON3_VERSION)/site-packages
  14. PYTHON3:=python$(PYTHON3_VERSION)
  15. PYTHON3PATH:=$(PYTHON3_LIB_DIR):$(STAGING_DIR)/$(PYTHON3_PKG_DIR):$(PKG_INSTALL_DIR)/$(PYTHON3_PKG_DIR)
  16. # These configure args are needed in detection of path to Python header files
  17. # using autotools.
  18. CONFIGURE_ARGS += \
  19. _python_sysroot="$(STAGING_DIR)" \
  20. _python_prefix="/usr" \
  21. _python_exec_prefix="/usr"
  22. PYTHON3_VARS = \
  23. CC="$(TARGET_CC)" \
  24. CCSHARED="$(TARGET_CC) $(FPIC)" \
  25. CXX="$(TARGET_CXX)" \
  26. LD="$(TARGET_CC)" \
  27. LDSHARED="$(TARGET_CC) -shared" \
  28. CFLAGS="$(TARGET_CFLAGS)" \
  29. CPPFLAGS="$(TARGET_CPPFLAGS) -I$(PYTHON3_INC_DIR)" \
  30. LDFLAGS="$(TARGET_LDFLAGS) -lpython$(PYTHON3_VERSION)" \
  31. _PYTHON_HOST_PLATFORM=linux2 \
  32. __PYVENV_LAUNCHER__="/usr/bin/$(PYTHON3)" \
  33. PYTHONPATH="$(PYTHON3PATH)" \
  34. PYTHONDONTWRITEBYTECODE=1 \
  35. PYTHONOPTIMIZE="" \
  36. _python_sysroot="$(STAGING_DIR)" \
  37. _python_prefix="/usr" \
  38. _python_exec_prefix="/usr"
  39. # $(1) => directory of python script
  40. # $(2) => python script and its arguments
  41. # $(3) => additional variables
  42. define Python3/Run
  43. cd "$(if $(strip $(1)),$(strip $(1)),.)" && \
  44. $(PYTHON3_VARS) \
  45. $(3) \
  46. $(HOST_PYTHON3_BIN) $(2)
  47. endef
  48. # $(1) => build subdir
  49. # $(2) => additional arguments to setup.py
  50. # $(3) => additional variables
  51. define Python3/ModSetup
  52. $(INSTALL_DIR) $(PKG_INSTALL_DIR)/$(PYTHON3_PKG_DIR)
  53. $(call Python3/Run, \
  54. $(PKG_BUILD_DIR)/$(strip $(1)), \
  55. setup.py $(2), \
  56. $(3))
  57. endef
  58. define Python3/FixShebang
  59. $(SED) "1"'!'"b;s,^#"'!'".*python.*,#"'!'"/usr/bin/python3," -i --follow-symlinks $(1)
  60. endef
  61. # default max recursion is 10
  62. PYTHON3_COMPILEALL_MAX_RECURSION_LEVEL:=20
  63. # $(1) => directory of python source files to compile
  64. #
  65. # XXX [So that you won't goof as I did]
  66. # Note: Yes, I tried to use the -O & -OO flags here.
  67. # However the generated byte-codes were not portable.
  68. # So, we just stuck to un-optimized byte-codes,
  69. # which is still way better/faster than running
  70. # Python sources all the time.
  71. #
  72. # Setting a fixed hash seed value is less secure than using
  73. # random seed values, but is necessary for reproducible builds
  74. # (for now).
  75. #
  76. # Should revisit this when https://bugs.python.org/issue37596
  77. # (and other related reproducibility issues) are fixed.
  78. define Python3/CompileAll
  79. $(call Python3/Run,, \
  80. -m compileall -r "$(PYTHON3_COMPILEALL_MAX_RECURSION_LEVEL)" -b -d '/' $(1),
  81. $(if $(SOURCE_DATE_EPOCH),PYTHONHASHSEED="$(SOURCE_DATE_EPOCH)")
  82. )
  83. endef
  84. # $(1) => target directory
  85. define Python3/DeleteSourceFiles
  86. $(FIND) $(1) -type f -name '*.py' -delete
  87. endef
  88. # $(1) => target directory
  89. define Python3/DeleteNonSourceFiles
  90. $(FIND) $(1) -not -type d -not -name '*.py' -delete
  91. endef
  92. # $(1) => target directory
  93. define Python3/DeleteEmptyDirs
  94. $(FIND) $(1) -mindepth 1 -empty -type d -not -path '$(1)/CONTROL' -not -path '$(1)/CONTROL/*' -delete
  95. endef
  96. # Py3Package
  97. define Py3Package/filespec/Default
  98. +|$(PYTHON3_PKG_DIR)
  99. endef
  100. # $(1) => package name
  101. # $(2) => src directory
  102. # $(3) => dest directory
  103. define Py3Package/ProcessFilespec
  104. $(eval $(call shexport,Py3Package/$(1)/filespec))
  105. $(SHELL) $(python3_mk_path)python-package-install.sh \
  106. "$(2)" "$(3)" "$$$$$(call shvar,Py3Package/$(1)/filespec)"
  107. endef
  108. define Py3Package
  109. define Package/$(1)-src
  110. $(call Package/$(1))
  111. DEPENDS:=
  112. CONFLICTS:=
  113. PROVIDES:=
  114. EXTRA_DEPENDS:=
  115. TITLE+= (sources)
  116. USERID:=
  117. MENU:=
  118. endef
  119. define Package/$(1)-src/description
  120. $$(call Package/$(1)/description)
  121. This package contains the Python source files for $(1).
  122. endef
  123. define Package/$(1)-src/config
  124. depends on PACKAGE_$(1)
  125. endef
  126. # Add default PyPackage filespec none defined
  127. ifeq ($(origin Py3Package/$(1)/filespec),undefined)
  128. Py3Package/$(1)/filespec=$$(Py3Package/filespec/Default)
  129. endif
  130. ifndef Py3Package/$(1)/install
  131. define Py3Package/$(1)/install
  132. if [ -d $(PKG_INSTALL_DIR)/usr/bin ]; then \
  133. $(INSTALL_DIR) $$(1)/usr/bin ; \
  134. $(CP) $(PKG_INSTALL_DIR)/usr/bin/* $$(1)/usr/bin/ ; \
  135. fi
  136. endef
  137. endif
  138. ifndef Package/$(1)/install
  139. define Package/$(1)/install
  140. $$(call Py3Package/$(1)/install,$$(1))
  141. $$(call Py3Package/ProcessFilespec,$(1),$(PKG_INSTALL_DIR),$$(1))
  142. $(FIND) $$(1) -name '*.exe' -delete
  143. $$(call Python3/CompileAll,$$(1))
  144. $$(call Python3/DeleteSourceFiles,$$(1))
  145. $$(call Python3/DeleteEmptyDirs,$$(1))
  146. if [ -d "$$(1)/usr/bin" ]; then \
  147. $$(call Python3/FixShebang,$$(1)/usr/bin/*) ; \
  148. fi
  149. endef
  150. define Package/$(1)-src/install
  151. $$(call Py3Package/$(1)/install,$$(1))
  152. $$(call Py3Package/ProcessFilespec,$(1),$(PKG_INSTALL_DIR),$$(1))
  153. $$(call Python3/DeleteNonSourceFiles,$$(1))
  154. $$(call Python3/DeleteEmptyDirs,$$(1))
  155. endef
  156. endif # Package/$(1)/install
  157. endef
  158. # Py3Build
  159. PYTHON3_PKG_SETUP_DIR ?=
  160. PYTHON3_PKG_SETUP_GLOBAL_ARGS ?=
  161. PYTHON3_PKG_SETUP_ARGS ?= --single-version-externally-managed
  162. PYTHON3_PKG_SETUP_VARS ?=
  163. define Py3Build/FindStdlibDepends
  164. $(SHELL) $(python3_mk_path)python3-find-stdlib-depends.sh -n "$(PKG_NAME)" "$(PKG_BUILD_DIR)"
  165. endef
  166. define Py3Build/Compile/Default
  167. $(if $(HOST_PYTHON3_PACKAGE_BUILD_DEPENDS),
  168. $(call HostPython3/PipInstall,$(HOST_PYTHON3_PACKAGE_BUILD_DEPENDS))
  169. )
  170. $(call Python3/ModSetup, \
  171. $(PYTHON3_PKG_SETUP_DIR), \
  172. $(PYTHON3_PKG_SETUP_GLOBAL_ARGS) \
  173. install --prefix="/usr" --root="$(PKG_INSTALL_DIR)" \
  174. $(PYTHON3_PKG_SETUP_ARGS), \
  175. $(PYTHON3_PKG_SETUP_VARS) \
  176. )
  177. endef
  178. Py3Build/Configure=$(Py3Build/Configure/Default)
  179. Py3Build/Compile=$(Py3Build/Compile/Default)
  180. PYTHON3_PKG_BUILD ?= 1
  181. ifeq ($(strip $(PYTHON3_PKG_BUILD)),1)
  182. ifeq ($(PY3),stdlib)
  183. Hooks/Configure/Post+=Py3Build/FindStdlibDepends
  184. endif
  185. Build/Compile=$(Py3Build/Compile)
  186. endif