python3.mk 9.7 KB

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