| 1 |
import os |
| 2 |
import sys |
| 3 |
import sysconfig |
| 4 |
import numpy |
| 5 |
|
| 6 |
from Cython.Build import cythonize |
| 7 |
from setuptools import Command, Extension, setup |
| 8 |
|
| 9 |
|
| 10 |
BASEDIR = os.path.abspath(os.path.dirname(__file__)) |
| 11 |
SRCDIR = os.path.join(BASEDIR,'src') |
| 12 |
CMDS_NOCYTHONIZE = ['clean','clean_cython','sdist'] |
| 13 |
COMPILER_DIRECTIVES = { |
| 14 |
# Cython 3.0.0 changes the default of the c_api_binop_methods directive to |
| 15 |
# False, resulting in errors in datetime and timedelta arithmetic: |
| 16 |
# https://github.com/Unidata/cftime/issues/271. We explicitly set it to |
| 17 |
# True to retain Cython 0.x behavior for future Cython versions. This |
| 18 |
# directive was added in Cython version 0.29.20. |
| 19 |
"c_api_binop_methods": True |
| 20 |
} |
| 21 |
COVERAGE_COMPILER_DIRECTIVES = { |
| 22 |
"linetrace": True, |
| 23 |
"warn.maybe_uninitialized": False, |
| 24 |
"warn.unreachable": False, |
| 25 |
"warn.unused": False, |
| 26 |
} |
| 27 |
DEFINE_MACROS = [("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")] |
| 28 |
FLAG_COVERAGE = '--cython-coverage' # custom flag enabling Cython line tracing |
| 29 |
NAME = 'cftime' |
| 30 |
CFTIME_DIR = os.path.join(SRCDIR, NAME) |
| 31 |
CYTHON_FNAME = os.path.join(CFTIME_DIR, '_{}.pyx'.format(NAME)) |
| 32 |
|
| 33 |
USE_PY_LIMITED_API = ( |
| 34 |
# require opt-in (builds are specialized by default) |
| 35 |
os.getenv('CFTIME_LIMITED_API', '0') == '1' |
| 36 |
# Cython + numpy + limited API de facto requires Python >=3.11 |
| 37 |
and sys.version_info >= (3, 11) |
| 38 |
# as of Python 3.14t, free-threaded builds don't support the limited API |
| 39 |
and not sysconfig.get_config_var("Py_GIL_DISABLED") |
| 40 |
) |
| 41 |
ABI3_TARGET_VERSION = "".join(str(_) for _ in sys.version_info[:2]) |
| 42 |
ABI3_TARGET_HEX = hex(sys.hexversion & 0xFFFF00F0) |
| 43 |
|
| 44 |
if USE_PY_LIMITED_API: |
| 45 |
DEFINE_MACROS += [(("Py_LIMITED_API", ABI3_TARGET_HEX))] |
| 46 |
SETUP_OPTIONS = {"bdist_wheel": {"py_limited_api": f"cp{ABI3_TARGET_VERSION}"}} |
| 47 |
else: |
| 48 |
SETUP_OPTIONS = {} |
| 49 |
|
| 50 |
class CleanCython(Command): |
| 51 |
description = 'Purge artifacts built by Cython' |
| 52 |
user_options = [] |
| 53 |
|
| 54 |
def initialize_options(self): |
| 55 |
pass |
| 56 |
|
| 57 |
def finalize_options(self): |
| 58 |
pass |
| 59 |
|
| 60 |
def run(self): |
| 61 |
for rpath, _, fnames in os.walk(CFTIME_DIR): |
| 62 |
for fname in fnames: |
| 63 |
_, ext = os.path.splitext(fname) |
| 64 |
if ext in ('.pyc', '.pyo', '.c', '.so'): |
| 65 |
artifact = os.path.join(rpath, fname) |
| 66 |
if os.path.exists(artifact): |
| 67 |
print('clean: removing file {!r}'.format(artifact)) |
| 68 |
os.remove(artifact) |
| 69 |
else: |
| 70 |
print('clean: skipping file {!r}'.format(artifact)) |
| 71 |
|
| 72 |
|
| 73 |
if ((FLAG_COVERAGE in sys.argv or os.environ.get('CYTHON_COVERAGE', None)) |
| 74 |
and cythonize): |
| 75 |
COMPILER_DIRECTIVES = { |
| 76 |
**COMPILER_DIRECTIVES, **COVERAGE_COMPILER_DIRECTIVES |
| 77 |
} |
| 78 |
DEFINE_MACROS += [('CYTHON_TRACE', '1'), |
| 79 |
('CYTHON_TRACE_NOGIL', '1')] |
| 80 |
|
| 81 |
if FLAG_COVERAGE in sys.argv: |
| 82 |
sys.argv.remove(FLAG_COVERAGE) |
| 83 |
print('enable: "linetrace" Cython compiler directive') |
| 84 |
|
| 85 |
# See https://github.com/Unidata/cftime/issues/91 |
| 86 |
if any([arg in CMDS_NOCYTHONIZE for arg in sys.argv]): |
| 87 |
ext_modules = [] |
| 88 |
else: |
| 89 |
extension = Extension('{}._{}'.format(NAME, NAME), |
| 90 |
sources=[os.path.relpath(CYTHON_FNAME, BASEDIR)], |
| 91 |
define_macros=DEFINE_MACROS, |
| 92 |
include_dirs=[numpy.get_include()], |
| 93 |
py_limited_api=USE_PY_LIMITED_API) |
| 94 |
|
| 95 |
ext_modules = cythonize( |
| 96 |
extension, |
| 97 |
compiler_directives=COMPILER_DIRECTIVES, |
| 98 |
language_level=3, |
| 99 |
) |
| 100 |
|
| 101 |
setup( |
| 102 |
cmdclass={'clean_cython': CleanCython}, |
| 103 |
ext_modules=ext_modules, |
| 104 |
options=SETUP_OPTIONS, |
| 105 |
) |