1 # Copyright 2012 Brandon Invergo <brandon@invergo.net>
3 # Copying and distribution of this file, with or without modification,
4 # are permitted in any medium without royalty provided the copyright
5 # notice and this notice are preserved. This file is offered as-is,
6 # without any warranty.
8 # Many of these macros were adapted from ones written by Andrew Dalke
9 # and James Henstridge and are included with the Automake utility
10 # under the following copyright terms:
12 # Copyright (C) 1999-2012 Free Software Foundation, Inc.
14 # This file is free software; the Free Software Foundation
15 # gives unlimited permission to copy and/or distribute it,
16 # with or without modifications, as long as this notice is preserved.
20 # 1. Language selection
21 # and routines to produce programs in a given language.
23 # 2. Producing programs in a given language.
25 # 3. Looking for a compiler
26 # And possibly the associated preprocessor.
28 # 4. Looking for specific libs & functionality
31 ## ----------------------- ##
32 ## 1. Language selection. ##
33 ## ----------------------- ##
38 AC_LANG_DEFINE([Python], [py], [PY], [PYTHON], [],
40 ac_compile='chmod +x conftest.$ac_ext >&AS_MESSAGE_LOG_FD'
41 ac_link='chmod +x conftest.$ac_ext && cp conftest.$ac_ext conftest >&AS_MESSAGE_LOG_FD'
47 AU_DEFUN([AC_LANG_PYTHON], [AC_LANG(Python)])
50 ## ----------------------- ##
51 ## 2. Producing programs. ##
52 ## ----------------------- ##
55 # AC_LANG_PROGRAM(Python)([PROLOGUE], [BODY])
56 # -------------------------------------------
57 m4_define([AC_LANG_PROGRAM(Python)], [dnl
60 m4_if([$2], [], [], [dnl
61 if __name__ == '__main__':
65 # _AC_LANG_IO_PROGRAM(Python)
66 # ---------------------------
67 # Produce source that performs I/O.
68 m4_define([_AC_LANG_IO_PROGRAM(Python)],
72 h = open('conftest.out')
81 # _AC_LANG_CALL(Python)([PROLOGUE], [FUNCTION])
82 # ---------------------
83 # Produce source that calls FUNCTION
84 m4_define([_AC_LANG_CALL(Python)],
85 [AC_LANG_PROGRAM([$1], [$2])])
89 ## -------------------------------------------- ##
90 ## 3. Looking for Compilers and Interpreters. ##
91 ## -------------------------------------------- ##
94 AC_DEFUN([AC_LANG_COMPILER(Python)],
95 [AC_REQUIRE([AC_PROG_PYTHON])])
98 # AC_PROG_PYTHON(PROG-TO-CHECK-FOR)
99 # ---------------------------------
100 # Find a Python interpreter. Python versions prior to 2.0 are not
101 # supported. (2.0 was released on October 16, 2000).
102 AC_DEFUN([AC_PROG_PYTHON],
103 [AC_ARG_VAR([PYTHON], [the Python interpreter])
104 m4_define_default([_PC_PYTHON_INTERPRETER_LIST],
105 [python python3 python3.2 python3.1 python3.0 python2 python2.7 dnl
106 python2.6 python2.5 python2.4 python2.3 python2.2 python2.1 python2.0])
108 [AC_PATH_PROGS(PYTHON, [$1 _PC_PYTHON_INTERPRETER_LIST])],
109 [AC_PATH_PROGS(PYTHON, [_PC_PYTHON_INTERPRETER_LIST])])
113 # PC_PYTHON_PROG_PYTHON_CONFIG(PROG-TO-CHECK-FOR)
114 # ----------------------------------------------
115 # Find the python-config program
116 AC_DEFUN([PC_PYTHON_PROG_PYTHON_CONFIG],
117 [AC_REQUIRE([AC_PROG_PYTHON])[]dnl
118 AC_ARG_VAR([PYTHON_CONFIG], [the Python-config program])
119 m4_define([_PYTHON_BASENAME], [`basename $PYTHON`])
121 [AC_PATH_PROGS(PYTHON_CONFIG, [$1 _PYTHON_BASENAME-config])],
122 [AC_PATH_PROG(PYTHON_CONFIG, _PYTHON_BASENAME-config)])
123 ]) # PC_PYTHON_PROG_PYTHON_CONFIG
126 # PC_PYTHON_VERIFY_VERSION(PYTHON-PROGRAM, VERSION, [ACTION-IF-TRUE], [ACTION-IF-NOT-FOUND])
127 # ---------------------------------------------------------------------------
128 # Run ACTION-IF-TRUE if the Python interpreter PROG has version >= VERSION.
129 # Run ACTION-IF-FALSE otherwise.
130 # This test uses sys.hexversion instead of the string equivalent (first
131 # word of sys.version), in order to cope with versions such as 2.2c1.
132 # This supports Python 2.0 or higher. (2.0 was released on October 16, 2000).
133 AC_DEFUN([PC_PYTHON_VERIFY_VERSION],
134 [AC_REQUIRE([AC_PROG_PYTHON])[]dnl
135 m4_define([pc_python_safe_ver], m4_bpatsubsts($2, [\.], [_]))
136 AC_CACHE_CHECK([if Python >= '$2'],
137 [[pc_cv_python_min_version_]pc_python_safe_ver],
138 [AC_LANG_PUSH(Python)[]dnl
140 [AC_LANG_PROGRAM([dnl
143 # split strings by '.' and convert to numeric. Append some zeros
144 # because we need at least 4 digits for the hex conversion.
145 # map returns an iterator in Python 3.0 and a list in 2.x
146 minver = list(map(int, '$2'.split('.'))) + [[0, 0, 0]]
148 # xrange is not present in Python 3.0 and range returns an iterator
149 for i in list(range(4)):
150 minverhex = (minverhex << 8) + minver[[i]]
151 sys.exit(sys.hexversion < minverhex)
153 [[pc_cv_python_min_version_]pc_python_safe_ver="yes"],
154 [[pc_cv_python_min_version_]pc_python_safe_ver="no"])
155 AC_LANG_POP(Python)[]dnl
157 AS_IF([test "$[pc_cv_python_min_version_]pc_python_safe_ver" = "no"], [$4], [$3])
158 ])# PC_PYTHON_VERIFY_VERSION
161 # PC_PYTHON_CHECK_VERSION
162 # -----------------------
163 # Query Python for its version number. Getting [:3] seems to be
164 # the best way to do this; it's what "site.py" does in the standard
166 AC_DEFUN([PC_PYTHON_CHECK_VERSION],
167 [AC_REQUIRE([AC_PROG_PYTHON])[]dnl
168 AC_CACHE_CHECK([for $1 version],
169 [pc_cv_python_version],
170 [AC_LANG_PUSH(Python)[]dnl
175 sys.stdout.write(sys.version[[:3]])
177 pc_cv_python_version=`$PYTHON conftest.py`
178 AC_LANG_POP(Python)[]dnl
180 AC_SUBST([PYTHON_VERSION], [$pc_cv_python_version])
181 ])# PC_PYTHON_CHECK_VERSION
184 # PC_PYTHON_CHECK_PREFIX
185 # ----------------------
186 # Use the value of $prefix for the corresponding value of
187 # PYTHON_PREFIX. This is made a distinct variable so it can be
188 # overridden if need be. However, general consensus is that you
189 # shouldn't need this ability.
190 AC_DEFUN([PC_PYTHON_CHECK_PREFIX],
191 [AC_REQUIRE([PC_PYTHON_PROG_PYTHON_CONFIG])[]dnl
192 AC_CACHE_CHECK([for Python prefix], [pc_cv_python_prefix],
193 [if test -x "$PYTHON_CONFIG"; then
194 pc_cv_python_prefix=`$PYTHON_CONFIG --prefix 2>> AS_MESSAGE_LOG_FD`
196 AC_LANG_PUSH(Python)[]dnl
197 pc_cv_python_prefix=AC_LANG_CONFTEST([AC_LANG_PROGRAM([dnl
202 AC_LANG_POP(Python)[]dnl
204 AC_SUBST([PYTHON_PREFIX], [$pc_cv_python_prefix])])
207 # PC_PYTHON_CHECK_EXEC_PREFIX
208 # --------------------------
209 # Like above, but for $exec_prefix
210 AC_DEFUN([PC_PYTHON_CHECK_EXEC_PREFIX],
211 [AC_REQUIRE([PC_PYTHON_PROG_PYTHON_CONFIG])[]dnl
212 AC_CACHE_CHECK([for Python exec-prefix], [pc_cv_python_exec_prefix],
213 [if test -x "$PYTHON_CONFIG"; then
214 pc_cv_python_exec_prefix=`$PYTHON_CONFIG --exec-prefix 2>> AS_MESSAGE_LOG_FD`
216 AC_LANG_PUSH(Python)[]dnl
217 pc_cv_python_exec_prefix=AC_LANG_CONFTEST([AC_LANG_PROGRAM([dnl
220 sys.exit(sys.exec_prefix)
222 AC_LANG_POP(Python)[]dnl
225 AC_SUBST([PYTHON_EXEC_PREFIX], [$pc_cv_python_exec_prefix])])
228 # PC_PYTHON_CHECK_INCLUDES
229 # ------------------------
230 # Find the Python header file include flags (ie
231 # '-I/usr/include/python')
232 AC_DEFUN([PC_PYTHON_CHECK_INCLUDES],
233 [AC_REQUIRE([PC_PYTHON_PROG_PYTHON_CONFIG])[]dnl
234 AC_CACHE_CHECK([for Python includes], [pc_cv_python_includes],
235 [if test -x "$PYTHON_CONFIG"; then
236 pc_cv_python_includes=`$PYTHON_CONFIG --includes 2>> AS_MESSAGE_LOG_FD`
238 pc_cv_python_includes="[-I$includedir/$_PYTHON_BASENAME]m4_ifdef(PYTHON_ABI_FLAGS,
242 AC_SUBST([PYTHON_INCLUDES], [$pc_cv_python_includes])])
245 # PC_PYTHON_CHECK_HEADERS([ACTION-IF-PRESENT], [ACTION-IF-ABSENT])
246 # -----------------------
247 # Check for the presence and usability of Python.h
248 AC_DEFUN([PC_PYTHON_CHECK_HEADERS],
249 [AC_REQUIRE([PC_PYTHON_CHECK_INCLUDES])[]dnl
250 pc_cflags_store=$CPPFLAGS
251 CPPFLAGS="$CFLAGS $PYTHON_INCLUDES"
252 AC_CHECK_HEADER([Python.h], [$1], [$2])
253 CPPFLAGS=$pc_cflags_store
257 # PC_PYTHON_CHECK_LIBS
258 # --------------------
259 # Find the Python lib flags (ie '-lpython')
260 AC_DEFUN([PC_PYTHON_CHECK_LIBS],
261 [AC_REQUIRE([PC_PYTHON_PROG_PYTHON_CONFIG])[]dnl
262 AC_CACHE_CHECK([for Python libs], [pc_cv_python_libs],
263 [if test -x "$PYTHON_CONFIG"; then
264 pc_cv_python_libs=`$PYTHON_CONFIG --libs 2>> AS_MESSAGE_LOG_FD`
266 pc_cv_python_libs="[-l$_PYTHON_BASENAME]m4_ifdef(PYTHON_ABI_FLAGS, PYTHON_ABI_FLAGS,)"
269 AC_SUBST([PYTHON_LIBS], [$pc_cv_python_libs])])
272 # PC_PYTHON_TEST_LIBS(LIBRARY-FUNCTION, [ACTION-IF-PRESENT], [ACTION-IF-ABSENT])
273 # -------------------
274 # Verify that the Python libs can be loaded
275 AC_DEFUN([PC_PYTHON_TEST_LIBS],
276 [AC_REQUIRE([PC_PYTHON_CHECK_LIBS])[]dnl
277 pc_libflags_store=$LIBS
278 for lflag in $PYTHON_LIBS; do
282 pc_libpython=`echo $lflag | sed -e 's/^-l//'`
287 AC_CHECK_LIB([$pc_libpython], [$1], [$2], [$3])])
290 # PC_PYTHON_CHECK_CFLAGS
291 # ----------------------
292 # Find the Python CFLAGS
293 AC_DEFUN([PC_PYTHON_CHECK_CFLAGS],
294 [AC_REQUIRE([PC_PYTHON_PROG_PYTHON_CONFIG])[]dnl
295 AC_CACHE_CHECK([for Python CFLAGS], [pc_cv_python_cflags],
296 [if test -x "$PYTHON_CONFIG"; then
297 pc_cv_python_cflags=`$PYTHON_CONFIG --cflags 2>> AS_MESSAGE_LOG_FD`
302 AC_SUBST([PYTHON_CFLAGS], [$pc_cv_python_cflags])])
305 # PC_PYTHON_CHECK_LDFLAGS
306 # -----------------------
307 # Find the Python LDFLAGS
308 AC_DEFUN([PC_PYTHON_CHECK_LDFLAGS],
309 [AC_REQUIRE([PC_PYTHON_PROG_PYTHON_CONFIG])[]dnl
310 AC_CACHE_CHECK([for Python LDFLAGS], [pc_cv_python_ldflags],
311 [if test -x "$PYTHON_CONFIG"; then
312 pc_cv_python_ldflags=`$PYTHON_CONFIG --ldflags 2>> AS_MESSAGE_LOG_FD`
314 pc_cv_python_ldflags=
317 AC_SUBST([PYTHON_LDFLAGS], [$pc_cv_python_ldflags])])
320 # PC_PYTHON_CHECK_EXTENSION_SUFFIX
321 # --------------------------------
322 # Find the Python extension suffix (i.e. '.cpython-32.so')
323 AC_DEFUN([PC_PYTHON_CHECK_EXTENSION_SUFFIX],
324 [AC_REQUIRE([PC_PYTHON_PROG_PYTHON_CONFIG])[]dnl
325 AC_CACHE_CHECK([for Python extension suffix], [pc_cv_python_extension_suffix],
326 [if test -x "$PYTHON_CONFIG"; then
327 pc_cv_python_extension_suffix=`$PYTHON_CONFIG --extension-suffix 2>> AS_MESSAGE_LOG_FD`
329 pc_cv_python_extension_suffix=
332 AC_SUBST([PYTHON_EXTENSION_SUFFIX], [$pc_cv_python_extension_suffix])])
335 # PC_PYTHON_CHECK_ABI_FLAGS
336 # -------------------------
337 # Find the Python ABI flags
338 AC_DEFUN([PC_PYTHON_CHECK_ABI_FLAGS],
339 [AC_REQUIRE([PC_PYTHON_PROG_PYTHON_CONFIG])[]dnl
340 AC_CACHE_CHECK([for Python ABI flags], [pc_cv_python_abi_flags],
341 [if test -x "$PYTHON_CONFIG"; then
342 pc_cv_python_abi_flags=`$PYTHON_CONFIG --abiflags 2>> AS_MESSAGE_LOG_FD`
344 pc_cv_python_abi_flags=
347 AC_SUBST([PYTHON_ABI_FLAGS], [$pc_cv_python_abi_flags])])
350 # PC_PYTHON_CHECK_PLATFORM
351 # ------------------------
352 # At times (like when building shared libraries) you may want
353 # to know which OS platform Python thinks this is.
354 AC_DEFUN([PC_PYTHON_CHECK_PLATFORM],
355 [AC_REQUIRE([AC_PROG_PYTHON])[]dnl
356 AC_CACHE_CHECK([for Python platform],
357 [pc_cv_python_platform],
358 [AC_LANG_PUSH(Python)[]dnl
363 sys.stdout.write(sys.platform)
365 pc_cv_python_platform=`$PYTHON conftest.py`
366 AC_LANG_POP(Python)[]dnl
368 AC_SUBST([PYTHON_PLATFORM], [$pc_cv_python_platform])
372 # PC_PYTHON_CHECK_SITE_DIR
373 # ---------------------
374 # The directory to which new libraries are installed (i.e. the
375 # "site-packages" directory.
376 AC_DEFUN([PC_PYTHON_CHECK_SITE_DIR],
377 [AC_REQUIRE([AC_PROG_PYTHON])AC_REQUIRE([PC_PYTHON_CHECK_PREFIX])[]dnl
378 AC_CACHE_CHECK([for Python site-packages directory],
379 [pc_cv_python_site_dir],
380 [AC_LANG_PUSH(Python)[]dnl
381 if test "x$prefix" = xNONE
383 pc_py_prefix=$ac_default_prefix
393 from distutils import sysconfig
394 sitedir = sysconfig.get_python_lib(False, False, prefix='$pc_py_prefix')
396 sitedir = sysconfig.get_path('purelib', vars={'base':'$pc_py_prefix'})
398 sys.stdout.write(sitedir)
400 pc_cv_python_site_dir=`$PYTHON conftest.py`
401 AC_LANG_POP(Python)[]dnl
402 case $pc_cv_python_site_dir in
404 pc__strip_prefix=`echo "$pc_py_prefix" | sed 's|.|.|g'`
405 pc_cv_python_site_dir=`echo "$pc_cv_python_site_dir" | sed "s,^$pc__strip_prefix/,,"`
408 case $pc_py_prefix in
411 pc_cv_python_site_dir=lib/python$PYTHON_VERSION/site-packages
417 AC_SUBST([pythondir], [\${prefix}/$pc_cv_python_site_dir])])# PC_PYTHON_CHECK_SITE_DIR
420 # PC_PYTHON_SITE_PACKAGE_DIR
421 # --------------------------
422 # $PACKAGE directory under PYTHON_SITE_DIR
423 AC_DEFUN([PC_PYTHON_SITE_PACKAGE_DIR],
424 [AC_REQUIRE([PC_PYTHON_CHECK_SITE_DIR])[]dnl
425 AC_SUBST([pkgpythondir], [\${pythondir}/$PACKAGE])])
428 # PC_PYTHON_CHECK_EXEC_DIR
429 # ------------------------
430 # directory for installing python extension modules (shared libraries)
431 AC_DEFUN([PC_PYTHON_CHECK_EXEC_DIR],
432 [AC_REQUIRE([AC_PROG_PYTHON])AC_REQUIRE([PC_PYTHON_CHECK_EXEC_PREFIX])[]dnl
433 AC_CACHE_CHECK([for Python extension module directory],
434 [pc_cv_python_exec_dir],
435 [AC_LANG_PUSH(Python)[]dnl
436 if test "x$pc_cv_python_exec_prefix" = xNONE
438 pc_py_exec_prefix=$pc_cv_python_prefix
440 pc_py_exec_prefix=$pc_cv_python_exec_prefix
448 from distutils import sysconfig
449 sitedir = sysconfig.get_python_lib(True, False, prefix='$pc_py_exec_prefix')
451 sitedir = sysconfig.get_path('platlib', vars={'platbase':'$pc_py_exec_prefix'})
453 sys.stdout.write(sitedir)
455 pc_cv_python_exec_dir=`$PYTHON conftest.py`
456 AC_LANG_POP(Python)[]dnl
457 case $pc_cv_python_exec_dir in
459 pc__strip_prefix=`echo "$pc_py_exec_prefix" | sed 's|.|.|g'`
460 pc_cv_python_exec_dir=`echo "$pc_cv_python_exec_dir" | sed "s,^$pc__strip_prefix/,,"`
463 case $pc_py_exec_prefix in
466 pc_cv_python_exec_dir=lib/python$PYTHON_VERSION/site-packages
472 AC_SUBST([pyexecdir], [\${exec_prefix}/$pc_cv_python_pyexecdir])]) #PY_PYTHON_CHECK_EXEC_LIB_DIR
475 # PC_PYTHON_EXEC_PACKAGE_DIR
476 # --------------------------
477 # $PACKAGE directory under PYTHON_SITE_DIR
478 AC_DEFUN([PC_PYTHON_EXEC_PACKAGE_DIR],
479 [AC_REQUIRE([PC_PYTHON_CHECK_EXEC_DIR])[]dnl
480 AC_SUBST([pkgpyexecdir], [\${pyexecdir}/$PACKAGE])])
483 ## -------------------------------------------- ##
484 ## 4. Looking for specific libs & functionality ##
485 ## -------------------------------------------- ##
488 # PC_PYTHON_CHECK_MODULE(LIBRARY, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
489 # ----------------------------------------------------------------------
490 # Macro for checking if a Python library is installed
491 AC_DEFUN([PC_PYTHON_CHECK_MODULE],
492 [AC_REQUIRE([AC_PROG_PYTHON])[]dnl
493 m4_define([pc_python_safe_mod], m4_bpatsubsts($1, [\.], [_]))
494 AC_CACHE_CHECK([for Python '$1' library],
495 [[pc_cv_python_module_]pc_python_safe_mod],
496 [AC_LANG_PUSH(Python)[]dnl
498 [AC_LANG_PROGRAM([dnl
507 [[pc_cv_python_module_]pc_python_safe_mod="yes"],
508 [[pc_cv_python_module_]pc_python_safe_mod="no"])
509 AC_LANG_POP(Python)[]dnl
511 AS_IF([test "$[pc_cv_python_module_]pc_python_safe_mod" = "no"], [$3], [$2])
512 ])# PC_PYTHON_CHECK_MODULE
515 # PC_PYTHON_CHECK_FUNC([LIBRARY], FUNCTION, ARGS, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
516 # ---------------------------------------------------------------------------------------
517 # Check to see if a given function call, optionally from a module, can
518 # be successfully called
519 AC_DEFUN([PC_PYTHON_CHECK_FUNC],
520 [AC_REQUIRE([AC_PROG_PYTHON])[]dnl
521 m4_define([pc_python_safe_mod], m4_bpatsubsts($1, [\.], [_]))
522 AC_CACHE_CHECK([for Python m4_ifnblank($1, '$1.$2()', '$2()') function],
523 [[pc_cv_python_func_]pc_python_safe_mod[_$2]],
524 [AC_LANG_PUSH(Python)[]dnl
526 [AC_LANG_PROGRAM([dnl
528 m4_ifnblank([$1], [dnl
545 [[pc_cv_python_func_]pc_python_safe_mod[_$2]="yes"],
546 [[pc_cv_python_func_]pc_python_safe_mod[_$2]="no"])
547 AC_LANG_POP(Python)[]dnl
549 AS_IF([test "$[pc_cv_python_func_]pc_python_safe_mod[_$2]" = "no"], [$5], [$4])
550 ])# PC_PYTHON_CHECK_FUNC