Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
executable file
·250 lines (210 loc) · 8.13 KB
/
Copy pathsetup.py
File metadata and controls
executable file
·250 lines (210 loc) · 8.13 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import platform
import sys
from io import open
from typing import List
from Cython.Build import cythonize
from setuptools import Extension, setup
def is_emscripten_build() -> bool:
"""Detect if we're building for Emscripten/Pyodide."""
return sys.platform == "emscripten" or os.environ.get(
"_PYTHON_HOST_PLATFORM", ""
).startswith("emscripten")
def bool_from_environ(key: str, default: bool = False):
value = os.environ.get(key)
if not value:
return default
if value == "1":
return True
if value == "0":
return False
raise ValueError(
f"Environment variable {key} has invalid value {value}. Please set it to 1, 0 or an empty string"
)
here = os.path.abspath(os.path.dirname(__file__))
# Get the long description from the README file
with open(os.path.join(here, "README.md"), encoding="utf-8") as f:
long_description = f.read()
use_system_libraries = bool_from_environ("USE_SYSTEM_LIBS")
use_cython_linetrace = bool_from_environ("CYTHON_LINETRACE")
use_cython_annotate = bool_from_environ("CYTHON_ANNOTATE")
# Python Limited API for stable ABI support is enabled by default.
# Set USE_PY_LIMITED_API=0 to turn it off.
# https://docs.python.org/3.14/c-api/stable.html#limited-c-api
use_py_limited_api = bool_from_environ("USE_PY_LIMITED_API", default=True)
# NOTE: this must be kept in sync with python_requires='>=3.11' below
limited_api_min_version = "0x030B0000"
def _configure_extensions_with_system_libs() -> List[Extension]:
import pkgconfig
include_dirs = []
define_macros = []
libraries = []
library_dirs = []
harfbuzz_components = ["harfbuzz-subset", "harfbuzz-raster"]
for harfbuzz_component in harfbuzz_components:
harfbuzz_component_configuration = pkgconfig.parse(harfbuzz_component)
include_dirs += harfbuzz_component_configuration["include_dirs"]
define_macros += harfbuzz_component_configuration["define_macros"]
libraries += harfbuzz_component_configuration["libraries"]
library_dirs += harfbuzz_component_configuration["library_dirs"]
if use_cython_linetrace:
define_macros.append(("CYTHON_TRACE_NOGIL", "1"))
if use_py_limited_api:
define_macros.append(("Py_LIMITED_API", limited_api_min_version))
extension = Extension(
"uharfbuzz._harfbuzz",
define_macros=define_macros,
include_dirs=include_dirs,
sources=[
"src/uharfbuzz/_harfbuzz.pyx",
],
language="c++",
libraries=libraries,
library_dirs=library_dirs,
py_limited_api=use_py_limited_api,
)
extension_test = Extension(
"uharfbuzz._harfbuzz_test",
define_macros=define_macros,
include_dirs=include_dirs,
sources=[
"src/uharfbuzz/_draw_test_funcs.cc",
"src/uharfbuzz/_harfbuzz_test.pyx",
],
language="c++",
libraries=libraries,
library_dirs=library_dirs,
py_limited_api=use_py_limited_api,
)
return [extension, extension_test]
def _configure_extensions_with_vendored_libs() -> List[Extension]:
define_macros = [
("HB_NO_MT", "1"),
("HB_HAS_SUBSET", "1"),
("HB_HAS_RASTER", "1"),
]
extra_compile_args = []
extra_link_args = []
libraries = []
sources = ["harfbuzz/src/harfbuzz-world.cc", "src/uharfbuzz/_harfbuzz.pyx"]
emscripten = is_emscripten_build()
if use_cython_linetrace:
define_macros.append(("CYTHON_TRACE_NOGIL", "1"))
limited_api = use_py_limited_api
if limited_api:
define_macros.append(("Py_LIMITED_API", limited_api_min_version))
if platform.system() == "Windows" and not emscripten:
define_macros.append(("HB_HAS_DIRECTWRITE", "1"))
define_macros.append(("HB_HAS_UNISCRIBE", "1"))
libraries += ["usp10", "gdi32", "user32", "rpcrt4", "dwrite"]
else:
extra_compile_args.append("-std=c++11")
extra_compile_args.append("-g0")
define_macros.append(("HAVE_MMAP", "1"))
define_macros.append(("HAVE_UNISTD_H", "1"))
define_macros.append(("HAVE_SYS_MMAN_H", "1"))
if platform.system() == "Darwin" and not emscripten:
define_macros.append(("HB_HAS_CORETEXT", "1"))
extra_link_args.extend(["-framework", "ApplicationServices"])
extension = Extension(
"uharfbuzz._harfbuzz",
define_macros=define_macros,
include_dirs=["harfbuzz/src"],
sources=sources,
language="c++",
libraries=libraries,
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args,
py_limited_api=limited_api,
)
extension_test = Extension(
"uharfbuzz._harfbuzz_test",
define_macros=define_macros,
include_dirs=["harfbuzz/src"],
sources=[
"src/uharfbuzz/_draw_test_funcs.cc",
"src/uharfbuzz/_harfbuzz_test.pyx",
],
language="c++",
libraries=libraries,
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args,
py_limited_api=limited_api,
)
return [extension, extension_test]
def configure_extensions() -> List[Extension]:
if use_system_libraries:
return _configure_extensions_with_system_libs()
else:
return _configure_extensions_with_vendored_libs()
def _generate_field_docs() -> None:
import ast
import re
import textwrap
pkg_dir = os.path.join(here, "src", "uharfbuzz")
output_name = "_generated_docs.pxi"
output = os.path.join(pkg_dir, output_name)
# Locate `class Foo(... NamedTuple ...):` headers; the body is parsed as
# AST so it tolerates any consistent indentation.
class_re = re.compile(
r"^class\s+(\w+)[^:]*\bNamedTuple\b[^:]*:[^\n]*\n"
r"((?:[ \t]+.*\n|\s*\n)*)",
re.MULTILINE,
)
assignments: List[str] = []
for fname in sorted(os.listdir(pkg_dir)):
if not fname.endswith(".pxi") or fname == output_name:
continue
with open(os.path.join(pkg_dir, fname), encoding="utf-8") as f:
source = f.read()
for cls_match in class_re.finditer(source):
cls_name = cls_match.group(1)
body_nodes = ast.parse(textwrap.dedent(cls_match.group(2))).body
for i, node in enumerate(body_nodes):
if (
isinstance(node, ast.AnnAssign)
and isinstance(node.target, ast.Name)
and i + 1 < len(body_nodes)
and isinstance(body_nodes[i + 1], ast.Expr)
and isinstance(body_nodes[i + 1].value, ast.Constant)
and isinstance(body_nodes[i + 1].value.value, str)
):
doc = " ".join(body_nodes[i + 1].value.value.split())
assignments.append(
f"{cls_name}.{node.target.id}.__doc__ = {doc!r}"
)
content = "# Generated by setup.py; do not edit.\n" + "\n".join(assignments) + "\n"
# Only rewrite if changed, so Cython doesn't rebuild needlessly.
try:
with open(output, encoding="utf-8") as f:
if f.read() == content:
return
except FileNotFoundError:
pass
with open(output, "w", encoding="utf-8") as f:
f.write(content)
_generate_field_docs()
setup(
name="uharfbuzz",
use_scm_version={"write_to": "src/uharfbuzz/_version.py"},
description="Streamlined Cython bindings for the harfbuzz shaping engine",
long_description=long_description,
long_description_content_type="text/markdown",
author="Adrien Tétar",
author_email="adri-from-59@hotmail.fr",
url="https://github.com/trufont/uharfbuzz",
license="Apache License 2.0",
package_dir={"": "src"},
packages=["uharfbuzz"],
zip_safe=False,
setup_requires=["setuptools_scm"],
python_requires=">=3.11",
ext_modules=cythonize(
configure_extensions(),
annotate=use_cython_annotate,
compiler_directives={"linetrace": use_cython_linetrace},
),
options={"bdist_wheel": {"py_limited_api": "cp311"}} if use_py_limited_api else {},
)