Skip to content

Commit

Permalink
first attempt to fix building; sdist works; wheel missed the compiled…
Browse files Browse the repository at this point in the history
… objects
  • Loading branch information
derNarr committed Sep 13, 2023
1 parent 10f9a9a commit 28cb7dc
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 49 deletions.
70 changes: 37 additions & 33 deletions build.py
Original file line number Diff line number Diff line change
@@ -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
}
})
2 changes: 2 additions & 0 deletions pyndl/correlation_openmp.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# cython: language_level=3

import numpy as np
cimport numpy as np

Expand Down
24 changes: 13 additions & 11 deletions pyndl/ndl_openmp.pyx
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
# cython: language_level=3

import numpy as np
import math
cimport numpy as np
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,
Expand Down
2 changes: 2 additions & 0 deletions pyndl/ndl_parallel.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# cython: language_level=3

import numpy as np
import math
from libc.stdlib cimport abort, malloc, free
Expand Down
13 changes: 8 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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'

0 comments on commit 28cb7dc

Please sign in to comment.