1#
2# Autoconf macros for configuring the build of Python extension modules
3#
4# config/python.m4
5#
6
7# PGAC_PATH_PYTHON
8# ----------------
9# Look for Python and set the output variable 'PYTHON' if found,
10# fail otherwise.
11#
12# As the Python 3 transition happens and PEP 394 isn't updated, we
13# need to cater to systems that don't have unversioned "python" by
14# default.  Some systems ship with "python3" by default and perhaps
15# have "python" in an optional package.  Some systems only have
16# "python2" and "python3", in which case it's reasonable to prefer the
17# newer version.
18AC_DEFUN([PGAC_PATH_PYTHON],
19[PGAC_PATH_PROGS(PYTHON, [python python3 python2])
20if test x"$PYTHON" = x""; then
21  AC_MSG_ERROR([Python not found])
22fi
23])
24
25
26# _PGAC_CHECK_PYTHON_DIRS
27# -----------------------
28# Determine the name of various directories of a given Python installation,
29# as well as the Python version.
30AC_DEFUN([_PGAC_CHECK_PYTHON_DIRS],
31[AC_REQUIRE([PGAC_PATH_PYTHON])
32AC_MSG_CHECKING([for Python distutils module])
33if "${PYTHON}" -c 'import distutils' 2>&AS_MESSAGE_LOG_FD
34then
35    AC_MSG_RESULT(yes)
36else
37    AC_MSG_RESULT(no)
38    AC_MSG_ERROR([distutils module not found])
39fi
40AC_MSG_CHECKING([Python configuration directory])
41python_majorversion=`${PYTHON} -c "import sys; print(sys.version[[0]])"`
42python_minorversion=`${PYTHON} -c "import sys; print(sys.version[[2]])"`
43python_version=`${PYTHON} -c "import sys; print(sys.version[[:3]])"`
44python_configdir=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('LIBPL'))))"`
45AC_MSG_RESULT([$python_configdir])
46
47# Reject unsupported Python versions as soon as practical.
48if test "$python_majorversion" -lt 3 -a "$python_minorversion" -lt 4; then
49  AC_MSG_ERROR([Python version $python_version is too old (version 2.4 or later is required)])
50fi
51
52AC_MSG_CHECKING([Python include directories])
53python_includespec=`${PYTHON} -c "
54import distutils.sysconfig
55a = '-I' + distutils.sysconfig.get_python_inc(False)
56b = '-I' + distutils.sysconfig.get_python_inc(True)
57if a == b:
58    print(a)
59else:
60    print(a + ' ' + b)"`
61if test "$PORTNAME" = win32 ; then
62    python_includespec=`echo $python_includespec | sed 's,[[\]],/,g'`
63fi
64AC_MSG_RESULT([$python_includespec])
65
66AC_SUBST(python_majorversion)[]dnl
67AC_SUBST(python_version)[]dnl
68AC_SUBST(python_includespec)[]dnl
69])# _PGAC_CHECK_PYTHON_DIRS
70
71
72# PGAC_CHECK_PYTHON_EMBED_SETUP
73# -----------------------------
74#
75# Set python_libdir to the path of the directory containing the Python shared
76# library.  Set python_libspec to the -L/-l linker switches needed to link it.
77# Set python_additional_libs to contain any additional linker switches needed
78# for subsidiary libraries.
79#
80# In modern, well-configured Python installations, LIBDIR gives the correct
81# directory name and LDLIBRARY is the file name of the shlib.  But in older
82# installations LDLIBRARY is frequently a useless path fragment, and it's also
83# possible that the shlib is in a standard library directory such as /usr/lib
84# so that LIBDIR is irrelevant.  Also, some packagers put the .so symlink for
85# the shlib in ${python_configdir} even though Python itself never does.
86# We must also check that what we found is a shared library not a plain
87# library, which we do by checking its extension.  (We used to rely on
88# Py_ENABLE_SHARED, but that only tells us that a shlib exists, not that
89# we found it.)
90AC_DEFUN([PGAC_CHECK_PYTHON_EMBED_SETUP],
91[AC_REQUIRE([_PGAC_CHECK_PYTHON_DIRS])
92AC_MSG_CHECKING([how to link an embedded Python application])
93
94python_libdir=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('LIBDIR'))))"`
95python_ldlibrary=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('LDLIBRARY'))))"`
96
97# If LDLIBRARY exists and has a shlib extension, use it verbatim.
98ldlibrary=`echo "${python_ldlibrary}" | sed -e 's/\.so$//' -e 's/\.dll$//' -e 's/\.dylib$//' -e 's/\.sl$//'`
99if test -e "${python_libdir}/${python_ldlibrary}" -a x"${python_ldlibrary}" != x"${ldlibrary}"
100then
101	ldlibrary=`echo "${ldlibrary}" | sed "s/^lib//"`
102	found_shlib=1
103else
104	# Otherwise, guess the base name of the shlib.
105	# LDVERSION was added in Python 3.2, before that use VERSION,
106	# or failing that, $python_version from _PGAC_CHECK_PYTHON_DIRS.
107	python_ldversion=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('LDVERSION'))))"`
108	if test x"${python_ldversion}" != x""; then
109		ldlibrary="python${python_ldversion}"
110	else
111		python_version_var=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('VERSION'))))"`
112		if test x"${python_version_var}" != x""; then
113			ldlibrary="python${python_version_var}"
114		else
115			ldlibrary="python${python_version}"
116		fi
117	fi
118	# Search for a likely-looking file.
119	found_shlib=0
120	for d in "${python_libdir}" "${python_configdir}" /usr/lib64 /usr/lib
121	do
122		# We don't know the platform DLSUFFIX here, so check 'em all.
123		for e in .so .dll .dylib .sl; do
124			if test -e "$d/lib${ldlibrary}$e"; then
125				python_libdir="$d"
126				found_shlib=1
127				break 2
128			fi
129		done
130	done
131	# Some platforms (OpenBSD) require us to accept a bare versioned shlib
132	# (".so.n.n") as well. However, check this only after failing to find
133	# ".so" anywhere, because yet other platforms (Debian) put the .so
134	# symlink in a different directory from the underlying versioned shlib.
135	if test "$found_shlib" != 1; then
136		for d in "${python_libdir}" "${python_configdir}" /usr/lib64 /usr/lib
137		do
138			for f in "$d/lib${ldlibrary}.so."* ; do
139				if test -e "$f"; then
140					python_libdir="$d"
141					found_shlib=1
142					break 2
143				fi
144			done
145		done
146	fi
147	# As usual, Windows has its own ideas.  Possible default library
148	# locations include c:/Windows/System32 and (for Cygwin) /usr/bin,
149	# and the "lib" prefix might not be there.
150	if test "$found_shlib" != 1 -a \( "$PORTNAME" = win32 -o "$PORTNAME" = cygwin \); then
151		for d in "${python_libdir}" "${python_configdir}" c:/Windows/System32 /usr/bin
152		do
153			for f in "$d/lib${ldlibrary}.dll" "$d/${ldlibrary}.dll" ; do
154				if test -e "$f"; then
155					python_libdir="$d"
156					found_shlib=1
157					break 2
158				fi
159			done
160		done
161	fi
162fi
163if test "$found_shlib" != 1; then
164	AC_MSG_ERROR([could not find shared library for Python
165You might have to rebuild your Python installation.  Refer to the
166documentation for details.  Use --without-python to disable building
167PL/Python.])
168fi
169python_libspec="-L${python_libdir} -l${ldlibrary}"
170
171python_additional_libs=`${PYTHON} -c "import distutils.sysconfig; print(' '.join(filter(None,distutils.sysconfig.get_config_vars('LIBS','LIBC','LIBM','BASEMODLIBS'))))"`
172
173AC_MSG_RESULT([${python_libspec} ${python_additional_libs}])
174
175AC_SUBST(python_libdir)[]dnl
176AC_SUBST(python_libspec)[]dnl
177AC_SUBST(python_additional_libs)[]dnl
178
179])# PGAC_CHECK_PYTHON_EMBED_SETUP
180