python.m4 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. ## ------------------------ -*- Autoconf -*-
  2. ## Python file handling
  3. ## From Andrew Dalke
  4. ## Updated by James Henstridge
  5. ## ------------------------
  6. # Copyright (C) 1999-2017 Free Software Foundation, Inc.
  7. #
  8. # This file is free software; the Free Software Foundation
  9. # gives unlimited permission to copy and/or distribute it,
  10. # with or without modifications, as long as this notice is preserved.
  11. # AM_PATH_PYTHON([MINIMUM-VERSION], [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
  12. # ---------------------------------------------------------------------------
  13. # Adds support for distributing Python modules and packages. To
  14. # install modules, copy them to $(pythondir), using the python_PYTHON
  15. # automake variable. To install a package with the same name as the
  16. # automake package, install to $(pkgpythondir), or use the
  17. # pkgpython_PYTHON automake variable.
  18. #
  19. # The variables $(pyexecdir) and $(pkgpyexecdir) are provided as
  20. # locations to install python extension modules (shared libraries).
  21. # Another macro is required to find the appropriate flags to compile
  22. # extension modules.
  23. #
  24. # If your package is configured with a different prefix to python,
  25. # users will have to add the install directory to the PYTHONPATH
  26. # environment variable, or create a .pth file (see the python
  27. # documentation for details).
  28. #
  29. # If the MINIMUM-VERSION argument is passed, AM_PATH_PYTHON will
  30. # cause an error if the version of python installed on the system
  31. # doesn't meet the requirement. MINIMUM-VERSION should consist of
  32. # numbers and dots only.
  33. AC_DEFUN([AM_PATH_PYTHON],
  34. [
  35. dnl Find a Python interpreter. Python versions prior to 2.0 are not
  36. dnl supported. (2.0 was released on October 16, 2000).
  37. dnl FIXME: Remove the need to hard-code Python versions here.
  38. m4_define_default([_AM_PYTHON_INTERPRETER_LIST],
  39. [python python2 python3 python3.5 python3.4 python3.3 python3.2 python3.1 python3.0 python2.7 dnl
  40. python2.6 python2.5 python2.4 python2.3 python2.2 python2.1 python2.0])
  41. AC_ARG_VAR([PYTHON], [the Python interpreter])
  42. m4_if([$1],[],[
  43. dnl No version check is needed.
  44. # Find any Python interpreter.
  45. if test -z "$PYTHON"; then
  46. AC_PATH_PROGS([PYTHON], _AM_PYTHON_INTERPRETER_LIST, :)
  47. fi
  48. am_display_PYTHON=python
  49. ], [
  50. dnl A version check is needed.
  51. if test -n "$PYTHON"; then
  52. # If the user set $PYTHON, use it and don't search something else.
  53. AC_MSG_CHECKING([whether $PYTHON version is >= $1])
  54. AM_PYTHON_CHECK_VERSION([$PYTHON], [$1],
  55. [AC_MSG_RESULT([yes])],
  56. [AC_MSG_RESULT([no])
  57. AC_MSG_ERROR([Python interpreter is too old])])
  58. am_display_PYTHON=$PYTHON
  59. else
  60. # Otherwise, try each interpreter until we find one that satisfies
  61. # VERSION.
  62. AC_CACHE_CHECK([for a Python interpreter with version >= $1],
  63. [am_cv_pathless_PYTHON],[
  64. for am_cv_pathless_PYTHON in _AM_PYTHON_INTERPRETER_LIST none; do
  65. test "$am_cv_pathless_PYTHON" = none && break
  66. AM_PYTHON_CHECK_VERSION([$am_cv_pathless_PYTHON], [$1], [break])
  67. done])
  68. # Set $PYTHON to the absolute path of $am_cv_pathless_PYTHON.
  69. if test "$am_cv_pathless_PYTHON" = none; then
  70. PYTHON=:
  71. else
  72. AC_PATH_PROG([PYTHON], [$am_cv_pathless_PYTHON])
  73. fi
  74. am_display_PYTHON=$am_cv_pathless_PYTHON
  75. fi
  76. ])
  77. if test "$PYTHON" = :; then
  78. dnl Run any user-specified action, or abort.
  79. m4_default([$3], [AC_MSG_ERROR([no suitable Python interpreter found])])
  80. else
  81. dnl Query Python for its version number. Getting [:3] seems to be
  82. dnl the best way to do this; it's what "site.py" does in the standard
  83. dnl library.
  84. AC_CACHE_CHECK([for $am_display_PYTHON version], [am_cv_python_version],
  85. [am_cv_python_version=`$PYTHON -c "import sys; sys.stdout.write(sys.version[[:3]])"`])
  86. AC_SUBST([PYTHON_VERSION], [$am_cv_python_version])
  87. dnl Use the values of $prefix and $exec_prefix for the corresponding
  88. dnl values of PYTHON_PREFIX and PYTHON_EXEC_PREFIX. These are made
  89. dnl distinct variables so they can be overridden if need be. However,
  90. dnl general consensus is that you shouldn't need this ability.
  91. AC_SUBST([PYTHON_PREFIX], ['${prefix}'])
  92. AC_SUBST([PYTHON_EXEC_PREFIX], ['${exec_prefix}'])
  93. dnl At times (like when building shared libraries) you may want
  94. dnl to know which OS platform Python thinks this is.
  95. AC_CACHE_CHECK([for $am_display_PYTHON platform], [am_cv_python_platform],
  96. [am_cv_python_platform=`$PYTHON -c "import sys; sys.stdout.write(sys.platform)"`])
  97. AC_SUBST([PYTHON_PLATFORM], [$am_cv_python_platform])
  98. # Just factor out some code duplication.
  99. am_python_setup_sysconfig="\
  100. import sys
  101. # Prefer sysconfig over distutils.sysconfig, for better compatibility
  102. # with python 3.x. See automake bug#10227.
  103. try:
  104. import sysconfig
  105. except ImportError:
  106. can_use_sysconfig = 0
  107. else:
  108. can_use_sysconfig = 1
  109. # Can't use sysconfig in CPython 2.7, since it's broken in virtualenvs:
  110. # <https://github.com/pypa/virtualenv/issues/118>
  111. try:
  112. from platform import python_implementation
  113. if python_implementation() == 'CPython' and sys.version[[:3]] == '2.7':
  114. can_use_sysconfig = 0
  115. except ImportError:
  116. pass"
  117. dnl Set up 4 directories:
  118. dnl pythondir -- where to install python scripts. This is the
  119. dnl site-packages directory, not the python standard library
  120. dnl directory like in previous automake betas. This behavior
  121. dnl is more consistent with lispdir.m4 for example.
  122. dnl Query distutils for this directory.
  123. AC_CACHE_CHECK([for $am_display_PYTHON script directory],
  124. [am_cv_python_pythondir],
  125. [if test "x$prefix" = xNONE
  126. then
  127. am_py_prefix=$ac_default_prefix
  128. else
  129. am_py_prefix=$prefix
  130. fi
  131. am_cv_python_pythondir=`$PYTHON -c "
  132. $am_python_setup_sysconfig
  133. if can_use_sysconfig:
  134. sitedir = sysconfig.get_path('purelib', vars={'base':'$am_py_prefix'})
  135. else:
  136. from distutils import sysconfig
  137. sitedir = sysconfig.get_python_lib(0, 0, prefix='$am_py_prefix')
  138. sys.stdout.write(sitedir)"`
  139. case $am_cv_python_pythondir in
  140. $am_py_prefix*)
  141. am__strip_prefix=`echo "$am_py_prefix" | sed 's|.|.|g'`
  142. am_cv_python_pythondir=`echo "$am_cv_python_pythondir" | sed "s,^$am__strip_prefix,$PYTHON_PREFIX,"`
  143. ;;
  144. *)
  145. case $am_py_prefix in
  146. /usr|/System*) ;;
  147. *)
  148. am_cv_python_pythondir=$PYTHON_PREFIX/lib/python$PYTHON_VERSION/site-packages
  149. ;;
  150. esac
  151. ;;
  152. esac
  153. ])
  154. AC_SUBST([pythondir], [$am_cv_python_pythondir])
  155. dnl pkgpythondir -- $PACKAGE directory under pythondir. Was
  156. dnl PYTHON_SITE_PACKAGE in previous betas, but this naming is
  157. dnl more consistent with the rest of automake.
  158. AC_SUBST([pkgpythondir], [\${pythondir}/$PACKAGE])
  159. dnl pyexecdir -- directory for installing python extension modules
  160. dnl (shared libraries)
  161. dnl Query distutils for this directory.
  162. AC_CACHE_CHECK([for $am_display_PYTHON extension module directory],
  163. [am_cv_python_pyexecdir],
  164. [if test "x$exec_prefix" = xNONE
  165. then
  166. am_py_exec_prefix=$am_py_prefix
  167. else
  168. am_py_exec_prefix=$exec_prefix
  169. fi
  170. am_cv_python_pyexecdir=`$PYTHON -c "
  171. $am_python_setup_sysconfig
  172. if can_use_sysconfig:
  173. sitedir = sysconfig.get_path('platlib', vars={'platbase':'$am_py_prefix'})
  174. else:
  175. from distutils import sysconfig
  176. sitedir = sysconfig.get_python_lib(1, 0, prefix='$am_py_prefix')
  177. sys.stdout.write(sitedir)"`
  178. case $am_cv_python_pyexecdir in
  179. $am_py_exec_prefix*)
  180. am__strip_prefix=`echo "$am_py_exec_prefix" | sed 's|.|.|g'`
  181. am_cv_python_pyexecdir=`echo "$am_cv_python_pyexecdir" | sed "s,^$am__strip_prefix,$PYTHON_EXEC_PREFIX,"`
  182. ;;
  183. *)
  184. case $am_py_exec_prefix in
  185. /usr|/System*) ;;
  186. *)
  187. am_cv_python_pyexecdir=$PYTHON_EXEC_PREFIX/lib/python$PYTHON_VERSION/site-packages
  188. ;;
  189. esac
  190. ;;
  191. esac
  192. ])
  193. AC_SUBST([pyexecdir], [$am_cv_python_pyexecdir])
  194. dnl pkgpyexecdir -- $(pyexecdir)/$(PACKAGE)
  195. AC_SUBST([pkgpyexecdir], [\${pyexecdir}/$PACKAGE])
  196. dnl Run any user-specified action.
  197. $2
  198. fi
  199. ])
  200. # AM_PYTHON_CHECK_VERSION(PROG, VERSION, [ACTION-IF-TRUE], [ACTION-IF-FALSE])
  201. # ---------------------------------------------------------------------------
  202. # Run ACTION-IF-TRUE if the Python interpreter PROG has version >= VERSION.
  203. # Run ACTION-IF-FALSE otherwise.
  204. # This test uses sys.hexversion instead of the string equivalent (first
  205. # word of sys.version), in order to cope with versions such as 2.2c1.
  206. # This supports Python 2.0 or higher. (2.0 was released on October 16, 2000).
  207. AC_DEFUN([AM_PYTHON_CHECK_VERSION],
  208. [prog="import sys
  209. # split strings by '.' and convert to numeric. Append some zeros
  210. # because we need at least 4 digits for the hex conversion.
  211. # map returns an iterator in Python 3.0 and a list in 2.x
  212. minver = list(map(int, '$2'.split('.'))) + [[0, 0, 0]]
  213. minverhex = 0
  214. # xrange is not present in Python 3.0 and range returns an iterator
  215. for i in list(range(0, 4)): minverhex = (minverhex << 8) + minver[[i]]
  216. sys.exit(sys.hexversion < minverhex)"
  217. AS_IF([AM_RUN_LOG([$1 -c "$prog"])], [$3], [$4])])