2020PY3 = (sys .version_info [0 ] >= 3 )
2121
2222
23+ # This is the version of freetype to use when building a local version
24+ # of freetype. It must match the value in
25+ # lib/matplotlib.__init__.py:validate_test_dependencies
26+ LOCAL_FREETYPE_VERSION = '2.5.2'
27+
28+
2329if sys .platform != 'win32' :
2430 if sys .version_info [0 ] < 3 :
2531 from commands import getstatusoutput
4753 config = configparser .SafeConfigParser ()
4854 config .read (setup_cfg )
4955
50- try :
56+ if config . has_option ( 'status' , 'suppress' ) :
5157 options ['display_status' ] = not config .getboolean ("status" , "suppress" )
52- except :
53- pass
5458
55- try :
59+ if config . has_option ( 'rc_options' , 'backend' ) :
5660 options ['backend' ] = config .get ("rc_options" , "backend" )
57- except :
58- pass
5961
60- try :
62+ if config . has_option ( 'directories' , 'basedirlist' ) :
6163 options ['basedirlist' ] = [
6264 x .strip () for x in
6365 config .get ("directories" , "basedirlist" ).split (',' )]
64- except :
65- pass
66+
67+ if config .has_option ('test' , 'local_freetype' ):
68+ options ['local_freetype' ] = config .get ("test" , "local_freetype" )
6669else :
6770 config = None
6871
@@ -448,6 +451,14 @@ def _check_for_pkg_config(self, package, include_file, min_version=None,
448451
449452 return 'version %s' % version
450453
454+ def do_custom_build (self ):
455+ """
456+ If a package needs to do extra custom things, such as building a
457+ third-party library, before building an extension, it should
458+ override this method.
459+ """
460+ pass
461+
451462
452463class OptionalPackage (SetupPackage ):
453464 optional = True
@@ -461,10 +472,9 @@ def get_config(cls):
461472 if the package is at default state ("auto"), forced by the user (True)
462473 or opted-out (False).
463474 """
464- try :
465- return config .getboolean (cls .config_category , cls .name )
466- except :
467- return "auto"
475+ if config is not None and config .has_option (cls .config_category , cls .name ):
476+ return config .get (cls .config_category , cls .name )
477+ return "auto"
468478
469479 def check (self ):
470480 """
@@ -872,6 +882,9 @@ class FreeType(SetupPackage):
872882 name = "freetype"
873883
874884 def check (self ):
885+ if options .get ('local_freetype' ):
886+ return "Using local version for testing"
887+
875888 if sys .platform == 'win32' :
876889 check_include_file (get_include_dirs (), 'ft2build.h' , 'freetype' )
877890 return 'Using unknown version found on system.'
@@ -917,15 +930,60 @@ def version_from_header(self):
917930 return '.' .join ([major , minor , patch ])
918931
919932 def add_flags (self , ext ):
920- pkg_config .setup_extension (
921- ext , 'freetype2' ,
922- default_include_dirs = [
923- 'include/freetype2' , 'freetype2' ,
924- 'lib/freetype2/include' ,
925- 'lib/freetype2/include/freetype2' ],
926- default_library_dirs = [
927- 'freetype2/lib' ],
928- default_libraries = ['freetype' , 'z' ])
933+ if options .get ('local_freetype' ):
934+ src_path = os .path .join (
935+ 'build' , 'freetype-{0}' .format (LOCAL_FREETYPE_VERSION ))
936+ # Statically link to the locally-built freetype.
937+ # This is certainly broken on Windows.
938+ ext .include_dirs .insert (0 , os .path .join (src_path , 'include' ))
939+ ext .extra_objects .insert (
940+ 0 , os .path .join (src_path , 'objs' , '.libs' , 'libfreetype.a' ))
941+ ext .define_macros .append (('FREETYPE_BUILD_TYPE' , 'local' ))
942+ else :
943+ pkg_config .setup_extension (
944+ ext , 'freetype2' ,
945+ default_include_dirs = [
946+ 'include/freetype2' , 'freetype2' ,
947+ 'lib/freetype2/include' ,
948+ 'lib/freetype2/include/freetype2' ],
949+ default_library_dirs = [
950+ 'freetype2/lib' ],
951+ default_libraries = ['freetype' , 'z' ])
952+ ext .define_macros .append (('FREETYPE_BUILD_TYPE' , 'system' ))
953+
954+ def do_custom_build (self ):
955+ # We're using a system freetype
956+ if not options .get ('local_freetype' ):
957+ return
958+
959+ src_path = os .path .join (
960+ 'build' , 'freetype-{0}' .format (LOCAL_FREETYPE_VERSION ))
961+
962+ # We've already built freetype
963+ if os .path .isfile (os .path .join (src_path , 'objs' , '.libs' , 'libfreetype.a' )):
964+ return
965+
966+ tarball = 'freetype-{0}.tar.gz' .format (LOCAL_FREETYPE_VERSION )
967+ tarball_path = os .path .join ('build' , tarball )
968+ if not os .path .isfile (tarball_path ):
969+ print ("Downloading {0}" .format (tarball ))
970+ if sys .version_info [0 ] == 2 :
971+ from urllib import urlretrieve
972+ else :
973+ from urllib .request import urlretrieve
974+
975+ urlretrieve (
976+ 'http://download.savannah.gnu.org/releases/freetype/{0}' .format (tarball ),
977+ tarball_path )
978+
979+ print ("Building {0}" .format (tarball ))
980+ subprocess .check_call (
981+ ['tar zxf {0}' .format (tarball )], shell = True , cwd = 'build' )
982+ subprocess .check_call (
983+ ['./configure --without-zlib --without-bzip2 --without-png' ],
984+ shell = True , cwd = src_path )
985+ subprocess .check_call (
986+ ['make' ], shell = True , cwd = src_path )
929987
930988
931989class FT2Font (SetupPackage ):
0 commit comments