Update build for python compat

- remove distutils deps
- add pyproject.toml
- add MANIFEST.in
- update install instructions
This commit is contained in:
Julien Zoubian 2024-09-04 21:33:21 +02:00
parent 58e65d7d2b
commit 54fe8df970
6 changed files with 238 additions and 250 deletions

218
setup.py
View file

@ -17,222 +17,120 @@
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#+
import stat
import os
import sys
import shutil
from sysconfig import get_config_var
from distutils.command.install_data import install_data
from distutils.command.build_scripts import build_scripts
import stat
import pathlib
from setuptools import find_packages, setup, Extension, Command
import struct
from sysconfig import get_config_var
from setuptools import find_packages, setup, Extension
from setuptools.command.build_ext import build_ext
from setuptools.command.install_lib import install_lib
from setuptools.command.install_scripts import install_scripts
import struct
BITS = struct.calcsize("P") * 8
PACKAGE_NAME = "vide"
class CMakeExtension(Extension):
"""
An extension to run the cmake build
This simply overrides the base extension class so that setuptools
doesn't try to build your sources for you
"""
def __init__(self, name, sources=["dummy_extension/empty.c"]):
super().__init__(name = name, sources = sources)
super().__init__(name=name, sources=sources)
self.SOURCE_DIR = str(pathlib.Path().absolute())
class InstallCMakeLibsData(install_data):
"""
Just a wrapper to get the install data into the egg-info
Listing the installed files in the egg-info guarantees that
all of the package files will be uninstalled when the user
uninstalls your package through pip
"""
def run(self):
"""
Outfiles are the libraries that were built using cmake
"""
# There seems to be no other way to do this; I tried listing the
# libraries during the execution of the InstallCMakeLibs.run() but
# setuptools never tracked them, seems like setuptools wants to
# track the libraries through package data more than anything...
# help would be appriciated
self.outfiles = self.distribution.data_files
class InstallCMakeLibs(install_lib):
"""
Get the libraries from the parent distribution, use those as the outfiles
Skip building anything; everything is already built, forward libraries to
the installation step
"""
def run(self):
"""
Copy libraries from the bin directory and place them as appropriate
"""
self.announce("Moving library files", level=3)
super().run()
self.distribution.run_command("install_data")
self.distribution.run_command("install_scripts")
class InstallScripts(install_scripts):
def run(self):
# Move the generated script from the build directory to the final installation directory
build_temp = self.get_finalized_command('build_ext').build_temp
generated_script = os.path.join(build_temp, 'vide_prepare_simulation')
if os.path.exists(generated_script):
target_script_dir = os.path.join(self.install_dir, 'bin')
os.makedirs(target_script_dir, exist_ok=True)
shutil.copy(generated_script, target_script_dir)
super().run()
class BuildCMakeExt(build_ext):
"""
Builds using cmake instead of the python setuptools implicit build
"""
def run(self):
"""
Perform build_cmake before doing the 'normal' stuff
"""
for extension in self.extensions:
if extension.name == 'vide':
self.package = 'vide'
self.build_cmake(extension)
super().run()
def build_cmake(self, extension: Extension):
"""
The steps required to build the extension
"""
self.announce("Preparing the build environment", level=3)
# Ensure absolute paths are used
package_dir = os.path.abspath(os.path.join(self.build_lib, 'vide'))
extension.build_dir = pathlib.Path(self.build_temp)
extension.build_dir = os.path.abspath(self.build_temp)
extension.bin_dir = str(pathlib.Path(os.path.join(extension.build_dir, 'private_install')).absolute())
SOURCE_DIR = extension.SOURCE_DIR
build_dir = extension.build_dir
SOURCE_DIR = os.path.abspath(extension.SOURCE_DIR) # Absolute path to source directory
extension_path = pathlib.Path(self.get_ext_fullpath(extension.name))
os.makedirs(build_dir, exist_ok=True)
# Make sure necessary directories exist
os.makedirs(extension.build_dir, exist_ok=True)
os.makedirs(extension_path.parent.absolute(), exist_ok=True)
cython_code = os.path.join(str(build_dir.absolute()),'mycython')
# Write the cython code if necessary
cython_code = os.path.join(str(extension.build_dir), 'mycython')
with open(cython_code, mode="wt") as ff:
ff.write(f"#!{sys.executable}\n"
ff.write(f"#!{sys.executable}\n"
"from Cython.Compiler.Main import setuptools_main\n"
"setuptools_main()")
os.chmod(cython_code, stat.S_IXUSR|stat.S_IWUSR|stat.S_IRUSR|stat.S_IRGRP)
# Now that the necessary directories are created, build
os.chmod(cython_code, stat.S_IXUSR | stat.S_IWUSR | stat.S_IRUSR | stat.S_IRGRP)
self.announce("Configuring cmake project", level=3)
# Change your cmake arguments below as necessary
# Below is just an example set of arguments for building Blender as a Python module
# Ensure the cmake command has the correct source directory
c_compiler = os.environ.get('CC', get_config_var("CC"))
cxx_compiler = os.environ.get('CXX', get_config_var("CXX"))
PYTHON_bin_package=f"{build_dir.absolute()}/private_install"
c_compiler=os.environ.get('CC', get_config_var("CC"))
cxx_compiler=os.environ.get('CXX', get_config_var("CXX"))
self.spawn(['cmake', '-H'+SOURCE_DIR, '-B'+self.build_temp,
'-DENABLE_OPENMP=ON','-DINTERNAL_BOOST=ON','-DINTERNAL_EIGEN=ON',
'-DINTERNAL_HDF5=ON','-DINTERNAL_NETCDF=ON','-DINTERNAL_GSL=ON',
self.spawn(['cmake', '-H' + SOURCE_DIR, '-B' + self.build_temp,
'-DENABLE_OPENMP=ON', '-DINTERNAL_BOOST=ON', '-DINTERNAL_EIGEN=ON',
'-DINTERNAL_HDF5=ON', '-DINTERNAL_NETCDF=ON', '-DINTERNAL_GSL=ON',
'-DBUILD_PYTHON=ON', '-DINSTALL_PYTHON_LOCAL=OFF',
'-DCOSMOTOOL_PYTHON_PACKAGING=ON',
f"-DCYTHON={cython_code}",
'-DINSTALL_CTOOLS_IN_PYTHON=ON',
f"-DCMAKE_C_COMPILER={c_compiler}", f"-DCMAKE_CXX_COMPILER={cxx_compiler}",
f"-DPYTHON_SITE_PACKAGES={PYTHON_bin_package}",
f"-DPYTHON_SITE_PACKAGES={extension.bin_dir}",
f"-DPYTHON_EXECUTABLE={sys.executable}"])
self.announce("Building binaries", level=3)
self.spawn(["cmake", "--build", self.build_temp, "--target", "all",
"--config", "Release","--","VERBOSE=1"])
self.spawn(["cmake", "--build", self.build_temp, "--target", "install",
"--config", "Release","--","VERBOSE=1"])
# Build finished, now copy the files into the copy directory
# The copy directory is the parent directory of the extension (.pyd)
self.announce("Moving built python module", level=3)
bin_dir = PYTHON_bin_package
#extension.bin_dir
self.distribution.bin_dir = bin_dir
target_dir = os.path.abspath(os.path.join(package_dir,'bin'))
print(target_dir)
shutil.rmtree(target_dir, ignore_errors=True)
shutil.move(f"{PYTHON_bin_package}/void_python_tools/bin", target_dir)
shutil.copy(f"{self.build_temp}/pipeline/prepareInputs.py", f"{self.build_temp}/vide_prepare_simulation")
class VideScripts(build_scripts):
user_options = build_scripts.user_options + [
('build-temp=', 't', "temporary directory where scripts are stored"),
]
vide_scripts = ["vide_prepare_simulation"]
def initialize_options(self):
super(VideScripts, self).initialize_options()
self.build_temp = None
def finalize_options(self):
super(VideScripts, self).finalize_options()
self.set_undefined_options('build_ext',
('build_temp', 'build_temp'))
self.scripts = [os.path.join(self.build_temp, v) for v in self.vide_scripts]
def run(self):
self.copy_scripts()
self.spawn(["cmake", "--build", self.build_temp, "--target", "all", "--config", "Release", "--", "VERBOSE=1"])
self.spawn(["cmake", "--build", self.build_temp, "--target", "install", "--config", "Release", "--", "VERBOSE=1"])
vide_extension = CMakeExtension(name="vide")
setup(name='vide',
version='2.0',
packages=find_packages('python_tools'),
package_dir={'': 'python_tools'},
setup_requires=['cython','setuptools','healpy','scipy','astropy','extension-helpers','netCDF4'],
install_requires=['scipy','astropy','extension-helpers','netCDF4','healpy'],
ext_modules=[vide_extension],
description='The VIDE pipeline analysis for Cosmic Voids',
long_description=open("./README.md", 'r').read(),
long_description_content_type="text/markdown",
keywords="cosmology, interpolation, cmake, extension",
classifiers=["Intended Audience :: Developers",
"License :: OSI Approved :: "
"GNU Lesser General Public License v3 (LGPLv3)",
"Natural Language :: English",
"Programming Language :: C",
"Programming Language :: C++",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: Implementation :: CPython"],
license='CeCILL-v2',
scripts=["vide_prepare_simulation"],
cmdclass={
'build_ext': BuildCMakeExt,
'install_data': InstallCMakeLibsData,
'install_lib': InstallCMakeLibs,
'build_scripts': VideScripts,
'install_scripts': install_scripts,
}
setup(
name='vide',
version='2.0',
packages=find_packages('python_tools'),
package_dir={'': 'python_tools'},
ext_modules=[vide_extension],
cmdclass={
'build_ext': BuildCMakeExt,
'install_lib': InstallCMakeLibs,
'install_scripts': InstallScripts,
},
data_files=[
('share/vide/data', ['data/file1.dat', 'data/file2.dat']),
],
install_requires=[
'scipy',
'astropy',
'extension-helpers',
'netCDF4',
'healpy',
],
description='The VIDE pipeline analysis for Cosmic Voids',
long_description=open("./README.md", 'r').read(),
long_description_content_type="text/markdown",
)