python3.mk 9.7 KB

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