summaryrefslogtreecommitdiff
path: root/libraries/matplotlib/setupext.py
blob: c083627811f451468d660f5c7bf99233da3b7d40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
from __future__ import print_function, absolute_import

from distutils import sysconfig
from distutils import version
from distutils.core import Extension
import glob
import io
import multiprocessing
import os
import re
import subprocess
import sys
import warnings
from textwrap import fill


PY3 = (sys.version_info[0] >= 3)


try:
    from subprocess import check_output
except ImportError:
    # check_output is not available in Python 2.6
    def check_output(*popenargs, **kwargs):
        """
        Run command with arguments and return its output as a byte
        string.

        Backported from Python 2.7 as it's implemented as pure python
        on stdlib.
        """
        process = subprocess.Popen(
            stdout=subprocess.PIPE, *popenargs, **kwargs)
        output, unused_err = process.communicate()
        retcode = process.poll()
        if retcode:
            cmd = kwargs.get("args")
            if cmd is None:
                cmd = popenargs[0]
            error = subprocess.CalledProcessError(retcode, cmd)
            error.output = output
            raise error
        return output


if sys.platform != 'win32':
    if sys.version_info[0] < 3:
        from commands import getstatusoutput
    else:
        from subprocess import getstatusoutput


if PY3:
    import configparser
else:
    import ConfigParser as configparser


# matplotlib build options, which can be altered using setup.cfg
options = {
    'display_status': True,
    'verbose': False,
    'backend': None,
    'basedirlist': None
    }


setup_cfg = os.environ.get('MPLSETUPCFG', 'setup.cfg')
if os.path.exists(setup_cfg):
    config = configparser.SafeConfigParser()
    config.read(setup_cfg)

    try:
        options['display_status'] = not config.getboolean("status", "suppress")
    except:
        pass

    try:
        options['backend'] = config.get("rc_options", "backend")
    except:
        pass

    try:
        options['basedirlist'] = [
            x.strip() for x in
            config.get("directories", "basedirlist").split(',')]
    except:
        pass
else:
    config = None


def get_win32_compiler():
    """
    Determine the compiler being used on win32.
    """
    # Used to determine mingw32 or msvc
    # This is pretty bad logic, someone know a better way?
    for v in sys.argv:
        if 'mingw32' in v:
            return 'mingw32'
    return 'msvc'
win32_compiler = get_win32_compiler()


def extract_versions():
    """
    Extracts version values from the main matplotlib __init__.py and
    returns them as a dictionary.
    """
    with open('lib/matplotlib/__init__.py') as fd:
        for line in fd.readlines():
            if (line.startswith('__version__')):
                exec(line.strip())
    return locals()


def has_include_file(include_dirs, filename):
    """
    Returns `True` if `filename` can be found in one of the
    directories in `include_dirs`.
    """
    if sys.platform == 'win32':
        include_dirs += os.environ.get('INCLUDE', '.').split(';')
    for dir in include_dirs:
        if os.path.exists(os.path.join(dir, filename)):
            return True
    return False


def check_include_file(include_dirs, filename, package):
    """
    Raises an exception if the given include file can not be found.
    """
    if not has_include_file(include_dirs, filename):
        raise CheckFailed(
            "The C/C++ header for %s (%s) could not be found.  You "
            "may need to install the development package." %
            (package, filename))


def get_base_dirs():
    """
    Returns a list of standard base directories on this platform.
    """
    if options['basedirlist']:
        return options['basedirlist']

    basedir_map = {
        'win32': ['win32_static',],
        'darwin': ['/usr/local/', '/usr', '/usr/X11', '/opt/local'],
        'sunos5': [os.getenv('MPLIB_BASE') or '/usr/local',],
        'gnu0': ['/usr'],
        'aix5': ['/usr/local'],
        }
    return basedir_map.get(sys.platform, ['/usr/local', '/usr'])


def get_include_dirs():
    """
    Returns a list of standard include directories on this platform.
    """
    return [os.path.join(d, 'include') for d in get_base_dirs()]


def is_min_version(found, minversion):
    """
    Returns `True` if `found` is at least as high a version as
    `minversion`.
    """
    expected_version = version.LooseVersion(minversion)
    found_version = version.LooseVersion(found)
    return found_version >= expected_version


# Define the display functions only if display_status is True.
if options['display_status']:
    def print_line(char='='):
        print(char * 76)

    def print_status(package, status):
        initial_indent = "%22s: " % package
        indent = ' ' * 24
        print(fill(str(status), width=76,
                   initial_indent=initial_indent,
                   subsequent_indent=indent))

    def print_message(message):
        indent = ' ' * 24 + "* "
        print(fill(str(message), width=76,
                   initial_indent=indent,
                   subsequent_indent=indent))

    def print_raw(section):
        print(section)
else:
    def print_line(*args, **kwargs):
        pass
    print_status = print_message = print_raw = print_line


# Remove the -Wstrict-prototypesoption, is it's not valid for C++
customize_compiler = sysconfig.customize_compiler
def my_customize_compiler(compiler):
    retval = customize_compiler(compiler)
    try:
        compiler.compiler_so.remove('-Wstrict-prototypes')
    except (ValueError, AttributeError):
        pass
    return retval

sysconfig.customize_compiler = my_customize_compiler


def make_extension(name, files, *args, **kwargs):
    """
    Make a new extension.  Automatically sets include_dirs and
    library_dirs to the base directories appropriate for this
    platform.

    `name` is the name of the extension.

    `files` is a list of source files.

    Any additional arguments are passed to the
    `distutils.core.Extension` constructor.
    """
    ext = DelayedExtension(name, files, *args, **kwargs)
    for dir in get_base_dirs():
        include_dir = os.path.join(dir, 'include')
        if os.path.exists(include_dir):
            ext.include_dirs.append(include_dir)
        for lib in ('lib', 'lib64'):
            lib_dir = os.path.join(dir, lib)
            if os.path.exists(lib_dir):
                ext.library_dirs.append(lib_dir)
    ext.include_dirs.append('.')

    return ext


class PkgConfig(object):
    """
    This is a class for communicating with pkg-config.
    """
    def __init__(self):
        """
        Determines whether pkg-config exists on this machine.
        """
        if sys.platform == 'win32':
            self.has_pkgconfig = False
        else:
            self.set_pkgconfig_path()
            status, output = getstatusoutput("pkg-config --help")
            self.has_pkgconfig = (status == 0)
            if not self.has_pkgconfig:
                print("IMPORTANT WARNING:")
                print(
                    "    pkg-config is not installed.\n"
                    "    matplotlib may not be able to find some of its dependencies")

    def set_pkgconfig_path(self):
        pkgconfig_path = sysconfig.get_config_var('LIBDIR')
        if pkgconfig_path is None:
            return

        pkgconfig_path = os.path.join(pkgconfig_path, 'pkgconfig')
        if not os.path.isdir(pkgconfig_path):
            return

        try:
            os.environ['PKG_CONFIG_PATH'] += ':' + pkgconfig_path
        except KeyError:
            os.environ['PKG_CONFIG_PATH'] = pkgconfig_path

    def setup_extension(self, ext, package, default_include_dirs=[],
                        default_library_dirs=[], default_libraries=[],
                        alt_exec=None):
        """
        Add parameters to the given `ext` for the given `package`.
        """
        flag_map = {
            '-I': 'include_dirs', '-L': 'library_dirs', '-l': 'libraries'}

        executable = alt_exec
        if self.has_pkgconfig:
            executable = 'pkg-config {0}'.format(package)

        use_defaults = True

        if executable is not None:
            command = "{0} --libs --cflags ".format(executable)

            try:
                output = check_output(command, shell=True,
                                      stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError:
                pass
            else:
                output = output.decode(sys.getfilesystemencoding())
                use_defaults = False
                for token in output.split():
                    attr = flag_map.get(token[:2])
                    if attr is not None:
                        getattr(ext, attr).insert(0, token[2:])

        if use_defaults:
            basedirs = get_base_dirs()
            for base in basedirs:
                for include in default_include_dirs:
                    dir = os.path.join(base, include)
                    if os.path.exists(dir):
                        ext.include_dirs.append(dir)
                for lib in default_library_dirs:
                    dir = os.path.join(base, lib)
                    if os.path.exists(dir):
                        ext.library_dirs.append(dir)
            ext.libraries.extend(default_libraries)
            return True

        return False

    def get_version(self, package):
        """
        Get the version of the package from pkg-config.
        """
        if not self.has_pkgconfig:
            return None

        status, output = getstatusoutput(
            "pkg-config %s --modversion" % (package))
        if status == 0:
            return output
        return None


# The PkgConfig class should be used through this singleton
pkg_config = PkgConfig()


class CheckFailed(Exception):
    """
    Exception thrown when a `SetupPackage.check` method fails.
    """
    pass


class SetupPackage(object):
    optional = False

    def check(self):
        """
        Checks whether the dependencies are met.  Should raise a
        `CheckFailed` exception if the dependency could not be met,
        otherwise return a string indicating a version number or some
        other message indicating what was found.
        """
        pass

    def get_packages(self):
        """
        Get a list of package names to add to the configuration.
        These are added to the `packages` list passed to
        `distutils.setup`.
        """
        return []

    def get_namespace_packages(self):
        """
        Get a list of namespace package names to add to the configuration.
        These are added to the `namespace_packages` list passed to
        `distutils.setup`.
        """
        return []


    def get_py_modules(self):
        """
        Get a list of top-level modules to add to the configuration.
        These are added to the `py_modules` list passed to
        `distutils.setup`.
        """
        return []

    def get_package_data(self):
        """
        Get a package data dictionary to add to the configuration.
        These are merged into to the `package_data` list passed to
        `distutils.setup`.
        """
        return {}

    def get_extension(self):
        """
        Get a list of C extensions (`distutils.core.Extension`
        objects) to add to the configuration.  These are added to the
        `extensions` list passed to `distutils.setup`.
        """
        return None

    def get_install_requires(self):
        """
        Get a list of Python packages that we require.
        pip/easy_install will attempt to download and install this
        package if it is not installed.
        """
        return []

    def get_setup_requires(self):
        """
        Get a list of Python packages that we require at build time.
        pip/easy_install will attempt to download and install this
        package if it is not installed.
        """
        return []

    def _check_for_pkg_config(self, package, include_file, min_version=None,
                              version=None):
        """
        A convenience function for writing checks for a
        pkg_config-defined dependency.

        `package` is the pkg_config package name.

        `include_file` is a top-level include file we expect to find.

        `min_version` is the minimum version required.

        `version` will override the found version if this package
        requires an alternate method for that.
        """
        if version is None:
            version = pkg_config.get_version(package)

            if version is None:
                raise CheckFailed(
                    "pkg-config information for '%s' could not be found." %
                    package)

        if min_version == 'PATCH':
            raise CheckFailed(
                "Requires patches that have not been merged upstream.")

        if min_version:
            if (not is_min_version(version, min_version)):
                raise CheckFailed(
                    "Requires %s %s or later.  Found %s." %
                    (package, min_version, version))

        ext = self.get_extension()
        if ext is None:
            ext = make_extension('test', [])
            pkg_config.setup_extension(ext, package)

        check_include_file(ext.include_dirs, include_file, package)

        return 'version %s' % version


class OptionalPackage(SetupPackage):
    optional = True
    force = False
    config_category = "packages"

    @classmethod
    def get_config(cls):
        """
        Look at `setup.cfg` and return one of ["auto", True, False] indicating
        if the package is at default state ("auto"), forced by the user (True)
        or opted-out (False).
        """
        try:
            return config.getboolean(cls.config_category, cls.name)
        except:
            return "auto"

    def check(self):
        """
        Do not override this method!

        For custom dependency checks override self.check_requirements().
        Two things are checked: Configuration file and requirements.
        """
        # Check configuration file
        conf = self.get_config()
        # Default "auto" state or install forced by user
        if conf in [True, 'auto']:
            message = "installing"
            # Set non-optional if user sets `True` in config
            if conf is True:
                self.optional = False
        # Configuration opt-out by user
        else:
            # Some backend extensions (e.g. Agg) need to be built for certain
            # other GUI backends (e.g. TkAgg) even when manually disabled
            if self.force is True:
                message = "installing forced (config override)"
            else:
                raise CheckFailed("skipping due to configuration")

        # Check requirements and add extra information (if any) to message.
        # If requirements are not met a CheckFailed should be raised in there.
        additional_info = self.check_requirements()
        if additional_info:
            message += ", " + additional_info

        # No CheckFailed raised until now, return install message.
        return message

    def check_requirements(self):
        """
        Override this method to do custom dependency checks.

         - Raise CheckFailed() if requirements are not met.
         - Return message with additional information, or an empty string
           (or None) for no additional information.
        """
        return ""


class OptionalBackendPackage(OptionalPackage):
    config_category = "gui_support"


class Platform(SetupPackage):
    name = "platform"

    def check(self):
        return sys.platform


class Python(SetupPackage):
    name = "python"

    def check(self):
        major, minor1, minor2, s, tmp = sys.version_info

        if major < 2:
            raise CheckFailed(
                "Requires Python 2.6 or later")
        elif major == 2 and minor1 < 6:
            raise CheckFailed(
                "Requires Python 2.6 or later (in the 2.x series)")
        elif major == 3 and minor1 < 1:
            raise CheckFailed(
                "Requires Python 3.1 or later (in the 3.x series)")

        return sys.version


class Matplotlib(SetupPackage):
    name = "matplotlib"

    def check(self):
        return extract_versions()['__version__']

    def get_packages(self):
        return [
            'matplotlib',
            'matplotlib.backends',
            'matplotlib.backends.qt_editor',
            'matplotlib.compat',
            'matplotlib.projections',
            'matplotlib.axes',
            'matplotlib.sphinxext',
            'matplotlib.style',
            'matplotlib.testing',
            'matplotlib.testing.jpl_units',
            'matplotlib.tri',
            ]

    def get_py_modules(self):
        return ['pylab']

    def get_package_data(self):
        return {
            'matplotlib':
            [
                'mpl-data/fonts/afm/*.afm',
                'mpl-data/fonts/pdfcorefonts/*.afm',
                'mpl-data/fonts/pdfcorefonts/*.txt',
                'mpl-data/fonts/ttf/*.ttf',
                'mpl-data/fonts/ttf/LICENSE_STIX',
                'mpl-data/fonts/ttf/COPYRIGHT.TXT',
                'mpl-data/fonts/ttf/README.TXT',
                'mpl-data/fonts/ttf/RELEASENOTES.TXT',
                'mpl-data/images/*.xpm',
                'mpl-data/images/*.svg',
                'mpl-data/images/*.gif',
                'mpl-data/images/*.png',
                'mpl-data/images/*.ppm',
                'mpl-data/example/*.npy',
                'mpl-data/matplotlibrc',
                'backends/web_backend/*.*',
                'backends/web_backend/jquery/js/*',
                'backends/web_backend/jquery/css/themes/base/*.*',
                'backends/web_backend/jquery/css/themes/base/images/*',
                'backends/web_backend/css/*.*',
                'backends/Matplotlib.nib/*',
                'mpl-data/stylelib/*.mplstyle',
             ]}


class SampleData(OptionalPackage):
    """
    This handles the sample data that ships with matplotlib.  It is
    technically optional, though most often will be desired.
    """
    name = "sample_data"

    def get_package_data(self):
        return {
            'matplotlib':
            [
                'mpl-data/sample_data/*.*',
                'mpl-data/sample_data/axes_grid/*.*',
            ]}


class Toolkits(OptionalPackage):
    name = "toolkits"

    def get_packages(self):
        return [
            'mpl_toolkits',
            'mpl_toolkits.mplot3d',
            'mpl_toolkits.axes_grid',
            'mpl_toolkits.axes_grid1',
            'mpl_toolkits.axisartist',
            ]

    def get_namespace_packages(self):
        return ['mpl_toolkits']


class Tests(OptionalPackage):
    name = "tests"
    nose_min_version = '0.11.1'

    def check(self):
        super(Tests, self).check()

        msgs = []
        msg_template = ('{package} is required to run the matplotlib test '
                        'suite.  pip/easy_install may attempt to install it '
                        'after matplotlib.')


        bad_nose = msg_template.format(
            package='nose %s or later' % self.nose_min_version
        )
        try:
            import nose
            if is_min_version(nose.__version__, self.nose_min_version):
                msgs += ['using nose version %s' % nose.__version__]
            else:
                msgs += [bad_nose]
        except ImportError:
            msgs += [bad_nose]


        if sys.version_info >= (3, 3):
            msgs += ['using unittest.mock']
        else:
            try:
                import mock
                msgs += ['using mock %s' % mock.__version__]
            except ImportError:
                msgs += [msg_template.format(package='mock')]

        return ' / '.join(msgs)

    def get_packages(self):
        return [
            'matplotlib.tests',
            ]

    def get_package_data(self):
        baseline_images = [
            'tests/baseline_images/%s/*' % x
            for x in os.listdir('lib/matplotlib/tests/baseline_images')]

        return {
            'matplotlib':
            baseline_images +
            [
                'tests/mpltest.ttf',
                'tests/test_rcparams.rc'
            ]}

    def get_install_requires(self):
        requires = ['nose>=%s' % self.nose_min_version]
        if not sys.version_info >= (3, 3):
            requires += ['mock']
        return requires

class Toolkits_Tests(Tests):
    name = "toolkits_tests"

    def check_requirements(self):
        conf = self.get_config()
        toolkits_conf = Toolkits.get_config()
        tests_conf = Tests.get_config()

        if conf is True:
            Tests.force = True
            Toolkits.force = True
        elif conf == "auto" and not (toolkits_conf and tests_conf):
            # Only auto-install if both toolkits and tests are set
            # to be installed
            raise CheckFailed("toolkits_tests needs 'toolkits' and 'tests'")
        return ""

    def get_packages(self):
        return [
            'mpl_toolkits.tests',
            ]

    def get_package_data(self):
        baseline_images = [
            'tests/baseline_images/%s/*' % x
            for x in os.listdir('lib/mpl_toolkits/tests/baseline_images')]

        return {'mpl_toolkits': baseline_images}

    def get_namespace_packages(self):
        return ['mpl_toolkits']


class DelayedExtension(Extension, object):
    """
    A distutils Extension subclass where some of its members
    may have delayed computation until reaching the build phase.

    This is so we can, for example, get the Numpy include dirs
    after pip has installed Numpy for us if it wasn't already
    on the system.
    """
    def __init__(self, *args, **kwargs):
        super(DelayedExtension, self).__init__(*args, **kwargs)
        self._finalized = False
        self._hooks = {}

    def add_hook(self, member, func):
        """
        Add a hook to dynamically compute a member.

        Parameters
        ----------
        member : string
            The name of the member

        func : callable
            The function to call to get dynamically-computed values
            for the member.
        """
        self._hooks[member] = func

    def finalize(self):
        self._finalized = True

    class DelayedMember(property):
        def __init__(self, name):
            self._name = name

        def __get__(self, obj, objtype=None):
            result = getattr(obj, '_' + self._name, [])

            if obj._finalized:
                if self._name in obj._hooks:
                    result = obj._hooks[self._name]() + result

            return result

        def __set__(self, obj, value):
            setattr(obj, '_' + self._name, value)

    include_dirs = DelayedMember('include_dirs')


class Numpy(SetupPackage):
    name = "numpy"

    @staticmethod
    def include_dirs_hook():
        if sys.version_info[0] >= 3:
            import builtins
            if hasattr(builtins, '__NUMPY_SETUP__'):
                del builtins.__NUMPY_SETUP__
            import imp
            import numpy
            imp.reload(numpy)
        else:
            import __builtin__
            if hasattr(__builtin__, '__NUMPY_SETUP__'):
                del __builtin__.__NUMPY_SETUP__
            import numpy
            reload(numpy)

        ext = Extension('test', [])
        ext.include_dirs.append(numpy.get_include())
        if not has_include_file(
                ext.include_dirs, os.path.join("numpy", "arrayobject.h")):
            warnings.warn(
                "The C headers for numpy could not be found. "
                "You may need to install the development package")

        return [numpy.get_include()]

    def check(self):
        min_version = extract_versions()['__version__numpy__']
        try:
            import numpy
        except ImportError:
            raise CheckFailed(
                    "could not be found" )
            #return 'not found. pip may install it below.'

        if not is_min_version(numpy.__version__, min_version):
            raise CheckFailed(
                "requires numpy %s or later to build; found %s" %
                (min_version, numpy.__version__))

        return 'version %s' % numpy.__version__

    def add_flags(self, ext):
        # Ensure that PY_ARRAY_UNIQUE_SYMBOL is uniquely defined for
        # each extension
        array_api_name = 'MPL_' + ext.name.replace('.', '_') + '_ARRAY_API'

        ext.define_macros.append(('PY_ARRAY_UNIQUE_SYMBOL', array_api_name))
        ext.add_hook('include_dirs', self.include_dirs_hook)

    def get_setup_requires(self):
        return ['numpy>=1.6']

    def get_install_requires(self):
        return ['numpy>=1.6']


class CXX(SetupPackage):
    name = 'pycxx'

    def check(self):
        if PY3:
            # There is no version of PyCXX in the wild that will work
            # with Python 3.x and matplotlib, since they lack support
            # for the buffer object.
            self.__class__.found_external = False
            return ("Official versions of PyCXX are not compatible "
                    "with matplotlib on Python 3.x, since they lack "
                    "support for the buffer object.  Using local "
                    "copy")

        self.__class__.found_external = True
        old_stdout = sys.stdout
        if PY3:
            sys.stdout = io.StringIO()
        else:
            sys.stdout = io.BytesIO()

        try:
            import CXX
        except ImportError:
            self.__class__.found_external = False
            return "Couldn't import.  Using local copy."
        finally:
            sys.stdout = old_stdout

        try:
            return self._check_for_pkg_config(
                'PyCXX', 'CXX/Extensions.hxx', min_version='6.2.4')
        except CheckFailed as e:
            # It's ok to just proceed here, since the `import CXX`
            # worked above, and PyCXX (at least upstream) ensures that
            # its header files are on the default distutils include
            # path (either in a standard C place such as /usr/include,
            # or in /usr/include/pythonX.Y.
            return 'Using system CXX (version unknown, no pkg-config info)'

    def add_flags(self, ext):
        if self.found_external and not 'sdist' in sys.argv:
            support_dir = os.path.normpath(
                   os.path.join(
                       sys.prefix,
                       'share',
                       'python%d.%d' % (
                           sys.version_info[0], sys.version_info[1]),
                       'CXX'))
            if not os.path.exists(support_dir):
                # On Fedora 17, these files are installed in /usr/share/CXX
                support_dir = '/usr/src/CXX'
            ext.sources.extend([
                os.path.join(support_dir, x) for x in
                ['cxxsupport.cxx', 'cxx_extensions.cxx',
                 'IndirectPythonInterface.cxx',
                 'cxxextensions.c']])
            pkg_config.setup_extension(ext, 'PyCXX')
        else:
            ext.include_dirs.append('extern')
            ext.sources.extend(glob.glob('extern/CXX/*.cxx'))
            ext.sources.extend(glob.glob('extern/CXX/*.c'))
        ext.define_macros.append(('PYCXX_ISO_CPP_LIB', '1'))
        if PY3:
            ext.define_macros.append(('PYCXX_PYTHON_2TO3', '1'))
        if not (sys.platform == 'win32' and win32_compiler == 'msvc'):
            ext.libraries.append('stdc++')
            ext.libraries.append('m')


class LibAgg(SetupPackage):
    name = 'libagg'

    def check(self):
        self.__class__.found_external = True
        try:
            return self._check_for_pkg_config(
                'libagg', 'agg2/agg_basics.h', min_version='PATCH')
        except CheckFailed as e:
            self.__class__.found_external = False
            return str(e) + ' Using local copy.'

    def add_flags(self, ext):
        if self.found_external:
            pkg_config.setup_extension(ext, 'libagg')
        else:
            ext.include_dirs.append('extern/agg24/include')
            agg_sources = [
                'agg_bezier_arc.cpp',
                'agg_curves.cpp',
                'agg_image_filters.cpp',
                'agg_trans_affine.cpp',
                'agg_vcgen_contour.cpp',
                'agg_vcgen_dash.cpp',
                'agg_vcgen_stroke.cpp',
                'agg_vpgen_segmentator.cpp'
                ]
            ext.sources.extend(
                os.path.join('extern', 'agg24', 'src', x) for x in agg_sources)


class FreeType(SetupPackage):
    name = "freetype"

    def check(self):
        if sys.platform == 'win32':
            check_include_file(get_include_dirs(), 'ft2build.h', 'freetype')
            return 'Using unknown version found on system.'

        status, output = getstatusoutput("freetype-config --ftversion")
        if status == 0:
            version = output
        else:
            version = None

        # Early versions of freetype grep badly inside freetype-config,
        # so catch those cases. (tested with 2.5.3).
        if version is None or 'No such file or directory\ngrep:' in version:
            version = self.version_from_header()

        return self._check_for_pkg_config(
            'freetype2', 'ft2build.h',
            min_version='2.3', version=version)

    def version_from_header(self):
        version = 'Failed to identify version.'
        ext = self.get_extension()
        if ext is None:
            return version
        # Return the first version found in the include dirs.
        for include_dir in ext.include_dirs:
            header_fname = os.path.join(include_dir, 'freetype.h')
            if os.path.exists(header_fname):
                major, minor, patch = 0, 0, 0
                with open(header_fname, 'r') as fh:
                    for line in fh:
                        if line.startswith('#define FREETYPE_'):
                            value = line.rsplit(' ', 1)[1].strip()
                            if 'MAJOR' in line:
                                major = value
                            elif 'MINOR' in line:
                                minor = value
                            else:
                                patch = value
                return '.'.join([major, minor, patch])

    def add_flags(self, ext):
        pkg_config.setup_extension(
            ext, 'freetype2',
            default_include_dirs=[
                'include/freetype2', 'freetype2',
                'lib/freetype2/include',
                'lib/freetype2/include/freetype2'],
            default_library_dirs=[
                'freetype2/lib'],
            default_libraries=['freetype', 'z'])



class FT2Font(SetupPackage):
    name = 'ft2font'

    def get_extension(self):
        sources = [
            'src/ft2font.cpp',
            'src/mplutils.cpp'
            ]
        ext = make_extension('matplotlib.ft2font', sources)
        FreeType().add_flags(ext)
        Numpy().add_flags(ext)
        CXX().add_flags(ext)
        return ext


class Png(SetupPackage):
    name = "png"

    def check(self):
        if sys.platform == 'win32':
            check_include_file(get_include_dirs(), 'png.h', 'png')
            return 'Using unknown version found on system.'

        status, output = getstatusoutput("libpng-config --version")
        if status == 0:
            version = output
        else:
            version = None

        try:
            return self._check_for_pkg_config(
                'libpng', 'png.h',
                min_version='1.2', version=version)
        except CheckFailed as e:
            if has_include_file(get_include_dirs(), 'png.h'):
                return str(e) + ' Using unknown version found on system.'
            raise

    def get_extension(self):
        sources = [
            'src/_png.cpp', 'src/mplutils.cpp'
            ]
        ext = make_extension('matplotlib._png', sources)
        pkg_config.setup_extension(
            ext, 'libpng', default_libraries=['png', 'z'],
            alt_exec='libpng-config --ldflags')
        Numpy().add_flags(ext)
        CXX().add_flags(ext)
        return ext


class Qhull(SetupPackage):
    name = "qhull"

    def check(self):
        self.__class__.found_external = True
        try:
            return self._check_for_pkg_config(
                'qhull', 'qhull/qhull_a.h', min_version='2003.1')
        except CheckFailed as e:
            self.__class__.found_pkgconfig = False
            # Qhull may not be in the pkg-config system but may still be
            # present on this system, so check if the header files can be
            # found.
            include_dirs = [
                os.path.join(x, 'qhull') for x in get_include_dirs()]
            if has_include_file(include_dirs, 'qhull_a.h'):
                return 'Using system Qhull (version unknown, no pkg-config info)'
            else:
                self.__class__.found_external = False
                return str(e) + ' Using local copy.'

    def add_flags(self, ext):
        if self.found_external:
            pkg_config.setup_extension(ext, 'qhull',
                                       default_libraries=['qhull'])
        else:
            ext.include_dirs.append('extern')
            ext.sources.extend(glob.glob('extern/qhull/*.c'))


class TTConv(SetupPackage):
    name = "ttconv"

    def get_extension(self):
        sources = [
            'src/_ttconv.cpp',
            'extern/ttconv/pprdrv_tt.cpp',
            'extern/ttconv/pprdrv_tt2.cpp',
            'extern/ttconv/ttutil.cpp'
            ]
        ext = make_extension('matplotlib.ttconv', sources)
        Numpy().add_flags(ext)
        CXX().add_flags(ext)
        ext.include_dirs.append('extern')
        return ext


class Path(SetupPackage):
    name = "path"

    def get_extension(self):
        sources = [
            'src/_path.cpp',
            'src/path_cleanup.cpp',
            'src/agg_py_transforms.cpp'
            ]

        ext = make_extension('matplotlib._path', sources)
        Numpy().add_flags(ext)
        LibAgg().add_flags(ext)
        CXX().add_flags(ext)
        return ext


class Image(SetupPackage):
    name = "image"

    def get_extension(self):
        sources = [
            'src/_image.cpp', 'src/mplutils.cpp'
            ]
        ext = make_extension('matplotlib._image', sources)
        Numpy().add_flags(ext)
        LibAgg().add_flags(ext)
        CXX().add_flags(ext)
        return ext


class Contour(SetupPackage):
    name = "contour"

    def get_extension(self):
        sources = [
            "src/cntr.c"
            ]
        ext = make_extension('matplotlib._cntr', sources)
        Numpy().add_flags(ext)
        return ext


class Delaunay(SetupPackage):
    name = "delaunay"

    def get_packages(self):
        return ['matplotlib.delaunay']

    def get_extension(self):
        sources = ["_delaunay.cpp", "VoronoiDiagramGenerator.cpp",
                   "delaunay_utils.cpp", "natneighbors.cpp"]
        sources = [os.path.join('lib/matplotlib/delaunay', s) for s in sources]
        ext = make_extension('matplotlib._delaunay', sources)
        Numpy().add_flags(ext)
        return ext


class QhullWrap(SetupPackage):
    name = "qhull_wrap"

    def get_extension(self):
        sources = ['src/qhull_wrap.c']
        ext = make_extension('matplotlib._qhull', sources,
                             define_macros=[('MPL_DEVNULL', os.devnull)])
        Numpy().add_flags(ext)
        Qhull().add_flags(ext)
        return ext


class Tri(SetupPackage):
    name = "tri"

    def get_extension(self):
        sources = [
            "lib/matplotlib/tri/_tri.cpp",
            "src/mplutils.cpp"
            ]
        ext = make_extension('matplotlib._tri', sources)
        Numpy().add_flags(ext)
        CXX().add_flags(ext)
        return ext


class Six(SetupPackage):
    name = "six"
    min_version = "1.4"

    def check(self):
        try:
            import six
        except ImportError:
            raise CheckFailed(
                "could not be found")

        if not is_min_version(six.__version__, self.min_version):
            raise CheckFailed(
                "requires six %s or later; found %s" %
                (self.min_version, six.__version__))

        return "using six version %s" % six.__version__

    def get_install_requires(self):
        return ['six>={0}'.format(self.min_version)]


class Pytz(SetupPackage):
    name = "pytz"

    def check(self):
        try:
            import pytz
        except ImportError:
            raise CheckFailed (
                "could not be found")

        return "using pytz version %s" % pytz.__version__

    def get_install_requires(self):
        return ['pytz']


class Dateutil(SetupPackage):
    name = "dateutil"

    def __init__(self, version=None):
        self.version = version

    def check(self):
        try:
            import dateutil
        except ImportError:
            # dateutil 2.1 has a file encoding bug that breaks installation on
            # python 3.3
            # https://github.com/matplotlib/matplotlib/issues/2373
            # hack around the problem by installing the (working) v2.0
            #major, minor1, _, _, _ = sys.version_info
            #if self.version is None and (major, minor1) == (3, 3):
                #self.version = '!=2.1'

            raise CheckFailed (
                "could not be found")
        major, minor1, _, _, _ = sys.version_info
        if dateutil.__version__ == '2.1' and (major, minor1) == (3, 3):
            raise CheckFailed (
                "dateutil v. 2.1 has a bug that breaks installation"
                "on python 3.3.x, use another dateutil version")
        return "using dateutil version %s" % dateutil.__version__

    def get_install_requires(self):
        dateutil = 'python-dateutil'
        if self.version is not None:
            dateutil += self.version
        return [dateutil]


class Tornado(SetupPackage):
    name = "tornado"

    def check(self):
        try:
            import tornado
        except ImportError:
            raise CheckFailed (
                "could not be found")

        return "using tornado version %s" % tornado.version


class Pyparsing(SetupPackage):
    name = "pyparsing"

    def is_ok(self):
        # pyparsing 2.0.0 bug, but it may be patched in distributions
        try:
            import pyparsing
            f = pyparsing.Forward()
            f <<= pyparsing.Literal('a')
            return f is not None
        except (ImportError, TypeError):
            return False

    def check(self):
        try:
            import pyparsing
        except ImportError:
            raise CheckFailed (
                "could not be found")

        required = [1, 5, 6]
        if [int(x) for x in pyparsing.__version__.split('.')] < required:
            raise CheckFailed (
                "matplotlib requires pyparsing >= {0}; yours is ".format(
                    '.'.join(str(x) for x in required)) + pyparsing.__version__)

        if not self.is_ok():
            return (
                "Your pyparsing contains a bug that will be monkey-patched by "
                "matplotlib.  For best results, upgrade to pyparsing 2.0.1 or "
                "later.")

        return "using pyparsing version %s" % pyparsing.__version__

    def get_install_requires(self):
        if self.is_ok():
            return ['pyparsing>=1.5.6']
        else:
            return ['pyparsing>=1.5.6,!=2.0.0']


class BackendAgg(OptionalBackendPackage):
    name = "agg"

    def get_extension(self):
        sources = [
            "src/mplutils.cpp",
            "src/agg_py_transforms.cpp",
            "src/_backend_agg.cpp"
            ]
        ext = make_extension('matplotlib.backends._backend_agg', sources)
        Numpy().add_flags(ext)
        LibAgg().add_flags(ext)
        FreeType().add_flags(ext)
        CXX().add_flags(ext)
        return ext


class BackendTkAgg(OptionalBackendPackage):
    name = "tkagg"

    def __init__(self):
        self.tcl_tk_cache = None

    def check_requirements(self):
        try:
            if PY3:
                import tkinter as Tkinter
            else:
                import Tkinter
        except ImportError:
            raise CheckFailed('TKAgg requires Tkinter.')
        except RuntimeError:
            raise CheckFailed('Tkinter present but import failed.')
        else:
            if Tkinter.TkVersion < 8.3:
                raise CheckFailed("Tcl/Tk v8.3 or later required.")

        ext = self.get_extension()
        check_include_file(ext.include_dirs, "tk.h", "Tk")

        try:
            tk_v = Tkinter.__version__.split()[-2]
        except (AttributeError, IndexError):
            # Tkinter.__version__ has been removed in python 3
            tk_v = 'version not identified'

        BackendAgg.force = True

        return "version %s" % tk_v

    def get_extension(self):
        sources = [
            'src/agg_py_transforms.cpp',
            'src/_tkagg.cpp'
            ]

        ext = make_extension('matplotlib.backends._tkagg', sources)
        self.add_flags(ext)
        Numpy().add_flags(ext)
        LibAgg().add_flags(ext)
        CXX().add_flags(ext)
        return ext

    def query_tcltk(self):
        """
        Tries to open a Tk window in order to query the Tk object
        about its library paths.  This should never be called more
        than once by the same process, as Tk intricacies may cause the
        Python interpreter to hang. The function also has a workaround
        if no X server is running (useful for autobuild systems).
        """
        # Use cached values if they exist, which ensures this function
        # only executes once
        if self.tcl_tk_cache is not None:
            return self.tcl_tk_cache

        # By this point, we already know that Tkinter imports correctly
        if PY3:
            import tkinter as Tkinter
        else:
            import Tkinter
        tcl_lib_dir = ''
        tk_lib_dir = ''
        # First try to open a Tk window (requires a running X server)
        try:
            tk = Tkinter.Tk()
        except Tkinter.TclError:
            # Next, start Tcl interpreter without opening a Tk window
            # (no need for X server) This feature is available in
            # python version 2.4 and up
            try:
                tcl = Tkinter.Tcl()
            except AttributeError:    # Python version not high enough
                pass
            except Tkinter.TclError:  # Something went wrong while opening Tcl
                pass
            else:
                tcl_lib_dir = str(tcl.getvar('tcl_library'))
                # Guess Tk location based on Tcl location
                (head, tail) = os.path.split(tcl_lib_dir)
                tail = tail.replace('Tcl', 'Tk').replace('tcl', 'tk')
                tk_lib_dir = os.path.join(head, tail)
                if not os.path.exists(tk_lib_dir):
                    tk_lib_dir = tcl_lib_dir.replace(
                        'Tcl', 'Tk').replace('tcl', 'tk')
        else:
            # Obtain Tcl and Tk locations from Tk widget
            tk.withdraw()
            tcl_lib_dir = str(tk.getvar('tcl_library'))
            tk_lib_dir = str(tk.getvar('tk_library'))
            tk.destroy()

        # Save directories and version string to cache
        self.tcl_tk_cache = tcl_lib_dir, tk_lib_dir, str(Tkinter.TkVersion)[:3]
        return self.tcl_tk_cache

    def parse_tcl_config(self, tcl_lib_dir, tk_lib_dir):
        try:
            if PY3:
                import tkinter as Tkinter
            else:
                import Tkinter
        except ImportError:
            return None

        tcl_poss = [tcl_lib_dir,
                    os.path.normpath(os.path.join(tcl_lib_dir, '..')),
                    "/usr/lib/tcl" + str(Tkinter.TclVersion),
                    "/usr/lib"]
        tk_poss = [tk_lib_dir,
                    os.path.normpath(os.path.join(tk_lib_dir, '..')),
                   "/usr/lib/tk" + str(Tkinter.TkVersion),
                   "/usr/lib"]
        for ptcl, ptk in zip(tcl_poss, tk_poss):
            tcl_config = os.path.join(ptcl, "tclConfig.sh")
            tk_config = os.path.join(ptk, "tkConfig.sh")
            if (os.path.exists(tcl_config) and os.path.exists(tk_config)):
                break
        if not (os.path.exists(tcl_config) and os.path.exists(tk_config)):
            return None

        def get_var(file, varname):
            p = subprocess.Popen(
                '. %s ; eval echo ${%s}' % (file, varname),
                shell=True,
                executable="/bin/sh",
                stdout=subprocess.PIPE)
            result = p.communicate()[0]
            return result.decode('ascii')

        tcl_lib_dir = get_var(
            tcl_config, 'TCL_LIB_SPEC').split()[0][2:].strip()
        tcl_inc_dir = get_var(
            tcl_config, 'TCL_INCLUDE_SPEC')[2:].strip()
        tcl_lib = get_var(tcl_config, 'TCL_LIB_FLAG')[2:].strip()

        tk_lib_dir = get_var(tk_config, 'TK_LIB_SPEC').split()[0][2:].strip()
        tk_inc_dir = get_var(tk_config, 'TK_INCLUDE_SPEC').strip()
        if tk_inc_dir == '':
            tk_inc_dir = tcl_inc_dir
        else:
            tk_inc_dir = tk_inc_dir[2:]
        tk_lib = get_var(tk_config, 'TK_LIB_FLAG')[2:].strip()

        if not os.path.exists(os.path.join(tk_inc_dir, 'tk.h')):
            return None

        return (tcl_lib_dir, tcl_inc_dir, tcl_lib,
                tk_lib_dir, tk_inc_dir, tk_lib)

    def guess_tcl_config(self, tcl_lib_dir, tk_lib_dir, tk_ver):
        if not (os.path.exists(tcl_lib_dir) and os.path.exists(tk_lib_dir)):
            return None

        tcl_lib = os.path.normpath(os.path.join(tcl_lib_dir, '../'))
        tk_lib = os.path.normpath(os.path.join(tk_lib_dir, '../'))

        tcl_inc = os.path.normpath(
            os.path.join(tcl_lib_dir,
                         '../../include/tcl' + tk_ver))
        if not os.path.exists(tcl_inc):
            tcl_inc = os.path.normpath(
                os.path.join(tcl_lib_dir,
                             '../../include'))

        tk_inc = os.path.normpath(os.path.join(
            tk_lib_dir,
            '../../include/tk' + tk_ver))
        if not os.path.exists(tk_inc):
            tk_inc = os.path.normpath(os.path.join(
                tk_lib_dir,
                '../../include'))

        if not os.path.exists(os.path.join(tk_inc, 'tk.h')):
            tk_inc = tcl_inc

        if not os.path.exists(tcl_inc):
            # this is a hack for suse linux, which is broken
            if (sys.platform.startswith('linux') and
                os.path.exists('/usr/include/tcl.h') and
                os.path.exists('/usr/include/tk.h')):
                tcl_inc = '/usr/include'
                tk_inc = '/usr/include'

        if not os.path.exists(os.path.join(tk_inc, 'tk.h')):
            return None

        return tcl_lib, tcl_inc, 'tcl' + tk_ver, tk_lib, tk_inc, 'tk' + tk_ver

    def hardcoded_tcl_config(self):
        tcl_inc = "/usr/local/include"
        tk_inc = "/usr/local/include"
        tcl_lib = "/usr/local/lib"
        tk_lib = "/usr/local/lib"
        return tcl_lib, tcl_inc, 'tcl', tk_lib, tk_inc, 'tk'

    def add_flags(self, ext):
        if sys.platform == 'win32':
            major, minor1, minor2, s, tmp = sys.version_info
            if sys.version_info[0:2] < (3, 4):
                ext.include_dirs.extend(['win32_static/include/tcl85'])
                ext.libraries.extend(['tk85', 'tcl85'])
            else:
                ext.include_dirs.extend(['win32_static/include/tcl86'])
                ext.libraries.extend(['tk86t', 'tcl86t'])
            ext.library_dirs.extend([os.path.join(sys.prefix, 'dlls')])

        elif sys.platform == 'darwin':
            # this config section lifted directly from Imaging - thanks to
            # the effbot!

            # First test for a MacOSX/darwin framework install
            from os.path import join, exists
            framework_dirs = [
                join(os.getenv('HOME'), '/Library/Frameworks'),
                '/Library/Frameworks',
                '/System/Library/Frameworks/',
            ]

            # Find the directory that contains the Tcl.framework and
            # Tk.framework bundles.
            tk_framework_found = 0
            for F in framework_dirs:
                # both Tcl.framework and Tk.framework should be present
                for fw in 'Tcl', 'Tk':
                    if not exists(join(F, fw + '.framework')):
                        break
                else:
                    # ok, F is now directory with both frameworks. Continure
                    # building
                    tk_framework_found = 1
                    break
            if tk_framework_found:
                # For 8.4a2, we must add -I options that point inside
                # the Tcl and Tk frameworks. In later release we
                # should hopefully be able to pass the -F option to
                # gcc, which specifies a framework lookup path.

                tk_include_dirs = [
                    join(F, fw + '.framework', H)
                    for fw in ('Tcl', 'Tk')
                    for H in ('Headers', 'Versions/Current/PrivateHeaders')
                ]

                # For 8.4a2, the X11 headers are not included. Rather
                # than include a complicated search, this is a
                # hard-coded path. It could bail out if X11 libs are
                # not found...

                # tk_include_dirs.append('/usr/X11R6/include')
                frameworks = ['-framework', 'Tcl', '-framework', 'Tk']
                ext.include_dirs.extend(tk_include_dirs)
                ext.extra_link_args.extend(frameworks)
                ext.extra_compile_args.extend(frameworks)

        # you're still here? ok we'll try it this way...
        else:
            # There are 3 methods to try, in decreasing order of "smartness"
            #
            #   1. Parse the tclConfig.sh and tkConfig.sh files that have
            #      all the information we need
            #
            #   2. Guess the include and lib dirs based on the location of
            #      Tkinter's 'tcl_library' and 'tk_library' variables.
            #
            #   3. Use some hardcoded locations that seem to work on a lot
            #      of distros.

            # Query Tcl/Tk system for library paths and version string
            try:
                tcl_lib_dir, tk_lib_dir, tk_ver = self.query_tcltk()
            except:
                tk_ver = ''
                result = self.hardcoded_tcl_config()
            else:
                result = self.parse_tcl_config(tcl_lib_dir, tk_lib_dir)
                if result is None:
                    result = self.guess_tcl_config(
                        tcl_lib_dir, tk_lib_dir, tk_ver)
                    if result is None:
                        result = self.hardcoded_tcl_config()

            # Add final versions of directories and libraries to ext lists
            (tcl_lib_dir, tcl_inc_dir, tcl_lib,
             tk_lib_dir, tk_inc_dir, tk_lib) = result
            ext.include_dirs.extend([tcl_inc_dir, tk_inc_dir])
            ext.library_dirs.extend([tcl_lib_dir, tk_lib_dir])
            ext.libraries.extend([tcl_lib, tk_lib])


class BackendGtk(OptionalBackendPackage):
    name = "gtk"

    def check_requirements(self):
        try:
            import gtk
        except ImportError:
            raise CheckFailed("Requires pygtk")
        except RuntimeError:
            raise CheckFailed('pygtk present, but import failed.')
        else:
            version = (2, 2, 0)
            if gtk.pygtk_version < version:
                raise CheckFailed(
                    "Requires pygtk %d.%d.%d or later. "
                    "Found %d.%d.%d" % (version + gtk.pygtk_version))

        ext = self.get_extension()
        self.add_flags(ext)
        check_include_file(ext.include_dirs,
                           os.path.join("gtk", "gtk.h"),
                           'gtk')
        check_include_file(ext.include_dirs,
                           os.path.join("pygtk", "pygtk.h"),
                           'pygtk')

        return 'Gtk: %s pygtk: %s' % (
            ".".join(str(x) for x in gtk.gtk_version),
            ".".join(str(x) for x in gtk.pygtk_version))

    def get_package_data(self):
        return {'matplotlib': ['mpl-data/*.glade']}

    def get_extension(self):
        sources = [
            'src/_backend_gdk.c'
            ]
        ext = make_extension('matplotlib.backends._backend_gdk', sources)
        self.add_flags(ext)
        Numpy().add_flags(ext)
        return ext

    def add_flags(self, ext):
        if sys.platform == 'win32':
            def getoutput(s):
                ret = os.popen(s).read().strip()
                return ret

            if 'PKG_CONFIG_PATH' not in os.environ:
                # If Gtk+ is installed, pkg-config is required to be installed
                os.environ['PKG_CONFIG_PATH'] = 'C:\\GTK\\lib\\pkgconfig'

                # popen broken on my win32 plaform so I can't use pkgconfig
                ext.library_dirs.extend(
                    ['C:/GTK/bin', 'C:/GTK/lib'])

                ext.include_dirs.extend(
                    ['win32_static/include/pygtk-2.0',
                     'C:/GTK/include',
                     'C:/GTK/include/gobject',
                     'C:/GTK/include/gext',
                     'C:/GTK/include/glib',
                     'C:/GTK/include/pango',
                     'C:/GTK/include/atk',
                     'C:/GTK/include/X11',
                     'C:/GTK/include/cairo',
                     'C:/GTK/include/gdk',
                     'C:/GTK/include/gdk-pixbuf',
                     'C:/GTK/include/gtk',
                     ])

            pygtkIncludes = getoutput(
                'pkg-config --cflags-only-I pygtk-2.0').split()
            gtkIncludes = getoutput(
                'pkg-config --cflags-only-I gtk+-2.0').split()
            includes = pygtkIncludes + gtkIncludes
            ext.include_dirs.extend([include[2:] for include in includes])

            pygtkLinker = getoutput('pkg-config --libs pygtk-2.0').split()
            gtkLinker = getoutput('pkg-config --libs gtk+-2.0').split()
            linkerFlags = pygtkLinker + gtkLinker

            ext.libraries.extend(
                [flag[2:] for flag in linkerFlags if flag.startswith('-l')])

            ext.library_dirs.extend(
                [flag[2:] for flag in linkerFlags if flag.startswith('-L')])

            ext.extra_link_args.extend(
                [flag for flag in linkerFlags if not
                 (flag.startswith('-l') or flag.startswith('-L'))])

            # visual studio doesn't need the math library
            if (sys.platform == 'win32' and
                win32_compiler == 'msvc' and
                'm' in ext.libraries):
                ext.libraries.remove('m')

        elif sys.platform != 'win32':
            pkg_config.setup_extension(ext, 'pygtk-2.0')
            pkg_config.setup_extension(ext, 'gtk+-2.0')


class BackendGtkAgg(BackendGtk):
    name = "gtkagg"

    def check(self):
        try:
            return super(BackendGtkAgg, self).check()
        except:
            raise
        else:
            BackendAgg.force = True

    def get_package_data(self):
        return {'matplotlib': ['mpl-data/*.glade']}

    def get_extension(self):
        sources = [
            'src/agg_py_transforms.cpp',
            'src/_gtkagg.cpp',
            'src/mplutils.cpp'
            ]
        ext = make_extension('matplotlib.backends._gtkagg', sources)
        self.add_flags(ext)
        LibAgg().add_flags(ext)
        CXX().add_flags(ext)
        Numpy().add_flags(ext)
        return ext


def backend_gtk3agg_internal_check(x):
    try:
        import gi
    except ImportError:
        return (False, "Requires pygobject to be installed.")

    try:
        gi.require_version("Gtk", "3.0")
    except ValueError:
        return (False, "Requires gtk3 development files to be installed.")
    except AttributeError:
        return (False, "pygobject version too old.")

    try:
        from gi.repository import Gtk, Gdk, GObject
    except (ImportError, RuntimeError):
        return (False, "Requires pygobject to be installed.")

    return (True, "version %s.%s.%s" % (
        Gtk.get_major_version(),
        Gtk.get_micro_version(),
        Gtk.get_minor_version()))


class BackendGtk3Agg(OptionalBackendPackage):
    name = "gtk3agg"

    def check_requirements(self):
        if 'TRAVIS' in os.environ:
            raise CheckFailed("Can't build with Travis")

        # This check needs to be performed out-of-process, because
        # importing gi and then importing regular old pygtk afterward
        # segfaults the interpreter.
        try:
            p = multiprocessing.Pool()
        except:
            return "unknown (can not use multiprocessing to determine)"
        try:
            success, msg = p.map(backend_gtk3agg_internal_check, [0])[0]
        except:
            success = False
            msg = "Could not determine"
        finally:
            p.close()
            p.join()
        if success:
            BackendAgg.force = True

            return msg
        else:
            raise CheckFailed(msg)

    def get_package_data(self):
        return {'matplotlib': ['mpl-data/*.glade']}


def backend_gtk3cairo_internal_check(x):
    try:
        import cairocffi
    except ImportError:
        try:
            import cairo
        except ImportError:
            return (False, "Requires cairocffi or pycairo to be installed.")

    try:
        import gi
    except ImportError:
        return (False, "Requires pygobject to be installed.")

    try:
        gi.require_version("Gtk", "3.0")
    except ValueError:
        return (False, "Requires gtk3 development files to be installed.")
    except AttributeError:
        return (False, "pygobject version too old.")

    try:
        from gi.repository import Gtk, Gdk, GObject
    except (RuntimeError, ImportError):
        return (False, "Requires pygobject to be installed.")

    return (True, "version %s.%s.%s" % (
        Gtk.get_major_version(),
        Gtk.get_micro_version(),
        Gtk.get_minor_version()))


class BackendGtk3Cairo(OptionalBackendPackage):
    name = "gtk3cairo"

    def check_requirements(self):
        if 'TRAVIS' in os.environ:
            raise CheckFailed("Can't build with Travis")

        # This check needs to be performed out-of-process, because
        # importing gi and then importing regular old pygtk afterward
        # segfaults the interpreter.
        try:
            p = multiprocessing.Pool()
        except:
            return "unknown (can not use multiprocessing to determine)"
        success, msg = p.map(backend_gtk3cairo_internal_check, [0])[0]
        p.close()
        p.join()
        if success:
            BackendAgg.force = True

            return msg
        else:
            raise CheckFailed(msg)

    def get_package_data(self):
        return {'matplotlib': ['mpl-data/*.glade']}


class BackendWxAgg(OptionalBackendPackage):
    name = "wxagg"

    def check_requirements(self):
        try:
            import wxversion
        except ImportError:
            raise CheckFailed("requires wxPython")

        try:
            _wx_ensure_failed = wxversion.AlreadyImportedError
        except AttributeError:
            _wx_ensure_failed = wxversion.VersionError

        try:
            wxversion.ensureMinimal('2.8')
        except _wx_ensure_failed:
            pass

        try:
            import wx
            backend_version = wx.VERSION_STRING
        except ImportError:
            raise CheckFailed("requires wxPython")

        # Extra version check in case wxversion lacks AlreadyImportedError;
        # then VersionError might have been raised and ignored when
        # there really *is* a problem with the version.
        major, minor = [int(n) for n in backend_version.split('.')[:2]]
        if major < 2 or (major < 3 and minor < 8):
            raise CheckFailed(
                "Requires wxPython 2.8, found %s" % backend_version)

        BackendAgg.force = True

        return "version %s" % backend_version


class BackendMacOSX(OptionalBackendPackage):
    name = 'macosx'

    def check_requirements(self):
        if sys.platform != 'darwin':
            raise CheckFailed("Mac OS-X only")

        return 'darwin'

    def get_extension(self):
        sources = [
            'src/_macosx.m',
            'src/agg_py_transforms.cpp',
            'src/path_cleanup.cpp'
            ]

        ext = make_extension('matplotlib.backends._macosx', sources)
        Numpy().add_flags(ext)
        LibAgg().add_flags(ext)
        CXX().add_flags(ext)
        ext.extra_link_args.extend(['-framework', 'Cocoa'])
        return ext


class Windowing(OptionalBackendPackage):
    """
    Builds the windowing extension.
    """
    name = "windowing"

    def check_requirements(self):
        if sys.platform != 'win32':
            raise CheckFailed("Microsoft Windows only")
        config = self.get_config()
        if config is False:
            raise CheckFailed("skipping due to configuration")
        return "installing"

    def get_extension(self):
        sources = [
            "src/_windowing.cpp"
            ]
        ext = make_extension('matplotlib._windowing', sources)
        ext.include_dirs.extend(['C:/include'])
        ext.libraries.extend(['user32'])
        ext.library_dirs.extend(['C:/lib'])
        ext.extra_link_args.append("-mwindows")
        return ext


class BackendQtBase(OptionalBackendPackage):

    def convert_qt_version(self, version):
        version = '%x' % version
        temp = []
        while len(version) > 0:
            version, chunk = version[:-2], version[-2:]
            temp.insert(0, str(int(chunk, 16)))
        return '.'.join(temp)

    def check_requirements(self):
        '''
        If PyQt4/PyQt5 is already imported, importing PyQt5/PyQt4 will fail
        so we need to test in a subprocess (as for Gtk3).
        '''
        try:
            p = multiprocessing.Pool()

        except:
            # Can't do multiprocessing, fall back to normal approach ( this will fail if importing both PyQt4 and PyQt5 )
            try:
                # Try in-process
                msg = self.callback(self)

            except RuntimeError:
                raise CheckFailed("Could not import: are PyQt4 & PyQt5 both installed?")

            except:
                # Raise any other exceptions
                raise

        else:
            # Multiprocessing OK
            try:
                msg = p.map(self.callback, [self])[0]
            except:
                # If we hit an error on multiprocessing raise it
                raise
            finally:
                # Tidy up multiprocessing
                p.close()
                p.join()

        return msg


def backend_qt4_internal_check(self):
    try:
        from PyQt4 import QtCore
    except ImportError:
        raise CheckFailed("PyQt4 not found")

    try:
        qt_version = QtCore.QT_VERSION
        pyqt_version_str = QtCore.QT_VERSION_STR
    except AttributeError:
        raise CheckFailed('PyQt4 not correctly imported')
    else:
        BackendAgg.force = True
        return ("Qt: %s, PyQt: %s" % (self.convert_qt_version(qt_version), pyqt_version_str))


class BackendQt4(BackendQtBase):
    name = "qt4agg"

    def __init__(self, *args, **kwargs):
        BackendQtBase.__init__(self, *args, **kwargs)
        self.callback = backend_qt4_internal_check


def backend_qt5_internal_check(self):
    try:
        from PyQt5 import QtCore
    except ImportError:
        raise CheckFailed("PyQt5 not found")

    try:
        qt_version = QtCore.QT_VERSION
        pyqt_version_str = QtCore.QT_VERSION_STR
    except AttributeError:
        raise CheckFailed('PyQt5 not correctly imported')
    else:
        BackendAgg.force = True
        return ("Qt: %s, PyQt: %s" % (self.convert_qt_version(qt_version), pyqt_version_str))


class BackendQt5(BackendQtBase):
    name = "qt5agg"

    def __init__(self, *args, **kwargs):
        BackendQtBase.__init__(self, *args, **kwargs)
        self.callback = backend_qt5_internal_check


def backend_pyside_internal_check(self):
    try:
        from PySide import __version__
        from PySide import QtCore
    except ImportError:
        raise CheckFailed("PySide not found")
    else:
        BackendAgg.force = True
        return ("Qt: %s, PySide: %s" %
                (QtCore.__version__, __version__))


class BackendPySide(BackendQtBase):
    name = "pyside"

    def __init__(self, *args, **kwargs):
        BackendQtBase.__init__(self, *args, **kwargs)
        self.callback = backend_pyside_internal_check



class BackendCairo(OptionalBackendPackage):
    name = "cairo"

    def check_requirements(self):
        try:
            import cairocffi
        except ImportError:
            try:
                import cairo
            except ImportError:
                raise CheckFailed("cairocffi or pycairo not found")
            else:
                return "pycairo version %s" % cairo.version
        else:
            return "cairocffi version %s" % cairocffi.version


class DviPng(SetupPackage):
    name = "dvipng"
    optional = True

    def check(self):
        try:
            output = check_output('dvipng -version', shell=True,
                                  stderr=subprocess.STDOUT)
            return "version %s" % output.splitlines()[1].decode().split()[-1]
        except (IndexError, ValueError, subprocess.CalledProcessError):
            raise CheckFailed()


class Ghostscript(SetupPackage):
    name = "ghostscript"
    optional = True

    def check(self):
        try:
            if sys.platform == 'win32':
                command = 'gswin32c --version'
                try:
                    output = check_output(command, shell=True,
                                          stderr=subprocess.STDOUT)
                except subprocess.CalledProcessError:
                    command = 'gswin64c --version'
                    output = check_output(command, shell=True,
                                          stderr=subprocess.STDOUT)
            else:
                command = 'gs --version'
                output = check_output(command, shell=True,
                                      stderr=subprocess.STDOUT)
            return "version %s" % output.decode()[:-1]
        except (IndexError, ValueError, subprocess.CalledProcessError):
            raise CheckFailed()


class LaTeX(SetupPackage):
    name = "latex"
    optional = True

    def check(self):
        try:
            output = check_output('latex -version', shell=True,
                                  stderr=subprocess.STDOUT)
            line = output.splitlines()[0].decode()
            pattern = '(3\.1\d+)|(MiKTeX \d+.\d+)'
            match = re.search(pattern, line)
            return "version %s" % match.group(0)
        except (IndexError, ValueError, AttributeError, subprocess.CalledProcessError):
            raise CheckFailed()


class PdfToPs(SetupPackage):
    name = "pdftops"
    optional = True

    def check(self):
        try:
            output = check_output('pdftops -v', shell=True,
                                  stderr=subprocess.STDOUT)
            for line in output.splitlines():
                line = line.decode()
                if 'version' in line:
                    return "version %s" % line.split()[2]
        except (IndexError, ValueError, subprocess.CalledProcessError):
            pass

        raise CheckFailed()