From 8a2358b5476cfe044b7ab6b01a3b0b9b8b28066e Mon Sep 17 00:00:00 2001 From: Christopher Albert Date: Fri, 10 Jan 2025 02:03:58 +0100 Subject: [PATCH] Fix #376 by adapting setup.py and pyproject.toml (#431) * Fix #376 by meson build * Replace meson by simpler solution * Formatting * Fix distutils for Python<3.12 * Makefile updated * Added MANIFEST to f2py * add build requirement * Fixing extension * Cleanup --- f2py/MANIFEST.in | 4 +++ f2py/pygacode/__init__.py | 2 +- f2py/pyproject.toml | 21 ++++++++++------ f2py/setup.py | 52 +++++++++++++++++++++++++++++++++------ 4 files changed, 62 insertions(+), 17 deletions(-) create mode 100644 f2py/MANIFEST.in diff --git a/f2py/MANIFEST.in b/f2py/MANIFEST.in new file mode 100644 index 000000000..50a2b9e78 --- /dev/null +++ b/f2py/MANIFEST.in @@ -0,0 +1,4 @@ +include expro/expro.f90 +include expro/expro_util.f90 +include expro/expro_pycomm.f90 +include geo/geo.f90 diff --git a/f2py/pygacode/__init__.py b/f2py/pygacode/__init__.py index 57e6118e0..21d5c78d3 100644 --- a/f2py/pygacode/__init__.py +++ b/f2py/pygacode/__init__.py @@ -31,4 +31,4 @@ def gapystr_set(s, l=10, n=200): __all__.extend(list(tmp.keys())) except: # Not using pygacode - pass + pass diff --git a/f2py/pyproject.toml b/f2py/pyproject.toml index b1c6b73b9..80fec8b99 100644 --- a/f2py/pyproject.toml +++ b/f2py/pyproject.toml @@ -1,13 +1,18 @@ [build-system] -requires = ['setuptools','wheel','numpy'] +requires = [ + 'setuptools>=41.2.0', 'wheel', + 'oldest-supported-numpy; python_version=="3.8"', + 'numpy>=2.0.2; python_version>="3.9"', + 'numpy>=2.1.3; python_version>="3.10"', + 'meson; python_version>="3.12"', +] +build-backend = 'setuptools.build_meta' [project] name = 'pygacode' -version = '1.0.1' +version = '1.0.2' description = 'Python-GACODE' -license = {text = 'MIT'} -requires-python = ">=3.8" - -[project.urls] -Homepage = "https://gacode.io" - +license = { text = 'MIT' } +requires-python = '>=3.8' +urls = { Homepage = 'https://gacode.io' } +dependencies = ['numpy'] diff --git a/f2py/setup.py b/f2py/setup.py index d0685709d..20b0714a8 100644 --- a/f2py/setup.py +++ b/f2py/setup.py @@ -1,10 +1,46 @@ -from numpy.distutils.core import setup,Extension +from setuptools import Extension, setup +from setuptools.command.build_ext import build_ext +from importlib.machinery import EXTENSION_SUFFIXES +import os +import shutil +import sys +import subprocess +import sysconfig + + +class F2PyExtension(Extension): + def __init__(self, name, fortran_sources, **kwargs): + super().__init__(name, sources=[], **kwargs) + self.fortran_sources = fortran_sources + + +class BuildF2PyExtension(build_ext): + def build_extension(self, ext): + output_module = ext.name + env = os.environ.copy() + if sys.version_info < (3, 12): + env["SETUPTOOLS_USE_DISTUTILS"] = "1" + subprocess.check_call([ + "f2py", "-c", "-m", output_module, *ext.fortran_sources + ], env=env) + + ext_suffix = sysconfig.get_config_var("EXT_SUFFIX") + lib_name = f"{output_module}{ext_suffix}" + + ext_path = self.get_ext_fullpath(ext.name) + ext_dir = os.path.dirname(ext_path) + if not os.path.exists(ext_dir): + os.makedirs(ext_dir) + + shutil.move(lib_name, ext_path) + + +ext = F2PyExtension('gacode_ext', + fortran_sources=['expro/expro.f90', + 'expro/expro_util.f90', + 'expro/expro_pycomm.f90', + 'geo/geo.f90']) -ext = Extension('gacode_ext', - sources=['expro/expro.f90', - 'expro/expro_util.f90', - 'expro/expro_pycomm.f90', - 'geo/geo.f90']) setup(py_modules=['pygacode.gacodefuncs', 'pygacode.gacodeinput'], @@ -16,6 +52,6 @@ 'pygacode.neo', 'pygacode.profiles_gen'], package_data={'pygacode.test': ['input.gacode']}, - ext_modules=[ext] + ext_modules=[ext], + cmdclass={'build_ext': BuildF2PyExtension}, ) -