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