|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright (c) 2015, Michael Droettboom All rights reserved. |
| 4 | + |
| 5 | +# Redistribution and use in source and binary forms, with or without |
| 6 | +# modification, are permitted provided that the following conditions |
| 7 | +# are met: |
| 8 | + |
| 9 | +# 1. Redistributions of source code must retain the above copyright |
| 10 | +# notice, this list of conditions and the following disclaimer. |
| 11 | +# 2. Redistributions in binary form must reproduce the above copyright |
| 12 | +# notice, this list of conditions and the following disclaimer in |
| 13 | +# the documentation and/or other materials provided with the |
| 14 | +# distribution. |
| 15 | + |
| 16 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 19 | +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 20 | +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 | +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 22 | +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 23 | +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 24 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 26 | +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | +# POSSIBILITY OF SUCH DAMAGE. |
| 28 | + |
| 29 | +# The views and conclusions contained in the software and |
| 30 | +# documentation are those of the authors and should not be interpreted |
| 31 | +# as representing official policies, either expressed or implied, of |
| 32 | +# the FreeBSD Project. |
| 33 | + |
| 34 | +from __future__ import print_function, unicode_literals, absolute_import |
| 35 | + |
| 36 | + |
| 37 | +import hashlib |
| 38 | +import os |
| 39 | +import subprocess |
| 40 | +import sys |
| 41 | + |
| 42 | + |
| 43 | +# This is the version of FreeType to use when building a local |
| 44 | +# version. It must match the value in |
| 45 | +# lib/matplotlib.__init__.py |
| 46 | +LOCAL_FREETYPE_VERSION = '2.6.1' |
| 47 | +# md5 hash of the freetype tarball |
| 48 | +LOCAL_FREETYPE_HASH = '348e667d728c597360e4a87c16556597' |
| 49 | + |
| 50 | + |
| 51 | +def get_file_hash(filename): |
| 52 | + """ |
| 53 | + Get the MD5 hash of a given filename. |
| 54 | + """ |
| 55 | + BLOCKSIZE = 1 << 16 |
| 56 | + hasher = hashlib.md5() |
| 57 | + with open(filename, 'rb') as fd: |
| 58 | + buf = fd.read(BLOCKSIZE) |
| 59 | + while len(buf) > 0: |
| 60 | + hasher.update(buf) |
| 61 | + buf = fd.read(BLOCKSIZE) |
| 62 | + return hasher.hexdigest() |
| 63 | + |
| 64 | + |
| 65 | +def build_local_freetype(): |
| 66 | + src_path = os.path.join( |
| 67 | + 'build', 'freetype-{0}'.format(LOCAL_FREETYPE_VERSION)) |
| 68 | + |
| 69 | + # We've already built freetype |
| 70 | + if os.path.isfile(os.path.join(src_path, 'objs', '.libs', 'libfreetype.a')): |
| 71 | + return |
| 72 | + |
| 73 | + tarball = 'freetype-{0}.tar.gz'.format(LOCAL_FREETYPE_VERSION) |
| 74 | + tarball_path = os.path.join('build', tarball) |
| 75 | + if not os.path.isfile(tarball_path): |
| 76 | + tarball_url = 'http://download.savannah.gnu.org/releases/freetype/{0}'.format(tarball) |
| 77 | + |
| 78 | + print("Downloading {0}".format(tarball_url)) |
| 79 | + if sys.version_info[0] == 2: |
| 80 | + from urllib import urlretrieve |
| 81 | + else: |
| 82 | + from urllib.request import urlretrieve |
| 83 | + |
| 84 | + if not os.path.exists('build'): |
| 85 | + os.makedirs('build') |
| 86 | + urlretrieve(tarball_url, tarball_path) |
| 87 | + |
| 88 | + if get_file_hash(tarball_path) != LOCAL_FREETYPE_HASH: |
| 89 | + raise IOError("{0} does not match expected hash.".format(tarball)) |
| 90 | + |
| 91 | + print("Building {0}".format(tarball)) |
| 92 | + cflags = 'CFLAGS="{0} -fPIC" '.format(os.environ.get('CFLAGS', '')) |
| 93 | + |
| 94 | + subprocess.check_call( |
| 95 | + ['tar', 'zxf', tarball], cwd='build') |
| 96 | + subprocess.check_call( |
| 97 | + [cflags + './configure --with-zlib=no --with-bzip2=no ' |
| 98 | + '--with-png=no --with-harfbuzz=no'], shell=True, cwd=src_path) |
| 99 | + subprocess.check_call( |
| 100 | + [cflags + 'make'], shell=True, cwd=src_path) |
| 101 | + |
| 102 | + |
| 103 | +def set_flags(ext): |
| 104 | + src_path = os.path.join( |
| 105 | + 'build', 'freetype-{0}'.format(LOCAL_FREETYPE_VERSION)) |
| 106 | + |
| 107 | + # Statically link to the locally-built freetype. |
| 108 | + # This is certainly broken on Windows. |
| 109 | + ext.include_dirs.insert(0, os.path.join(src_path, 'include')) |
| 110 | + ext.extra_objects.insert( |
| 111 | + 0, os.path.join(src_path, 'objs', '.libs', 'libfreetype.a')) |
| 112 | + ext.define_macros.append(('FREETYPE_BUILD_TYPE', 'local')) |
0 commit comments