From 28cb7dc0813692057283c53b96eff076c113d3b8 Mon Sep 17 00:00:00 2001 From: "Konstantin (Tino) Sering" Date: Wed, 13 Sep 2023 16:29:59 +0200 Subject: [PATCH] first attempt to fix building; sdist works; wheel missed the compiled objects --- build.py | 70 +++++++++++++++++++----------------- pyndl/correlation_openmp.pyx | 2 ++ pyndl/ndl_openmp.pyx | 24 +++++++------ pyndl/ndl_parallel.pyx | 2 ++ pyproject.toml | 13 ++++--- 5 files changed, 62 insertions(+), 49 deletions(-) diff --git a/build.py b/build.py index db63f5b..cdfe847 100644 --- a/build.py +++ b/build.py @@ -1,43 +1,47 @@ -from setuptools import Extension -from setuptools.command.build_ext import build_ext as _build_ext +import os +import shutil import sys +from distutils.core import Distribution, Extension +from Cython.Build import build_ext, cythonize +import numpy -# bootstrap numpy -# https://stackoverflow.com/questions/19919905/how-to-bootstrap-numpy-installation-in-setup-py -class build_ext(_build_ext): - def finalize_options(self): - _build_ext.finalize_options(self) - # Prevent numpy from thinking it is still in its setup process: - #__builtins__.__NUMPY_SETUP__ = False - import numpy - self.include_dirs.append(numpy.get_include()) +cython_dir = "pyndl" +ndl_parallel = Extension("pyndl.ndl_parallel", + ["pyndl/ndl_parallel.pyx"], + extra_compile_args=['-O3'], + include_dirs=[numpy.get_include()]) +ndl_openmp = Extension("pyndl.ndl_openmp", + ["pyndl/ndl_openmp.pyx"], + extra_compile_args=['-O3', '-fopenmp'], + extra_link_args=['-fopenmp'], + include_dirs=[numpy.get_include()]) +corr_parallel = Extension("pyndl.correlation_openmp", + ["pyndl/correlation_openmp.pyx"], + extra_compile_args=['-O3', '-fopenmp'], + extra_link_args=['-O3', '-fopenmp'], + include_dirs=[numpy.get_include()]) -ndl_parallel = Extension("pyndl.ndl_parallel", ["pyndl/ndl_parallel.pyx"]) -ndl_openmp = Extension("pyndl.ndl_openmp", ["pyndl/ndl_openmp.pyx"], - extra_compile_args=['-fopenmp'], extra_link_args=['-fopenmp']) -corr_parallel = Extension("pyndl.correlation_openmp", ["pyndl/correlation_openmp.pyx"], - extra_compile_args=['-fopenmp'], extra_link_args=['-fopenmp']) -# by giving ``cython`` as ``install_requires`` this will be ``cythonized`` -# automagically - -ext_modules = [] +extensions = [] +include_paths = [] if sys.platform.startswith('linux'): - ext_modules = [ndl_parallel, ndl_openmp, corr_parallel] + extensions = [ndl_parallel, ndl_openmp, corr_parallel] + include_paths = [cython_dir, cython_dir, cython_dir] elif sys.platform.startswith('win32'): - ext_modules = [ndl_parallel] # skip openmp installation on windows for now + extensions = [ndl_parallel] # skip openmp installation on windows for now + include_paths = [cython_dir] elif sys.platform.startswith('darwin'): - ext_modules = [ndl_parallel] # skip openmp installation on macos for now + extensions = [ndl_parallel] # skip openmp installation on macos for now + include_paths = [cython_dir] + +ext_modules = cythonize(extensions, include_path=include_paths) +dist = Distribution({"ext_modules": ext_modules}) +cmd = build_ext(dist) +cmd.ensure_finalized() +cmd.run() +for output in cmd.get_outputs(): + relative_extension = os.path.relpath(output, cmd.build_lib) + shutil.copyfile(output, relative_extension) -def build(setup_kwargs): - """ - This function is mandatory in order to build the extensions. - """ - setup_kwargs.update({ - 'ext_modules': ext_modules, - 'cmdclass': { - 'build_ext': build_ext - } - }) diff --git a/pyndl/correlation_openmp.pyx b/pyndl/correlation_openmp.pyx index ec1d23b..1939cc2 100644 --- a/pyndl/correlation_openmp.pyx +++ b/pyndl/correlation_openmp.pyx @@ -1,3 +1,5 @@ +# cython: language_level=3 + import numpy as np cimport numpy as np diff --git a/pyndl/ndl_openmp.pyx b/pyndl/ndl_openmp.pyx index d6c3e50..a5306f1 100644 --- a/pyndl/ndl_openmp.pyx +++ b/pyndl/ndl_openmp.pyx @@ -1,3 +1,5 @@ +# cython: language_level=3 + import numpy as np import math cimport numpy as np @@ -5,17 +7,17 @@ ctypedef np.float64_t dtype_t cimport cython from cython.parallel cimport parallel, prange -from ndl_parallel cimport (learn_inplace_binary_to_binary_ptr, - learn_inplace_binary_to_real_ptr, - learn_inplace_real_to_real_ptr, - learn_inplace_real_to_binary_ptr) -from error_codes cimport (ErrorCode, - NO_ERROR, - MAGIC_NUMBER_DOES_NOT_MATCH, - VERSION_NUMBER_DOES_NOT_MATCH, - INITIAL_ERROR_CODE, - ONLY_ONE_OUTCOME_PER_EVENT, - ERROR_CODES) +from .ndl_parallel cimport (learn_inplace_binary_to_binary_ptr, + learn_inplace_binary_to_real_ptr, + learn_inplace_real_to_real_ptr, + learn_inplace_real_to_binary_ptr) +from .error_codes cimport (ErrorCode, + NO_ERROR, + MAGIC_NUMBER_DOES_NOT_MATCH, + VERSION_NUMBER_DOES_NOT_MATCH, + INITIAL_ERROR_CODE, + ONLY_ONE_OUTCOME_PER_EVENT, + ERROR_CODES) def learn_inplace_binary_to_binary(binary_file_paths, diff --git a/pyndl/ndl_parallel.pyx b/pyndl/ndl_parallel.pyx index a1d727e..d1ef52d 100644 --- a/pyndl/ndl_parallel.pyx +++ b/pyndl/ndl_parallel.pyx @@ -1,3 +1,5 @@ +# cython: language_level=3 + import numpy as np import math from libc.stdlib cimport abort, malloc, free diff --git a/pyproject.toml b/pyproject.toml index a93fb8f..8498931 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "pyndl" -version = "1.1.1" +version = "1.1.2" description = "Naive discriminative learning implements learning and classification models based on the Rescorla-Wagner equations." license = "MIT" @@ -27,8 +27,6 @@ classifiers = ['Development Status :: 5 - Production/Stable', 'Topic :: Scientific/Engineering :: Artificial Intelligence', 'Topic :: Scientific/Engineering :: Information Analysis',] -build = "build.py" - [tool.poetry.dependencies] python = ">=3.8,<3.12" # Compatible python versions must be declared here numpy = "^1.23.1" @@ -70,5 +68,10 @@ addopts = '--doctest-glob "*.rst"' disable = "E1101" [build-system] -requires = ["poetry>=1.0.0", "setuptools", "Cython", "numpy"] -build-backend = "poetry.masonry.api" +requires = ["poetry-core", "setuptools", "Cython", "numpy"] +build-backend = "poetry.core.masonry.api" + +[tool.poetry.build] +generate-setup-file = false +script = 'build.py' +