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