From ca7f6eaceae3a9771d3e191d3a3a7c0a1026f52b Mon Sep 17 00:00:00 2001 From: Rob Davies Date: Tue, 21 Jan 2025 13:39:27 +0000 Subject: [PATCH] Added build wrapper so build_ext runs before build_py SWIG, run by build_ext, generates a shared object file and a python library. The latter needs to be generated before build_py is run so that build_py finds it. Unfortunately the default in setuptools is to run build_py before build_ext. The solution is to override the build class so that the build order can be changed. With this change, "pip install" works better, including when lsf-python-api is a dependency of another module. See: https://bugs.python.org/issue2624 https://bugs.python.org/issue1016626 https://stackoverflow.com/questions/12491328/python-distutils-not-include-the-swig-generated-module https://stackoverflow.com/questions/50239473/building-a-module-with-setuptools-and-swig Signed-off-by: Robert Davies --- setup.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 57992b7..97edc28 100755 --- a/setup.py +++ b/setup.py @@ -13,6 +13,7 @@ else: from distutils.core import setup, Extension from distutils.command.bdist_rpm import bdist_rpm +from distutils.command.build import build from distutils.command.install import INSTALL_SCHEMES class bdist_rpm_custom(bdist_rpm): @@ -28,6 +29,21 @@ def finalize_package_data (self): self.no_autoreq = 1 bdist_rpm.finalize_package_data(self) +# Build extensions before python modules, so the generated pythonlsf/lsf.py +# file is created before attempting to install it. This makes building with +# "pip" easier. +# See: +# https://bugs.python.org/issue2624 +# https://bugs.python.org/issue1016626 +# https://stackoverflow.com/questions/12491328/python-distutils-not-include-the-swig-generated-module +# https://stackoverflow.com/questions/50239473/building-a-module-with-setuptools-and-swig + +class build_ext_first(build): + def finalize_options(self): + super().finalize_options() + new_order = list(filter(lambda x: x[0] == 'build_ext', self.sub_commands)) + list(filter(lambda x: x[0] != 'build_ext', self.sub_commands)) + self.sub_commands[:] = new_order + def get_lsf_libdir(): try: _lsf_envdir = os.environ['LSF_ENVDIR'] @@ -145,7 +161,8 @@ def set_gccflag_lsf_version(): extra_objects=lsf_static_lib, libraries=lsf_dynamic_lib)], py_modules=['pythonlsf.lsf'], - cmdclass = { 'bdist_rpm': bdist_rpm_custom }, + cmdclass = { 'bdist_rpm': bdist_rpm_custom, + 'build': build_ext_first }, classifiers=["Development Status :: 2 - Pre-Alpha", "License :: OSI Approved :: Eclipse Public License", "Operating System :: OS Independent",