Skip to content

Commit d3bed31

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 014aa5b commit d3bed31

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
@@ -283,13 +283,13 @@ def get_package_data(self):
283283
"""
284284
return {}
285285

286-
def get_extension(self):
286+
def get_extensions(self):
287287
"""
288-
Get a list of C extensions (`distutils.core.Extension`
288+
Return or yield a list of C extensions (`distutils.core.Extension`
289289
objects) to add to the configuration. These are added to the
290290
`extensions` list passed to `distutils.setup`.
291291
"""
292-
return None
292+
return []
293293

294294
def do_custom_build(self):
295295
"""
@@ -388,6 +388,70 @@ def get_package_data(self):
388388
],
389389
}
390390

391+
def get_extensions(self):
392+
# contour
393+
ext = Extension('matplotlib._contour', [
394+
"src/_contour.cpp",
395+
"src/_contour_wrapper.cpp",
396+
'src/py_converters.cpp',
397+
])
398+
add_numpy_flags(ext)
399+
add_libagg_flags(ext)
400+
yield ext
401+
# ft2font
402+
ext = Extension('matplotlib.ft2font', [
403+
'src/ft2font.cpp',
404+
'src/ft2font_wrapper.cpp',
405+
'src/mplutils.cpp',
406+
'src/py_converters.cpp',
407+
])
408+
FreeType().add_flags(ext)
409+
add_numpy_flags(ext)
410+
add_libagg_flags(ext)
411+
yield ext
412+
# image
413+
ext = Extension('matplotlib._image', [
414+
'src/_image.cpp',
415+
'src/mplutils.cpp',
416+
'src/_image_wrapper.cpp',
417+
'src/py_converters.cpp'
418+
])
419+
add_numpy_flags(ext)
420+
add_libagg_flags_and_sources(ext)
421+
yield ext
422+
# path
423+
ext = Extension('matplotlib._path', [
424+
'src/py_converters.cpp',
425+
'src/_path_wrapper.cpp'
426+
])
427+
add_numpy_flags(ext)
428+
add_libagg_flags_and_sources(ext)
429+
yield ext
430+
# qhull
431+
ext = Extension('matplotlib._qhull', ['src/qhull_wrap.c'],
432+
define_macros=[('MPL_DEVNULL', os.devnull)])
433+
add_numpy_flags(ext)
434+
add_qhull_flags(ext)
435+
yield ext
436+
# tri
437+
ext = Extension('matplotlib._tri', [
438+
"src/tri/_tri.cpp",
439+
"src/tri/_tri_wrapper.cpp",
440+
"src/mplutils.cpp"
441+
])
442+
add_numpy_flags(ext)
443+
yield ext
444+
# ttconv
445+
ext = Extension('matplotlib.ttconv', [
446+
'src/_ttconv.cpp',
447+
'extern/ttconv/pprdrv_tt.cpp',
448+
'extern/ttconv/pprdrv_tt2.cpp',
449+
'extern/ttconv/ttutil.cpp'
450+
])
451+
add_numpy_flags(ext)
452+
ext.include_dirs.insert(0, 'extern')
453+
yield ext
454+
391455

392456
class SampleData(OptionalPackage):
393457
"""
@@ -436,26 +500,38 @@ def add_numpy_flags(ext):
436500
])
437501

438502

439-
class LibAgg(SetupPackage):
440-
name = 'libagg'
441-
442-
def add_flags(self, ext, add_sources=True):
443-
# We need a patched Agg not available elsewhere, so always use the
444-
# vendored version.
445-
ext.include_dirs.insert(0, 'extern/agg24-svn/include')
446-
if add_sources:
447-
agg_sources = [
448-
'agg_bezier_arc.cpp',
449-
'agg_curves.cpp',
450-
'agg_image_filters.cpp',
451-
'agg_trans_affine.cpp',
452-
'agg_vcgen_contour.cpp',
453-
'agg_vcgen_dash.cpp',
454-
'agg_vcgen_stroke.cpp',
455-
'agg_vpgen_segmentator.cpp'
456-
]
457-
ext.sources.extend(os.path.join('extern', 'agg24-svn', 'src', x)
458-
for x in agg_sources)
503+
def add_libagg_flags(ext):
504+
# We need a patched Agg not available elsewhere, so always use the vendored
505+
# version.
506+
ext.include_dirs.insert(0, 'extern/agg24-svn/include')
507+
508+
509+
def add_libagg_flags_and_sources(ext):
510+
# We need a patched Agg not available elsewhere, so always use the vendored
511+
# version.
512+
ext.include_dirs.insert(0, 'extern/agg24-svn/include')
513+
agg_sources = [
514+
'agg_bezier_arc.cpp',
515+
'agg_curves.cpp',
516+
'agg_image_filters.cpp',
517+
'agg_trans_affine.cpp',
518+
'agg_vcgen_contour.cpp',
519+
'agg_vcgen_dash.cpp',
520+
'agg_vcgen_stroke.cpp',
521+
'agg_vpgen_segmentator.cpp',
522+
]
523+
ext.sources.extend(
524+
os.path.join('extern', 'agg24-svn', 'src', x) for x in agg_sources)
525+
526+
527+
def add_qhull_flags(ext):
528+
if options.get('system_qhull'):
529+
ext.libraries.append('qhull')
530+
else:
531+
ext.include_dirs.insert(0, 'extern')
532+
ext.sources.extend(sorted(glob.glob('extern/libqhull/*.c')))
533+
if sysconfig.get_config_var('LIBM') == '-lm':
534+
ext.libraries.extend('m')
459535

460536

461537
# First compile checkdep_freetype2.c, which aborts the compilation either
@@ -589,129 +665,11 @@ def do_custom_build(self):
589665
shutil.copy2(lib_path, src_path / "objs/.libs/libfreetype.lib")
590666

591667

592-
class FT2Font(SetupPackage):
593-
name = 'ft2font'
594-
595-
def get_extension(self):
596-
sources = [
597-
'src/ft2font.cpp',
598-
'src/ft2font_wrapper.cpp',
599-
'src/mplutils.cpp',
600-
'src/py_converters.cpp',
601-
]
602-
ext = Extension('matplotlib.ft2font', sources)
603-
FreeType().add_flags(ext)
604-
add_numpy_flags(ext)
605-
LibAgg().add_flags(ext, add_sources=False)
606-
return ext
607-
608-
609-
class Qhull(SetupPackage):
610-
name = "qhull"
611-
612-
def add_flags(self, ext):
613-
if options.get('system_qhull'):
614-
ext.libraries.append('qhull')
615-
else:
616-
ext.include_dirs.insert(0, 'extern')
617-
ext.sources.extend(sorted(glob.glob('extern/libqhull/*.c')))
618-
if sysconfig.get_config_var('LIBM') == '-lm':
619-
ext.libraries.extend('m')
620-
621-
622-
class TTConv(SetupPackage):
623-
name = "ttconv"
624-
625-
def get_extension(self):
626-
sources = [
627-
'src/_ttconv.cpp',
628-
'extern/ttconv/pprdrv_tt.cpp',
629-
'extern/ttconv/pprdrv_tt2.cpp',
630-
'extern/ttconv/ttutil.cpp'
631-
]
632-
ext = Extension('matplotlib.ttconv', sources)
633-
add_numpy_flags(ext)
634-
ext.include_dirs.insert(0, 'extern')
635-
return ext
636-
637-
638-
class Path(SetupPackage):
639-
name = "path"
640-
641-
def get_extension(self):
642-
sources = [
643-
'src/py_converters.cpp',
644-
'src/_path_wrapper.cpp'
645-
]
646-
ext = Extension('matplotlib._path', sources)
647-
add_numpy_flags(ext)
648-
LibAgg().add_flags(ext)
649-
return ext
650-
651-
652-
class Image(SetupPackage):
653-
name = "image"
654-
655-
def get_extension(self):
656-
sources = [
657-
'src/_image.cpp',
658-
'src/mplutils.cpp',
659-
'src/_image_wrapper.cpp',
660-
'src/py_converters.cpp'
661-
]
662-
ext = Extension('matplotlib._image', sources)
663-
add_numpy_flags(ext)
664-
LibAgg().add_flags(ext)
665-
666-
return ext
667-
668-
669-
class Contour(SetupPackage):
670-
name = "contour"
671-
672-
def get_extension(self):
673-
sources = [
674-
"src/_contour.cpp",
675-
"src/_contour_wrapper.cpp",
676-
'src/py_converters.cpp',
677-
]
678-
ext = Extension('matplotlib._contour', sources)
679-
add_numpy_flags(ext)
680-
LibAgg().add_flags(ext, add_sources=False)
681-
return ext
682-
683-
684-
class QhullWrap(SetupPackage):
685-
name = "qhull_wrap"
686-
687-
def get_extension(self):
688-
sources = ['src/qhull_wrap.c']
689-
ext = Extension('matplotlib._qhull', sources,
690-
define_macros=[('MPL_DEVNULL', os.devnull)])
691-
add_numpy_flags(ext)
692-
Qhull().add_flags(ext)
693-
return ext
694-
695-
696-
class Tri(SetupPackage):
697-
name = "tri"
698-
699-
def get_extension(self):
700-
sources = [
701-
"src/tri/_tri.cpp",
702-
"src/tri/_tri_wrapper.cpp",
703-
"src/mplutils.cpp"
704-
]
705-
ext = Extension('matplotlib._tri', sources)
706-
add_numpy_flags(ext)
707-
return ext
708-
709-
710668
class BackendAgg(OptionalBackendPackage):
711669
name = "agg"
712670
force = True
713671

714-
def get_extension(self):
672+
def get_extensions(self):
715673
sources = [
716674
"src/mplutils.cpp",
717675
"src/py_converters.cpp",
@@ -720,9 +678,9 @@ def get_extension(self):
720678
]
721679
ext = Extension('matplotlib.backends._backend_agg', sources)
722680
add_numpy_flags(ext)
723-
LibAgg().add_flags(ext)
681+
add_libagg_flags_and_sources(ext)
724682
FreeType().add_flags(ext)
725-
return ext
683+
yield ext
726684

727685

728686
class BackendTkAgg(OptionalBackendPackage):
@@ -732,7 +690,7 @@ class BackendTkAgg(OptionalBackendPackage):
732690
def check(self):
733691
return "installing; run-time loading from Python Tcl/Tk"
734692

735-
def get_extension(self):
693+
def get_extensions(self):
736694
sources = [
737695
'src/_tkagg.cpp',
738696
'src/py_converters.cpp',
@@ -741,8 +699,8 @@ def get_extension(self):
741699
ext = Extension('matplotlib.backends._tkagg', sources)
742700
self.add_flags(ext)
743701
add_numpy_flags(ext)
744-
LibAgg().add_flags(ext, add_sources=False)
745-
return ext
702+
add_libagg_flags(ext)
703+
yield ext
746704

747705
def add_flags(self, ext):
748706
ext.include_dirs.insert(0, 'src')
@@ -763,12 +721,12 @@ def check(self):
763721
raise CheckFailed("Mac OS-X only")
764722
return super().check()
765723

766-
def get_extension(self):
724+
def get_extensions(self):
767725
sources = [
768726
'src/_macosx.m'
769727
]
770728
ext = Extension('matplotlib.backends._macosx', sources)
771729
ext.extra_link_args.extend(['-framework', 'Cocoa'])
772730
if platform.python_implementation().lower() == 'pypy':
773731
ext.extra_compile_args.append('-DPYPY=1')
774-
return ext
732+
yield ext

0 commit comments

Comments
 (0)