# HG changeset patch
# User David Seifert <soap@gentoo.org>
# Date 1648898274 -7200
#      Sat Apr 02 13:17:54 2022 +0200
# Node ID ef2e401757110cd710b31cc5a22f38ffe3b63c9b
# Parent  1948063619859c31fb33d2063164e3e8b22b1d63
Use AM_PATH_PYTHON/PKG_CHECK_MODULES for python bindings

* `AM_PATH_PYTHON` is a much more common idiom for building
  and installing python modules than writing your own.
* It also makes cross-compiling the python bindings possible.
  Previously the `PYTHON_CFLAGS`/`PYTHON_LIBS` would have been
  based on the `--build` python and not the `--host` python.
  By using `pkg-config`, we can always redirect the python-X.Y.pc.

diff --git a/.gitignore b/.gitignore
--- a/.gitignore
+++ b/.gitignore
@@ -36,6 +36,7 @@
 libxslt/xsltconfig.h
 ltmain.sh
 missing
+py-compile
 python/*.pyc
 python/gen_prog
 python/libxslt-export.c
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -26,12 +26,9 @@
 
 gcc:python3:
   extends: .test
-  only:
-    - schedules
   variables:
+    CFLAGS: "-O2"
     PYTHON: "/usr/bin/python3"
-    CONFIG: "--with-python=/usr/bin/python3"
-    CFLAGS: "-O2"
 
 clang:asan:
   extends: .test
diff --git a/Makefile.am b/Makefile.am
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,10 +1,7 @@
-SUBDIRS = \
-	libxslt \
-	libexslt \
-	xsltproc \
-	doc \
-	$(PYTHON_SUBDIR) \
-	tests
+SUBDIRS = libxslt libexslt xsltproc doc tests
+if WITH_PYTHON
+SUBDIRS += python
+endif
 
 DIST_SUBDIRS = libxslt libexslt xsltproc python doc tests
 
@@ -50,7 +47,9 @@
 	@echo '## Running the regression test suite'
 	@(cd tests ; $(MAKE) -s tests)
 	@(cd xsltproc ; $(MAKE) -s tests)
-	@(if [ "$(PYTHON_SUBDIR)" != "" ] ; then cd python ; $(MAKE) -s tests ; fi)
+if WITH_PYTHON
+	@cd python && $(MAKE) tests
+endif
 
 valgrind:
 	@echo '## Running the regression tests under Valgrind'
diff --git a/configure.ac b/configure.ac
--- a/configure.ac
+++ b/configure.ac
@@ -186,121 +186,14 @@
 dnl check for python
 dnl
 
-PYTHON_VERSION=
-PYTHON_INCLUDES=
-PYTHON_SITE_PACKAGES=
-PYTHON_TESTS=
-pythondir=
 AC_ARG_WITH(python,
-[  --with-python[[=DIR]]     build Python bindings if found])
-AC_ARG_WITH(python_install_dir,
-[  --with-python-install-dir=DIR
-                          install Python bindings in DIR])
-if test "$with_python" != "no" ; then
-    if test -x "$with_python/bin/python"
-    then
-        echo Found python in $with_python/bin/python
-        PYTHON="$with_python/bin/python"
-    else
-        if test -x "$with_python/python.exe"
-        then
-            echo Found python in $with_python/python.exe
-            PYTHON="$with_python/python.exe"
-        else
-            if test -x "$with_python"
-            then
-                echo Found python in $with_python
-                PYTHON="$with_python"
-            else
-                if test -x "$PYTHON"
-                then
-                    echo Found python in environment PYTHON=$PYTHON
-                    with_python=`$PYTHON -c "import sys; print(sys.exec_prefix)"`
-                else
-                    AC_PATH_PROG(PYTHON, python python2.6 python2.5 python2.4 python2.3 python2.2 python2.1 python2.0 python1.6 python1.5)
-		fi
-	    fi
-	fi
-    fi
-    if test "$PYTHON" != ""
-    then
-        PYTHON_VERSION=`$PYTHON -c "from distutils import sysconfig; print(sysconfig.get_python_version())"`
-	PYTHON_INCLUDES=`$PYTHON -c "from distutils import sysconfig; print(sysconfig.get_python_inc())"`
-# does not work as it produce a /usr/lib/python path instead of/usr/lib64/python
-#
-#	PYTHON_SITE_PACKAGES=`$PYTHON -c "from distutils import sysconfig; print(sysconfig.get_python_lib())"`
-	echo Found Python version $PYTHON_VERSION
-  LIBXML2_PYTHON=`$PYTHON -c "import sys
-try:
-    import libxml2
-    sys.stdout.write('1')
-except:
-    sys.stdout.write('0')
-"`
-	if test "$LIBXML2_PYTHON" = "1"
-	then
-	    echo Found libxml2-python module
-	else
-	    echo Warning: Missing libxml2-python
-  fi
-    fi
-    if test "$PYTHON_VERSION" != "" -a "$PYTHON_INCLUDES" = ""
-    then
-	if test -r $with_python/include/python$PYTHON_VERSION/Python.h
-	then
-	    PYTHON_INCLUDES=$with_python/include/python$PYTHON_VERSION
-	else
-	    if test -r $prefix/include/python$PYTHON_VERSION/Python.h
-	    then
-	        PYTHON_INCLUDES=$prefix/include/python$PYTHON_VERSION
-	    else
-		if test -r /usr/include/python$PYTHON_VERSION/Python.h
-		then
-		    PYTHON_INCLUDES=/usr/include/python$PYTHON_VERSION
-		else
-	            if test -r $with_python/include/Python.h
-	            then
-	                PYTHON_INCLUDES=$with_python/include
-	            else
-		        echo could not find python$PYTHON_VERSION/Python.h or $with_python/include/Python.h
-		    fi
-		fi
-	    fi
-	fi
-    fi
-    if test "$with_python_install_dir" != ""
-    then
-	PYTHON_SITE_PACKAGES="$with_python_install_dir"
-    fi
-    if test "$PYTHON_VERSION" != "" -a "$PYTHON_SITE_PACKAGES" = ""
-    then
-	if test -d $libdir/python$PYTHON_VERSION/site-packages
-	then
-	    PYTHON_SITE_PACKAGES=$libdir/python$PYTHON_VERSION/site-packages
-	else
-	    if test -d $with_python/lib/site-packages
-	    then
-		PYTHON_SITE_PACKAGES=$with_python/lib/site-packages
-	    else
-		PYTHON_SITE_PACKAGES=$($PYTHON -c 'from distutils import sysconfig; print(sysconfig.get_python_lib(True,False,"${exec_prefix}"))')
-	    fi
-	fi
-    fi
-    pythondir='$(PYTHON_SITE_PACKAGES)'
-    PYTHON_LIBS=`python$PYTHON_VERSION-config --ldflags`
-else
-    PYTHON=
-fi
-AM_CONDITIONAL(WITH_PYTHON, test "$PYTHON_INCLUDES" != "")
-if test "$PYTHON_INCLUDES" != ""
-then
-    PYTHON_SUBDIR=python
-else
-    PYTHON_SUBDIR=
-fi
-AC_SUBST(pythondir)
-AC_SUBST(PYTHON_SUBDIR)
-AC_SUBST(PYTHON_LIBS)
+[  --with-python           build Python bindings (on)])
+
+AS_IF([test "x$with_python" != "xno"], [
+    AM_PATH_PYTHON
+    PKG_CHECK_MODULES([PYTHON], [python-${PYTHON_VERSION}])
+])
+AM_CONDITIONAL([WITH_PYTHON], [test "x$with_python" != "xno"])
 
 AC_ARG_WITH(crypto, [  --with-crypto           Add crypto support to exslt (on)])
 WITH_CRYPTO=0
@@ -623,14 +516,9 @@
 
 
 AC_SUBST(XSLTPROCDV)
-AC_SUBST(PYTHONSODV)
 AC_SUBST(XML_CONFIG)
 AC_SUBST(LIBXML_LIBS)
 AC_SUBST(LIBXML_CFLAGS)
-AC_SUBST(PYTHON)
-AC_SUBST(PYTHON_VERSION)
-AC_SUBST(PYTHON_INCLUDES)
-AC_SUBST(PYTHON_SITE_PACKAGES)
 
 XSLT_LIBDIR='-L${libdir}'
 XSLT_INCLUDEDIR='-I${includedir}'
diff --git a/python/Makefile.am b/python/Makefile.am
--- a/python/Makefile.am
+++ b/python/Makefile.am
@@ -1,13 +1,7 @@
 # Makefile for libxml2 python library
 AUTOMAKE_OPTIONS = 1.4 foreign
 
-SUBDIRS= . tests
-
-AM_CFLAGS = $(LIBXML_CFLAGS)
-
-DOCS_DIR = $(datadir)/doc/libxslt-python-$(LIBXSLT_VERSION)
-# libxsltclass.txt is generated
-DOCS=
+SUBDIRS = . tests
 
 EXTRA_DIST = 			\
 	libxslt.c		\
@@ -16,54 +10,44 @@
 	libxml_wrap.h		\
 	libxslt_wrap.h		\
 	libxsl.py		\
-	libxslt-python-api.xml	\
-	$(DOCS)
-
-libxsltmod_la_LDFLAGS =  -module -avoid-version
+	libxslt-python-api.xml
 
 if WITH_PYTHON
-mylibs = \
-	$(top_builddir)/libxslt/libxslt.la \
-	$(top_builddir)/libexslt/libexslt.la
 
-all-local: libxslt.py
-
-python_LTLIBRARIES = libxsltmod.la
+pyexec_LTLIBRARIES = libxsltmod.la
 
 libxsltmod_la_CPPFLAGS = \
-	-I$(PYTHON_INCLUDES) \
 	-I$(top_srcdir)/libxslt \
 	-I$(top_srcdir) \
-	-I../libexslt
+	-I../libexslt \
+	$(PYTHON_CFLAGS) $(LIBXML_CFLAGS)
 libxsltmod_la_SOURCES = libxslt.c types.c
 nodist_libxsltmod_la_SOURCES = libxslt-py.c
-libxsltmod_la_LIBADD = $(mylibs) $(PYTHON_LIBS)
+libxsltmod_la_LIBADD = \
+	$(top_builddir)/libxslt/libxslt.la \
+	$(top_builddir)/libexslt/libexslt.la \
+	$(PYTHON_LIBS) $(LIBXML_LIBS)
+libxsltmod_la_LDFLAGS = -module -avoid-version
 
 libxslt.py: $(srcdir)/libxsl.py libxsltclass.py
 	cat $(srcdir)/libxsl.py libxsltclass.py > $@
 
-install-data-local:
-	$(MKDIR_P) $(DESTDIR)$(pythondir)
-	$(INSTALL) -m 0644 libxslt.py $(DESTDIR)$(pythondir)
-	$(MKDIR_P) $(DESTDIR)$(DOCS_DIR)
-	@(for doc in $(DOCS) ; \
-	   do $(INSTALL) -m 0644 $(srcdir)/$$doc $(DESTDIR)$(DOCS_DIR) ; done)
-
-uninstall-local:
-	rm -f $(DESTDIR)$(pythondir)/libxslt.py
-	rm -rf $(DESTDIR)$(DOCS_DIR)
+python_PYTHON = libxslt.py
 
 GENERATE = generator.py
 API_DESC = $(top_srcdir)/doc/libxslt-api.xml $(srcdir)/libxslt-python-api.xml
-GENERATED= libxsltclass.py \
+GENERATED = libxsltclass.py \
 	   libxslt-export.c \
 	   libxslt-py.c \
 	   libxslt-py.h \
 	   libxsltclass.txt
 
+# Generated sources
+all-local: libxslt.py
+
 $(GENERATED): gen_prog
 
-gen_prog:  $(srcdir)/$(GENERATE) $(API_DESC)
+gen_prog: $(srcdir)/$(GENERATE) $(API_DESC)
 	SRCDIR=$(srcdir) $(PYTHON) $(srcdir)/$(GENERATE)
 	touch gen_prog
 
@@ -74,4 +58,4 @@
 tests test: all
 	cd tests && $(MAKE) tests
 
-CLEANFILES= $(GENERATED) *.o libxslt.so *.pyc libxslt.py gen_prog
+CLEANFILES = $(GENERATED) *.o libxslt.so *.pyc libxslt.py gen_prog