Skip to content

Commit

Permalink
MAINT: Simplify and standardize setup
Browse files Browse the repository at this point in the history
Simplify setup since NumPy wikk always be installed when using pip
install.
Standardize library usage

xref statsmodels#9333
  • Loading branch information
bashtage committed Sep 12, 2024
1 parent 90b2b43 commit 5a1dd21
Showing 1 changed file with 28 additions and 69 deletions.
97 changes: 28 additions & 69 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
pytest --cov=statsmodels statsmodels
coverage html
"""

from setuptools import Command, Extension, find_packages, setup
from setuptools.dist import Distribution

from packaging.version import parse
from collections import defaultdict
import fnmatch
import inspect
Expand All @@ -18,6 +18,9 @@
import shutil
import sys

import numpy as np
from packaging.version import parse

SETUP_DIR = Path(__file__).parent.resolve()


Expand All @@ -37,13 +40,6 @@

HAS_CYTHON = CYTHON_3 = False

try:
import numpy # noqa: F401

HAS_NUMPY = True
except ImportError:
HAS_NUMPY = False

###############################################################################
# Key Values that Change Each Release
###############################################################################
Expand Down Expand Up @@ -222,46 +218,7 @@ def run(self) -> None:
sys.exit(1)


def update_extension(extension, requires_math=True):
import numpy as np

numpy_includes = [np.get_include()]
extra_incl = pjoin(dirname(inspect.getfile(np.core)), "include")
numpy_includes += [extra_incl]
numpy_includes = sorted(set(numpy_includes))
numpy_math_libs = {
"include_dirs": [np.get_include()],
"library_dirs": [os.path.join(np.get_include(), '..', 'lib')],
"libraries": ["npymath"]
}

if not hasattr(extension, "include_dirs"):
return
extension.include_dirs = sorted(
set(extension.include_dirs + numpy_includes)
)
if requires_math:
extension.include_dirs += numpy_math_libs["include_dirs"]
extension.libraries += numpy_math_libs["libraries"]
extension.library_dirs += numpy_math_libs["library_dirs"]


class DeferredBuildExt(build_ext):
"""build_ext command for use when numpy headers are needed."""

def build_extensions(self):
self._update_extensions()
build_ext.build_extensions(self)

def _update_extensions(self):
for extension in self.extensions:
requires_math = extension.name in EXT_REQUIRES_NUMPY_MATH_LIBS
update_extension(extension, requires_math=requires_math)


cmdclass = {"clean": CleanCommand}
if not HAS_NUMPY:
cmdclass["build_ext"] = DeferredBuildExt


def check_source(source_name):
Expand Down Expand Up @@ -301,21 +258,30 @@ def process_tempita(source_name):
return source_name


EXT_REQUIRES_NUMPY_MATH_LIBS = []
NUMPY_INCLUDES = sorted(
set([np.get_include(), pjoin(dirname(inspect.getfile(np.core)), "include")])
)
NUMPY_MATH_LIBS = {
"include_dirs": [np.get_include()],
"library_dirs": [os.path.join(np.get_include(), "..", "lib")],
"libraries": ["npymath"],
}


extensions = []
for config in exts.values():
uses_blas = True
source, ext = check_source(config["source"])
source = process_tempita(source)
name = source.replace("/", ".").replace(ext, "")
include_dirs = config.get("include_dirs", [])
depends = config.get("depends", [])
libraries = config.get("libraries", [])
library_dirs = config.get("library_dirs", [])

uses_numpy_libraries = config.get("numpy_libraries", False)
if uses_blas or uses_numpy_libraries:
EXT_REQUIRES_NUMPY_MATH_LIBS.append(name)

include_dirs = sorted(set(include_dirs + NUMPY_MATH_LIBS["include_dirs"]))
libraries = sorted(set(libraries + NUMPY_MATH_LIBS["libraries"]))
library_dirs = sorted(set(library_dirs + NUMPY_MATH_LIBS["library_dirs"]))

ext = Extension(
name,
Expand All @@ -333,31 +299,24 @@ def process_tempita(source_name):
source = process_tempita(source)
name = source.replace("/", ".").replace(ext, "")

EXT_REQUIRES_NUMPY_MATH_LIBS.append(name)
ext = Extension(
name,
[source],
include_dirs=["statsmodels/src"],
include_dirs=["statsmodels/src"] + NUMPY_MATH_LIBS["include_dirs"],
depends=[],
libraries=[],
library_dirs=[],
libraries=NUMPY_MATH_LIBS["libraries"],
library_dirs=NUMPY_MATH_LIBS["library_dirs"],
define_macros=DEFINE_MACROS,
)
extensions.append(ext)

if HAS_NUMPY:
for extension in extensions:
requires_math = extension.name in EXT_REQUIRES_NUMPY_MATH_LIBS
update_extension(extension, requires_math=requires_math)
if HAS_CYTHON:
if CYTHON_3:
COMPILER_DIRECTIVES["cpow"] = True
extensions = cythonize(
extensions,
compiler_directives=COMPILER_DIRECTIVES,
language_level=3,
force=CYTHON_COVERAGE,
)
COMPILER_DIRECTIVES["cpow"] = True
extensions = cythonize(
extensions,
compiler_directives=COMPILER_DIRECTIVES,
language_level=3,
force=CYTHON_COVERAGE,
)

##############################################################################
# Construct package data
Expand Down

0 comments on commit 5a1dd21

Please sign in to comment.