python3.mk 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. ################################################################################
  2. #
  3. # python3
  4. #
  5. ################################################################################
  6. PYTHON3_VERSION_MAJOR = 3.9
  7. PYTHON3_VERSION = $(PYTHON3_VERSION_MAJOR).7
  8. PYTHON3_SOURCE = Python-$(PYTHON3_VERSION).tar.xz
  9. PYTHON3_SITE = https://python.org/ftp/python/$(PYTHON3_VERSION)
  10. PYTHON3_LICENSE = Python-2.0, others
  11. PYTHON3_LICENSE_FILES = LICENSE
  12. PYTHON3_CPE_ID_VENDOR = python
  13. PYTHON3_CPE_ID_PRODUCT = python
  14. # This host Python is installed in $(HOST_DIR), as it is needed when
  15. # cross-compiling third-party Python modules.
  16. HOST_PYTHON3_CONF_OPTS += \
  17. --without-ensurepip \
  18. --without-cxx-main \
  19. --disable-sqlite3 \
  20. --disable-tk \
  21. --with-expat=system \
  22. --disable-curses \
  23. --disable-codecs-cjk \
  24. --disable-nis \
  25. --enable-unicodedata \
  26. --disable-test-modules \
  27. --disable-idle3 \
  28. --disable-ossaudiodev
  29. # Make sure that LD_LIBRARY_PATH overrides -rpath.
  30. # This is needed because libpython may be installed at the same time that
  31. # python is called.
  32. # Make python believe we don't have 'hg', so that it doesn't try to
  33. # communicate over the network during the build.
  34. HOST_PYTHON3_CONF_ENV += \
  35. LDFLAGS="$(HOST_LDFLAGS) -Wl,--enable-new-dtags" \
  36. ac_cv_prog_HAS_HG=/bin/false
  37. PYTHON3_DEPENDENCIES = host-python3 libffi
  38. HOST_PYTHON3_DEPENDENCIES = host-autoconf-archive host-expat host-zlib host-libffi
  39. ifeq ($(BR2_PACKAGE_HOST_PYTHON3_BZIP2),y)
  40. HOST_PYTHON3_DEPENDENCIES += host-bzip2
  41. else
  42. HOST_PYTHON3_CONF_OPTS += --disable-bzip2
  43. endif
  44. ifeq ($(BR2_PACKAGE_HOST_PYTHON3_SSL),y)
  45. HOST_PYTHON3_DEPENDENCIES += host-openssl
  46. else
  47. HOST_PYTHON3_CONF_OPTS += --disable-openssl
  48. endif
  49. PYTHON3_INSTALL_STAGING = YES
  50. ifeq ($(BR2_PACKAGE_PYTHON3_2TO3),y)
  51. PYTHON3_CONF_OPTS += --enable-lib2to3
  52. else
  53. PYTHON3_CONF_OPTS += --disable-lib2to3
  54. endif
  55. ifeq ($(BR2_PACKAGE_PYTHON3_BERKELEYDB),y)
  56. PYTHON3_DEPENDENCIES += berkeleydb
  57. else
  58. PYTHON3_CONF_OPTS += --disable-berkeleydb
  59. endif
  60. ifeq ($(BR2_PACKAGE_PYTHON3_READLINE),y)
  61. PYTHON3_DEPENDENCIES += readline
  62. else
  63. PYTHON3_CONF_OPTS += --disable-readline
  64. endif
  65. ifeq ($(BR2_PACKAGE_PYTHON3_CURSES),y)
  66. PYTHON3_DEPENDENCIES += ncurses
  67. else
  68. PYTHON3_CONF_OPTS += --disable-curses
  69. endif
  70. ifeq ($(BR2_PACKAGE_PYTHON3_DECIMAL),y)
  71. PYTHON3_DEPENDENCIES += mpdecimal
  72. PYTHON3_CONF_OPTS += --with-libmpdec=system
  73. else
  74. PYTHON3_CONF_OPTS += --with-libmpdec=none
  75. endif
  76. ifeq ($(BR2_PACKAGE_PYTHON3_PYEXPAT),y)
  77. PYTHON3_DEPENDENCIES += expat
  78. PYTHON3_CONF_OPTS += --with-expat=system
  79. else
  80. PYTHON3_CONF_OPTS += --with-expat=none
  81. endif
  82. ifeq ($(BR2_PACKAGE_PYTHON3_SQLITE),y)
  83. PYTHON3_DEPENDENCIES += sqlite
  84. else
  85. PYTHON3_CONF_OPTS += --disable-sqlite3
  86. endif
  87. ifeq ($(BR2_PACKAGE_PYTHON3_SSL),y)
  88. PYTHON3_DEPENDENCIES += openssl
  89. else
  90. PYTHON3_CONF_OPTS += --disable-openssl
  91. endif
  92. ifneq ($(BR2_PACKAGE_PYTHON3_CODECSCJK),y)
  93. PYTHON3_CONF_OPTS += --disable-codecs-cjk
  94. endif
  95. ifneq ($(BR2_PACKAGE_PYTHON3_UNICODEDATA),y)
  96. PYTHON3_CONF_OPTS += --disable-unicodedata
  97. endif
  98. # Disable auto-detection of uuid.h (util-linux)
  99. # which would add _uuid module support, instead
  100. # default to the pure python implementation
  101. PYTHON3_CONF_OPTS += --disable-uuid
  102. ifeq ($(BR2_PACKAGE_PYTHON3_BZIP2),y)
  103. PYTHON3_DEPENDENCIES += bzip2
  104. else
  105. PYTHON3_CONF_OPTS += --disable-bzip2
  106. endif
  107. ifeq ($(BR2_PACKAGE_PYTHON3_XZ),y)
  108. PYTHON3_DEPENDENCIES += xz
  109. else
  110. PYTHON3_CONF_OPTS += --disable-xz
  111. endif
  112. ifeq ($(BR2_PACKAGE_PYTHON3_ZLIB),y)
  113. PYTHON3_DEPENDENCIES += zlib
  114. else
  115. PYTHON3_CONF_OPTS += --disable-zlib
  116. endif
  117. ifeq ($(BR2_PACKAGE_PYTHON3_OSSAUDIODEV),y)
  118. PYTHON3_CONF_OPTS += --enable-ossaudiodev
  119. else
  120. PYTHON3_CONF_OPTS += --disable-ossaudiodev
  121. endif
  122. # Make python believe we don't have 'hg', so that it doesn't try to
  123. # communicate over the network during the build.
  124. PYTHON3_CONF_ENV += \
  125. ac_cv_have_long_long_format=yes \
  126. ac_cv_file__dev_ptmx=yes \
  127. ac_cv_file__dev_ptc=yes \
  128. ac_cv_working_tzset=yes \
  129. ac_cv_prog_HAS_HG=/bin/false
  130. # GCC is always compliant with IEEE754
  131. ifeq ($(BR2_ENDIAN),"LITTLE")
  132. PYTHON3_CONF_ENV += ac_cv_little_endian_double=yes
  133. else
  134. PYTHON3_CONF_ENV += ac_cv_big_endian_double=yes
  135. endif
  136. # uClibc is known to have a broken wcsftime() implementation, so tell
  137. # Python 3 to fall back to strftime() instead.
  138. ifeq ($(BR2_TOOLCHAIN_USES_UCLIBC),y)
  139. PYTHON3_CONF_ENV += ac_cv_func_wcsftime=no
  140. endif
  141. ifeq ($(BR2_PACKAGE_GETTEXT_PROVIDES_LIBINTL),y)
  142. PYTHON3_DEPENDENCIES += gettext
  143. endif
  144. PYTHON3_CONF_OPTS += \
  145. --without-ensurepip \
  146. --without-cxx-main \
  147. --with-system-ffi \
  148. --disable-pydoc \
  149. --disable-test-modules \
  150. --disable-tk \
  151. --disable-nis \
  152. --disable-idle3 \
  153. --disable-pyc-build
  154. #
  155. # Some of CPython's source code is generated using Python interpreter
  156. # and some helper tools such as "Programs/_freeze_importlib" or
  157. # "Parser/pgen" (look for regen-* targets in Makefile.pre.in for more
  158. # info). Normally CPython codebase ships with those files
  159. # pre-generated, so just regular "make" with no additional steps
  160. # should be sufficient for a succesfull build, however due to
  161. # Buildroot's "Add importlib fix for PEP 3147 issue" custom patch we
  162. # end up modifying "Lib/importlib/_bootstrap_external.py" which means
  163. # we have to do "regen-importlib" step before building CPython
  164. # (Importlib is a builtin module that needs to be "frozen"/converted
  165. # to a C array of bytecode using "Programs/_freeze_importlib")
  166. #
  167. # To achive that we add pre-build steps to host-python3 as well as
  168. # python3 that execute "regen-importlib" target.
  169. #
  170. # Unfortunately, for the target Python, "Programs/_freeze_importlib"
  171. # is built for the target, while we need to run them at build time. So
  172. # when installing host-python3, we copy them to $(HOST_DIR)/bin...
  173. #
  174. define HOST_PYTHON3_MAKE_REGEN_IMPORTLIB
  175. $(HOST_MAKE_ENV) $(PYTHON3_CONF_ENV) $(MAKE) $(HOST_CONFIGURE_OPTS) -C $(@D) regen-importlib
  176. cp $(@D)/Programs/_freeze_importlib $(HOST_DIR)/bin/python-freeze-importlib
  177. endef
  178. HOST_PYTHON3_PRE_BUILD_HOOKS += HOST_PYTHON3_MAKE_REGEN_IMPORTLIB
  179. #
  180. # ... And then, when building the target python we first buid
  181. # 'Programs/_freeze_importlib' to force GNU Make to update all of the
  182. # prerequisites of 'Programs/_freeze_importlib', then copy our stashed
  183. # "host-usable" version over the one that was just build and then
  184. # build "regen-importlib" target
  185. #
  186. define PYTHON3_MAKE_REGEN_IMPORTLIB
  187. $(TARGET_MAKE_ENV) $(PYTHON3_CONF_ENV) $(MAKE) $(TARGET_CONFIGURE_OPTS) -C $(@D) Programs/_freeze_importlib
  188. cp $(HOST_DIR)/bin/python-freeze-importlib $(@D)/Programs/_freeze_importlib
  189. $(TARGET_MAKE_ENV) $(PYTHON3_CONF_ENV) $(MAKE) $(TARGET_CONFIGURE_OPTS) -C $(@D) regen-importlib
  190. endef
  191. PYTHON3_PRE_BUILD_HOOKS += PYTHON3_MAKE_REGEN_IMPORTLIB
  192. #
  193. # Remove useless files. In the config/ directory, only the Makefile
  194. # and the pyconfig.h files are needed at runtime.
  195. #
  196. define PYTHON3_REMOVE_USELESS_FILES
  197. rm -f $(TARGET_DIR)/usr/bin/python$(PYTHON3_VERSION_MAJOR)-config
  198. rm -f $(TARGET_DIR)/usr/bin/python$(PYTHON3_VERSION_MAJOR)m-config
  199. rm -f $(TARGET_DIR)/usr/bin/python3-config
  200. rm -f $(TARGET_DIR)/usr/bin/smtpd.py.3
  201. rm -f $(TARGET_DIR)/usr/lib/python$(PYTHON3_VERSION_MAJOR)/distutils/command/wininst*.exe
  202. for i in `find $(TARGET_DIR)/usr/lib/python$(PYTHON3_VERSION_MAJOR)/config-$(PYTHON3_VERSION_MAJOR)m-*/ \
  203. -type f -not -name Makefile` ; do \
  204. rm -f $$i ; \
  205. done
  206. rm -rf $(TARGET_DIR)/usr/lib/python$(PYTHON3_VERSION_MAJOR)/__pycache__/
  207. rm -rf $(TARGET_DIR)/usr/lib/python$(PYTHON3_VERSION_MAJOR)/lib-dynload/sysconfigdata/__pycache__
  208. rm -rf $(TARGET_DIR)/usr/lib/python$(PYTHON3_VERSION_MAJOR)/collections/__pycache__
  209. rm -rf $(TARGET_DIR)/usr/lib/python$(PYTHON3_VERSION_MAJOR)/importlib/__pycache__
  210. endef
  211. PYTHON3_POST_INSTALL_TARGET_HOOKS += PYTHON3_REMOVE_USELESS_FILES
  212. #
  213. # Make sure libpython gets stripped out on target
  214. #
  215. define PYTHON3_ENSURE_LIBPYTHON_STRIPPED
  216. chmod u+w $(TARGET_DIR)/usr/lib/libpython$(PYTHON3_VERSION_MAJOR)*.so
  217. endef
  218. PYTHON3_POST_INSTALL_TARGET_HOOKS += PYTHON3_ENSURE_LIBPYTHON_STRIPPED
  219. PYTHON3_AUTORECONF = YES
  220. PYTHON3_AUTORECONF_OPTS = --include=$(HOST_DIR)/share/autoconf-archive
  221. define PYTHON3_INSTALL_SYMLINK
  222. ln -fs python3 $(TARGET_DIR)/usr/bin/python
  223. endef
  224. ifneq ($(BR2_PACKAGE_PYTHON),y)
  225. PYTHON3_POST_INSTALL_TARGET_HOOKS += PYTHON3_INSTALL_SYMLINK
  226. endif
  227. # Some packages may have build scripts requiring python3, whatever is the
  228. # python version chosen for the target.
  229. # Only install the python symlink in the host tree if python3 is enabled
  230. # for the target.
  231. ifeq ($(BR2_PACKAGE_PYTHON3),y)
  232. define HOST_PYTHON3_INSTALL_SYMLINK
  233. ln -fs python3 $(HOST_DIR)/bin/python
  234. ln -fs python3-config $(HOST_DIR)/bin/python-config
  235. endef
  236. HOST_PYTHON3_POST_INSTALL_HOOKS += HOST_PYTHON3_INSTALL_SYMLINK
  237. endif
  238. # Provided to other packages
  239. PYTHON3_PATH = $(STAGING_DIR)/usr/lib/python$(PYTHON3_VERSION_MAJOR)/
  240. # Support for socket.AF_BLUETOOTH
  241. ifeq ($(BR2_PACKAGE_BLUEZ5_UTILS_HEADERS),y)
  242. PYTHON3_DEPENDENCIES += bluez5_utils-headers
  243. endif
  244. $(eval $(autotools-package))
  245. $(eval $(host-autotools-package))
  246. ifeq ($(BR2_REPRODUCIBLE),y)
  247. define PYTHON3_FIX_TIME
  248. find $(TARGET_DIR)/usr/lib/python$(PYTHON3_VERSION_MAJOR) -name '*.py' -print0 | \
  249. xargs -0 --no-run-if-empty touch -d @$(SOURCE_DATE_EPOCH)
  250. endef
  251. endif
  252. define PYTHON3_CREATE_PYC_FILES
  253. $(PYTHON3_FIX_TIME)
  254. PYTHONPATH="$(PYTHON3_PATH)" \
  255. $(HOST_DIR)/bin/python$(PYTHON3_VERSION_MAJOR) \
  256. $(TOPDIR)/support/scripts/pycompile.py \
  257. $(if $(VERBOSE),--verbose) \
  258. --strip-root $(TARGET_DIR) \
  259. $(TARGET_DIR)/usr/lib/python$(PYTHON3_VERSION_MAJOR)
  260. endef
  261. ifeq ($(BR2_PACKAGE_PYTHON3_PYC_ONLY)$(BR2_PACKAGE_PYTHON3_PY_PYC),y)
  262. PYTHON3_TARGET_FINALIZE_HOOKS += PYTHON3_CREATE_PYC_FILES
  263. endif
  264. ifeq ($(BR2_PACKAGE_PYTHON3_PYC_ONLY),y)
  265. define PYTHON3_REMOVE_PY_FILES
  266. find $(TARGET_DIR)/usr/lib/python$(PYTHON3_VERSION_MAJOR) -name '*.py' \
  267. $(if $(strip $(KEEP_PYTHON_PY_FILES)),-not \( $(call finddirclauses,$(TARGET_DIR),$(KEEP_PYTHON_PY_FILES)) \) ) \
  268. -print0 | \
  269. xargs -0 --no-run-if-empty rm -f
  270. endef
  271. PYTHON3_TARGET_FINALIZE_HOOKS += PYTHON3_REMOVE_PY_FILES
  272. endif
  273. # Normally, *.pyc files should not have been compiled, but just in
  274. # case, we make sure we remove all of them.
  275. ifeq ($(BR2_PACKAGE_PYTHON3_PY_ONLY),y)
  276. define PYTHON3_REMOVE_PYC_FILES
  277. find $(TARGET_DIR)/usr/lib/python$(PYTHON3_VERSION_MAJOR) -name '*.pyc' -print0 | \
  278. xargs -0 --no-run-if-empty rm -f
  279. endef
  280. PYTHON3_TARGET_FINALIZE_HOOKS += PYTHON3_REMOVE_PYC_FILES
  281. endif
  282. # In all cases, we don't want to keep the optimized .opt-1.pyc and
  283. # .opt-2.pyc files, since they can't work without their non-optimized
  284. # variant.
  285. define PYTHON3_REMOVE_OPTIMIZED_PYC_FILES
  286. find $(TARGET_DIR)/usr/lib/python$(PYTHON3_VERSION_MAJOR) -name '*.opt-1.pyc' -print0 -o -name '*.opt-2.pyc' -print0 | \
  287. xargs -0 --no-run-if-empty rm -f
  288. endef
  289. PYTHON3_TARGET_FINALIZE_HOOKS += PYTHON3_REMOVE_OPTIMIZED_PYC_FILES