matplotlib / matplotlib Public
-
Sponsor
Sponsor matplotlib/matplotlib
- Notifications
- Star 14.2k
- Fork 6k
Permalink
matplotlib/setupext.py
Newer
100644
700 lines (607 sloc)
23.8 KB
7
import logging
19
_log = logging.getLogger(__name__)
20
26
See https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
28
cache_dir = os.environ.get('XDG_CACHE_HOME')
29
if not cache_dir:
30
cache_dir = os.path.expanduser('~/.cache')
31
if cache_dir.startswith('~/'): # Expansion failed.
32
return None
36
def _get_hash(data):
37
"""Compute the sha256 hash of *data*."""
38
hasher = hashlib.sha256()
40
return hasher.hexdigest()
41
42
43
@functools.lru_cache()
44
def _get_ssl_context():
45
import certifi
46
import ssl
47
return ssl.create_default_context(cafile=certifi.where())
48
49
50
def get_from_cache_or_download(url, sha):
51
"""
52
Get bytes from the given url or local cache.
53
54
Parameters
55
----------
56
url : str
60
61
Returns
62
-------
63
BytesIO
64
The file loaded into memory.
65
"""
66
cache_dir = _get_xdg_cache_dir()
67
68
if cache_dir is not None: # Try to read from cache.
69
try:
70
data = (cache_dir / sha).read_bytes()
71
except IOError:
72
pass
73
else:
74
if _get_hash(data) == sha:
75
return BytesIO(data)
77
# jQueryUI's website blocks direct downloads from urllib.request's
78
# default User-Agent, but not (for example) wget; so I don't feel too
79
# bad passing in an empty User-Agent.
81
urllib.request.Request(url, headers={"User-Agent": ""}),
82
context=_get_ssl_context()) as req:
88
f"The downloaded file does not match the expected sha. {url} was "
89
f"expected to have {sha} but it had {file_sha}")
91
if cache_dir is not None: # Try to cache the downloaded file.
92
try:
93
cache_dir.mkdir(parents=True, exist_ok=True)
94
with open(cache_dir / sha, "xb") as fout:
95
fout.write(data)
96
except IOError:
97
pass
102
def get_and_extract_tarball(urls, sha, dirname):
103
"""
104
Obtain a tarball (from cache or download) and extract it.
105
106
Parameters
107
----------
108
urls : list[str]
109
URLs from which download is attempted (in order of attempt), if the
110
tarball is not in the cache yet.
111
sha : str
112
SHA256 hash of the tarball; used both as a cache key (by
113
`get_from_cache_or_download`) and to validate a downloaded tarball.
114
dirname : path-like
115
Directory where the tarball is extracted.
116
"""
117
toplevel = Path("build", dirname)
118
if not toplevel.exists(): # Download it or load it from cache.
119
Path("build").mkdir(exist_ok=True)
120
for url in urls:
121
try:
122
tar_contents = get_from_cache_or_download(url, sha)
123
break
124
except Exception:
125
pass
126
else:
127
raise IOError(
128
f"Failed to download any of the following: {urls}. "
129
f"Please download one of these urls and extract it into "
130
f"'build/' at the top-level of the source repository.")
131
print("Extracting {}".format(urllib.parse.urlparse(url).path))
132
with tarfile.open(fileobj=tar_contents, mode="r:gz") as tgz:
133
if os.path.commonpath(tgz.getnames()) != dirname:
134
raise IOError(
135
f"The downloaded tgz file was expected to have {dirname} "
136
f"as sole top-level directory, but that is not the case")
137
tgz.extractall("build")
138
return toplevel
139
140
141
# SHA256 hashes of the FreeType tarballs
142
_freetype_hashes = {
143
'2.6.1':
144
'0a3c7dfbda6da1e8fce29232e8e96d987ababbbf71ebc8c75659e4132c367014',
145
'2.6.2':
146
'8da42fc4904e600be4b692555ae1dcbf532897da9c5b9fb5ebd3758c77e5c2d4',
147
'2.6.3':
148
'7942096c40ee6fea882bd4207667ad3f24bff568b96b10fd3885e11a7baad9a3',
149
'2.6.4':
150
'27f0e38347a1850ad57f84fc4dfed68ba0bc30c96a6fa6138ef84d485dd9a8d7',
151
'2.6.5':
152
'3bb24add9b9ec53636a63ea8e867ed978c4f8fdd8f1fa5ccfd41171163d4249a',
153
'2.7':
154
'7b657d5f872b0ab56461f3bd310bd1c5ec64619bd15f0d8e08282d494d9cfea4',
155
'2.7.1':
156
'162ef25aa64480b1189cdb261228e6c5c44f212aac4b4621e28cf2157efb59f5',
157
'2.8':
158
'33a28fabac471891d0523033e99c0005b95e5618dc8ffa7fa47f9dadcacb1c9b',
159
'2.8.1':
160
'876711d064a6a1bd74beb18dd37f219af26100f72daaebd2d86cb493d7cd7ec6',
161
'2.9':
162
'bf380e4d7c4f3b5b1c1a7b2bf3abb967bda5e9ab480d0df656e0e08c5019c5e6',
163
'2.9.1':
164
'ec391504e55498adceb30baceebd147a6e963f636eb617424bcfc47a169898ce',
165
'2.10.0':
166
'955e17244e9b38adb0c98df66abb50467312e6bb70eac07e49ce6bd1a20e809a',
167
'2.10.1':
168
'3a60d391fd579440561bf0e7f31af2222bc610ad6ce4d9d7bd2165bca8669110',
170
# This is the version of FreeType to use when building a local
171
# version. It must match the value in
172
# lib/matplotlib.__init__.py and also needs to be changed below in the
173
# embedded windows build script (grep for "REMINDER" in this file)
175
LOCAL_FREETYPE_HASH = _freetype_hashes.get(LOCAL_FREETYPE_VERSION, 'unknown')
180
# Matplotlib build options, which can be altered using mplsetup.cfg
181
mplsetup_cfg = os.environ.get('MPLSETUPCFG') or 'mplsetup.cfg'
183
if os.path.exists(mplsetup_cfg):
184
config.read(mplsetup_cfg)
185
options = {
186
'backend': config.get('rc_options', 'backend', fallback=None),
187
'system_freetype': config.getboolean(
188
'libs', 'system_freetype', fallback=sys.platform.startswith('aix')),
189
'system_qhull': config.getboolean(
190
'libs', 'system_qhull', fallback=False),
194
if '-q' in sys.argv or '--quiet' in sys.argv:
195
def print_raw(*args, **kwargs): pass # Suppress our own output.
196
else:
197
print_raw = print
198
199
200
def print_status(package, status):
201
initial_indent = "%12s: " % package
202
indent = ' ' * 18
203
print_raw(textwrap.fill(str(status), width=80,
204
initial_indent=initial_indent,
205
subsequent_indent=indent))
208
@functools.lru_cache(1) # We only need to compute this once.
209
def get_pkg_config():
210
"""
211
Get path to pkg-config and set up the PKG_CONFIG environment variable.
212
"""
213
if sys.platform == 'win32':
214
return None
215
pkg_config = os.environ.get('PKG_CONFIG') or 'pkg-config'
217
print(
218
"IMPORTANT WARNING:\n"
219
" pkg-config is not installed.\n"
220
" Matplotlib may not be able to find some of its dependencies.")
221
return None
222
pkg_config_path = sysconfig.get_config_var('LIBDIR')
223
if pkg_config_path is not None:
224
pkg_config_path = os.path.join(pkg_config_path, 'pkgconfig')
226
os.environ['PKG_CONFIG_PATH'] += ':' + pkg_config_path
228
os.environ['PKG_CONFIG_PATH'] = pkg_config_path
229
return pkg_config
232
def pkg_config_setup_extension(
233
ext, package,
234
atleast_version=None, alt_exec=None, default_libraries=()):
235
"""Add parameters to the given *ext* for the given *package*."""
237
# First, try to get the flags from pkg-config.
239
pkg_config = get_pkg_config()
240
cmd = [pkg_config, package] if pkg_config else alt_exec
241
if cmd is not None:
242
try:
243
if pkg_config and atleast_version:
244
subprocess.check_call(
245
[*cmd, f"--atleast-version={atleast_version}"])
246
# Use sys.getfilesystemencoding() to allow round-tripping
247
# when passed back to later subprocess calls; do not use
248
# locale.getpreferredencoding() which universal_newlines=True
249
# would do.
250
cflags = shlex.split(
251
os.fsdecode(subprocess.check_output([*cmd, "--cflags"])))
252
libs = shlex.split(
253
os.fsdecode(subprocess.check_output([*cmd, "--libs"])))
254
except (OSError, subprocess.CalledProcessError):
255
pass
256
else:
257
ext.extra_compile_args.extend(cflags)
258
ext.extra_link_args.extend(libs)
259
return
261
# If that fails, fall back on the defaults.
263
# conda Windows header and library paths.
264
# https://github.com/conda/conda/issues/2312 re: getting the env dir.
265
if sys.platform == 'win32':
266
conda_env_path = (os.getenv('CONDA_PREFIX') # conda >= 4.1
267
or os.getenv('CONDA_DEFAULT_ENV')) # conda < 4.1
268
if conda_env_path and os.path.isdir(conda_env_path):
269
conda_env_path = Path(conda_env_path)
270
ext.include_dirs.append(str(conda_env_path / "Library/include"))
271
ext.library_dirs.append(str(conda_env_path / "Library/lib"))
273
# Default linked libs.
274
ext.libraries.extend(default_libraries)
279
Exception thrown by `SetupPackage.check` to indicate that a package should
280
be skipped.
288
If the package should be installed, return an informative string, or
289
None if no information should be displayed at all.
290
291
If the package should be skipped, raise a `Skipped` exception.
292
293
If a missing build dependency is fatal, call `sys.exit`.
294
"""
295
296
def get_package_data(self):
297
"""
298
Get a package data dictionary to add to the configuration.
299
These are merged into to the *package_data* list passed to
300
`setuptools.setup`.
304
def get_extensions(self):
306
Return or yield a list of C extensions (`distutils.core.Extension`
307
objects) to add to the configuration. These are added to the
313
"""
314
If a package needs to do extra custom things, such as building a
315
third-party library, before building an extension, it should
316
override this method.
317
"""
318
321
config_category = "packages"
326
Check whether ``mplsetup.cfg`` requests this package to be installed.
328
May be overridden by subclasses for additional checks.
330
if config.getboolean(self.config_category, self.name,
331
fallback=self.default_config):
333
else: # Configuration opt-out by user
335
336
337
class Platform(SetupPackage):
338
name = "platform"
339
340
def check(self):
341
return sys.platform
342
343
344
class Python(SetupPackage):
345
name = "python"
346
347
def check(self):
348
return sys.version
349
350
351
def _pkg_data_helper(pkg, subdir):
352
"""Glob "lib/$pkg/$subdir/**/*", returning paths relative to "lib/$pkg"."""
354
return [str(path.relative_to(base)) for path in (base / subdir).rglob("*")]
355
356
357
class Matplotlib(SetupPackage):
358
name = "matplotlib"
359
360
def get_package_data(self):
361
return {
364
*_pkg_data_helper('matplotlib', 'mpl-data'),
365
*_pkg_data_helper('matplotlib', 'backends/web_backend'),
366
'*.dll', # Only actually matters on Windows.
370
def get_extensions(self):
371
# agg
372
ext = Extension(
373
"matplotlib.backends._backend_agg", [
374
"src/py_converters.cpp",
375
"src/_backend_agg.cpp",
376
"src/_backend_agg_wrapper.cpp",
377
])
378
add_numpy_flags(ext)
379
add_libagg_flags_and_sources(ext)
382
# c_internal_utils
383
ext = Extension(
384
"matplotlib._c_internal_utils", ["src/_c_internal_utils.c"],
385
libraries=({
386
"linux": ["dl"],
387
"win32": ["ole32", "shell32", "user32"],
388
}.get(sys.platform, [])))
390
# contour
391
ext = Extension(
392
"matplotlib._contour", [
393
"src/_contour.cpp",
394
"src/_contour_wrapper.cpp",
395
"src/py_converters.cpp",
396
])
397
add_numpy_flags(ext)
398
add_libagg_flags(ext)
399
yield ext
400
# ft2font
401
ext = Extension(
402
"matplotlib.ft2font", [
403
"src/ft2font.cpp",
404
"src/ft2font_wrapper.cpp",
405
"src/py_converters.cpp",
406
])
408
add_numpy_flags(ext)
409
add_libagg_flags(ext)
410
yield ext
411
# image
412
ext = Extension(
413
"matplotlib._image", [
414
"src/_image_wrapper.cpp",
415
"src/py_converters.cpp",
416
])
417
add_numpy_flags(ext)
418
add_libagg_flags_and_sources(ext)
419
yield ext
420
# path
421
ext = Extension(
422
"matplotlib._path", [
423
"src/py_converters.cpp",
424
"src/_path_wrapper.cpp",
425
])
426
add_numpy_flags(ext)
427
add_libagg_flags_and_sources(ext)
428
yield ext
429
# qhull
430
ext = Extension(
432
define_macros=[("MPL_DEVNULL", os.devnull)])
433
add_numpy_flags(ext)
435
yield ext
436
# tkagg
437
ext = Extension(
438
"matplotlib.backends._tkagg", [
439
"src/_tkagg.cpp",
440
],
441
include_dirs=["src"],
442
# psapi library needed for finding Tcl/Tk at run time.
443
libraries={"linux": ["dl"], "win32": ["comctl32", "psapi"],
444
"cygwin": ["comctl32", "psapi"]}.get(sys.platform, []),
445
extra_link_args={"win32": ["-mwindows"]}.get(sys.platform, []))
446
add_numpy_flags(ext)
447
add_libagg_flags(ext)
448
yield ext
449
# tri
450
ext = Extension(
451
"matplotlib._tri", [
452
"src/tri/_tri.cpp",
453
"src/tri/_tri_wrapper.cpp",
454
])
455
add_numpy_flags(ext)
456
yield ext
457
# ttconv
458
ext = Extension(
460
"src/_ttconv.cpp",
461
"extern/ttconv/pprdrv_tt.cpp",
462
"extern/ttconv/pprdrv_tt2.cpp",
463
"extern/ttconv/ttutil.cpp",
464
],
465
include_dirs=["extern"])
466
add_numpy_flags(ext)
467
yield ext
468
469
470
class Tests(OptionalPackage):
471
name = "tests"
476
'matplotlib': [
477
*_pkg_data_helper('matplotlib', 'tests/baseline_images'),
478
*_pkg_data_helper('matplotlib', 'tests/tinypages'),
481
],
482
'mpl_toolkits': [
483
*_pkg_data_helper('mpl_toolkits', 'tests/baseline_images'),
488
def add_numpy_flags(ext):
489
import numpy as np
490
ext.include_dirs.append(np.get_include())
491
ext.define_macros.extend([
492
# Ensure that PY_ARRAY_UNIQUE_SYMBOL is uniquely defined for each
493
# extension.
494
('PY_ARRAY_UNIQUE_SYMBOL',
495
'MPL_' + ext.name.replace('.', '_') + '_ARRAY_API'),
496
('NPY_NO_DEPRECATED_API', 'NPY_1_7_API_VERSION'),
497
# Allow NumPy's printf format specifiers in C++.
498
('__STDC_FORMAT_MACROS', 1),
499
])
502
def add_libagg_flags(ext):
503
# We need a patched Agg not available elsewhere, so always use the vendored
504
# version.
505
ext.include_dirs.insert(0, "extern/agg24-svn/include")
506
507
508
def add_libagg_flags_and_sources(ext):
509
# We need a patched Agg not available elsewhere, so always use the vendored
510
# version.
511
ext.include_dirs.insert(0, "extern/agg24-svn/include")
512
agg_sources = [
513
"agg_bezier_arc.cpp",
514
"agg_curves.cpp",
515
"agg_image_filters.cpp",
516
"agg_trans_affine.cpp",
517
"agg_vcgen_contour.cpp",
518
"agg_vcgen_dash.cpp",
519
"agg_vcgen_stroke.cpp",
520
"agg_vpgen_segmentator.cpp",
521
]
522
ext.sources.extend(
523
os.path.join("extern", "agg24-svn", "src", x) for x in agg_sources)
524
525
526
# First compile checkdep_freetype2.c, which aborts the compilation either
527
# with "foo.h: No such file or directory" if the header is not found, or an
528
# appropriate error message if the header indicates a too-old version.
531
class FreeType(SetupPackage):
532
name = "freetype"
533
534
@classmethod
535
def add_flags(cls, ext):
536
ext.sources.insert(0, 'src/checkdep_freetype2.c')
537
if options.get('system_freetype'):
538
pkg_config_setup_extension(
539
# FreeType 2.3 has libtool version 9.11.3 as can be checked
540
# from the tarball. For FreeType>=2.4, there is a conversion
541
# table in docs/VERSIONS.txt in the FreeType source tree.
542
ext, 'freetype2',
543
atleast_version='9.11.3',
544
alt_exec=['freetype-config'],
545
default_libraries=['freetype'])
546
ext.define_macros.append(('FREETYPE_BUILD_TYPE', 'system'))
547
else:
548
src_path = Path('build', f'freetype-{LOCAL_FREETYPE_VERSION}')
549
# Statically link to the locally-built freetype.
550
# This is certainly broken on Windows.
551
ext.include_dirs.insert(0, str(src_path / 'include'))
552
if sys.platform == 'win32':
553
libfreetype = 'libfreetype.lib'
554
else:
555
libfreetype = 'libfreetype.a'
557
0, str(src_path / 'objs' / '.libs' / libfreetype))
558
ext.define_macros.append(('FREETYPE_BUILD_TYPE', 'local'))
559
565
tarball = f'freetype-{LOCAL_FREETYPE_VERSION}.tar.gz'
566
src_path = get_and_extract_tarball(
567
urls=[
568
(f'https://downloads.sourceforge.net/project/freetype'
569
f'/freetype2/{LOCAL_FREETYPE_VERSION}/{tarball}'),
570
(f'https://download.savannah.gnu.org/releases/freetype'
571
f'/{tarball}')
572
],
573
sha=LOCAL_FREETYPE_HASH,
574
dirname=f'freetype-{LOCAL_FREETYPE_VERSION}',
575
)
577
if sys.platform == 'win32':
578
libfreetype = 'libfreetype.lib'
579
else:
580
libfreetype = 'libfreetype.a'
581
if (src_path / 'objs' / '.libs' / libfreetype).is_file():
582
return # Bail out because we have already built FreeType.
584
print(f"Building freetype in {src_path}")
585
if sys.platform != 'win32': # compilation on non-windows
586
env = {**env, "CFLAGS": "{} -fPIC".format(env.get("CFLAGS", ""))}
589
"--with-png=no", "--with-harfbuzz=no", "--enable-static",
590
"--disable-shared"],
592
if 'GNUMAKE' in env:
593
make = env['GNUMAKE']
594
elif 'MAKE' in env:
595
make = env['MAKE']
596
else:
597
try:
598
output = subprocess.check_output(['make', '-v'],
599
stderr=subprocess.DEVNULL)
600
except subprocess.CalledProcessError:
601
output = b''
602
if b'GNU' not in output and b'makepp' not in output:
603
make = 'gmake'
604
else:
605
make = 'make'
606
subprocess.check_call([make], env=env, cwd=src_path)
607
else: # compilation on windows
608
shutil.rmtree(src_path / "objs", ignore_errors=True)
609
msbuild_platform = (
610
'x64' if platform.architecture()[0] == '64bit' else 'Win32')
611
base_path = Path("build/freetype-2.6.1/builds/windows")
613
sln_path = (
614
base_path / vc / "freetype.sln"
615
)
616
# https://developercommunity.visualstudio.com/comments/190992/view.html
617
(sln_path.parent / "Directory.Build.props").write_text("""
618
<Project>
619
<PropertyGroup>
620
<!-- The following line *cannot* be split over multiple lines. -->
621
<WindowsTargetPlatformVersion>$([Microsoft.Build.Utilities.ToolLocationHelper]::GetLatestSDKTargetPlatformVersion('Windows', '10.0'))</WindowsTargetPlatformVersion>
622
</PropertyGroup>
623
</Project>
625
# It is not a trivial task to determine PlatformToolset to plug it
626
# into msbuild command, and Directory.Build.props will not override
627
# the value in the project file.
628
# The DefaultPlatformToolset is from Microsoft.Cpp.Default.props
629
with open(base_path / vc / "freetype.vcxproj", 'r+b') as f:
630
toolset_repl = b'PlatformToolset>$(DefaultPlatformToolset)<'
631
vcxproj = f.read().replace(b'PlatformToolset>v100<',
632
toolset_repl)
633
assert toolset_repl in vcxproj, (
634
'Upgrading Freetype might break this')
635
f.seek(0)
636
f.truncate()
637
f.write(vcxproj)
638
640
cc.initialize() # Get msbuild in the %PATH% of cc.spawn.
641
cc.spawn(["msbuild", str(sln_path),
642
"/t:Clean;Build",
643
f"/p:Configuration=Release;Platform={msbuild_platform}"])
647
lib_path, = (src_path / "objs" / vc / msbuild_platform).glob(
648
"freetype*.lib")
649
shutil.copy2(lib_path, src_path / "objs/.libs/libfreetype.lib")
652
class Qhull(SetupPackage):
653
name = "qhull"
654
_extensions_to_update = []
655
656
@classmethod
657
def add_flags(cls, ext):
658
if options.get("system_qhull"):
659
ext.libraries.append("qhull_r")
660
else:
661
cls._extensions_to_update.append(ext)
662
663
def do_custom_build(self, env):
664
if options.get('system_qhull'):
665
return
666
667
toplevel = get_and_extract_tarball(
668
urls=["http://www.qhull.org/download/qhull-2020-src-8.0.2.tgz"],
669
sha="b5c2d7eb833278881b952c8a52d20179eab87766b00b865000469a45c1838b7e",
670
dirname=f"qhull-{LOCAL_QHULL_VERSION}",
671
)
672
shutil.copyfile(toplevel / "COPYING.txt", "LICENSE/LICENSE_QHULL")
673
674
for ext in self._extensions_to_update:
675
qhull_path = Path(f'build/qhull-{LOCAL_QHULL_VERSION}/src')
676
ext.include_dirs.insert(0, str(qhull_path))
677
ext.sources.extend(map(str, sorted(qhull_path.glob('libqhull_r/*.c'))))
678
if sysconfig.get_config_var("LIBM") == "-lm":
679
ext.libraries.extend("m")
680
681
682
class BackendMacOSX(OptionalPackage):
683
config_category = 'gui_support'
691
def get_extensions(self):
695
ext = Extension('matplotlib.backends._macosx', sources)
696
ext.extra_compile_args.extend(['-Werror=unguarded-availability'])
697
ext.extra_link_args.extend(['-framework', 'Cocoa'])
698
if platform.python_implementation().lower() == 'pypy':
699
ext.extra_compile_args.append('-DPYPY=1')