29ef3f
From 67d490a38a319126f371eaf66a5fc922d7005b1f Mon Sep 17 00:00:00 2001
29ef3f
From: Petr Lautrbach <plautrba@redhat.com>
29ef3f
Date: Thu, 16 May 2019 15:01:59 +0200
c17228
Subject: [PATCH] libselinux: Use Python distutils to install SELinux python
c17228
 bindings
29ef3f
29ef3f
SWIG-4.0 changed its behavior so that it uses: from . import _selinux  which
29ef3f
looks for _selinux module in the same directory as where __init__.py is -
29ef3f
$(PYLIBDIR)/site-packages/selinux. But _selinux module is installed into
29ef3f
$(PYLIBDIR)/site-packages/ since a9604c30a5e2f ("libselinux: Change the location
29ef3f
of _selinux.so").
29ef3f
29ef3f
In order to prevent such breakage in future use Python's distutils instead of
29ef3f
building and installing python bindings manually in Makefile.
29ef3f
29ef3f
Fixes:
29ef3f
>>> import selinux
29ef3f
Traceback (most recent call last):
29ef3f
  File "<stdin>", line 1, in <module>
29ef3f
  File "/usr/lib64/python3.7/site-packages/selinux/__init__.py", line 13, in <module>
29ef3f
    from . import _selinux
29ef3f
ImportError: cannot import name '_selinux' from 'selinux' (/usr/lib64/python3.7/site-packages/selinux/__init__.py)
29ef3f
>>>
29ef3f
29ef3f
Signed-off-by: Petr Lautrbach <plautrba@redhat.com>
29ef3f
---
29ef3f
 libselinux/src/.gitignore |  2 +-
29ef3f
 libselinux/src/Makefile   | 37 ++++++++-----------------------------
29ef3f
 libselinux/src/setup.py   | 24 ++++++++++++++++++++++++
29ef3f
 3 files changed, 33 insertions(+), 30 deletions(-)
29ef3f
 create mode 100644 libselinux/src/setup.py
29ef3f
29ef3f
diff --git a/libselinux/src/.gitignore b/libselinux/src/.gitignore
29ef3f
index 4dcc3b3b..428afe5a 100644
29ef3f
--- a/libselinux/src/.gitignore
29ef3f
+++ b/libselinux/src/.gitignore
29ef3f
@@ -1,4 +1,4 @@
29ef3f
 selinux.py
29ef3f
-selinuxswig_wrap.c
29ef3f
+selinuxswig_python_wrap.c
29ef3f
 selinuxswig_python_exception.i
29ef3f
 selinuxswig_ruby_wrap.c
29ef3f
diff --git a/libselinux/src/Makefile b/libselinux/src/Makefile
29ef3f
index e9ed0383..826c830c 100644
29ef3f
--- a/libselinux/src/Makefile
29ef3f
+++ b/libselinux/src/Makefile
29ef3f
@@ -36,7 +36,7 @@ TARGET=libselinux.so
29ef3f
 LIBPC=libselinux.pc
29ef3f
 SWIGIF= selinuxswig_python.i selinuxswig_python_exception.i
29ef3f
 SWIGRUBYIF= selinuxswig_ruby.i
29ef3f
-SWIGCOUT= selinuxswig_wrap.c
29ef3f
+SWIGCOUT= selinuxswig_python_wrap.c
29ef3f
 SWIGPYOUT= selinux.py
29ef3f
 SWIGRUBYCOUT= selinuxswig_ruby_wrap.c
29ef3f
 SWIGLOBJ:= $(patsubst %.c,$(PYPREFIX)%.lo,$(SWIGCOUT))
29ef3f
@@ -55,7 +55,7 @@ ifeq ($(LIBSEPOLA),)
29ef3f
         LDLIBS_LIBSEPOLA := -l:libsepol.a
29ef3f
 endif
29ef3f
 
29ef3f
-GENERATED=$(SWIGCOUT) $(SWIGRUBYCOUT) selinuxswig_python_exception.i
29ef3f
+GENERATED=$(SWIGCOUT) $(SWIGRUBYCOUT) $(SWIGCOUT) selinuxswig_python_exception.i
29ef3f
 SRCS= $(filter-out $(GENERATED) audit2why.c, $(sort $(wildcard *.c)))
29ef3f
 
29ef3f
 MAX_STACK_SIZE=32768
29ef3f
@@ -125,25 +125,18 @@ DISABLE_FLAGS+= -DNO_ANDROID_BACKEND
29ef3f
 SRCS:= $(filter-out label_backends_android.c, $(SRCS))
29ef3f
 endif
29ef3f
 
29ef3f
-SWIG = swig -Wall -python -o $(SWIGCOUT) -outdir ./ $(DISABLE_FLAGS)
29ef3f
-
29ef3f
 SWIGRUBY = swig -Wall -ruby -o $(SWIGRUBYCOUT) -outdir ./ $(DISABLE_FLAGS)
29ef3f
 
29ef3f
 all: $(LIBA) $(LIBSO) $(LIBPC)
29ef3f
 
29ef3f
-pywrap: all $(SWIGFILES) $(AUDIT2WHYSO)
29ef3f
+pywrap: all selinuxswig_python_exception.i
29ef3f
+	CFLAGS="$(SWIG_CFLAGS)" $(PYTHON) setup.py build_ext -I $(DESTDIR)$(INCLUDEDIR) -L $(DESTDIR)$(LIBDIR)
29ef3f
 
29ef3f
 rubywrap: all $(SWIGRUBYSO)
29ef3f
 
29ef3f
-$(SWIGLOBJ): $(SWIGCOUT)
29ef3f
-	$(CC) $(CFLAGS) $(SWIG_CFLAGS) $(PYINC) -fPIC -DSHARED -c -o $@ $<
29ef3f
-
29ef3f
 $(SWIGRUBYLOBJ): $(SWIGRUBYCOUT)
29ef3f
 	$(CC) $(CFLAGS) $(SWIG_CFLAGS) $(RUBYINC) -fPIC -DSHARED -c -o $@ $<
29ef3f
 
29ef3f
-$(SWIGSO): $(SWIGLOBJ)
29ef3f
-	$(CC) $(CFLAGS) $(LDFLAGS) -L. -shared -o $@ $< -lselinux $(PYLIBS)
29ef3f
-
29ef3f
 $(SWIGRUBYSO): $(SWIGRUBYLOBJ)
29ef3f
 	$(CC) $(CFLAGS) $(LDFLAGS) -L. -shared -o $@ $^ -lselinux $(RUBYLIBS)
29ef3f
 
29ef3f
@@ -161,29 +154,15 @@ $(LIBPC): $(LIBPC).in ../VERSION
29ef3f
 selinuxswig_python_exception.i: ../include/selinux/selinux.h
29ef3f
 	bash -e exception.sh > $@ || (rm -f $@ ; false)
29ef3f
 
29ef3f
-$(AUDIT2WHYLOBJ): audit2why.c
29ef3f
-	$(CC) $(filter-out -Werror, $(CFLAGS)) $(PYINC) -fPIC -DSHARED -c -o $@ $<
29ef3f
-
29ef3f
-$(AUDIT2WHYSO): $(AUDIT2WHYLOBJ) $(LIBSEPOLA)
29ef3f
-	$(CC) $(CFLAGS) $(LDFLAGS) -L. -shared -o $@ $^ -lselinux $(LDLIBS_LIBSEPOLA) $(PYLIBS) -Wl,-soname,audit2why.so,--version-script=audit2why.map,-z,defs
29ef3f
-
29ef3f
 %.o:  %.c policy.h
29ef3f
 	$(CC) $(CFLAGS) $(TLSFLAGS) -c -o $@ $<
29ef3f
 
29ef3f
 %.lo:  %.c policy.h
29ef3f
 	$(CC) $(CFLAGS) -fPIC -DSHARED -c -o $@ $<
29ef3f
 
29ef3f
-$(SWIGCOUT): $(SWIGIF)
29ef3f
-	$(SWIG) $<
29ef3f
-
29ef3f
-$(SWIGPYOUT): $(SWIGCOUT)
29ef3f
-
29ef3f
 $(SWIGRUBYCOUT): $(SWIGRUBYIF)
29ef3f
 	$(SWIGRUBY) $<
29ef3f
 
29ef3f
-swigify: $(SWIGIF)
29ef3f
-	$(SWIG) $<
29ef3f
-
29ef3f
 install: all 
29ef3f
 	test -d $(DESTDIR)$(LIBDIR) || install -m 755 -d $(DESTDIR)$(LIBDIR)
29ef3f
 	install -m 644 $(LIBA) $(DESTDIR)$(LIBDIR)
29ef3f
@@ -194,10 +173,8 @@ install: all
29ef3f
 	ln -sf --relative $(DESTDIR)$(SHLIBDIR)/$(LIBSO) $(DESTDIR)$(LIBDIR)/$(TARGET)
29ef3f
 
29ef3f
 install-pywrap: pywrap
29ef3f
-	test -d $(DESTDIR)$(PYTHONLIBDIR)/selinux || install -m 755 -d $(DESTDIR)$(PYTHONLIBDIR)/selinux
29ef3f
-	install -m 755 $(SWIGSO) $(DESTDIR)$(PYTHONLIBDIR)/_selinux$(PYCEXT)
29ef3f
-	install -m 755 $(AUDIT2WHYSO) $(DESTDIR)$(PYTHONLIBDIR)/selinux/audit2why$(PYCEXT)
29ef3f
-	install -m 644 $(SWIGPYOUT) $(DESTDIR)$(PYTHONLIBDIR)/selinux/__init__.py
29ef3f
+	$(PYTHON) setup.py install --prefix=$(PREFIX) `test -n "$(DESTDIR)" && echo --root $(DESTDIR)`
29ef3f
+	install -m 644 selinux.py $(DESTDIR)$(PYTHONLIBDIR)/selinux/__init__.py
29ef3f
 
29ef3f
 install-rubywrap: rubywrap
29ef3f
 	test -d $(DESTDIR)$(RUBYINSTALL) || install -m 755 -d $(DESTDIR)$(RUBYINSTALL) 
29ef3f
@@ -208,6 +185,8 @@ relabel:
29ef3f
 
29ef3f
 clean-pywrap:
29ef3f
 	-rm -f $(SWIGLOBJ) $(SWIGSO) $(AUDIT2WHYLOBJ) $(AUDIT2WHYSO)
29ef3f
+	$(PYTHON) setup.py clean
29ef3f
+	-rm -rf build *~ \#* *pyc .#*
29ef3f
 
29ef3f
 clean-rubywrap:
29ef3f
 	-rm -f $(SWIGRUBYLOBJ) $(SWIGRUBYSO)
29ef3f
diff --git a/libselinux/src/setup.py b/libselinux/src/setup.py
29ef3f
new file mode 100644
29ef3f
index 00000000..b12e7869
29ef3f
--- /dev/null
29ef3f
+++ b/libselinux/src/setup.py
29ef3f
@@ -0,0 +1,24 @@
29ef3f
+#!/usr/bin/python3
29ef3f
+
29ef3f
+from distutils.core import Extension, setup
29ef3f
+
29ef3f
+setup(
29ef3f
+    name="selinux",
29ef3f
+    version="2.9",
29ef3f
+    description="SELinux python 3 bindings",
29ef3f
+    author="SELinux Project",
29ef3f
+    author_email="selinux@vger.kernel.org",
29ef3f
+    ext_modules=[
29ef3f
+        Extension('selinux._selinux',
29ef3f
+                  sources=['selinuxswig_python.i'],
29ef3f
+                  include_dirs=['../include'],
29ef3f
+                  library_dirs=['.'],
29ef3f
+                  libraries=['selinux']),
29ef3f
+        Extension('selinux.audit2why',
29ef3f
+                  sources=['audit2why.c'],
29ef3f
+                  include_dirs=['../include'],
29ef3f
+                  library_dirs=['.'],
29ef3f
+                  libraries=['selinux'],
29ef3f
+                  extra_link_args=['-l:libsepol.a'])
29ef3f
+    ],
29ef3f
+)
29ef3f
-- 
c17228
2.21.0
29ef3f