1#! /bin/sh
2# Copyright (C) 2011-2021 Free Software Foundation, Inc.
3#
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 2, or (at your option)
7# 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# You should have received a copy of the GNU General Public License
15# along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17# Check that AM_PATH_PYTHON correctly sets all the output variables
18# advertised in the manual, both with the GNU prefix values and the
19# Python sys.* prefix values.
20
21required=python
22. test-init.sh
23
24# In case the user's config.site defines pythondir or pyexecdir.
25CONFIG_SITE=/dev/null; export CONFIG_SITE
26
27# Update the definition below if the documentation changes. The values
28# of the 'pythondir' and 'pyexecdir' variables vary among different
29# python installations, so we need more relaxed and ad-hoc checks for
30# them. Also, more proper "functional" checks on them should be done in
31# the 'python-virtualenv.sh' test.
32#
33# This version identification is duplicated in python.m4 (and the manual).
34PYTHON_VERSION=$($PYTHON -c 'import sys; print ("%u.%u" % sys.version_info[:2])') || exit 1
35PYTHON_PLATFORM=$($PYTHON -c 'import sys; print (sys.platform)') || exit 1
36PYTHON_EXEC_PREFIX=$($PYTHON -c 'import sys; print (sys.exec_prefix)') || exit 1
37PYTHON_PREFIX=$($PYTHON -c 'import sys; print (sys.prefix)') || exit 1
38pkgpythondir="\${pythondir}/$me"
39pkgpyexecdir="\${pyexecdir}/$me"
40
41pyvars='PYTHON_VERSION PYTHON_PLATFORM PYTHON_PREFIX PYTHON_EXEC_PREFIX
42        pkgpythondir pkgpyexecdir'
43
44cat >> configure.ac << 'END'
45AC_CONFIG_FILES([vars-got pythondir pyexecdir])
46AM_PATH_PYTHON
47AC_OUTPUT
48END
49
50cat > my.py << 'END'
51def my():
52    return 1
53END
54
55cat > Makefile.am << 'END'
56
57python_PYTHON = my.py
58
59EXTRA_DIST = vars-exp
60
61check-local: test-in test-am
62.PHONY: test-in test-am
63
64test-in:
65	@echo "> doing test-in"
66	@echo ">> contents of pythondir:"
67	cat pythondir
68	case `cat pythondir` in '$${PYTHON_PREFIX}'/*);; *) exit 1;; esac
69	@echo ">> contents of pyexecdir:"
70	cat pyexecdir
71	case `cat pyexecdir` in '$${PYTHON_EXEC_PREFIX}'/*);; *) exit 1;; esac
72	@echo ">> contents of vars-exp:"
73	cat $(srcdir)/vars-exp
74	@echo ">> contents of vars-got:"
75	cat $(builddir)/vars-got
76	diff $(srcdir)/vars-exp $(builddir)/vars-got
77
78## Note: this target's rules will be extended in the "for" loop below.
79test-am:
80	@echo "> doing test-am"
81	case '$(pythondir)' in '$(PYTHON_PREFIX)'/*);; *) exit 1;; esac
82	case '$(pyexecdir)' in '$(PYTHON_EXEC_PREFIX)'/*);; *) exit 1;; esac
83END
84
85echo @pythondir@ > pythondir.in
86echo @pyexecdir@ > pyexecdir.in
87
88# This depends on whether we're doing GNU or Python values, per arg.
89setup_vars_file ()
90{
91  vartype=$1
92  : > vars-exp
93  : > vars-got.in
94
95  for var in $pyvars; do
96    if test x"$vartype" = xgnu; then
97      # when not using Python sys.* values, PYTHON_*PREFIX will vary;
98      # the computed value will be (something like) "/usr",
99      # but the expected value will be "${prefix}".
100      if test x"$var" = xPYTHON_PREFIX \
101         || test x"$var" = xPYTHON_EXEC_PREFIX; then
102        continue
103      fi
104    fi
105    eval val=\$$var
106    echo "var=$val  #$var"   >> vars-exp
107    echo "var=@$var@  #$var" >> vars-got.in
108    echo "${tab}test x'\$($var)' = x'$val' || test \"\$NO_CHECK_PYTHON_PREFIX\"" >> Makefile.am
109  done
110}
111
112setup_vars_file gnu
113
114$ACLOCAL
115$AUTOMAKE --add-missing
116
117# some debugging output.
118for var in pythondir pyexecdir $pyvars; do
119  grep "^$var *=" Makefile.in
120done
121
122$AUTOCONF
123
124# Do GNU values.
125./configure PYTHON="$PYTHON"
126$MAKE test-in test-am
127run_make distcheck
128
129# Do Python values.
130setup_vars_file python
131instdir=$(pwd)/inst
132./configure PYTHON="$PYTHON" --with-python-sys-prefix --prefix="$instdir"
133$MAKE test-in test-am
134#
135# This tries to install to $PYTHON_PREFIX, which may not be writable.
136# Override it to something safe, but then of course we have to skip
137# checking that it is what we originally set it to.
138run_make distcheck \
139  PYTHON_PREFIX="$instdir" \
140  NO_CHECK_PYTHON_PREFIX=1 \
141  AM_DISTCHECK_CONFIGURE_FLAGS=--with-python-sys-prefix
142
143:
144