Skip to content

Commit b534081

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 45b9201 commit b534081

File tree

2 files changed

+112
-160
lines changed

2 files changed

+112
-160
lines changed

setup.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,7 @@
5555
setupext.Matplotlib(),
5656
setupext.Python(),
5757
setupext.Platform(),
58-
setupext.LibAgg(),
5958
setupext.FreeType(),
60-
setupext.FT2Font(),
61-
setupext.Qhull(),
62-
setupext.Image(),
63-
setupext.TTConv(),
64-
setupext.Path(),
65-
setupext.Contour(),
66-
setupext.QhullWrap(),
67-
setupext.Tri(),
6859
setupext.SampleData(),
6960
setupext.Tests(),
7061
setupext.BackendAgg(),
@@ -93,8 +84,11 @@ def __init__(self, dist):
9384

9485
class BuildExtraLibraries(BuildExtCommand):
9586
def finalize_options(self):
96-
self.distribution.ext_modules[:] = filter(
97-
None, (package.get_extension() for package in good_packages))
87+
self.distribution.ext_modules[:] = [
88+
ext
89+
for package in good_packages
90+
for ext in package.get_extensions()
91+
]
9892
super().finalize_options()
9993

10094
def build_extensions(self):

setupext.py

Lines changed: 107 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,13 @@ def get_package_data(self):
314314
"""
315315
return {}
316316

317-
def get_extension(self):
317+
def get_extensions(self):
318318
"""
319-
Get a list of C extensions (`distutils.core.Extension`
319+
Return or yield a list of C extensions (`distutils.core.Extension`
320320
objects) to add to the configuration. These are added to the
321321
`extensions` list passed to `distutils.setup`.
322322
"""
323-
return None
323+
return []
324324

325325
def do_custom_build(self):
326326
"""
@@ -420,6 +420,70 @@ def get_package_data(self):
420420
],
421421
}
422422

423+
def get_extensions(self):
424+
# contour
425+
ext = Extension('matplotlib._contour', [
426+
"src/_contour.cpp",
427+
"src/_contour_wrapper.cpp",
428+
'src/py_converters.cpp',
429+
])
430+
add_numpy_flags(ext)
431+
add_libagg_flags(ext)
432+
yield ext
433+
# ft2font
434+
ext = Extension('matplotlib.ft2font', [
435+
'src/ft2font.cpp',
436+
'src/ft2font_wrapper.cpp',
437+
'src/mplutils.cpp',
438+
'src/py_converters.cpp',
439+
])
440+
FreeType().add_flags(ext)
441+
add_numpy_flags(ext)
442+
add_libagg_flags(ext)
443+
yield ext
444+
# image
445+
ext = Extension('matplotlib._image', [
446+
'src/_image.cpp',
447+
'src/mplutils.cpp',
448+
'src/_image_wrapper.cpp',
449+
'src/py_converters.cpp'
450+
])
451+
add_numpy_flags(ext)
452+
add_libagg_flags_and_sources(ext)
453+
yield ext
454+
# path
455+
ext = Extension('matplotlib._path', [
456+
'src/py_converters.cpp',
457+
'src/_path_wrapper.cpp'
458+
])
459+
add_numpy_flags(ext)
460+
add_libagg_flags_and_sources(ext)
461+
yield ext
462+
# qhull
463+
ext = Extension('matplotlib._qhull', ['src/qhull_wrap.c'],
464+
define_macros=[('MPL_DEVNULL', os.devnull)])
465+
add_numpy_flags(ext)
466+
add_qhull_flags(ext)
467+
yield ext
468+
# tri
469+
ext = Extension('matplotlib._tri', [
470+
"src/tri/_tri.cpp",
471+
"src/tri/_tri_wrapper.cpp",
472+
"src/mplutils.cpp"
473+
])
474+
add_numpy_flags(ext)
475+
yield ext
476+
# ttconv
477+
ext = Extension('matplotlib.ttconv', [
478+
'src/_ttconv.cpp',
479+
'extern/ttconv/pprdrv_tt.cpp',
480+
'extern/ttconv/pprdrv_tt2.cpp',
481+
'extern/ttconv/ttutil.cpp'
482+
])
483+
add_numpy_flags(ext)
484+
ext.include_dirs.insert(0, 'extern')
485+
yield ext
486+
423487

424488
class SampleData(OptionalPackage):
425489
"""
@@ -468,26 +532,38 @@ def add_numpy_flags(ext):
468532
])
469533

470534

471-
class LibAgg(SetupPackage):
472-
name = 'libagg'
473-
474-
def add_flags(self, ext, add_sources=True):
475-
# We need a patched Agg not available elsewhere, so always use the
476-
# vendored version.
477-
ext.include_dirs.insert(0, 'extern/agg24-svn/include')
478-
if add_sources:
479-
agg_sources = [
480-
'agg_bezier_arc.cpp',
481-
'agg_curves.cpp',
482-
'agg_image_filters.cpp',
483-
'agg_trans_affine.cpp',
484-
'agg_vcgen_contour.cpp',
485-
'agg_vcgen_dash.cpp',
486-
'agg_vcgen_stroke.cpp',
487-
'agg_vpgen_segmentator.cpp'
488-
]
489-
ext.sources.extend(os.path.join('extern', 'agg24-svn', 'src', x)
490-
for x in agg_sources)
535+
def add_libagg_flags(ext):
536+
# We need a patched Agg not available elsewhere, so always use the vendored
537+
# version.
538+
ext.include_dirs.insert(0, 'extern/agg24-svn/include')
539+
540+
541+
def add_libagg_flags_and_sources(ext):
542+
# We need a patched Agg not available elsewhere, so always use the vendored
543+
# version.
544+
ext.include_dirs.insert(0, 'extern/agg24-svn/include')
545+
agg_sources = [
546+
'agg_bezier_arc.cpp',
547+
'agg_curves.cpp',
548+
'agg_image_filters.cpp',
549+
'agg_trans_affine.cpp',
550+
'agg_vcgen_contour.cpp',
551+
'agg_vcgen_dash.cpp',
552+
'agg_vcgen_stroke.cpp',
553+
'agg_vpgen_segmentator.cpp',
554+
]
555+
ext.sources.extend(
556+
os.path.join('extern', 'agg24-svn', 'src', x) for x in agg_sources)
557+
558+
559+
def add_qhull_flags(ext):
560+
# Qhull doesn't distribute pkg-config info, so we have no way of knowing
561+
# whether a system install is recent enough. Thus, always use the vendored
562+
# version.
563+
ext.include_dirs.insert(0, 'extern')
564+
ext.sources.extend(sorted(glob.glob('extern/libqhull/*.c')))
565+
if sysconfig.get_config_var('LIBM') == '-lm':
566+
ext.libraries.extend('m')
491567

492568

493569
# First compile checkdep_freetype2.c, which aborts the compilation either
@@ -621,129 +697,11 @@ def do_custom_build(self):
621697
shutil.copy2(lib_path, src_path / "objs/.libs/libfreetype.lib")
622698

623699

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

746-
def get_extension(self):
704+
def get_extensions(self):
747705
sources = [
748706
"src/mplutils.cpp",
749707
"src/py_converters.cpp",
@@ -752,9 +710,9 @@ def get_extension(self):
752710
]
753711
ext = Extension('matplotlib.backends._backend_agg', sources)
754712
add_numpy_flags(ext)
755-
LibAgg().add_flags(ext)
713+
add_libagg_flags_and_sources(ext)
756714
FreeType().add_flags(ext)
757-
return ext
715+
yield ext
758716

759717

760718
class BackendTkAgg(OptionalBackendPackage):
@@ -764,7 +722,7 @@ class BackendTkAgg(OptionalBackendPackage):
764722
def check(self):
765723
return "installing; run-time loading from Python Tcl/Tk"
766724

767-
def get_extension(self):
725+
def get_extensions(self):
768726
sources = [
769727
'src/_tkagg.cpp',
770728
'src/py_converters.cpp',
@@ -773,8 +731,8 @@ def get_extension(self):
773731
ext = Extension('matplotlib.backends._tkagg', sources)
774732
self.add_flags(ext)
775733
add_numpy_flags(ext)
776-
LibAgg().add_flags(ext, add_sources=False)
777-
return ext
734+
add_libagg_flags(ext)
735+
yield ext
778736

779737
def add_flags(self, ext):
780738
ext.include_dirs.insert(0, 'src')
@@ -795,12 +753,12 @@ def check(self):
795753
raise CheckFailed("Mac OS-X only")
796754
return super().check()
797755

798-
def get_extension(self):
756+
def get_extensions(self):
799757
sources = [
800758
'src/_macosx.m'
801759
]
802760
ext = Extension('matplotlib.backends._macosx', sources)
803761
ext.extra_link_args.extend(['-framework', 'Cocoa'])
804762
if platform.python_implementation().lower() == 'pypy':
805763
ext.extra_compile_args.append('-DPYPY=1')
806-
return ext
764+
yield ext

0 commit comments

Comments
 (0)