2
1

0003-libselinux-Use-Python-distutils-to-install-SELinux-p.patch 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. From 89c296e7e9219f54c74f8c3f42940100cbcac962 Mon Sep 17 00:00:00 2001
  2. From: Petr Lautrbach <plautrba@redhat.com>
  3. Date: Fri, 7 Jun 2019 17:35:44 +0200
  4. Subject: [PATCH] libselinux: Use Python distutils to install SELinux python
  5. bindings
  6. Follow officially documented way how to build C extension modules using
  7. distutils - https://docs.python.org/3.8/extending/building.html#building
  8. Fixes:
  9. - selinux python module fails to load when it's built using SWIG-4.0:
  10. >>> import selinux
  11. Traceback (most recent call last):
  12. File "<stdin>", line 1, in <module>
  13. File "/usr/lib64/python3.7/site-packages/selinux/__init__.py", line 13, in <module>
  14. from . import _selinux
  15. ImportError: cannot import name '_selinux' from 'selinux' (/usr/lib64/python3.7/site-packages/selinux/__init__.py)
  16. SWIG-4.0 changed (again?) its behavior so that it uses: from . import _selinux
  17. which looks for _selinux module in the same directory as where __init__.py is -
  18. $(PYLIBDIR)/site-packages/selinux. But _selinux module is installed into
  19. $(PYLIBDIR)/site-packages/ since a9604c30a5e2f ("libselinux: Change the location
  20. of _selinux.so").
  21. - audit2why python module fails to build with Python 3.8
  22. cc -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -DOVERRIDE_GETTID=0 -I../include -D_GNU_SOURCE -DDISABLE_RPM -DNO_ANDROID_BACKEND -DUSE_PCRE2 -DPCRE2_CODE_UNIT_WIDTH=8 -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -L. -shared -o python-3.8audit2why.so python-3.8audit2why.lo -lselinux -l:libsepol.a -Wl,-soname,audit2why.so,--version-script=audit2why.map,-z,defs
  23. /usr/bin/ld: python-3.8audit2why.lo: in function `finish':
  24. /builddir/build/BUILD/libselinux-2.9/src/audit2why.c:166: undefined reference to `PyArg_ParseTuple'
  25. /usr/bin/ld: python-3.8audit2why.lo: in function `_Py_INCREF':
  26. /usr/include/python3.8/object.h:449: undefined reference to `_Py_NoneStruct'
  27. /usr/bin/ld: /usr/include/python3.8/object.h:449: undefined reference to `_Py_NoneStruct'
  28. /usr/bin/ld: python-3.8audit2why.lo: in function `check_booleans':
  29. /builddir/build/BUILD/libselinux-2.9/src/audit2why.c:84: undefined reference to `PyExc_RuntimeError'
  30. ...
  31. It's related to the following Python change
  32. https://docs.python.org/dev/whatsnew/3.8.html#debug-build-uses-the-same-abi-as-release-build
  33. Python distutils adds correct link options automatically.
  34. - selinux python module doesn't provide any Python metadata
  35. When selinux python module was built manually, it didn't provide any metadata.
  36. distutils takes care about that so that selinux Python module is visible for
  37. pip:
  38. $ pip3 list | grep selinux
  39. selinux 2.9
  40. Signed-off-by: Petr Lautrbach <plautrba@redhat.com>
  41. [Upstream: commit 2efa06857575e4118e91ca250b6b92da68b130d5]
  42. Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
  43. ---
  44. src/.gitignore | 2 +-
  45. src/Makefile | 36 ++++++++----------------------------
  46. src/setup.py | 24 ++++++++++++++++++++++++
  47. 3 files changed, 33 insertions(+), 29 deletions(-)
  48. create mode 100644 libselinux/src/setup.py
  49. diff --git a/src/.gitignore b/src/.gitignore
  50. index 4dcc3b3b..428afe5a 100644
  51. --- a/src/.gitignore
  52. +++ b/src/.gitignore
  53. @@ -1,4 +1,4 @@
  54. selinux.py
  55. -selinuxswig_wrap.c
  56. +selinuxswig_python_wrap.c
  57. selinuxswig_python_exception.i
  58. selinuxswig_ruby_wrap.c
  59. diff --git a/src/Makefile b/src/Makefile
  60. index e9ed0383..2b1696a0 100644
  61. --- a/src/Makefile
  62. +++ b/src/Makefile
  63. @@ -36,7 +36,7 @@ TARGET=libselinux.so
  64. LIBPC=libselinux.pc
  65. SWIGIF= selinuxswig_python.i selinuxswig_python_exception.i
  66. SWIGRUBYIF= selinuxswig_ruby.i
  67. -SWIGCOUT= selinuxswig_wrap.c
  68. +SWIGCOUT= selinuxswig_python_wrap.c
  69. SWIGPYOUT= selinux.py
  70. SWIGRUBYCOUT= selinuxswig_ruby_wrap.c
  71. SWIGLOBJ:= $(patsubst %.c,$(PYPREFIX)%.lo,$(SWIGCOUT))
  72. @@ -55,7 +55,7 @@ ifeq ($(LIBSEPOLA),)
  73. LDLIBS_LIBSEPOLA := -l:libsepol.a
  74. endif
  75. -GENERATED=$(SWIGCOUT) $(SWIGRUBYCOUT) selinuxswig_python_exception.i
  76. +GENERATED=$(SWIGCOUT) $(SWIGRUBYCOUT) $(SWIGCOUT) selinuxswig_python_exception.i
  77. SRCS= $(filter-out $(GENERATED) audit2why.c, $(sort $(wildcard *.c)))
  78. MAX_STACK_SIZE=32768
  79. @@ -125,25 +125,18 @@ DISABLE_FLAGS+= -DNO_ANDROID_BACKEND
  80. SRCS:= $(filter-out label_backends_android.c, $(SRCS))
  81. endif
  82. -SWIG = swig -Wall -python -o $(SWIGCOUT) -outdir ./ $(DISABLE_FLAGS)
  83. -
  84. SWIGRUBY = swig -Wall -ruby -o $(SWIGRUBYCOUT) -outdir ./ $(DISABLE_FLAGS)
  85. all: $(LIBA) $(LIBSO) $(LIBPC)
  86. -pywrap: all $(SWIGFILES) $(AUDIT2WHYSO)
  87. +pywrap: all selinuxswig_python_exception.i
  88. + CFLAGS="$(CFLAGS) $(SWIG_CFLAGS)" $(PYTHON) setup.py build_ext -I $(DESTDIR)$(INCLUDEDIR) -L $(DESTDIR)$(LIBDIR)
  89. rubywrap: all $(SWIGRUBYSO)
  90. -$(SWIGLOBJ): $(SWIGCOUT)
  91. - $(CC) $(CFLAGS) $(SWIG_CFLAGS) $(PYINC) -fPIC -DSHARED -c -o $@ $<
  92. -
  93. $(SWIGRUBYLOBJ): $(SWIGRUBYCOUT)
  94. $(CC) $(CFLAGS) $(SWIG_CFLAGS) $(RUBYINC) -fPIC -DSHARED -c -o $@ $<
  95. -$(SWIGSO): $(SWIGLOBJ)
  96. - $(CC) $(CFLAGS) $(LDFLAGS) -L. -shared -o $@ $< -lselinux $(PYLIBS)
  97. -
  98. $(SWIGRUBYSO): $(SWIGRUBYLOBJ)
  99. $(CC) $(CFLAGS) $(LDFLAGS) -L. -shared -o $@ $^ -lselinux $(RUBYLIBS)
  100. @@ -161,29 +154,15 @@ $(LIBPC): $(LIBPC).in ../VERSION
  101. selinuxswig_python_exception.i: ../include/selinux/selinux.h
  102. bash -e exception.sh > $@ || (rm -f $@ ; false)
  103. -$(AUDIT2WHYLOBJ): audit2why.c
  104. - $(CC) $(filter-out -Werror, $(CFLAGS)) $(PYINC) -fPIC -DSHARED -c -o $@ $<
  105. -
  106. -$(AUDIT2WHYSO): $(AUDIT2WHYLOBJ) $(LIBSEPOLA)
  107. - $(CC) $(CFLAGS) $(LDFLAGS) -L. -shared -o $@ $^ -lselinux $(LDLIBS_LIBSEPOLA) $(PYLIBS) -Wl,-soname,audit2why.so,--version-script=audit2why.map,-z,defs
  108. -
  109. %.o: %.c policy.h
  110. $(CC) $(CFLAGS) $(TLSFLAGS) -c -o $@ $<
  111. %.lo: %.c policy.h
  112. $(CC) $(CFLAGS) -fPIC -DSHARED -c -o $@ $<
  113. -$(SWIGCOUT): $(SWIGIF)
  114. - $(SWIG) $<
  115. -
  116. -$(SWIGPYOUT): $(SWIGCOUT)
  117. -
  118. $(SWIGRUBYCOUT): $(SWIGRUBYIF)
  119. $(SWIGRUBY) $<
  120. -swigify: $(SWIGIF)
  121. - $(SWIG) $<
  122. -
  123. install: all
  124. test -d $(DESTDIR)$(LIBDIR) || install -m 755 -d $(DESTDIR)$(LIBDIR)
  125. install -m 644 $(LIBA) $(DESTDIR)$(LIBDIR)
  126. @@ -194,10 +173,9 @@ install: all
  127. ln -sf --relative $(DESTDIR)$(SHLIBDIR)/$(LIBSO) $(DESTDIR)$(LIBDIR)/$(TARGET)
  128. install-pywrap: pywrap
  129. - test -d $(DESTDIR)$(PYTHONLIBDIR)/selinux || install -m 755 -d $(DESTDIR)$(PYTHONLIBDIR)/selinux
  130. - install -m 755 $(SWIGSO) $(DESTDIR)$(PYTHONLIBDIR)/_selinux$(PYCEXT)
  131. - install -m 755 $(AUDIT2WHYSO) $(DESTDIR)$(PYTHONLIBDIR)/selinux/audit2why$(PYCEXT)
  132. + $(PYTHON) setup.py install --prefix=$(PREFIX) `test -n "$(DESTDIR)" && echo --root $(DESTDIR)`
  133. install -m 644 $(SWIGPYOUT) $(DESTDIR)$(PYTHONLIBDIR)/selinux/__init__.py
  134. + ln -sf --relative $(DESTDIR)$(PYTHONLIBDIR)/selinux/_selinux$(PYCEXT) $(DESTDIR)$(PYTHONLIBDIR)/_selinux$(PYCEXT)
  135. install-rubywrap: rubywrap
  136. test -d $(DESTDIR)$(RUBYINSTALL) || install -m 755 -d $(DESTDIR)$(RUBYINSTALL)
  137. @@ -208,6 +186,8 @@ relabel:
  138. clean-pywrap:
  139. -rm -f $(SWIGLOBJ) $(SWIGSO) $(AUDIT2WHYLOBJ) $(AUDIT2WHYSO)
  140. + $(PYTHON) setup.py clean
  141. + -rm -rf build *~ \#* *pyc .#*
  142. clean-rubywrap:
  143. -rm -f $(SWIGRUBYLOBJ) $(SWIGRUBYSO)
  144. diff --git a/src/setup.py b/src/setup.py
  145. new file mode 100644
  146. index 00000000..4dc03f55
  147. --- /dev/null
  148. +++ b/src/setup.py
  149. @@ -0,0 +1,24 @@
  150. +#!/usr/bin/python3
  151. +
  152. +from distutils.core import Extension, setup
  153. +
  154. +setup(
  155. + name="selinux",
  156. + version="2.9",
  157. + description="SELinux python 3 bindings",
  158. + author="SELinux Project",
  159. + author_email="selinux@vger.kernel.org",
  160. + ext_modules=[
  161. + Extension('selinux._selinux',
  162. + sources=['selinuxswig_python.i'],
  163. + include_dirs=['../include'],
  164. + library_dirs=['.'],
  165. + libraries=['selinux']),
  166. + Extension('selinux.audit2why',
  167. + sources=['audit2why.c'],
  168. + include_dirs=['../include'],
  169. + library_dirs=['.'],
  170. + libraries=['selinux'],
  171. + extra_link_args=['-l:libsepol.a', '-Wl,--version-script=audit2why.map'])
  172. + ],
  173. +)
  174. --
  175. 2.21.0