build-sys: add hooks to build python bindings

Add --enable-python configure switch so we build the python bindings. We
also pass version.py through SED_PROCESS macro, so the version is kept
in sync with kmod.

Acked-by: Andy Grover <agrover@redhat.com>
diff --git a/Makefile.am b/Makefile.am
index 46b7652..e65fdfb 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -6,7 +6,8 @@
 
 DISTCLEAN_LOCAL_HOOKS =
 EXTRA_DIST =
-CLEANFILES =
+CLEANFILES = $(BUILT_FILES)
+BUILT_FILES =
 ACLOCAL_AMFLAGS = -I m4 ${ACLOCAL_FLAGS}
 AM_MAKEFLAGS = --no-print-directory
 
@@ -125,6 +126,70 @@
 endif
 
 # ------------------------------------------------------------------------------
+# PYTHON BINDINGS
+# ------------------------------------------------------------------------------
+
+CYTHON_FLAGS_VERBOSE_ =
+CYTHON_FLAGS_VERBOSE_0 =
+CYTHON_FLAGS_VERBOSE_1 = -v
+CYTHON_FLAGS = $(CYTHON_FLAGS_VERBOSE_$(V))
+AM_V_CYTHON = $(am__v_CYTHON_$(V))
+am__v_CYTHON_ = $(am__v_CYTHON_$(AM_DEFAULT_VERBOSITY))
+am__v_CYTHON_0 = @echo "  CYTHON " $@;
+
+.pyx.c:
+	$(AM_V_CYTHON)$(CYTHON) -o $@ $<
+
+%.py: %.py.in Makefile
+	$(SED_PROCESS)
+
+# Remove some warnings for generated code
+PYTHON_NOWARN = -Wno-redundant-decls -Wno-shadow -Wno-strict-aliasing
+
+CPYTHON_MODULE_CFLAGS = $(AM_CFLAGS) -DCPYTHON_COMPILING_IN_PYPY=0 \
+			$(PYTHON_NOWARN) $(PYTHON_CFLAGS) \
+			-fvisibility=default
+CPYTHON_MODULE_LDFLAGS = $(AM_LDFLAGS) -module -avoid-version -shared
+
+if BUILD_PYTHON
+pkgpyexec_LTLIBRARIES = libkmod/python/kmod/kmod.la \
+			libkmod/python/kmod/list.la \
+			libkmod/python/kmod/module.la \
+			libkmod/python/kmod/_util.la
+
+libkmod_python_kmod_kmod_la_SOURCES = libkmod/python/kmod/kmod.c
+libkmod_python_kmod_kmod_la_CFLAGS = $(CPYTHON_MODULE_CFLAGS)
+libkmod_python_kmod_kmod_la_LDFLAGS = $(CPYTHON_MODULE_LDFLAGS)
+libkmod_python_kmod_kmod_la_LIBADD = $(PYTHON_LIBS) libkmod/libkmod.la
+
+libkmod_python_kmod_list_la_SOURCES = libkmod/python/kmod/list.c
+libkmod_python_kmod_list_la_CFLAGS = $(CPYTHON_MODULE_CFLAGS)
+libkmod_python_kmod_list_la_LDFLAGS = $(CPYTHON_MODULE_LDFLAGS)
+libkmod_python_kmod_list_la_LIBADD = $(PYTHON_LIBS) libkmod/libkmod.la
+
+libkmod_python_kmod_module_la_SOURCES = libkmod/python/kmod/module.c
+libkmod_python_kmod_module_la_CFLAGS = $(CPYTHON_MODULE_CFLAGS)
+libkmod_python_kmod_module_la_LDFLAGS = $(CPYTHON_MODULE_LDFLAGS)
+libkmod_python_kmod_module_la_LIBADD = $(PYTHON_LIBS) libkmod/libkmod.la
+
+libkmod_python_kmod__util_la_SOURCES = libkmod/python/kmod/_util.c
+libkmod_python_kmod__util_la_CFLAGS = $(CPYTHON_MODULE_CFLAGS)
+libkmod_python_kmod__util_la_LDFLAGS = $(CPYTHON_MODULE_LDFLAGS)
+libkmod_python_kmod__util_la_LIBADD = $(PYTHON_LIBS) libkmod/libkmod.la
+
+BUILT_FILES += $(libkmod_python_kmod_kmod_la_SOURCES) \
+	       $(libkmod_python_kmod_list_la_SOURCES) \
+	       $(libkmod_python_kmod_module_la_SOURCES) \
+	       $(libkmod_python_kmod__util_la_SOURCES)
+
+dist_pkgpyexec_PYTHON = libkmod/python/kmod/error.py \
+			libkmod/python/kmod/__init__.py \
+			libkmod/python/kmod/version.py
+
+BUILT_FILES += libkmod/python/kmod/version.py
+
+endif
+# ------------------------------------------------------------------------------
 # TESTSUITE
 # ------------------------------------------------------------------------------
 
@@ -211,7 +276,8 @@
 DISTCLEAN_LOCAL_HOOKS += testsuite-distclean
 EXTRA_DIST += testsuite/rootfs-pristine
 
-DISTCHECK_CONFIGURE_FLAGS=--enable-gtk-doc --sysconfdir=/etc --with-zlib \
+DISTCHECK_CONFIGURE_FLAGS=--enable-gtk-doc --enable-python --sysconfdir=/etc \
+	--with-zlib \
 	--with-bashcompletiondir=$$dc_install_base/$(bashcompletiondir)
 
 distclean-local: $(DISTCLEAN_LOCAL_HOOKS)
diff --git a/configure.ac b/configure.ac
index f2070c6..b195296 100644
--- a/configure.ac
+++ b/configure.ac
@@ -127,6 +127,24 @@
 	AC_DEFINE(ENABLE_DEBUG, [1], [Debug messages.])
 ])
 
+AC_ARG_ENABLE([python],
+	AS_HELP_STRING([--enable-python], [enable Python libkmod bindings @<:@default=disabled@:>@]),
+	[], [enable_python=no])
+AS_IF([test "x$enable_python" = "xyes"], [
+	AM_PATH_PYTHON(,,[:])
+	AC_PATH_PROG([CYTHON], [cython], [:])
+
+	PKG_CHECK_MODULES([PYTHON], [python-${PYTHON_VERSION}],
+			  [have_python=yes],
+			  [PKG_CHECK_MODULES([PYTHON], [python],
+					     [have_python=yes],
+					     [have_python=no])])
+
+	AS_IF([test "x$have_python" = xno],
+	      [AC_MSG_ERROR([*** python support requested but libraries not found])])
+])
+AM_CONDITIONAL([BUILD_PYTHON], [test "x$enable_python" = "xyes"])
+
 m4_ifdef([GTK_DOC_CHECK], [
 GTK_DOC_CHECK([1.14],[--flavour no-tmpl-flat])
 ], [
@@ -222,6 +240,7 @@
 	ldflags:		${with_ldflags} ${LDFLAGS}
 
 	tools:			${enable_tools}
+	python bindings:	${enable_python}
 	logging:		${enable_logging}
 	compression:		xz=${with_xz}  zlib=${with_zlib}
 	debug:			${enable_debug}
diff --git a/libkmod/python/.gitignore b/libkmod/python/.gitignore
index 46ec2ea..69af451 100644
--- a/libkmod/python/.gitignore
+++ b/libkmod/python/.gitignore
@@ -3,3 +3,4 @@
 *.c
 *.pyc
 *.so
+kmod/version.py
diff --git a/libkmod/python/kmod/version.py b/libkmod/python/kmod/version.py.in
similarity index 95%
rename from libkmod/python/kmod/version.py
rename to libkmod/python/kmod/version.py.in
index 1ef04eb..4daa94d 100644
--- a/libkmod/python/kmod/version.py
+++ b/libkmod/python/kmod/version.py.in
@@ -14,4 +14,4 @@
 # You should have received a copy of the GNU Lesser General Public License
 # along with python-kmod.  If not, see <http://www.gnu.org/licenses/>.
 
-__version__ = '0.9.1'
+__version__ = '@VERSION@'