8000 In the build, declare all (compulsory) extension modules together. · matplotlib/matplotlib@b9bc3e6 · GitHub
[go: up one dir, main page]

Skip to content

Commit b9bc3e6

Browse files
committed
In the build, declare all (compulsory) extension modules together.
... in a single function. Splitting them over multiple classes doesn't really buy much. Also convert the LibAgg and Qhull classes to toplevel functions, as they play a role similar to add_numpy_flags.
1 parent b88d6b5 commit b9bc3e6

File tree

2 files changed

+130
-186
lines changed

2 files changed

+130
-186
lines changed

setup.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,7 @@
5757
setupext.Matplotlib(),
5858
setupext.Python(),
5959
setupext.Platform(),
60-
setupext.LibAgg(),
6160
setupext.FreeType(),
62-
setupext.FT2Font(),
63-
setupext.Png(),
64-
setupext.Qhull(),
65-
setupext.Image(),
66-
setupext.TTConv(),
67-
setupext.Path(),
68-
setupext.Contour(),
69-
setupext.QhullWrap(),
70-
setupext.Tri(),
7161
'Optional subpackages',
7262
setupext.SampleData(),
7363
setupext.Tests(),
@@ -98,8 +88,11 @@ def __init__(self, dist):
9888

9989
class BuildExtraLibraries(BuildExtCommand):
10090
def finalize_options(self):
101-
self.distribution.ext_modules[:] = filter(
102-
None, (package.get_extension() for package in good_packages))
91+
self.distribution.ext_modules[:] = [
92+
ext
93+
for package in good_packages
94+
for ext in package.get_extensions()
95+
]
10396
super().finalize_options()
10497

10598
def build_extensions(self):

setupext.py

Lines changed: 125 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -305,13 +305,13 @@ def get_package_data(self):
305305
"""
306306
return {}
307307

308-
def get_extension(self):
308+
def get_extensions(self):
309309
"""
310-
Get a list of C extensions (`distutils.core.Extension`
310+
Return or yield a list of C extensions (`distutils.core.Extension`
311311
objects) to add to the configuration. These are added to the
312312
`extensions` list passed to `distutils.setup`.
313313
"""
314-
return None
314+
return []
315315

316316
def do_custom_build(self):
317317
"""
@@ -411,6 +411,88 @@ def get_package_data(self):
411411
],
412412
}
413413

414+
def get_extensions(self):
415+
# contour
416+
ext = Extension('matplotlib._contour', [
417+
"src/_contour.cpp",
418+
"src/_contour_wrapper.cpp",
419+
'src/py_converters.cpp',
420+
])
421+
add_numpy_flags(ext)
422+
add_libagg_flags(ext)
423+
yield ext
424+
# ft2font
425+
ext = Extension('matplotlib.ft2font', [
426+
'src/ft2font.cpp',
427+
'src/ft2font_wrapper.cpp',
428+
'src/mplutils.cpp',
429+
'src/py_converters.cpp',
430+
])
431+
FreeType().add_flags(ext)
432+
add_numpy_flags(ext)
433+
add_libagg_flags(ext)
434+
yield ext
435+
# image
436+
ext = Extension('matplotlib._image', [
437+
'src/_image.cpp',
438+
'src/mplutils.cpp',
439+
'src/_image_wrapper.cpp',
440+
'src/py_converters.cpp'
441+
])
442+
add_numpy_flags(ext)
443+
add_libagg_flags_and_sources(ext)
444+
yield ext
445+
# path
446+
ext = Extension('matplotlib._path', [
447+
'src/py_converters.cpp',
448+
'src/_path_wrapper.cpp'
449+
])
450+
add_numpy_flags(ext)
451+
add_libagg_flags_and_sources(ext)
452+
yield ext
453+
# png
454+
ext = Extension('matplotlib._png', [
455+
'src/checkdep_libpng.c',
456+
'src/_png.cpp',
457+
'src/mplutils.cpp',
458+
])
459+
pkg_config_setu F438 p_extension(
460+
ext, 'libpng',
461+
atleast_version='1.2',
462+
alt_exec=['libpng-config', '--ldflags'],
463+
default_libraries=(
464+
['png', 'z'] if os.name == 'posix' else
465+
# libpng upstream names their lib libpng16.lib, not png.lib.
466+
# zlib upstream names their lib zlib.lib, not z.lib.
467+
['libpng16', 'zlib'] if os.name == 'nt' else
468+
[]))
469+
add_numpy_flags(ext)
470+
yield ext
471+
# qhull
472+
ext = Extension('matplotlib._qhull', ['src/qhull_wrap.c'],
473+
define_macros=[('MPL_DEVNULL', os.devnull)])
474+
add_numpy_flags(ext)
475+
add_qhull_flags(ext)
476+
yield ext
477+
# tri
478+
ext = Extension('matplotlib._tri', [
479+
"src/tri/_tri.cpp",
480+
"src/tri/_tri_wrapper.cpp",
481+
"src/mplutils.cpp"
482+
])
483+
add_numpy_flags(ext)
484+
yield ext
485+
# ttconv
486+
ext = Extension('matplotlib.ttconv', [
487+
'src/_ttconv.cpp',
488+
'extern/ttconv/pprdrv_tt.cpp',
489+
'extern/ttconv/pprdrv_tt2.cpp',
490+
'extern/ttconv/ttutil.cpp'
491+
])
492+
add_numpy_flags(ext)
493+
ext.include_dirs.insert(0, 'extern')
494+
yield ext
495+
414496

415497
class SampleData(OptionalPackage):
416498
"""
@@ -459,26 +541,38 @@ def add_numpy_flags(ext):
459541
])
460542

461543

462-
class LibAgg(SetupPackage):
463-
name = 'libagg'
464-
465-
def add_flags(self, ext, add_sources=True):
466-
# We need a patched Agg not available elsewhere, so always use the
467-
# vendored version.
468-
ext.include_dirs.insert(0, 'extern/agg24-svn/include')
469-
if add_sources:
470-
agg_sources = [
471-
'agg_bezier_arc.cpp',
472-
'agg_curves.cpp',
473-
'agg_image_filters.cpp',
474-
'agg_trans_affine.cpp',
475-
'agg_vcgen_contour.cpp',
476-
'agg_vcgen_dash.cpp',
477-
'agg_vcgen_stroke.cpp',
478-
'agg_vpgen_segmentator.cpp'
479-
]
480-
ext.sources.extend(os.path.join('extern', 'agg24-svn', 'src', x)
481-
for x in agg_sources)
544+
def add_libagg_flags(ext):
545+
# We need a patched Agg not available elsewhere, so always use the vendored
546+
# version.
547+
ext.include_dirs.insert(0, 'extern/agg24-svn/include')
548+
549+
550+
def add_libagg_flags_and_sources(ext):
551+
# We need a patched Agg not available elsewhere, so always use the vendored
552+
# version.
553+
ext.include_dirs.insert(0, 'extern/agg24-svn/include')
554+
agg_sources = [
555+
'agg_bezier_arc.cpp',
556+
'agg_curves.cpp',
557+
'agg_image_filters.cpp',
558+
'agg_trans_affine.cpp',
559+
'agg_vcgen_contour.cpp',
560+
'agg_vcgen_dash.cpp',
561+
'agg_vcgen_stroke.cpp',
562+
'agg_vpgen_segmentator.cpp',
563+
]
564+
ext.sources.extend(
565+
os.path.join('extern', 'agg24-svn', 'src', x) for x in agg_sources)
566+
567+
568+
def add_qhull_flags(ext):
569+
# Qhull doesn't distribute pkg-config info, so we have no way of knowing
570+
# whether a system install is recent enough. Thus, always use the vendored
571+
# version.
572+
ext.include_dirs.insert(0, 'extern')
573+
ext.sources.extend(sorted(glob.glob('extern/libqhull/*.c')))
574+
if sysconfig.get_config_var('LIBM') == '-lm':
575+
ext.libraries.extend('m')
482576

483577

484578
# For FreeType2 and libpng, we add a separate checkdep_foo.c source to at the
@@ -619,154 +713,11 @@ def do_custom_build(self):
619713
str(pathlib.Path(src_path, "objs/.libs/libfreetype.lib")))
620714

621715

622-
class FT2Font(SetupPackage):
623-
name = 'ft2font'
624-
625-
def get_extension(self):
626-
sources = [
627-
'src/ft2font.cpp',
628-
'src/ft2font_wrapper.cpp',
629-
'src/mplutils.cpp',
630-
'src/py_converters.cpp',
631-
]
632-
ext = Extension('matplotlib.ft2font', sources)
633-
FreeType().add_flags(ext)
634-
add_numpy_flags(ext)
635-
LibAgg().add_flags(ext, add_sources=False)
636-
return ext
637-
638-
639-
class Png(SetupPackage):
640-
name = "png"
641-
642-
def get_extension(self):
643-
sources = [
644-
'src/checkdep_libpng.c',
645-
'src/_png.cpp',
646-
'src/mplutils.cpp',
647-
]
648-
ext = Extension('matplotlib._png', sources)
649-
pkg_config_setup_extension(
650-
ext, 'libpng',
651-
atleast_version='1.2',
652-
alt_exec=['libpng-config', '--ldflags'],
653-
default_libraries=(
654-
['png', 'z'] if os.name == 'posix' else
655-
# libpng upstream names their lib libpng16.lib, not png.lib.
656-
# zlib upstream names their lib zlib.lib, not z.lib.
657-
['libpng16', 'zlib'] if os.name == 'nt' else
658-
[]
659-
))
660-
add_numpy_flags(ext)
661-
return ext
662-
663-
664-
class Qhull(SetupPackage):
665-
name = "qhull"
666-
667-
def add_flags(self, ext):
668-
# Qhull doesn't distribute pkg-config info, so we have no way of
669-
# knowing whether a system install is recent enough. Thus, always use
670-
# the vendored version.
671-
ext.include_dirs.insert(0, 'extern')
672-
ext.sources.extend(sorted(glob.glob('extern/libqhull/*.c')))
673-
if sysconfig.get_config_var('LIBM') == '-lm':
674-
ext.libraries.extend('m')
675-
676-
677-
class TTConv(SetupPackage):
678-
name = "ttconv"
679-
680-
def get_extension(self):
681-
sources = [
682-
'src/_ttconv.cpp',
683-
'extern/ttconv/pprdrv_tt.cpp',
684-
'extern/ttconv/pprdrv_tt2.cpp',
685-
'extern/ttconv/ttutil.cpp'
686-
]
687-
ext = Extension('matplotlib.ttconv', sources)
688-
add_numpy_flags(ext)
689-
ext.include_dirs.insert(0, 'extern')
690-
return ext
691-
692-
693-
class Path(SetupPackage):
694-
name = "path"
695-
696-
def get_extension(self):
697-
sources = [
698-
'src/py_converters.cpp',
699-
'src/_path_wrapper.cpp'
700-
]
701-
ext = Extension('matplotlib._path', sources)
702-
add_numpy_flags(ext)
703-
LibAgg().add_flags(ext)
704-
return ext
705-
706-
707-
class Image(SetupPackage):
708-
name = "image"
709-
710-
def get_extension(self):
711-
sources = [
712-
'src/_image.cpp',
713-
'src/mplutils.cpp',
714-
'src/_image_wrapper.cpp',
715-
'src/py_converters.cpp'
716-
]
717-
ext = Extension('matplotlib._image', sources)
718-
add_numpy_flags(ext)
719-
LibAgg().add_flags(ext)
720-
721-
return ext
722-
723-
724-
class Contour(SetupPackage):
725-
name = "contour"
726-
727-
def get_extension(self):
728-
sources = [
729-
"src/_contour.cpp",
730-
"src/_contour_wrapper.cpp",
731-
'src/py_converters.cpp',
732-
]
733-
ext = Extension('matplotlib._contour', sources)
734-
add_numpy_flags(ext)
735-
LibAgg().add_flags(ext, add_sources=False)
736-
return ext
737-
738-
739-
class QhullWrap(SetupPackage):
740-
name = "qhull_wrap"
741-
742-
def get_extension(self):
743-
sources = ['src/qhull_wrap.c']
744-
ext = Extension('matplotlib._qhull', sources,
745-
define_macros=[('MPL_DEVNULL', os.devnull)])
746-
add_numpy_flags(ext)
747-
Qhull().add_flags(ext)
748-
return ext
749-
750-
751-
class Tri(SetupPackage):
752-
name = "tri"
753-
754-
def get_extension(self):
755-
sources = [
756-
"src/tri/_tri.cpp",
757-
"src/tri/_tri_wrapper.cpp",
758-
"src/mplutils.cpp"
759-
]
760-
ext = Extension('matplotlib._tri', sources)
761-
add_numpy_flags(ext)
762-
return ext
763-
764-
765716
class BackendAgg(OptionalBackendPackage):
766717
name = "agg"
767718
force = True
768719

769-
def get_extension(self):
720+
def get_extensions(self):
770721
sources = [
771722
"src/mplutils.cpp",
772723
"src/py_converters.cpp",
@@ -775,9 +726,9 @@ def get_extension(self):
775726
]
776727
ext = Extension('matplotlib.backends._backend_agg', sources)
777728
add_numpy_flags(ext)
778-
LibAgg().add_flags(ext)
729+
add_libagg_flags_and_sources(ext)
779730
FreeType().add_flags(ext)
780-
return ext
731+
yield ext
781732

782733

783734
class BackendTkAgg(OptionalBackendPackage):
@@ -787,7 +738,7 @@ class BackendTkAgg(OptionalBackendPackage):
787738
def check(self):
788739
return "installing; run-time loading from Python Tcl/Tk"
789740

790-
def get_extension(self):
741+
def get_extensions(self):
791742
sources = [
792743
'src/_tkagg.cpp',
793744
'src/py_converters.cpp',
@@ -796,8 +747,8 @@ def get_extension(self):
796747
ext = Extension('matplotlib.backends._tkagg', sources)
797748
self.add_flags(ext)
798749
add_numpy_flags(ext)
799-
LibAgg().add_flags(ext, add_sources=False)
800-
return ext
750+
add_libagg_flags(ext)
751+
yield ext
801752

802753
def add_flags(self, ext):
803754
ext.include_dirs.insert(0, 'src')
@@ -818,12 +769,12 @@ def check(self):
818769
raise CheckFailed("Mac OS-X only")
819770
return super().check()
820771

821-
def get_extension(self):
772+
def get_extensions(self):
822773
sources = [
823774
'src/_macosx.m'
824775
]
825776
ext = Extension('matplotlib.backends._macosx', sources)
826777
ext.extra_link_args.extend(['-framework', 'Cocoa'])
827778
if platform.python_implementation().lower() == 'pypy':
828779
ext.extra_compile_args.append('-DPYPY=1')
829-
return ext
780+
yield ext

0 commit comments

Comments
 (0)
0