Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
147 lines (127 loc) · 4.82 KB
/
Copy pathsetup.py
File metadata and controls
147 lines (127 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# Licensed under a 3-clause BSD style license - see LICENSE.rst
import os
import re
import subprocess
import sys
import sysconfig
from pathlib import Path
from warnings import warn
import numpy as np
import packaging.version
import setuptools
from setuptools_scm import Configuration, ScmVersion, git
from setuptools_scm.version import guess_next_version
LIBERFADIR = Path("liberfa", "erfa")
ERFA_SRC = LIBERFADIR / "src"
BUILDING_FROM_SDIST = not Path(".git").is_dir()
# build with Py_LIMITED_API unless in freethreading build (which does not currently
# support the limited API in py313t)
USE_PY_LIMITED_API = not sysconfig.get_config_var("Py_GIL_DISABLED")
define_macros: list[tuple[str, str | None]] = []
options = {}
if USE_PY_LIMITED_API:
define_macros.append(("Py_LIMITED_API", "0x30c0000"))
options["bdist_wheel"] = {"py_limited_api": "cp312"}
def get_liberfa_versions(
path: Path = LIBERFADIR / "configure.ac",
) -> list[tuple[str, int | str]]:
s = path.read_text()
mobj = re.search(r'AC_INIT\(\[erfa\],\[(?P<version>[0-9.]+)\]\)', s)
if not mobj:
warn('unable to detect liberfa version')
return []
version = packaging.version.parse(mobj["version"])
mobj = re.search(
r'AC_DEFINE\(\[SOFA_VERSION\], \["(?P<version>\d{8}(_\w)?)"\],', s)
if not mobj:
warn('unable to detect SOFA version')
return []
return [
('PACKAGE_VERSION', version.base_version),
('PACKAGE_VERSION_MAJOR', version.major),
('PACKAGE_VERSION_MINOR', version.minor),
('PACKAGE_VERSION_MICRO', version.micro),
('SOFA_VERSION', mobj["version"]),
]
def guess_next_dev(version: ScmVersion) -> str:
version_string = str(version.tag)
erfa_version = git.parse(LIBERFADIR, config=Configuration(root=LIBERFADIR))
if erfa_version is None:
# Must be installing from an sdist so we cannot check the ERFA tag. We will
# assume it is consistent with the pyerfa version tag, otherwise there was
# a problem when the sdist was built that we cannot fix at this point.
return (
version_string
if version.exact
else version.format_next_version(guess_next_version)
)
if not erfa_version.exact:
warn(f"liberfa/erfa not at a tagged release, but at {erfa_version}")
erfa_tag = erfa_version.format_with("{tag}")
if version.exact:
if not version_string.startswith(erfa_tag):
warn(
f"tag {version_string} does not start with liberfa/erfa tag {erfa_tag}"
)
return version_string
return (
version.format_with("{guessed}.0.dev{distance}", guessed=erfa_tag)
if erfa_tag > version_string
else version.format_next_version(guess_next_version)
)
if not BUILDING_FROM_SDIST:
print('Run "erfa_generator.py"')
try:
subprocess.run([sys.executable, "erfa_generator.py"], check=True)
except subprocess.CalledProcessError as err:
raise RuntimeError(
"unable to generate pyerfa files. Please check your source tree. "
'Maybe "git submodule update" could help.'
) from err
sources = [Path("erfa", "ufunc.c")]
include_dirs = [np.get_include()]
libraries = []
if int(os.environ.get("PYERFA_USE_SYSTEM_LIBERFA", "0")):
print("Using system liberfa")
libraries.append("erfa")
else:
sources.extend(
file
for file in ERFA_SRC.iterdir()
if file.suffix == ".c" and not file.name.startswith("t_")
)
# liberfa configuration
if not BUILDING_FROM_SDIST:
print("Configure liberfa")
try:
subprocess.run(["./bootstrap.sh"], check=True, cwd=LIBERFADIR)
except (subprocess.CalledProcessError, FileNotFoundError) as err:
warn(f"{err}\nMaybe installing autoconf, automake and libtool could help")
try:
subprocess.run(["./configure"], check=True, cwd=LIBERFADIR)
except (subprocess.CalledProcessError, FileNotFoundError):
if version_definitions := "\n".join(
f"#define {name} {value!r}".replace("'", '"')
for name, value in get_liberfa_versions()
):
print('Configure liberfa ("configure.ac" scan)')
(LIBERFADIR / "config.h").write_text(version_definitions)
else:
raise RuntimeError("unable to get liberfa version")
include_dirs.extend(map(str, [ERFA_SRC, LIBERFADIR]))
define_macros.append(("HAVE_CONFIG_H", "1"))
setuptools.setup(
use_scm_version={"version_scheme": guess_next_dev},
ext_modules=[
setuptools.Extension(
name="erfa.ufunc",
sources=sources,
include_dirs=include_dirs,
libraries=libraries,
define_macros=define_macros,
py_limited_api=USE_PY_LIMITED_API,
language="c",
)
],
options=options,
)