2
1

python3.mk 9.8 KB

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