Add omitted --system-site-packages for Python 3 on Guix, add update_extlib.sh docs.
[mediagoblin.git] / m4 / python.m4
1 # Copyright 2012, 2013, 2014 Brandon Invergo <brandon@invergo.net>
2 #
3 # This file is part of pyconfigure. This program is free
4 # software; you can redistribute it and/or modify it under the
5 # terms of the GNU General Public License as published by the
6 # Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # Under Section 7 of GPL version 3, you are granted additional
15 # permissions described in the Autoconf Configure Script Exception,
16 # version 3.0, as published by the Free Software Foundation.
17 #
18 # You should have received a copy of the GNU General Public License
19 # and a copy of the Autoconf Configure Script Exception along with
20 # this program; see the files COPYINGv3 and COPYING.EXCEPTION
21 # respectively. If not, see <http://www.gnu.org/licenses/>.
22
23
24 # Many of these macros were adapted from ones written by Andrew Dalke
25 # and James Henstridge and are included with the Automake utility
26 # under the following copyright terms:
27 #
28 # Copyright (C) 1999-2012 Free Software Foundation, Inc.
29 #
30 # This file is free software; the Free Software Foundation
31 # gives unlimited permission to copy and/or distribute it,
32 # with or without modifications, as long as this notice is preserved.
33
34 # Table of Contents:
35 #
36 # 1. Language selection
37 # and routines to produce programs in a given language.
38 #
39 # 2. Producing programs in a given language.
40 #
41 # 3. Looking for a compiler
42 # And possibly the associated preprocessor.
43 #
44 # 4. Looking for specific libs & functionality
45
46
47 ## ----------------------- ##
48 ## 1. Language selection. ##
49 ## ----------------------- ##
50
51
52 # AC_LANG(Python)
53 # ---------------
54 AC_LANG_DEFINE([Python], [py], [PY], [PYTHON], [],
55 [ac_ext=py
56 ac_compile='chmod +x conftest.$ac_ext >&AS_MESSAGE_LOG_FD'
57 ac_link='chmod +x conftest.$ac_ext && cp conftest.$ac_ext conftest >&AS_MESSAGE_LOG_FD'
58 ])
59
60
61 # AC_LANG_PYTHON
62 # --------------
63 AU_DEFUN([AC_LANG_PYTHON], [AC_LANG(Python)])
64
65
66 ## ----------------------- ##
67 ## 2. Producing programs. ##
68 ## ----------------------- ##
69
70
71 # AC_LANG_PROGRAM(Python)([PROLOGUE], [BODY])
72 # -------------------------------------------
73 m4_define([AC_LANG_PROGRAM(Python)], [dnl
74 @%:@!$PYTHON
75 $1
76 m4_if([$2], [], [], [dnl
77 if __name__ == '__main__':
78 $2])])
79
80
81 # _AC_LANG_IO_PROGRAM(Python)
82 # ---------------------------
83 # Produce source that performs I/O.
84 m4_define([_AC_LANG_IO_PROGRAM(Python)],
85 [AC_LANG_PROGRAM([dnl
86 import sys
87 try:
88 h = open('conftest.out')
89 except:
90 sys.exit(1)
91 else:
92 close(h)
93 sys.exit(0)
94 ], [])])
95
96
97 # _AC_LANG_CALL(Python)([PROLOGUE], [FUNCTION])
98 # ---------------------
99 # Produce source that calls FUNCTION
100 m4_define([_AC_LANG_CALL(Python)],
101 [AC_LANG_PROGRAM([$1], [$2])])
102
103
104 ## -------------------------------------------- ##
105 ## 3. Looking for Compilers and Interpreters. ##
106 ## -------------------------------------------- ##
107
108
109 AC_DEFUN([AC_LANG_COMPILER(Python)],
110 [AC_REQUIRE([PC_PROG_PYTHON])])
111
112
113 # PC_INIT([MIN-VERSION], [MAX-VERSION])
114 # -----------------------------
115 # Initialize pyconfigure, finding a Python interpreter with a given
116 # minimum and/or maximum version.
117 AC_DEFUN([PC_INIT],
118 [PC_PROG_PYTHON([], [$1], [$2])
119 dnl If we found something, do a sanity check that the interpreter really
120 dnl has the version its name would suggest.
121 m4_ifval([PYTHON],
122 [PC_PYTHON_VERIFY_VERSION([>=], [pc_min_ver], [],
123 [AC_MSG_FAILURE([No compatible Python interpreter found. If you're sure that you have one, try setting the PYTHON environment variable to the location of the interpreter.])])])
124 m4_ifval([PYTHON],
125 [PC_PYTHON_VERIFY_VERSION([<=], [pc_max_ver], [],
126 [AC_MSG_FAILURE([No compatible Python interpreter found. If you're sure that you have one, try setting the PYTHON environment variable to the location of the interpreter.])])])
127 ])# PC_INIT
128
129 # PC_PROG_PYTHON([PROG-TO-CHECK-FOR], [MIN-VERSION], [MAX-VERSION])
130 # ---------------------------------
131 # Find a Python interpreter. Python versions prior to 2.0 are not
132 # supported. (2.0 was released on October 16, 2000).
133 AC_DEFUN_ONCE([PC_PROG_PYTHON],
134 [AC_ARG_VAR([PYTHON], [the Python interpreter])
135 dnl The default minimum version is 2.0
136 m4_define_default([pc_min_ver], m4_ifval([$2], [$2], [2.0]))
137 dnl The default maximum version is 3.3
138 m4_define_default([pc_max_ver], m4_ifval([$3], [$3], [4.0]))
139 dnl Build up a list of possible interpreter names.
140 m4_define_default([_PC_PYTHON_INTERPRETER_LIST],
141 [[python] \
142 dnl If we want some Python 3 versions (max version >= 3.0),
143 dnl also search for "python3"
144 m4_if(m4_version_compare(pc_max_ver, [2.9]), [1], [python3], []) \
145 dnl If we want some Python 2 versions (min version <= 2.7),
146 dnl also search for "python2".
147 m4_if(m4_version_compare(pc_min_ver, [2.8]), [-1], [python2], []) \
148 dnl Construct a comma-separated list of interpreter names (python2.6,
149 dnl python2.7, etc). We only care about the first 3 characters of the
150 dnl version strings (major-dot-minor; not
151 dnl major-dot-minor-dot-bugfix[-dot-whatever])
152 m4_foreach([pc_ver],
153 m4_esyscmd_s(seq -s[[", "]] -f["[[%.1f]]"] m4_substr(pc_max_ver, [0], [3]) -0.1 m4_substr(pc_min_ver, [0], [3])),
154 dnl Remove python2.8 and python2.9 since they will never exist
155 [m4_bmatch(pc_ver, [2.[89]], [], [python]pc_ver)])])
156 dnl Do the actual search at last.
157 m4_ifval([$1],
158 [AC_PATH_PROGS(PYTHON, [$1 _PC_PYTHON_INTERPRETER_LIST])],
159 [AC_PATH_PROGS(PYTHON, [_PC_PYTHON_INTERPRETER_LIST])])
160 ])# PC_PROG_PYTHON
161
162
163 # PC_PYTHON_PROG_PYTHON_CONFIG(PROG-TO-CHECK-FOR)
164 # ----------------------------------------------
165 # Find the python-config program
166 AC_DEFUN([PC_PYTHON_PROG_PYTHON_CONFIG],
167 [AC_REQUIRE([PC_PROG_PYTHON])[]dnl
168 AC_ARG_VAR([PYTHON_CONFIG], [the Python-config program])
169 dnl python-config's binary name is normally based on the Python interpreter's
170 dnl binary name (i.e. python2.7 -> python2.7-config)
171 m4_define([_PYTHON_BASENAME], [`basename $PYTHON`])
172 m4_ifval([$1],
173 [AC_PATH_PROGS(PYTHON_CONFIG, [$1 _PYTHON_BASENAME-config])],
174 [AC_PATH_PROG(PYTHON_CONFIG, _PYTHON_BASENAME-config)])
175 ]) # PC_PYTHON_PROG_PYTHON_CONFIG
176
177
178 # PC_PYTHON_VERIFY_VERSION([RELATION], [VERSION], [ACTION-IF-TRUE], [ACTION-IF-FALSE])
179 # ---------------------------------------------------------------------------
180 # Run ACTION-IF-TRUE if the Python interpreter PROG has version [RELATION] VERSION.
181 # i.e if RELATION is "<", check if PROG has a version number less than VERSION.
182 # Run ACTION-IF-FALSE otherwise.
183 # Specify RELATION as any mathematical comparison "<", ">", "<=", ">=", "==" or "!="
184 # This test uses sys.hexversion instead of the string equivalent (first
185 # word of sys.version), in order to cope with versions such as 2.2c1.
186 # This supports Python 2.0 or higher. (2.0 was released on October 16, 2000).
187 AC_DEFUN([PC_PYTHON_VERIFY_VERSION],
188 [m4_define([pc_python_safe_ver], m4_bpatsubsts($2, [\.], [_]))
189 AC_CACHE_CHECK([if Python $1 '$2'],
190 [[pc_cv_python_req_version_]pc_python_safe_ver],
191 [AC_LANG_PUSH(Python)[]dnl
192 AC_RUN_IFELSE(
193 [AC_LANG_PROGRAM([dnl
194 import sys
195 ], [dnl
196 # split strings by '.' and convert to numeric. Append some zeros
197 # because we need at least 4 digits for the hex conversion.
198 # map returns an iterator in Python 3.0 and a list in 2.x
199 reqver = list(map(int, '$2'.split('.'))) + [[0, 0, 0]]
200 reqverhex = 0
201 # xrange is not present in Python 3.0 and range returns an iterator
202 for i in list(range(4)):
203 reqverhex = (reqverhex << 8) + reqver[[i]]
204 # the final 8 bits are "0xf0" for final versions, which are all
205 # we'll test against, since it's doubtful that a released software
206 # will depend on an alpha- or beta-state Python.
207 reqverhex += 0xf0
208 if sys.hexversion $1 reqverhex:
209 sys.exit()
210 else:
211 sys.exit(1)
212 ])],
213 [[pc_cv_python_req_version_]pc_python_safe_ver=yes],
214 [[pc_cv_python_req_version_]pc_python_safe_ver=no])
215 AC_LANG_POP(Python)[]dnl
216 ])
217 AS_IF([test "$[pc_cv_python_req_version_]pc_python_safe_ver" = "no"], [$4], [$3])
218 ])# PC_PYTHON_VERIFY_VERSION
219
220
221 # PC_PYTHON_CHECK_VERSION
222 # -----------------------
223 # Query Python for its version number. Getting [:3] seems to be
224 # the best way to do this; it's what "site.py" does in the standard
225 # library.
226 AC_DEFUN([PC_PYTHON_CHECK_VERSION],
227 [AC_REQUIRE([PC_PROG_PYTHON])[]dnl
228 AC_CACHE_CHECK([for $1 version],
229 [pc_cv_python_version],
230 [AC_LANG_PUSH(Python)[]dnl
231 AC_LINK_IFELSE([AC_LANG_PROGRAM([dnl
232 import sys
233 ], [dnl
234 sys.stdout.write(sys.version[[:3]])
235 ])],
236 [pc_cv_python_version=`./conftest`],
237 [AC_MSG_FAILURE([failed to run Python program])])
238 AC_LANG_POP(Python)[]dnl
239 ])
240 AC_SUBST([PYTHON_VERSION], [$pc_cv_python_version])
241 ])# PC_PYTHON_CHECK_VERSION
242
243
244 # PC_PYTHON_CHECK_PREFIX
245 # ----------------------
246 # Use the value of $prefix for the corresponding value of
247 # PYTHON_PREFIX. This is made a distinct variable so it can be
248 # overridden if need be. However, general consensus is that you
249 # shouldn't need this ability.
250 AC_DEFUN([PC_PYTHON_CHECK_PREFIX],
251 [AC_REQUIRE([PC_PYTHON_PROG_PYTHON_CONFIG])[]dnl
252 dnl Try to get it with python-config otherwise do it from within Python
253 AC_CACHE_CHECK([for Python prefix], [pc_cv_python_prefix],
254 [if test -x "$PYTHON_CONFIG"; then
255 pc_cv_python_prefix=`$PYTHON_CONFIG --prefix 2>&AS_MESSAGE_LOG_FD`
256 else
257 AC_LANG_PUSH(Python)[]dnl
258 AC_LINK_IFELSE([AC_LANG_PROGRAM([dnl
259 import sys
260 ], [dnl
261 sys.stdout.write(sys.prefix)
262 ])], [pc_cv_python_prefix=`./conftest`;
263 if test $? != 0; then
264 AC_MSG_FAILURE([could not determine Python prefix])
265 fi],
266 [AC_MSG_FAILURE([failed to run Python program])])
267 AC_LANG_POP(Python)[]dnl
268 fi])
269 AC_SUBST([PYTHON_PREFIX], [$pc_cv_python_prefix])])
270
271
272 # PC_PYTHON_CHECK_EXEC_PREFIX
273 # --------------------------
274 # Like above, but for $exec_prefix
275 AC_DEFUN([PC_PYTHON_CHECK_EXEC_PREFIX],
276 [AC_REQUIRE([PC_PYTHON_PROG_PYTHON_CONFIG])[]dnl
277 dnl Try to get it with python-config otherwise do it from within Python
278 AC_CACHE_CHECK([for Python exec-prefix], [pc_cv_python_exec_prefix],
279 [if test -x "$PYTHON_CONFIG"; then
280 pc_cv_python_exec_prefix=`$PYTHON_CONFIG --exec-prefix 2>&AS_MESSAGE_LOG_FD`
281 else
282 AC_LANG_PUSH(Python)[]dnl
283 AC_LINK_IFELSE([AC_LANG_PROGRAM([dnl
284 import sys
285 ], [dnl
286 sys.stdout.write(sys.exec_prefix)
287 ])],
288 [pc_cv_python_exec_prefix=`./conftest`;
289 if test $? != 0; then
290 AC_MSG_FAILURE([could not determine Python exec_prefix])
291 fi],
292 [AC_MSG_FAILURE([failed to run Python program])])
293 AC_LANG_POP(Python)[]dnl
294 fi
295 ])
296 AC_SUBST([PYTHON_EXEC_PREFIX], [$pc_cv_python_exec_prefix])])
297
298
299 # PC_PYTHON_CHECK_INCLUDES
300 # ------------------------
301 # Find the Python header file include flags (ie
302 # '-I/usr/include/python')
303 AC_DEFUN([PC_PYTHON_CHECK_INCLUDES],
304 [AC_REQUIRE([PC_PYTHON_PROG_PYTHON_CONFIG])[]dnl
305 dnl Try to find the headers location with python-config otherwise guess
306 AC_CACHE_CHECK([for Python includes], [pc_cv_python_includes],
307 [if test -x "$PYTHON_CONFIG"; then
308 pc_cv_python_includes=`$PYTHON_CONFIG --includes 2>&AS_MESSAGE_LOG_FD`
309 else
310 pc_cv_python_includes="[-I$includedir/$_PYTHON_BASENAME]m4_ifdef(PYTHON_ABI_FLAGS,
311 PYTHON_ABI_FLAGS,)"
312 fi
313 ])
314 AC_SUBST([PYTHON_INCLUDES], [$pc_cv_python_includes])])
315
316
317 # PC_PYTHON_CHECK_HEADERS([ACTION-IF-PRESENT], [ACTION-IF-ABSENT])
318 # -----------------------
319 # Check for the presence and usability of Python.h
320 AC_DEFUN([PC_PYTHON_CHECK_HEADERS],
321 [AC_REQUIRE([PC_PYTHON_CHECK_INCLUDES])[]dnl
322 pc_cflags_store=$CPPFLAGS
323 CPPFLAGS="$CFLAGS $PYTHON_INCLUDES"
324 AC_CHECK_HEADER([Python.h], [$1], [$2])
325 CPPFLAGS=$pc_cflags_store
326 ])
327
328
329 # PC_PYTHON_CHECK_LIBS
330 # --------------------
331 # Find the Python lib flags (ie '-lpython')
332 AC_DEFUN([PC_PYTHON_CHECK_LIBS],
333 [AC_REQUIRE([PC_PYTHON_PROG_PYTHON_CONFIG])[]dnl
334 dnl Try to find the lib flags with python-config otherwise guess
335 AC_CACHE_CHECK([for Python libs], [pc_cv_python_libs],
336 [if test -x "$PYTHON_CONFIG"; then
337 pc_cv_python_libs=`$PYTHON_CONFIG --libs 2>&AS_MESSAGE_LOG_FD`
338 else
339 pc_cv_python_libs="[-l$_PYTHON_BASENAME]m4_ifdef(PYTHON_ABI_FLAGS, PYTHON_ABI_FLAGS,)"
340 fi
341 ])
342 AC_SUBST([PYTHON_LIBS], [$pc_cv_python_libs])])
343
344
345 # PC_PYTHON_TEST_LIBS(LIBRARY-FUNCTION, [ACTION-IF-PRESENT], [ACTION-IF-ABSENT])
346 # -------------------
347 # Verify that the Python libs can be loaded
348 AC_DEFUN([PC_PYTHON_TEST_LIBS],
349 [AC_REQUIRE([PC_PYTHON_CHECK_LIBS])[]dnl
350 pc_libflags_store=$LIBS
351 for lflag in $PYTHON_LIBS; do
352 case $lflag in
353 -lpython*@:}@
354 LIBS="$LIBS $lflag"
355 pc_libpython=`echo $lflag | sed -e 's/^-l//'`
356 ;;
357 *@:}@;;
358 esac
359 done
360 AC_CHECK_LIB([$pc_libpython], [$1], [$2], [$3])])
361
362
363 # PC_PYTHON_CHECK_CFLAGS
364 # ----------------------
365 # Find the Python CFLAGS
366 AC_DEFUN([PC_PYTHON_CHECK_CFLAGS],
367 [AC_REQUIRE([PC_PYTHON_PROG_PYTHON_CONFIG])[]dnl
368 dnl Try to find the CFLAGS with python-config otherwise give up
369 AC_CACHE_CHECK([for Python CFLAGS], [pc_cv_python_cflags],
370 [if test -x "$PYTHON_CONFIG"; then
371 pc_cv_python_cflags=`$PYTHON_CONFIG --cflags 2>&AS_MESSAGE_LOG_FD`
372 else
373 pc_cv_python_cflags=
374 fi
375 ])
376 AC_SUBST([PYTHON_CFLAGS], [$pc_cv_python_cflags])])
377
378
379 # PC_PYTHON_CHECK_LDFLAGS
380 # -----------------------
381 # Find the Python LDFLAGS
382 AC_DEFUN([PC_PYTHON_CHECK_LDFLAGS],
383 [AC_REQUIRE([PC_PYTHON_PROG_PYTHON_CONFIG])[]dnl
384 dnl Try to find the LDFLAGS with python-config otherwise give up
385 AC_CACHE_CHECK([for Python LDFLAGS], [pc_cv_python_ldflags],
386 [if test -x "$PYTHON_CONFIG"; then
387 pc_cv_python_ldflags=`$PYTHON_CONFIG --ldflags 2>&AS_MESSAGE_LOG_FD`
388 else
389 pc_cv_python_ldflags=
390 fi
391 ])
392 AC_SUBST([PYTHON_LDFLAGS], [$pc_cv_python_ldflags])])
393
394
395 # PC_PYTHON_CHECK_EXTENSION_SUFFIX
396 # --------------------------------
397 # Find the Python extension suffix (i.e. '.cpython-32.so')
398 AC_DEFUN([PC_PYTHON_CHECK_EXTENSION_SUFFIX],
399 [AC_REQUIRE([PC_PYTHON_PROG_PYTHON_CONFIG])[]dnl
400 dnl Try to find the suffix with python-config otherwise give up
401 AC_CACHE_CHECK([for Python extension suffix], [pc_cv_python_extension_suffix],
402 [if test -x "$PYTHON_CONFIG"; then
403 pc_cv_python_extension_suffix=`$PYTHON_CONFIG --extension-suffix 2>&AS_MESSAGE_LOG_FD`
404 else
405 pc_cv_python_extension_suffix=
406 fi
407 ])
408 AC_SUBST([PYTHON_EXTENSION_SUFFIX], [$pc_cv_python_extension_suffix])])
409
410
411 # PC_PYTHON_CHECK_ABI_FLAGS
412 # -------------------------
413 # Find the Python ABI flags
414 AC_DEFUN([PC_PYTHON_CHECK_ABI_FLAGS],
415 [AC_REQUIRE([PC_PYTHON_PROG_PYTHON_CONFIG])[]dnl
416 dnl Try to find the ABI flags with python-config otherwise give up
417 AC_CACHE_CHECK([for Python ABI flags], [pc_cv_python_abi_flags],
418 [if test -x "$PYTHON_CONFIG"; then
419 pc_cv_python_abi_flags=`$PYTHON_CONFIG --abiflags 2>&AS_MESSAGE_LOG_FD`
420 else
421 pc_cv_python_abi_flags=
422 fi
423 ])
424 AC_SUBST([PYTHON_ABI_FLAGS], [$pc_cv_python_abi_flags])])
425
426
427 # PC_PYTHON_CHECK_PLATFORM
428 # ------------------------
429 # At times (like when building shared libraries) you may want
430 # to know which OS platform Python thinks this is.
431 AC_DEFUN([PC_PYTHON_CHECK_PLATFORM],
432 [AC_REQUIRE([PC_PROG_PYTHON])[]dnl
433 dnl Get the platform from within Python (sys.platform)
434 AC_CACHE_CHECK([for Python platform], [pc_cv_python_platform],
435 [AC_LANG_PUSH(Python)[]dnl
436 AC_LINK_IFELSE([AC_LANG_PROGRAM([dnl
437 import sys
438 ], [dnl
439 sys.stdout.write(sys.platform)
440 ])], [pc_cv_python_platform=`./conftest`;
441 if test $? != 0; then
442 AC_MSG_FAILURE([could not determine Python platform])
443 fi],
444 [AC_MSG_FAILURE([failed to run Python program])])
445 AC_LANG_POP(Python)[]dnl
446 ])
447 AC_SUBST([PYTHON_PLATFORM], [$pc_cv_python_platform])
448 ])
449
450
451 # PC_PYTHON_CHECK_SITE_DIR
452 # ---------------------
453 # The directory to which new libraries are installed (i.e. the
454 # "site-packages" directory.
455 AC_DEFUN([PC_PYTHON_CHECK_SITE_DIR],
456 [AC_REQUIRE([PC_PROG_PYTHON])AC_REQUIRE([PC_PYTHON_CHECK_PREFIX])[]dnl
457 AC_CACHE_CHECK([for Python site-packages directory],
458 [pc_cv_python_site_dir],
459 [AC_LANG_PUSH(Python)[]dnl
460 if test "x$prefix" = xNONE
461 then
462 pc_py_prefix=$ac_default_prefix
463 else
464 pc_py_prefix=$prefix
465 fi
466 AC_LINK_IFELSE([AC_LANG_PROGRAM([dnl
467 import sys
468 from platform import python_implementation
469 # sysconfig in CPython 2.7 doesn't work in virtualenv
470 # <https://github.com/pypa/virtualenv/issues/118>
471 try:
472 import sysconfig
473 except:
474 can_use_sysconfig = False
475 else:
476 can_use_sysconfig = True
477 if can_use_sysconfig:
478 if python_implementation() == "CPython" and sys.version[[:3]] == '2.7':
479 can_use_sysconfig = False
480 if not can_use_sysconfig:
481 from distutils import sysconfig
482 sitedir = sysconfig.get_python_lib(False, False, prefix='$pc_py_prefix')
483 else:
484 sitedir = sysconfig.get_path('purelib', vars={'base':'$pc_py_prefix'})
485 ], [dnl
486 sys.stdout.write(sitedir)
487 ])], [pc_cv_python_site_dir=`./conftest`],
488 [AC_MSG_FAILURE([failed to run Python program])])
489 AC_LANG_POP(Python)[]dnl
490 case $pc_cv_python_site_dir in
491 $pc_py_prefix*)
492 pc__strip_prefix=`echo "$pc_py_prefix" | sed 's|.|.|g'`
493 pc_cv_python_site_dir=`echo "$pc_cv_python_site_dir" | sed "s,^$pc__strip_prefix/,,"`
494 ;;
495 *)
496 case $pc_py_prefix in
497 /usr|/System*) ;;
498 *)
499 pc_cv_python_site_dir=lib/python$PYTHON_VERSION/site-packages
500 ;;
501 esac
502 ;;
503 esac
504 ])
505 AC_SUBST([pythondir], [\${prefix}/$pc_cv_python_site_dir])])# PC_PYTHON_CHECK_SITE_DIR
506
507 # PC_PYTHON_SITE_PACKAGE_DIR
508 # --------------------------
509 # $PACKAGE directory under PYTHON_SITE_DIR
510 AC_DEFUN([PC_PYTHON_SITE_PACKAGE_DIR],
511 [AC_REQUIRE([PC_PYTHON_CHECK_SITE_DIR])[]dnl
512 AC_SUBST([pkgpythondir], [\${pythondir}/$PACKAGE_NAME])])
513
514
515 # PC_PYTHON_CHECK_EXEC_DIR
516 # ------------------------
517 # directory for installing python extension modules (shared libraries)
518 AC_DEFUN([PC_PYTHON_CHECK_EXEC_DIR],
519 [AC_REQUIRE([PC_PROG_PYTHON])AC_REQUIRE([PC_PYTHON_CHECK_EXEC_PREFIX])[]dnl
520 AC_CACHE_CHECK([for Python extension module directory],
521 [pc_cv_python_exec_dir],
522 [AC_LANG_PUSH(Python)[]dnl
523 if test "x$pc_cv_python_exec_prefix" = xNONE
524 then
525 pc_py_exec_prefix=$pc_cv_python_prefix
526 else
527 pc_py_exec_prefix=$pc_cv_python_exec_prefix
528 fi
529 AC_LINK_IFELSE([AC_LANG_PROGRAM([dnl
530 import sys
531 from platform import python_implementation
532 # sysconfig in CPython 2.7 doesn't work in virtualenv
533 # <https://github.com/pypa/virtualenv/issues/118>
534 try:
535 import sysconfig
536 except:
537 can_use_sysconfig = False
538 else:
539 can_use_sysconfig = True
540 if can_use_sysconfig:
541 if python_implementation() == "CPython" and sys.version[[:3]] == '2.7':
542 can_use_sysconfig = False
543 if not can_use_sysconfig:
544 from distutils import sysconfig
545 sitedir = sysconfig.get_python_lib(False, False, prefix='$pc_py__exec_prefix')
546 else:
547 sitedir = sysconfig.get_path('purelib', vars={'platbase':'$pc_py_exec_prefix'})
548 ], [dnl
549 sys.stdout.write(sitedir)
550 ])], [pc_cv_python_exec_dir=`./conftest`],
551 [AC_MSG_FAILURE([failed to run Python program])])
552 AC_LANG_POP(Python)[]dnl
553 case $pc_cv_python_exec_dir in
554 $pc_py_exec_prefix*)
555 pc__strip_prefix=`echo "$pc_py_exec_prefix" | sed 's|.|.|g'`
556 pc_cv_python_exec_dir=`echo "$pc_cv_python_exec_dir" | sed "s,^$pc__strip_prefix/,,"`
557 ;;
558 *)
559 case $pc_py_exec_prefix in
560 /usr|/System*) ;;
561 *)
562 pc_cv_python_exec_dir=lib/python$PYTHON_VERSION/site-packages
563 ;;
564 esac
565 ;;
566 esac
567 ])
568 AC_SUBST([pyexecdir], [\${exec_prefix}/$pc_cv_python_pyexecdir])]) #PY_PYTHON_CHECK_EXEC_LIB_DIR
569
570
571 # PC_PYTHON_EXEC_PACKAGE_DIR
572 # --------------------------
573 # $PACKAGE directory under PYTHON_SITE_DIR
574 AC_DEFUN([PC_PYTHON_EXEC_PACKAGE_DIR],
575 [AC_REQUIRE([PC_PYTHON_CHECK_EXEC_DIR])[]dnl
576 AC_SUBST([pkgpyexecdir], [\${pyexecdir}/$PACKAGE_NAME])])
577
578
579 ## -------------------------------------------- ##
580 ## 4. Looking for specific libs & functionality ##
581 ## -------------------------------------------- ##
582
583
584 # PC_PYTHON_CHECK_MODULE(LIBRARY, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
585 # ----------------------------------------------------------------------
586 # Macro for checking if a Python library is installed
587 AC_DEFUN([PC_PYTHON_CHECK_MODULE],
588 [AC_REQUIRE([PC_PROG_PYTHON])[]dnl
589 m4_define([pc_python_safe_mod], m4_bpatsubsts($1, [\.], [_]))
590 AC_CACHE_CHECK([for Python '$1' library],
591 [[pc_cv_python_module_]pc_python_safe_mod],
592 [AC_LANG_PUSH(Python)[]dnl
593 AC_RUN_IFELSE(
594 [AC_LANG_PROGRAM([dnl
595 import sys
596 try:
597 import $1
598 except:
599 sys.exit(1)
600 else:
601 sys.exit(0)
602 ], [])],
603 [[pc_cv_python_module_]pc_python_safe_mod="yes"],
604 [[pc_cv_python_module_]pc_python_safe_mod="no"])
605 AC_LANG_POP(Python)[]dnl
606 ])
607 AS_IF([test "$[pc_cv_python_module_]pc_python_safe_mod" = "no"], [$3], [$2])
608 ])# PC_PYTHON_CHECK_MODULE
609
610
611 # PC_PYTHON_CHECK_FUNC([LIBRARY], FUNCTION, ARGS, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
612 # ---------------------------------------------------------------------------------------
613 # Check to see if a given function call, optionally from a module, can
614 # be successfully called
615 AC_DEFUN([PC_PYTHON_CHECK_FUNC],
616 [AC_REQUIRE([PC_PROG_PYTHON])[]dnl
617 m4_define([pc_python_safe_mod], m4_bpatsubsts($1, [\.], [_]))
618 AC_CACHE_CHECK([for Python m4_ifnblank($1, '$1.$2()', '$2()') function],
619 [[pc_cv_python_func_]pc_python_safe_mod[_$2]],
620 [AC_LANG_PUSH(Python)[]dnl
621 AC_RUN_IFELSE(
622 [AC_LANG_PROGRAM([dnl
623 import sys
624 m4_ifnblank([$1], [dnl
625 try:
626 import $1
627 except:
628 sys.exit(1)
629 ], [])],
630 [
631 m4_ifnblank([$1], [
632 try:
633 $1.$2($3)], [
634 try:
635 $2($3)])
636 except:
637 sys.exit(1)
638 else:
639 sys.exit(0)
640 ])],
641 [[pc_cv_python_func_]pc_python_safe_mod[_$2]="yes"],
642 [[pc_cv_python_func_]pc_python_safe_mod[_$2]="no"])
643 AC_LANG_POP(Python)[]dnl
644 ])
645 AS_IF([test "$[pc_cv_python_func_]pc_python_safe_mod[_$2]" = "no"], [$5], [$4])
646 ])# PC_PYTHON_CHECK_FUNC