Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
165 lines (137 loc) · 5.25 KB
/
Copy pathsetup.py
File metadata and controls
165 lines (137 loc) · 5.25 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
#!/usr/bin/env python
#
# Build script for the aggdraw C extension.
#
# Package metadata lives in pyproject.toml; this file only handles locating
# FreeType and describing the `aggdraw._aggdraw` extension module.
#
# Usage:
#
# To build in place (editable install):
# $ python -m pip install -e .
#
# To build and install:
# $ python -m pip install .
#
import os
import re
import sys
import subprocess
import platform
from pathlib import Path
from sysconfig import get_config_var
from packaging.version import Version
from setuptools import setup, Extension
def get_version(path):
version_regex = re.compile(r'\nVERSION = "([\w\.]+)"')
return version_regex.findall(Path(path).read_text(encoding="utf-8"))[0]
VERSION = get_version(os.path.join("aggdraw", "__init__.py"))
def is_platform_mac():
return sys.platform == "darwin"
# For mac, ensure extensions are built for macos 10.9 when compiling on a
# 10.9 system or above, overriding distuitls behaviour which is to target
# the version that python was built for. This may be overridden by setting
# MACOSX_DEPLOYMENT_TARGET before calling setup.py
if is_platform_mac():
if "MACOSX_DEPLOYMENT_TARGET" not in os.environ:
current_system = Version(platform.mac_ver()[0])
python_target = Version(get_config_var("MACOSX_DEPLOYMENT_TARGET"))
if python_target < Version("10.9") and current_system >= Version("10.9"):
os.environ["MACOSX_DEPLOYMENT_TARGET"] = "10.9"
def _get_freetype_config():
print("Trying freetype-config to find freetype library...")
try:
# pointer to freetype build directory (tweak as necessary)
prefix = subprocess.check_output(["freetype-config", "--prefix"]) # noqa: S607
return prefix.strip().replace(b'"', b"").decode()
except (OSError, subprocess.CalledProcessError):
return None
def _get_freetype_with_ctypes():
print("Using ctypes to find freetype library...")
from ctypes.util import find_library
ft_lib_path = find_library("freetype")
if ft_lib_path is None:
return None
if not sys.platform.startswith("linux") and not os.path.isfile(ft_lib_path):
return None
elif not os.path.isfile(ft_lib_path):
# try prefix since find_library doesn't give a full path on linux
for bdir in (sys.prefix, "/usr", "/usr/local"):
lib_path = os.path.join(bdir, "lib", ft_lib_path)
if os.path.isfile(lib_path):
return bdir
else:
# freetype is somewhere on the system, but we don't know where
return None
ft_lib_path = os.path.dirname(ft_lib_path)
lib_path = os.path.realpath(os.path.join(ft_lib_path, ".."))
return lib_path
def _get_freetype_with_pkgconfig():
print("Trying 'pkgconfig' to find freetype library...")
try:
import pkgconfig
return pkgconfig.variables("freetype2")["prefix"]
except (ImportError, KeyError, ValueError):
return None
FREETYPE_ROOT = os.getenv("AGGDRAW_FREETYPE_ROOT")
# Setting AGGDRAW_FREETYPE_ROOT to an empty string is the supported way to force
# a build *without* freetype, skipping autodetection entirely. The Windows wheels
# rely on this to avoid picking up unintended installations (see pyproject.toml).
FREETYPE_DISABLED = FREETYPE_ROOT == ""
if not FREETYPE_DISABLED:
for func in (_get_freetype_config, _get_freetype_with_ctypes, _get_freetype_with_pkgconfig):
# Not `is None`: a detector can also return an empty string (a
# freetype-config that exits 0 but prints nothing), which must not be
# mistaken for a usable prefix or stop the remaining detectors.
if not FREETYPE_ROOT:
FREETYPE_ROOT = func()
if FREETYPE_DISABLED:
print("=== freetype disabled by AGGDRAW_FREETYPE_ROOT")
elif FREETYPE_ROOT:
print(f"=== freetype found: '{FREETYPE_ROOT}'")
else:
print("=== freetype not available")
sources = [
# source code currently used by aggdraw
# FIXME: link against AGG library instead?
"agg2/src/agg_arc.cpp",
"agg2/src/agg_bezier_arc.cpp",
"agg2/src/agg_curves.cpp",
"agg2/src/agg_rounded_rect.cpp",
"agg2/src/agg_path_storage.cpp",
"agg2/src/agg_rasterizer_scanline_aa.cpp",
"agg2/src/agg_trans_affine.cpp",
"agg2/src/agg_vcgen_contour.cpp",
# "agg2/src/agg_vcgen_dash.cpp",
"agg2/src/agg_vcgen_stroke.cpp",
]
# define VERSION macro in C++ code, need to quote it
defines = [("VERSION", VERSION)]
include_dirs = ["agg2/include"]
library_dirs = []
libraries = []
if FREETYPE_ROOT:
defines.append(("HAVE_FREETYPE2", None))
sources.extend(
[
"agg2/font_freetype/agg_font_freetype.cpp",
]
)
include_dirs.append("agg2/font_freetype")
include_dirs.append(os.path.join(FREETYPE_ROOT, "include"))
include_dirs.append(os.path.join(FREETYPE_ROOT, "include/freetype"))
include_dirs.append(os.path.join(FREETYPE_ROOT, "include/freetype2"))
library_dirs.append(os.path.join(FREETYPE_ROOT, "lib"))
libraries.append("freetype")
setup(
ext_modules=[
Extension(
"aggdraw._aggdraw",
["aggdraw/_aggdraw.cxx"] + sources,
define_macros=defines,
include_dirs=include_dirs,
library_dirs=library_dirs,
libraries=libraries,
)
],
)