ac_python_devel.m4 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. # ===========================================================================
  2. # http://autoconf-archive.cryp.to/ac_python_devel.html
  3. # ===========================================================================
  4. #
  5. # SYNOPSIS
  6. #
  7. # AC_PYTHON_DEVEL([version])
  8. #
  9. # DESCRIPTION
  10. #
  11. # Note: Defines as a precious variable "PYTHON_VERSION". Don't override it
  12. # in your configure.ac.
  13. #
  14. # This macro checks for Python and tries to get the include path to
  15. # 'Python.h'. It provides the $(PYTHON_CPPFLAGS) and $(PYTHON_LDFLAGS)
  16. # output variables. It also exports $(PYTHON_EXTRA_LIBS) and
  17. # $(PYTHON_EXTRA_LDFLAGS) for embedding Python in your code.
  18. #
  19. # You can search for some particular version of Python by passing a
  20. # parameter to this macro, for example ">= '2.3.1'", or "== '2.4'". Please
  21. # note that you *have* to pass also an operator along with the version to
  22. # match, and pay special attention to the single quotes surrounding the
  23. # version number. Don't use "PYTHON_VERSION" for this: that environment
  24. # variable is declared as precious and thus reserved for the end-user.
  25. #
  26. # This macro should work for all versions of Python >= 2.1.0. As an end
  27. # user, you can disable the check for the python version by setting the
  28. # PYTHON_NOVERSIONCHECK environment variable to something else than the
  29. # empty string.
  30. #
  31. # If you need to use this macro for an older Python version, please
  32. # contact the authors. We're always open for feedback.
  33. #
  34. # LAST MODIFICATION
  35. #
  36. # 2008-04-12
  37. #
  38. # COPYLEFT
  39. #
  40. # Copyright (c) 2008 Sebastian Huber <sebastian-huber@web.de>
  41. # Copyright (c) 2008 Alan W. Irwin <irwin@beluga.phys.uvic.ca>
  42. # Copyright (c) 2008 Rafael Laboissiere <rafael@laboissiere.net>
  43. # Copyright (c) 2008 Andrew Collier <colliera@ukzn.ac.za>
  44. # Copyright (c) 2008 Matteo Settenvini <matteo@member.fsf.org>
  45. # Copyright (c) 2008 Horst Knorr <hk_classes@knoda.org>
  46. #
  47. # This program is free software: you can redistribute it and/or modify it
  48. # under the terms of the GNU General Public License as published by the
  49. # Free Software Foundation, either version 3 of the License, or (at your
  50. # option) any later version.
  51. #
  52. # This program is distributed in the hope that it will be useful, but
  53. # WITHOUT ANY WARRANTY; without even the implied warranty of
  54. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  55. # Public License for more details.
  56. #
  57. # You should have received a copy of the GNU General Public License along
  58. # with this program. If not, see <http://www.gnu.org/licenses/>.
  59. #
  60. # As a special exception, the respective Autoconf Macro's copyright owner
  61. # gives unlimited permission to copy, distribute and modify the configure
  62. # scripts that are the output of Autoconf when processing the Macro. You
  63. # need not follow the terms of the GNU General Public License when using
  64. # or distributing such scripts, even though portions of the text of the
  65. # Macro appear in them. The GNU General Public License (GPL) does govern
  66. # all other use of the material that constitutes the Autoconf Macro.
  67. #
  68. # This special exception to the GPL applies to versions of the Autoconf
  69. # Macro released by the Autoconf Macro Archive. When you make and
  70. # distribute a modified version of the Autoconf Macro, you may extend this
  71. # special exception to the GPL to apply to your modified version as well.
  72. AC_DEFUN([AC_PYTHON_DEVEL],[
  73. #
  74. # Allow the use of a (user set) custom python version
  75. #
  76. AC_ARG_VAR([PYTHON_VERSION],[The installed Python
  77. version to use, for example '2.3'. This string
  78. will be appended to the Python interpreter
  79. canonical name.])
  80. AC_PATH_PROG([PYTHON],[python[$PYTHON_VERSION]])
  81. if test -z "$PYTHON"; then
  82. AC_MSG_ERROR([Cannot find python$PYTHON_VERSION in your system path])
  83. PYTHON_VERSION=""
  84. fi
  85. #
  86. # Check for a version of Python >= 2.1.0
  87. #
  88. AC_MSG_CHECKING([for a version of Python >= '2.1.0'])
  89. ac_supports_python_ver=`$PYTHON -c "import sys; \
  90. ver = sys.version.split()[[0]]; \
  91. print(ver >= '2.1.0')"`
  92. if test "$ac_supports_python_ver" != "True"; then
  93. if test -z "$PYTHON_NOVERSIONCHECK"; then
  94. AC_MSG_RESULT([no])
  95. AC_MSG_FAILURE([
  96. This version of the AC@&t@_PYTHON_DEVEL macro
  97. doesn't work properly with versions of Python before
  98. 2.1.0. You may need to re-run configure, setting the
  99. variables PYTHON_CPPFLAGS, PYTHON_LDFLAGS, PYTHON_SITE_PKG,
  100. PYTHON_EXTRA_LIBS and PYTHON_EXTRA_LDFLAGS by hand.
  101. Moreover, to disable this check, set PYTHON_NOVERSIONCHECK
  102. to something else than an empty string.
  103. ])
  104. else
  105. AC_MSG_RESULT([skip at user request])
  106. fi
  107. else
  108. AC_MSG_RESULT([yes])
  109. fi
  110. #
  111. # if the macro parameter ``version'' is set, honour it
  112. #
  113. if test -n "$1"; then
  114. AC_MSG_CHECKING([for a version of Python $1])
  115. ac_supports_python_ver=`$PYTHON -c "import sys; \
  116. ver = sys.version.split()[[0]]; \
  117. print(ver >= '2.1.0')"`
  118. if test "$ac_supports_python_ver" = "True"; then
  119. AC_MSG_RESULT([yes])
  120. else
  121. AC_MSG_RESULT([no])
  122. AC_MSG_ERROR([this package requires Python $1.
  123. If you have it installed, but it isn't the default Python
  124. interpreter in your system path, please pass the PYTHON_VERSION
  125. variable to configure. See ``configure --help'' for reference.
  126. ])
  127. PYTHON_VERSION=""
  128. fi
  129. fi
  130. #
  131. # Check if you have distutils, else fail
  132. #
  133. AC_MSG_CHECKING([for the distutils Python package])
  134. ac_distutils_result=`$PYTHON -c "import distutils" 2>&1`
  135. if test -z "$ac_distutils_result"; then
  136. AC_MSG_RESULT([yes])
  137. else
  138. AC_MSG_RESULT([no])
  139. AC_MSG_ERROR([cannot import Python module "distutils".
  140. Please check your Python installation. The error was:
  141. $ac_distutils_result])
  142. PYTHON_VERSION=""
  143. fi
  144. #
  145. # Check for Python include path
  146. #
  147. AC_MSG_CHECKING([for Python include path])
  148. if test -z "$PYTHON_CPPFLAGS"; then
  149. python_path=`$PYTHON -c "import distutils.sysconfig; \
  150. print(distutils.sysconfig.get_python_inc());"`
  151. if test -n "${python_path}"; then
  152. python_path="-I$python_path"
  153. fi
  154. PYTHON_CPPFLAGS=$python_path
  155. fi
  156. AC_MSG_RESULT([$PYTHON_CPPFLAGS])
  157. AC_SUBST([PYTHON_CPPFLAGS])
  158. #
  159. # Check for Python library path
  160. #
  161. AC_MSG_CHECKING([for Python library path])
  162. if test -z "$PYTHON_LDFLAGS"; then
  163. # (makes two attempts to ensure we've got a version number
  164. # from the interpreter)
  165. py_version=`$PYTHON -c "from distutils.sysconfig import *; \
  166. print(' '.join(get_config_vars('VERSION')))"`
  167. if test "$py_version" = "[None]"; then
  168. if test -n "$PYTHON_VERSION"; then
  169. py_version=$PYTHON_VERSION
  170. else
  171. py_version=`$PYTHON -c "import sys; \
  172. print(sys.version[[:3]]")`
  173. fi
  174. fi
  175. PYTHON_LDFLAGS=`$PYTHON -c "from distutils.sysconfig import *; \
  176. print('-L' + get_python_lib(0,1) + \
  177. ' -lpython');"`$py_version
  178. fi
  179. AC_MSG_RESULT([$PYTHON_LDFLAGS])
  180. AC_SUBST([PYTHON_LDFLAGS])
  181. #
  182. # Check for site packages
  183. #
  184. AC_MSG_CHECKING([for Python site-packages path])
  185. if test -z "$PYTHON_SITE_PKG"; then
  186. PYTHON_SITE_PKG=`$PYTHON -c "import distutils.sysconfig; \
  187. print(distutils.sysconfig.get_python_lib(0,0))"`
  188. fi
  189. AC_MSG_RESULT([$PYTHON_SITE_PKG])
  190. AC_SUBST([PYTHON_SITE_PKG])
  191. #
  192. # libraries which must be linked in when embedding
  193. #
  194. AC_MSG_CHECKING(python extra libraries)
  195. if test -z "$PYTHON_EXTRA_LIBS"; then
  196. PYTHON_EXTRA_LIBS=`$PYTHON -c "import distutils.sysconfig; \
  197. conf = distutils.sysconfig.get_config_var; \
  198. print(conf('LOCALMODLIBS'), conf('LIBS'))"`
  199. fi
  200. AC_MSG_RESULT([$PYTHON_EXTRA_LIBS])
  201. AC_SUBST(PYTHON_EXTRA_LIBS)
  202. #
  203. # linking flags needed when embedding
  204. #
  205. AC_MSG_CHECKING(python extra linking flags)
  206. if test -z "$PYTHON_EXTRA_LDFLAGS"; then
  207. PYTHON_EXTRA_LDFLAGS=`$PYTHON -c "import distutils.sysconfig; \
  208. conf = distutils.sysconfig.get_config_var; \
  209. print(conf('LINKFORSHARED'))"`
  210. fi
  211. AC_MSG_RESULT([$PYTHON_EXTRA_LDFLAGS])
  212. AC_SUBST(PYTHON_EXTRA_LDFLAGS)
  213. #
  214. # final check to see if everything compiles alright
  215. #
  216. AC_MSG_CHECKING([consistency of all components of python development environment])
  217. AC_LANG_PUSH([C])
  218. # save current global flags
  219. LIBS="$ac_save_LIBS $PYTHON_LDFLAGS"
  220. CPPFLAGS="$ac_save_CPPFLAGS $PYTHON_CPPFLAGS"
  221. AC_TRY_LINK([
  222. #include <Python.h>
  223. ],[
  224. Py_Initialize();
  225. ],[pythonexists=yes],[pythonexists=no])
  226. AC_MSG_RESULT([$pythonexists])
  227. if test ! "$pythonexists" = "yes"; then
  228. AC_MSG_ERROR([
  229. Could not link test program to Python. Maybe the main Python library has been
  230. installed in some non-standard library path. If so, pass it to configure,
  231. via the LDFLAGS environment variable.
  232. Example: ./configure LDFLAGS="-L/usr/non-standard-path/python/lib"
  233. ============================================================================
  234. ERROR!
  235. You probably have to install the development version of the Python package
  236. for your distribution. The exact name of this package varies among them.
  237. ============================================================================
  238. ])
  239. PYTHON_VERSION=""
  240. fi
  241. AC_LANG_POP
  242. # turn back to default flags
  243. CPPFLAGS="$ac_save_CPPFLAGS"
  244. LIBS="$ac_save_LIBS"
  245. #
  246. # all done!
  247. #
  248. ])