Skip to content

Commit 72b247e

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 66628cc commit 72b247e

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
@@ -54,17 +54,7 @@
5454
setupext.Matplotlib(),
5555
setupext.Python(),
5656
setupext.Platform(),
57-
setupext.LibAgg(),
5857
setupext.FreeType(),
59-
setupext.FT2Font(),
60-
setupext.Png(),
61-
setupext.Qhull(),
62-
setupext.Image(),
63-
setupext.TTConv(),
64-
setupext.Path(),
65-
setupext.Contour(),
66-
setupext.QhullWrap(),
67-
setupext.Tri(),
6858
setupext.SampleData(),
6959
setupext.Tests(),
7060
setupext.BackendAgg(),
@@ -93,8 +83,11 @@ def __init__(self, dist):
9383

9484
class BuildExtraLibraries(BuildExtCommand):
9585
def finalize_options(self):
96-
self.distribution.ext_modules[:] = filter(
97-
None, (package.get_extension() for package in good_packages))
86+
self.distribution.ext_modules[:] = [
87+
ext
88+
for package in good_packages
89+
for ext in package.get_extensions()
90+
]
9891
super().finalize_options()
9992

10093
def build_extensions(self):

setupext.py

Lines changed: 125 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,13 @@ def get_package_data(self):
294294
"""
295295
return {}
296296

297-
def get_extension(self):
297+
def get_extensions(self):
298298
"""
299-
Get a list of C extensions (`distutils.core.Extension`
299+
Return or yield a list of C extensions (`distutils.core.Extension`
300300
objects) to add to the configuration. These are added to the
301301
`extensions` list passed to `distutils.setup`.
302302
"""
303-
return None
303+
return []
304304

305305
def do_custom_build(self):
306306
"""
@@ -400,6 +400,88 @@ def get_package_data(self):
400400
],
401401
}
402402

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

404486
class SampleData(OptionalPackage):
405487
"""
@@ -448,26 +530,38 @@ def add_numpy_flags(ext):
448530
])
449531

450532

451-
class LibAgg(SetupPackage):
452-
name = 'libagg'
453-
454-
def add_flags(self, ext, add_sources=True):
455-
# We need a patched Agg not available elsewhere, so always use the
456-
# vendored version.
457-
ext.include_dirs.insert(0, 'extern/agg24-svn/include')
458-
if add_sources:
459-
agg_sources = [
460-
'agg_bezier_arc.cpp',
461-
'agg_curves.cpp',
462-
'agg_image_filters.cpp',
463-
'agg_trans_affine.cpp',
464-
'agg_vcgen_contour.cpp',
465-
'agg_vcgen_dash.cpp',
466-
'agg_vcgen_stroke.cpp',
467-
'agg_vpgen_segmentator.cpp'
468-
]
469-
ext.sources.extend(os.path.join('extern', 'agg24-svn', 'src', x)
470-
for x in agg_sources)
533+
def add_libagg_flags(ext):
534+
# We need a patched Agg not available elsewhere, so always use the vendored
535+
# version.
536+
ext.include_dirs.insert(0, 'extern/agg24-svn/include')
537+
538+
539+
def add_libagg_flags_and_sources(ext):
540+
# We need a patched Agg not available elsewhere, so always use the vendored
541+
# version.
542+
ext.include_dirs.insert(0, 'extern/agg24-svn/include')
543+
agg_sources = [
544+
'agg_bezier_arc.cpp',
545+
'agg_curves.cpp',
546+
'agg_image_filters.cpp',
547+
'agg_trans_affine.cpp',
548+
'agg_vcgen_contour.cpp',
549+
'agg_vcgen_dash.cpp',
550+
'agg_vcgen_stroke.cpp',
551+
'agg_vpgen_segmentator.cpp',
552+
]
553+
ext.sources.extend(
554+
os.path.join('extern', 'agg24-svn', 'src', x) for x in agg_sources)
555+
556+
557+
def add_qhull_flags(ext):
558+
# Qhull doesn't distribute pkg-config info, so we have no way of knowing
559+
# whether a system install is recent enough. Thus, always use the vendored
560+
# version.
561+
ext.include_dirs.insert(0, 'extern')
562+
ext.sources.extend(sorted(glob.glob('extern/libqhull/*.c')))
563+
if sysconfig.get_config_var('LIBM') == '-lm':
564+
ext.libraries.extend('m')
471565

472566

473567
# For FreeType2 and libpng, we add a separate checkdep_foo.c source to at the
@@ -608,154 +702,11 @@ def do_custom_build(self):
608702
str(pathlib.Path(src_path, "objs/.libs/libfreetype.lib")))
609703

610704

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

758-
def get_extension(self):
709+
def get_extensions(self):
759710
sources = [
760711
"src/mplutils.cpp",
761712
"src/py_converters.cpp",
@@ -764,9 +715,9 @@ def get_extension(self):
764715
]
765716
ext = Extension('matplotlib.backends._backend_agg', sources)
766717
add_numpy_flags(ext)
767-
LibAgg().add_flags(ext)
718+
add_libagg_flags_and_sources(ext)
768719
FreeType().add_flags(ext)
769-
return ext
720+
yield ext
770721

771722

772723
class BackendTkAgg(OptionalBackendPackage):
@@ -776,7 +727,7 @@ class BackendTkAgg(OptionalBackendPackage):
776727
def check(self):
777728
return "installing; run-time loading from Python Tcl/Tk"
778729

779-
def get_extension(self):
730+
def get_extensions(self):
780731
sources = [
781732
'src/_tkagg.cpp',
782733
'src/py_converters.cpp',
@@ -785,8 +736,8 @@ def get_extension(self):
785736
ext = Extension('matplotlib.backends._tkagg', sources)
786737
self.add_flags(ext)
787738
add_numpy_flags(ext)
788-
LibAgg().add_flags(ext, add_sources=False)
789-
return ext
739+
add_libagg_flags(ext)
740+
yield ext
790741

791742
def add_flags(self, ext):
792743
ext.include_dirs.insert(0, 'src')
@@ -807,12 +758,12 @@ def check(self):
807758
raise CheckFailed("Mac OS-X only")
808759
return super().check()
809760

810-
def get_extension(self):
761+
def get_extensions(self):
811762
sources = [
812763
'src/_macosx.m'
813764
]
814765
ext = Extension('matplotlib.backends._macosx', sources)
815766
ext.extra_link_args.extend(['-framework', 'Cocoa'])
816767
if platform.python_implementation().lower() == 'pypy':
817768
ext.extra_compile_args.append('-DPYPY=1')
818-
return ext
769+
yield ext

0 commit comments

Comments
 (0)