File: configure.py

package info (click to toggle)
python-qt4 4.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 40,300 kB
  • ctags: 6,185
  • sloc: python: 125,988; cpp: 13,291; xml: 292; makefile: 246; php: 27; sh: 2
file content (2303 lines) | stat: -rw-r--r-- 77,419 bytes parent folder | download
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
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
# This script generates the PyQt configuration and generates the Makefiles.
#
# Copyright (c) 2016 Riverbank Computing Limited <info@riverbankcomputing.com>
# 
# This file is part of PyQt4.
# 
# This file may be used under the terms of the GNU General Public License
# version 3.0 as published by the Free Software Foundation and appearing in
# the file LICENSE included in the packaging of this file.  Please review the
# following information to ensure the GNU General Public License version 3.0
# requirements will be met: http://www.gnu.org/copyleft/gpl.html.
# 
# If you do not wish to use this file under the terms of the GPL version 3.0
# then you may purchase a commercial license.  For more information contact
# info@riverbankcomputing.com.
# 
# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.


import sys
import os
import glob
import optparse
import shutil

import sipconfig


# Initialise the globals.
pyqt_version = 0x040c01
pyqt_version_str = "4.12.1"

sip_min_version = 0x041301

qt_version = 0
qt_edition = ""
qt_licensee = None
qt_dir = None
qt_incdir = None
qt_libdir = None
qt_bindir = None
qt_datadir = None
qt_archdatadir = None
qt_pluginsdir = None
qt_xfeatures = None
qt_shared = ''
qt_framework = 0

# This default value will be used by the code that bootstraps the Qt
# configuration.
qt_macx_spec = 'macx-g++'

qt_sip_flags = []

pyqt_modules = []
pyqt_modroot = None
src_dir = os.path.dirname(os.path.abspath(__file__))

# Get the SIP configuration.
sipcfg = sipconfig.Configuration()

pydbusmoddir = None
dbusincdirs = []
dbuslibdirs = []
dbuslibs = []


# Under Windows qmake and the Qt DLLs must be on the system PATH otherwise the
# dynamic linker won't be able to resolve the symbols.  On other systems we
# assume we can just run qmake by using its full pathname.
if sys.platform == 'win32':
    MSG_CHECK_QMAKE = "Make sure you have a working Qt qmake on your PATH."
else:
    MSG_CHECK_QMAKE = "Make sure you have a working Qt qmake on your PATH or use the -q argument to explicitly specify a working Qt qmake."


def find_default_qmake():
    """Find a default qmake, ie. the first on the path.
    """
    try:
        path = os.environ["PATH"]
    except KeyError:
        path = ""

    if sys.platform == 'win32':
        base_qmake = "qmake.exe"
    else:
        base_qmake = "qmake"

    for d in path.split(os.pathsep):
        qmake = os.path.join(d, base_qmake)
  
        if os.access(qmake, os.X_OK):
            return qmake

    return ""


def create_optparser():
    """Create the parser for the command line.
    """
    qmake = find_default_qmake()

    def store_abspath(option, opt_str, value, parser):
        setattr(parser.values, option.dest, os.path.abspath(value))

    def store_abspath_dir(option, opt_str, value, parser):
        if not os.path.isdir(value):
            raise optparse.OptionValueError("'%s' is not a directory" % value)
        setattr(parser.values, option.dest, os.path.abspath(value))
        
    def store_abspath_file(option, opt_str, value, parser):
        if not os.path.isfile(value):
            raise optparse.OptionValueError("'%s' is not a file" % value)
        setattr(parser.values, option.dest, os.path.abspath(value))

    p = optparse.OptionParser(usage="python %prog [opts] [macro=value] "
            "[macro+=value]", version=pyqt_version_str)

    # Note: we don't use %default to be compatible with Python 2.3.
    p.add_option("-k", "--static", action="store_true", default=False,
            dest="static", help="build modules as static libraries")
    p.add_option("--no-docstrings", action="store_true", default=False,
            dest="no_docstrings", help="disable the generation of docstrings")
    p.add_option("-r", "--trace", action="store_true", default=False,
            dest="tracing", help="build modules with tracing enabled")
    p.add_option("-u", "--debug", action="store_true", default=False,
            help="build modules with debugging symbols")
    p.add_option("-w", "--verbose", action="count", default=0, dest="verbose",
            help="verbose output during configuration")

    p.add_option("-c", "--concatenate", action="store_true", default=False,
            dest="concat", help="concatenate each module's C++ source files")
    p.add_option("-j", "--concatenate-split", type="int", default=1,
            metavar="N", dest="split",
            help="split the concatenated C++ source files into N pieces "
            "[default: 1]")
    p.add_option("-g", "--consolidate", action="store_true", default=False,
            dest="bigqt", help="create a single module which links against "
            "all the Qt libraries")

    # These are internal options used to build the mega Windows GPL package.
    p.add_option("--mwg-odbc", action="store_true", default=False,
            dest="mwg_odbc", help=optparse.SUPPRESS_HELP)
    p.add_option("--mwg-openssl", action="callback", default=None,
            dest="mwg_ssl_dir", metavar="DIR", callback=store_abspath_dir,
            type="string", help=optparse.SUPPRESS_HELP)
    p.add_option("--mwg-qsci", action="callback", default=None,
            dest="mwg_qsci_dir", metavar="DIR", callback=store_abspath_dir,
            type="string", help=optparse.SUPPRESS_HELP)
    p.add_option("--mwg-qwt", action="callback", default=None,
            dest="mwg_qwt_dir", metavar="DIR", callback=store_abspath_dir,
            type="string", help=optparse.SUPPRESS_HELP)

    # Configuration.
    g = optparse.OptionGroup(p, title="Configuration")
    g.add_option("--confirm-license", action="store_true", default=False,
            dest="license_confirmed", help="confirm acceptance of the license")
    g.add_option("-e", "--enable", action="append", default=[],
            metavar="MODULE", dest="enabled", help="enable checks for the "
            "specified MODULE [default: checks for all modules will be "
            "enabled]")
    g.add_option("--no-designer-plugin", action="store_false", default=True,
            dest="designer_plugin", help="disable the building of the "
            "Python plugin for Qt Designer [default: enabled]")
    g.add_option("-t", "--plugin", action="append", default=[],
            metavar="PLUGIN", dest="staticplugins", help="add PLUGIN to the "
            "list be linked (if Qt is built as static libraries)")
    g.add_option("--assume-shared", action="store_true", default=False,
            dest="assume_shared", help="assume that the Qt libraries have "
            "been built as shared libraries [default: check]")
    g.add_option("-T", "--no-timestamp", action="store_true", default=False,
            dest="no_timestamp", help="suppress timestamps in the header "
            "comments of generated code [default: include timestamps]")
    g.add_option("--no-deprecated", action="store_false", default=True,
            dest="with_deprecated", help="disable Qt v4 features deprecated "
            "in Qt v5 [default: enabled]")

    if sys.platform != 'win32':
        if sys.platform.startswith('linux') or sys.platform == 'darwin':
            pip_default = True
            pip_default_str = "enabled"
        else:
            pip_default = False
            pip_default_str = "disabled"

        g.add_option("--protected-is-public", action="store_true",
                default=pip_default, dest="prot_is_public",
                help="enable building with 'protected' redefined as 'public' "
                        "[default: %s]" % pip_default_str)
        g.add_option("--protected-not-public", action="store_false",
                dest="prot_is_public",
                help="disable building with 'protected' redefined as 'public'")
        g.add_option("-q", "--qmake", action="callback", metavar="FILE",
                default=qmake, dest="qmake", callback=store_abspath_file,
                type="string",
                help="the pathname of qmake [default: %s]" % (qmake or "none"))

    g.add_option("-s", "--dbus", action="callback", metavar="DIR",
            dest="pydbusincdir", callback=store_abspath_dir, type="string",
            help="the directory containing the dbus/dbus-python.h header file "
            "[default: supplied by pkg-config]")
    p.add_option_group(g)

    if sys.platform == 'darwin':
        g = optparse.OptionGroup(p, title="MacOS X Configuration")
        g.add_option("--use-arch", action="append", metavar="ARCH",
                dest="use_arch", choices=["i386", "x86_64", "ppc"],
                help="add ARCH to the list of architectures to use when "
                        "running pyuic4 [default: system default]")
        p.add_option_group(g)

    # Installation.
    g = optparse.OptionGroup(p, title="Installation")
    g.add_option("-b", "--bindir", action="callback",
            default=sipcfg.default_bin_dir, type="string", metavar="DIR",
            dest="pyqtbindir", callback=store_abspath, help="where pyuic4, "
            "pyrcc4 and pylupdate4 will be installed [default: %s]" %
            sipcfg.default_bin_dir)
    g.add_option("-d", "--destdir", action="callback",
            default=sipcfg.default_mod_dir, type="string", metavar="DIR",
            dest="pyqtmoddir", callback=store_abspath, help="where the PyQt4 "
            "Python package will be installed [default: %s]" %
            sipcfg.default_mod_dir)
    g.add_option("-p", "--plugin-destdir", action="callback", type="string",
            metavar="DIR", dest="plugindir", callback=store_abspath,
            help="where any plugins will be installed [default: "
            "QTDIR/plugins]")
    g.add_option("--no-sip-files", action="store_false", default=True,
            dest="install_sipfiles", help="disable the installation of the "
            ".sip files [default: enabled]")
    g.add_option("-v", "--sipdir", action="callback",
            default=os.path.join(sipcfg.default_sip_dir, "PyQt4"),
            metavar="DIR", dest="pyqtsipdir", callback=store_abspath,
            type="string", help="where the PyQt4 .sip files will be installed "
            "[default: %s]" % sipcfg.default_sip_dir)
    p.add_option_group(g)

    # Vendor ID.
    g = optparse.OptionGroup(p, title="VendorID support")
    g.add_option("-i", "--vendorid", action="store_true", default=False,
            dest="vendorcheck", help="enable checking of signed interpreters "
            "using the VendorID package [default: disabled]")
    g.add_option("-l", "--vendorid-incdir", action="callback",
            default=sipcfg.py_inc_dir, type="string", metavar="DIR",
            dest="vendincdir", callback=store_abspath_dir, help="the "
            "directory containing the VendorID header file [default: %s]" %
            sipcfg.py_inc_dir)
    g.add_option("-m", "--vendorid-libdir", action="callback",
            default=sipcfg.py_lib_dir, type="string", metavar="DIR",
            dest="vendlibdir", callback=store_abspath_dir, help="the "
            "directory containing the VendorID library [default: %s]" %
            sipcfg.py_lib_dir)
    p.add_option_group(g)

    # QScintilla.
    g = optparse.OptionGroup(p, title="QScintilla support")
    g.add_option("-a", "--qsci-api", action="store_true", default=None,
            dest="api", help="always install the PyQt API file for QScintilla "
            "[default: install only if QScintilla installed]")
    g.add_option("--no-qsci-api", action="store_false", default=None,
            dest="api", help="do not install the PyQt API file for QScintilla "
            "[default: install only if QScintilla installed]")
    g.add_option("-n", "--qsci-api-destdir", action="callback", dest="qscidir",
            metavar="DIR", callback=store_abspath, type="string", help="where "
            "the PyQt API file for QScintilla will be installed [default: "
            "QTDIR/qsci]")
    p.add_option_group(g)

    return p


class pyrccMakefile(sipconfig.ProgramMakefile):
    """This class implements the Makefile for pyrcc.  This was specialised so
    that pyrcc was automatically run against the examples (no longer done).
    """

    def __init__(self):
        sipconfig.ProgramMakefile.__init__(self, configuration=sipcfg,
                build_file=os.path.join(src_dir, "pyrcc", "pyrcc.sbf"),
                dir="pyrcc", install_dir=opts.pyqtbindir, console=1,
                qt=["QtCore", "QtXml"], debug=opts.debug, warnings=1,
                universal=sipcfg.universal, arch=sipcfg.arch,
                deployment_target=sipcfg.deployment_target)


class ConfigurePyQt4:
    """This class defines the methods to configure PyQt4.
    """
    def __init__(self, generator):
        self.generator = generator

    def qt_version_tags(self):
        """Get the versions tags for the configuration.

        Returns a dictionary of versions and corresponding tags.
        """
        return {
            0x040101: None,
            0x040102: "Qt_4_1_1",
            0x040103: "Qt_4_1_2",
            0x040200: "Qt_4_1_3",
            0x040202: "Qt_4_2_0",
            0x040300: "Qt_4_2_2",
            0x040303: "Qt_4_3_0",
            0x040400: "Qt_4_3_3",
            0x040401: "Qt_4_4_0",
            0x040500: "Qt_4_4_1",
            0x040501: "Qt_4_5_0",
            0x040600: "Qt_4_5_1",
            0x040601: "Qt_4_6_0",
            0x040602: "Qt_4_6_1",
            0x040603: "Qt_4_6_2",
            0x040700: "Qt_4_6_3",
            0x040701: "Qt_4_7_0",
            0x040702: "Qt_4_7_1",
            0x040800: "Qt_4_7_2",
            0x040803: "Qt_4_8_0",
            0x040804: "Qt_4_8_3",
            0x040806: "Qt_4_8_4",
            0x050000: "Qt_4_8_6",
            0x060000: "Qt_5_0_0"
        }

    def check_modules(self):
        if opts.mwg_odbc:
            sql_libs = ["odbc32"]
        else:
            sql_libs = None

        if opts.mwg_ssl_dir:
            ass_lib_dirs = [os.path.join(opts.mwg_ssl_dir, "lib")]
            ass_libs = ["ssleay32", "libeay32"]
        else:
            ass_lib_dirs = None
            ass_libs = None

        # Note that the order in which we check is important for the
        # consolidated module - a module's dependencies must be checked first.
        pyqt_modules.append("QtCore")

        check_module("QtGui", "qwidget.h", "new QWidget()")
        check_module("QtHelp", "qhelpengine.h", "new QHelpEngine(\"foo\")")
        check_module("QtMultimedia", "QAudioDeviceInfo",
                "new QAudioDeviceInfo()")
        check_module("QtNetwork", "qhostaddress.h", "new QHostAddress()")

        # Qt v4.7 was current when we added support for QtDBus and we didn't
        # bother properly versioning its API.
        if qt_version >= 0x040700:
            check_module("QtDBus", "qdbusconnection.h",
                    "QDBusConnection::systemBus()")

        if qt_version < 0x050000 or opts.with_deprecated:
            check_module("QtDeclarative", "qdeclarativeview.h",
                    "new QDeclarativeView()")
            check_module("QtScript", "qscriptengine.h", "new QScriptEngine()")
            check_module("QtScriptTools", "qscriptenginedebugger.h",
                    "new QScriptEngineDebugger()")

        check_module("QtOpenGL", "qgl.h", "new QGLWidget()")
        check_module("QtSql", "qsqldatabase.h", "new QSqlDatabase()",
                extra_libs=sql_libs)
        check_module("QtSvg", "qsvgwidget.h", "new QSvgWidget()")
        check_module("QtTest", "QtTest", "QTest::qSleep(0)")
        check_module("QtWebKit", "qwebpage.h", "new QWebPage()")
        check_module("QtXml", "qdom.h", "new QDomDocument()")
        check_module("QtXmlPatterns", "qxmlname.h", "new QXmlName()")
        check_module("phonon", "phonon/videowidget.h",
                "new Phonon::VideoWidget()")
        check_module("QtAssistant", "qassistantclient.h",
                "new QAssistantClient(\"foo\")", extra_lib_dirs=ass_lib_dirs,
                extra_libs=ass_libs)

        if qt_shared == '':
            sipconfig.inform("QtDesigner module disabled with static Qt libraries.")
        else:
            check_module("QtDesigner", "QExtensionFactory",
                    "new QExtensionFactory()")

        check_module("QAxContainer", "qaxobject.h", "new QAxObject()")

        if os.path.isdir(os.path.join(src_dir, "dbus")):
            check_dbus()

    def code(self):
        cons_xtra_incdirs = []
        cons_xtra_libdirs = []
        cons_xtra_libs = []

        sp_libs, sp_libdirs = self._static_plugins("QtCore")
        sp_incdirs = []

        qpy_inc_dir, qpy_lib_dir, qpy_lib = self._qpy_directories("QtCore", "qpycore")
        sp_incdirs.append(qpy_inc_dir)
        sp_libdirs.append(qpy_lib_dir)
        sp_libs.append(qpy_lib)

        if opts.vendorcheck:
            sp_incdirs.append(opts.vendincdir)
            sp_libdirs.append(opts.vendlibdir)
            sp_libs.append("vendorid")

        if opts.bigqt:
            cons_xtra_incdirs.extend(sp_incdirs)
            cons_xtra_libdirs.extend(sp_libdirs)
            cons_xtra_libs.extend(sp_libs)

            generate_code("QtCore")
        else:
            generate_code("QtCore", extra_include_dirs=sp_incdirs,
                        extra_lib_dirs=sp_libdirs, extra_libs=sp_libs)

        if "QtDeclarative" in pyqt_modules:
            qpy_inc_dir, qpy_lib_dir, qpy_lib = self._qpy_directories("QtDeclarative", "qpydeclarative")

            if opts.bigqt:
                cons_xtra_incdirs.append(qpy_inc_dir)
                cons_xtra_libdirs.append(qpy_lib_dir)
                cons_xtra_libs.append(qpy_lib)

                generate_code("QtDeclarative")
            else:
                generate_code("QtDeclarative", extra_include_dirs=[qpy_inc_dir],
                        extra_lib_dirs=[qpy_lib_dir], extra_libs=[qpy_lib])

        if "QtGui" in pyqt_modules:
            sp_libs, sp_libdirs = self._static_plugins("QtGui")
            sp_incdirs = []

            qpy_inc_dir, qpy_lib_dir, qpy_lib = self._qpy_directories("QtGui", "qpygui")
            sp_incdirs.append(qpy_inc_dir)
            sp_libdirs.append(qpy_lib_dir)
            sp_libs.append(qpy_lib)

            if opts.bigqt:
                cons_xtra_incdirs.extend(sp_incdirs)
                cons_xtra_libdirs.extend(sp_libdirs)
                cons_xtra_libs.extend(sp_libs)

                generate_code("QtGui")
            else:
                generate_code("QtGui", extra_include_dirs=sp_incdirs,
                        extra_lib_dirs=sp_libdirs, extra_libs=sp_libs)

        if "QtHelp" in pyqt_modules:
            generate_code("QtHelp")

        if "QtMultimedia" in pyqt_modules:
            generate_code("QtMultimedia")

        if "QtNetwork" in pyqt_modules:
            generate_code("QtNetwork")

        if "QtDBus" in pyqt_modules:
            qpy_inc_dir, qpy_lib_dir, qpy_lib = self._qpy_directories("QtDBus", "qpydbus")

            if opts.bigqt:
                cons_xtra_incdirs.append(qpy_inc_dir)
                cons_xtra_libdirs.append(qpy_lib_dir)
                cons_xtra_libs.append(qpy_lib)

                generate_code("QtDBus")
            else:
                generate_code("QtDBus", extra_include_dirs=[qpy_inc_dir],
                        extra_lib_dirs=[qpy_lib_dir], extra_libs=[qpy_lib])

        if "QtOpenGL" in pyqt_modules:
            qpy_inc_dir, qpy_lib_dir, qpy_lib = self._qpy_directories("QtOpenGL", "qpyopengl")

            if opts.bigqt:
                cons_xtra_incdirs.append(qpy_inc_dir)
                cons_xtra_libdirs.append(qpy_lib_dir)
                cons_xtra_libs.append(qpy_lib)

                generate_code("QtOpenGL")
            else:
                generate_code("QtOpenGL", extra_include_dirs=[qpy_inc_dir],
                        extra_lib_dirs=[qpy_lib_dir], extra_libs=[qpy_lib])

        if "QtScript" in pyqt_modules:
            generate_code("QtScript")

        if "QtScriptTools" in pyqt_modules:
            generate_code("QtScriptTools")

        if "QtSql" in pyqt_modules:
            sp_libs, sp_libdirs = self._static_plugins("QtSql")

            if opts.bigqt:
                cons_xtra_libdirs.extend(sp_libdirs)
                cons_xtra_libs.extend(sp_libs)

                generate_code("QtSql")
            else:
                generate_code("QtSql", extra_lib_dirs=sp_libdirs,
                        extra_libs=sp_libs)

        if "QtSvg" in pyqt_modules:
            generate_code("QtSvg")

        if "QtTest" in pyqt_modules:
            generate_code("QtTest")

        if "QtWebKit" in pyqt_modules:
            generate_code("QtWebKit")

        if "QtXml" in pyqt_modules:
            generate_code("QtXml")

        if "QtXmlPatterns" in pyqt_modules:
            generate_code("QtXmlPatterns")

        if "phonon" in pyqt_modules:
            generate_code("phonon")

        if "QtAssistant" in pyqt_modules:
            generate_code("QtAssistant")

        if "QtDesigner" in pyqt_modules:
            qpy_inc_dir, qpy_lib_dir, qpy_lib = self._qpy_directories("QtDesigner", "qpydesigner")

            if opts.bigqt:
                cons_xtra_incdirs.append(qpy_inc_dir)
                cons_xtra_libdirs.append(qpy_lib_dir)
                cons_xtra_libs.append(qpy_lib)

                generate_code("QtDesigner")
            else:
                generate_code("QtDesigner", extra_include_dirs=[qpy_inc_dir],
                        extra_lib_dirs=[qpy_lib_dir], extra_libs=[qpy_lib])

        if "QAxContainer" in pyqt_modules:
            generate_code("QAxContainer")

        # Generate the composite module.
        qtmod_sipdir = os.path.join("sip", "Qt")
        mk_clean_dir(qtmod_sipdir)

        qtmod_sipfile = os.path.join(qtmod_sipdir, "Qtmod.sip")
        f = open(qtmod_sipfile, "w")

        f.write("""%CompositeModule PyQt4.Qt

""")

        for m in pyqt_modules:
            f.write("%%Include %s/%smod.sip\n" % (m, m))

        f.close()

        generate_code("Qt")

        # Generate the consolidated module if required.
        if opts.bigqt:
            xtra_sip_flags = []

            _qtmod_sipdir = os.path.join("sip", "_qt")
            mk_clean_dir(_qtmod_sipdir)

            _qtmod_sipfile = os.path.join(_qtmod_sipdir, "_qtmod.sip")
            f = open(_qtmod_sipfile, "w")

            f.write("""%ConsolidatedModule PyQt4._qt

""")

            for m in pyqt_modules:
                f.write("%%Include %s/%smod.sip\n" % (m, m))

            if opts.mwg_qsci_dir:
                f.write("%Include Qsci/Qscimod.sip\n")
                cons_xtra_libs.append("qscintilla2")

                # Copy in the QScintilla .sip files and fix the main one.
                src_dir = os.path.join(opts.mwg_qsci_dir, "Python", "sip")
                dst_dir = os.path.join("sip", "Qsci")

                try:
                    shutil.rmtree(dst_dir);
                except:
                    pass

                shutil.copytree(src_dir, dst_dir)
                os.rename(os.path.join(dst_dir, "qscimod4.sip"), os.path.join(dst_dir, "Qscimod.sip"))

                generate_code("Qsci")

            if opts.mwg_qwt_dir:
                f.write("%Include Qwt5/Qwt5mod.sip\n")
                cons_xtra_incdirs.append(os.path.join(opts.mwg_qwt_dir, "support"))
                cons_xtra_libs.append("qwt")

                # Copy in the PyQwt .sip files and fix the main one.
                src_dir = os.path.join(opts.mwg_qwt_dir, "sip", "qwt5qt4")
                dst_dir = os.path.join("sip", "Qwt5")

                try:
                    shutil.rmtree(dst_dir);
                except:
                    pass

                shutil.copytree(src_dir, dst_dir)
                os.rename(os.path.join(dst_dir, "QwtModule.sip"), os.path.join(dst_dir, "Qwt5mod.sip"))

                xtra_sip_flags = ["-t", "Qwt_5_0_1",
                                  "-x", "CXX_DYNAMIC_CAST",
                                  "-x", "HAS_QWT4",
                                  "-x", "HAS_NUMARRAY",
                                  "-x", "HAS_NUMERIC",
                                  "-x", "HAS_NUMPY"]

                generate_code("Qwt5", extra_sip_flags=xtra_sip_flags)

            f.close()

            if opts.mwg_odbc:
                cons_xtra_libs.append("odbc32")

            if opts.mwg_ssl_dir:
                cons_xtra_libdirs.append(os.path.join(opts.mwg_ssl_dir, "lib"))
                cons_xtra_libs.extend(["ssleay32", "libeay32"])

            generate_code("_qt", extra_include_dirs=cons_xtra_incdirs,
                    extra_lib_dirs=cons_xtra_libdirs,
                    extra_libs=cons_xtra_libs, extra_sip_flags=xtra_sip_flags)

        # Tell the user about any plugins not found.
        if opts.staticplugins:
            sipconfig.inform("Unable to find the following static plugins: %s" % ", ".join(opts.staticplugins))

        # Generate the QScintilla API file.
        sipconfig.inform("Creating QScintilla API file...")
        f = open("PyQt4.api", "w")

        for m in pyqt_modules:
            api = open(m + ".api")

            for l in api:
                f.write("PyQt4." + l)

            api.close()
            os.remove(m + ".api")

        f.close()

    def _qpy_directories(self, mname, lib_name):
        """Return a 3-tuple of the directories containing the header files, the
        directory containing the library, and the name of the support library
        for the given module.

        mname is the name of the module.
        lib_name is the normal name of the support library.  (Note that this is
        historical and the unchanged name is always returned.)
        """
        qpy_dir = os.path.join("qpy", mname)

        if sys.platform == 'win32':
            if opts.debug:
                qpy_lib_dir = os.path.join(qpy_dir, "debug")
            else:
                qpy_lib_dir = os.path.join(qpy_dir, "release")
        else:
            qpy_lib_dir = qpy_dir

        return os.path.join(src_dir, qpy_dir), os.path.abspath(qpy_lib_dir), lib_name

    def _static_plugins(self, mname):
        """Return a tuple of the libraries (in platform neutral format) and the
        directories they are contained in for all the requested static plugins
        for the given module.  Generate the additional .sip file needed to
        ensure the plugins get linked.

        mname is the name of the module.
        """
        plugin_dirs = {
            "QtCore":   ("codecs", ),
            # Note that we put iconengines after imageformats so that qsvg is
            # found in the latter rather than the former.  The name clash is
            # probably a Qt bug.
            "QtGui":    ("inputmethods", "imageformats", "iconengines"),
            "QtSql":    ("sqldrivers", )
        }

        libs = []
        libdirs = []

        for plug in opts.staticplugins:
            # Convert the plugin name to a platform specific filename.
            if self.generator in ("MSVC", "MSVC.NET", "BMAKE"):
                pfname = plug + ".lib"
            else:
                pfname = "lib" + plug + ".a"

            for pdir in plugin_dirs[mname]:
                ppath = os.path.join(qt_pluginsdir, pdir)

                # See if the plugin exists.
                if os.access(os.path.join(ppath, pfname), os.F_OK):
                    sipconfig.inform("Adding the %s static plugin to the %s module..." % (plug, mname))

                    libs.append(plug)

                    if ppath not in libdirs:
                        libdirs.append(ppath)

                    break

        # Remove those plugins we have handled.
        opts.staticplugins = [p for p in opts.staticplugins if p not in libs]

        # If we have any plugins for this module then generate a .sip file that
        # will include the code needed to ensure the plugin gets linked.
        if libs:
            sp_sipfile = os.path.join("sip", mname, "staticplugins.sip")

            f = open(sp_sipfile, "w")

            f.write("""%ModuleCode

#include <QtPlugin>

""")

            for l in libs:
                f.write("Q_IMPORT_PLUGIN(%s)\n" % l)

            f.write("""
%End
""")

            f.close()

        return libs, libdirs

    def module_installs(self):
        return [os.path.join(src_dir, "__init__.py"), "pyqtconfig.py"]

    def qpy_libs(self):
        # See which QPy support libraries to build.
        qpylibs = {}

        if "QtCore" in pyqt_modules:
            qpylibs["QtCore"] = "qpycore.pro"

        if "QtGui" in pyqt_modules:
            qpylibs["QtGui"] = "qpygui.pro"

        if "QtDBus" in pyqt_modules:
            qpylibs["QtDBus"] = "qpydbus.pro"

        if "QtDeclarative" in pyqt_modules:
            qpylibs["QtDeclarative"] = "qpydeclarative.pro"

        if "QtDesigner" in pyqt_modules:
            qpylibs["QtDesigner"] = "qpydesigner.pro"

        if "QtOpenGL" in pyqt_modules:
            qpylibs["QtOpenGL"] = "qpyopengl.pro"

        # Run qmake to generate the Makefiles.
        qmake_args = fix_qmake_args()
        cwd = os.getcwd()

        for qpy, pro in qpylibs.items():
            sipconfig.inform("Creating QPy support library for %s Makefile..." % qpy)

            qpydir = os.path.join("qpy", qpy)
            mk_clean_dir(qpydir, clean=0)
            os.chdir(qpydir)

            wrapped_pro = "w_" + pro

            f = open(wrapped_pro, 'w+')

            if sipcfg.arch:
                f.write(arch_config())

            if sipcfg.universal:
                f.write("QMAKE_MAC_SDK = %s\n" % sipcfg.universal)

            if sipcfg.deployment_target:
                f.write("QMAKE_MACOSX_DEPLOYMENT_TARGET = %s\n" % sipcfg.deployment_target)

            inc_path = [sipcfg.py_inc_dir]
            if qpy in ("QtCore", "QtDBus", "QtDeclarative", "QtOpenGL"):
                if sipcfg.sip_inc_dir != sipcfg.py_inc_dir:
                    inc_path.insert(0, sipcfg.sip_inc_dir)

                if sipcfg.py_conf_inc_dir != sipcfg.py_inc_dir:
                    inc_path.insert(0, sipcfg.py_conf_inc_dir)

                if opts.bigqt:
                    api_dir = "../../_qt"
                else:
                    api_dir = "../../" + qpy

                inc_path.append(api_dir)

            if opts.debug:
                pro_config = 'debug'
            else:
                pro_config = 'release'

            if src_dir != os.path.curdir:
                src_qpydir = os.path.join(src_dir, "qpy", qpy)
                pro = os.path.join(src_qpydir, pro)
                vpath = "VPATH = " + src_qpydir
                inc_path.append(src_qpydir)
            else:
                vpath = ""

            f.write(
"""# Tell the original .pro file about additional directories.
INCLUDEPATH = %s
CONFIG += %s
%s
include(%s)
""" % (" ".join(['"' + i + '"' for i in inc_path]), pro_config, vpath, pro))

            f.close()

            run_command("%s %s %s" % (opts.qmake, qmake_args, wrapped_pro))
            os.chdir(cwd)

        sipconfig.inform("Creating QPy support libraries Makefile...")

        sipconfig.ParentMakefile(
            configuration=sipcfg,
            dir="qpy",
            subdirs=list(qpylibs.keys())
        ).generate()

        return ["qpy"]

    def tools(self):
        tool = []

        if pydbusmoddir:
            sipconfig.inform("Creating dbus support module Makefile...")

            makefile = sipconfig.ModuleMakefile(
                configuration=sipcfg,
                build_file=os.path.join(src_dir, "dbus", "dbus.sbf"),
                dir="dbus",
                install_dir=pydbusmoddir,
                qt=["QtCore"],
                debug=opts.debug,
                universal=sipcfg.universal,
                arch=sipcfg.arch,
                deployment_target=sipcfg.deployment_target
            )

            add_makefile_extras(makefile, dbusincdirs, dbuslibdirs, dbuslibs)

            makefile.generate()
            tool.append("dbus")

        # The pyuic directory may not exist if we are building away from the
        # source directory.
        try:
            os.mkdir("pyuic")
        except OSError:
            pass

        uicdir=os.path.join(pyqt_modroot, "uic")

        sipconfig.inform("Creating pyuic4 Makefile...")

        makefile = sipconfig.PythonModuleMakefile(
            configuration=sipcfg,
            dstdir=uicdir,
            srcdir=os.path.join(src_dir, "pyuic", "uic"),
            dir="pyuic",
        )

        makefile.generate()
        tool.append("pyuic")

        sipconfig.inform("Creating pylupdate4 Makefile...")

        cxxflags_app = sipcfg.build_macros().get("CXXFLAGS_APP", "")

        makefile = sipconfig.ProgramMakefile(
            configuration=sipcfg,
            build_file=os.path.join(src_dir, "pylupdate", "pylupdate.sbf"),
            dir="pylupdate",
            install_dir=opts.pyqtbindir,
            console=1,
            qt=["QtCore", "QtXml"],
            debug=opts.debug,
            warnings=1,
            universal=sipcfg.universal,
            arch=sipcfg.arch,
            deployment_target=sipcfg.deployment_target
        )

        makefile.extra_include_dirs.append(os.path.join(src_dir, "pylupdate"))

        if cxxflags_app != "":
            makefile.extra_cxxflags.append(cxxflags_app)

        makefile.generate()
        tool.append("pylupdate")

        sipconfig.inform("Creating pyrcc4 Makefile...")

        makefile = pyrccMakefile()

        if cxxflags_app != "":
            makefile.extra_cxxflags.append(cxxflags_app)

        makefile.generate()
        tool.append("pyrcc")

        if opts.designer_plugin and "QtDesigner" in pyqt_modules:
            py_major = sipcfg.py_version >> 16
            py_minor = (sipcfg.py_version >> 8) & 0x0ff

            abi = getattr(sys, 'abiflags', '')

            if sys.platform == 'win32':
                # Use abiflags in case it is supported in a future version.
                lib_dir_flag = quote("-L%s" % sipcfg.py_lib_dir)
                link = "%s -lpython%d%d%s" % (lib_dir_flag, py_major, py_minor, abi)
                pysh_lib = "python%d%d%s.dll" % (py_major, py_minor, abi)
            else:
                # Use distutils to get the additional configuration.
                from distutils.sysconfig import get_config_vars
                ducfg = get_config_vars()

                config_args = ducfg.get("CONFIG_ARGS", "")

                if sys.platform == "darwin":
                    dynamic_pylib = "--enable-framework" in config_args

                    # It's probably a Python bug that the library name doesn't
                    # include the ABI information.
                    abi = ""
                else:
                    dynamic_pylib = "--enable-shared" in config_args

                if dynamic_pylib:
                    if glob.glob("%s/lib/libpython%d.%d*" % (ducfg["exec_prefix"], py_major, py_minor)):
                        lib_dir_flag = quote("-L%s/lib" % ducfg["exec_prefix"])
                    elif 'MULTIARCH' in ducfg and glob.glob('%s/lib/%s/libpython%d.%d*' % (ducfg['exec_prefix'], ducfg['MULTIARCH'], py_major, py_minor)):
                        lib_dir_flag = quote('-L%s/lib/%s' % (ducfg['exec_prefix'], ducfg['MULTIARCH']))
                    elif glob.glob("%s/libpython%d.%d*" % (ducfg["LIBDIR"], py_major, py_minor)):
                        lib_dir_flag = quote("-L%s" % ducfg["LIBDIR"])
                    else:
                        sipconfig.inform("Qt Designer plugin disabled because Python library couldn't be found")
                        lib_dir_flag = ''
                        opts.designer_plugin = False

                    link = "%s -lpython%d.%d%s" % (lib_dir_flag, py_major, py_minor, abi)
                else:
                    sipconfig.inform("The Qt Designer plugin was disabled because a dynamic Python library couldn't be found")
                    opts.designer_plugin = False

                pysh_lib = ducfg.get("LDLIBRARY", "")

            if opts.designer_plugin:
                sipconfig.inform("Creating Qt Designer plugin Makefile...")

                # Run qmake to generate the Makefile.
                qmake_args = fix_qmake_args()
                cwd = os.getcwd()

                mk_clean_dir("designer", clean=0)
                os.chdir("designer")

                # Create the qmake project file.
                fin = open(os.path.join(src_dir, "designer", "designer.pro-in"))
                prj = fin.read()
                fin.close()

                prj = prj.replace("@PYINCDIR@", quote(sipcfg.py_inc_dir))
                prj = prj.replace("@PYINCDIR@", " ".join((quote(sipcfg.py_conf_inc_dir), quote(sipcfg.py_inc_dir))))
                prj = prj.replace("@PYLINK@", link)
                prj = prj.replace("@PYSHLIB@", pysh_lib)
                prj = prj.replace("@QTPLUGINDIR@", quote(opts.plugindir + "/designer"))

                fout = open("designer.pro", "w+")

                if sipcfg.arch:
                    fout.write(arch_config())

                if sipcfg.universal:
                    fout.write("QMAKE_MAC_SDK = %s\n" % sipcfg.universal)

                if sipcfg.deployment_target:
                    fout.write("QMAKE_MACOSX_DEPLOYMENT_TARGET = %s\n" % sipcfg.deployment_target)

                if src_dir != os.path.curdir:
                    fout.write("VPATH = %s\n" % os.path.join(src_dir, "designer"))

                fout.write("QMAKE_CFLAGS += %s\n" % macros["CFLAGS"])
                fout.write("QMAKE_CXXFLAGS += %s\n" % macros["CXXFLAGS"])
                fout.write("QMAKE_LFLAGS += %s\n" % macros["LFLAGS"])

                fout.write(prj)
                fout.close()

                run_command("%s %s" % (opts.qmake, qmake_args))
                os.chdir(cwd)

                tool.append("designer")

        return tool


def arch_config():
    """Return the qmake CONFIG line for a MacOS binary."""

    qmake_archs = []
    for a in sipcfg.arch.split():
        if a == 'i386':
            qmake_archs.append('x86')
        elif a == 'x86_64':
            qmake_archs.append('x86_64')
        elif a == 'ppc':
            qmake_archs.append('ppc')

    return 'CONFIG += %s\n' % ' '.join(qmake_archs)


def quote(path):
    """Return a path with quotes added if it contains spaces."""
    if " " in path:
        path = '"%s"' % path

    return path


def inform_user():
    """Tell the user the option values that are going to be used.
    """
    if qt_edition:
        edstr = qt_edition + " edition "
    else:
        edstr = ""

    if qt_shared:
        lib_type = "shared"
    else:
        lib_type = "static"

    sipconfig.inform("Qt v%s %sis being used." % (sipconfig.version_to_string(qt_version), edstr))

    if qt_licensee:
        sipconfig.inform("Qt is licensed to %s." % qt_licensee)

    if sys.platform == "darwin" and qt_framework:
        sipconfig.inform("Qt is built as a framework.")

    sipconfig.inform("SIP %s is being used." % sipcfg.sip_version_str)
    sipconfig.inform("The Qt header files are in %s." % qt_incdir)
    sipconfig.inform("The %s Qt libraries are in %s." % (lib_type, qt_libdir))
    sipconfig.inform("The Qt binaries are in %s." % qt_bindir)
    sipconfig.inform("The Qt mkspecs directory is in %s." % qt_archdatadir)
    sipconfig.inform("These PyQt modules will be built: %s." % ", ".join(pyqt_modules))
    sipconfig.inform("The PyQt Python package will be installed in %s." % opts.pyqtmoddir)

    if qt_version >= 0x050000:
        if opts.with_deprecated:
            sipconfig.inform("PyQt is being built with deprecated Qt v4 features.")
        else:
            sipconfig.inform("PyQt is being built without deprecated Qt v4 features.")

    if opts.no_docstrings:
        sipconfig.inform("PyQt is being built without generated docstrings.")
    else:
        sipconfig.inform("PyQt is being built with generated docstrings.")

    if opts.prot_is_public:
        sipconfig.inform("PyQt is being built with 'protected' redefined as 'public'.")

    if opts.designer_plugin:
        sipconfig.inform("The Designer plugin will be installed in %s." % os.path.join(opts.plugindir, "designer"))

    if opts.api:
        sipconfig.inform("The QScintilla API file will be installed in %s." % os.path.join(opts.qscidir, "api", "python"))

    if pydbusmoddir:
        sipconfig.inform("The dbus support module will be installed in %s." % pydbusmoddir)

    sipconfig.inform("The PyQt .sip files will be installed in %s." % opts.pyqtsipdir)

    sipconfig.inform("pyuic4, pyrcc4 and pylupdate4 will be installed in %s." % opts.pyqtbindir)

    if opts.vendorcheck:
        sipconfig.inform("PyQt will only be usable with signed interpreters.")


def create_config(module, template, macros):
    """Create the PyQt configuration module so that it can be imported by build
    scripts.

    module is the module file name.
    template is the template file name.
    macros is the dictionary of platform specific build macros.
    """
    sipconfig.inform("Creating %s..." % module)

    content = {
        "pyqt_config_args":   sys.argv[1:],
        "pyqt_version":       pyqt_version,
        "pyqt_version_str":   pyqt_version_str,
        "pyqt_bin_dir":       opts.pyqtbindir,
        "pyqt_mod_dir":       pyqt_modroot,
        "pyqt_sip_dir":       opts.pyqtsipdir,
        "pyqt_modules":       pyqt_modules,
        "pyqt_sip_flags":     qt_sip_flags,
        "qt_version":         qt_version,
        "qt_edition":         qt_edition,
        "qt_winconfig":       qt_shared,
        "qt_framework":       qt_framework,
        "qt_threaded":        1,
        "qt_dir":             qt_dir,
        "qt_data_dir":        qt_datadir,
        "qt_archdata_dir":    qt_archdatadir,
        "qt_inc_dir":         qt_incdir,
        "qt_lib_dir":         qt_libdir
    }

    sipconfig.create_config_module(module, template, content, macros)


def run_command(cmd, envvars=None):
    """Run a command and display the output if verbose mode is enabled.

    cmd is the command to run.
    """
    if opts.verbose:
        sys.stdout.write(cmd + "\n")

    fout = get_command_stdout(cmd, and_stderr=True, envvars=envvars)

    # Read stdout and stderr until there is no more output.
    lout = fout.readline()
    while lout:
        if opts.verbose:
            if sys.hexversion >= 0x03000000:
                sys.stdout.write(str(lout, encoding=sys.stdout.encoding))
            else:
                sys.stdout.write(lout)

        lout = fout.readline()

    fout.close()

    try:
        os.wait()
    except:
        pass


def remove_file(fname):
    """Remove a file which may or may not exist.

    fname is the name of the file.
    """
    try:
        os.remove(fname)
    except OSError:
        pass


def compile_qt_program(name, mname, extra_include_dirs=None, extra_lib_dirs=None, extra_libs=None):
    """Compile a simple Qt application.

    name is the name of the single source file.
    mname is the name of the Qt module.
    extra_include_dirs is an optional list of extra include directories.
    extra_lib_dirs is an optional list of extra library directories.
    extra_libs is an optional list of extra libraries.

    Returns the name of the executable suitable for running or None if it
    wasn't created.
    """
    opengl = (mname == "QtOpenGL")

    qt = []
    needed_qt_libs(mname, qt)

    makefile = sipconfig.ProgramMakefile(sipcfg, console=1, qt=qt, warnings=0,
            opengl=opengl, debug=opts.debug, arch=sipcfg.arch,
            deployment_target=sipcfg.deployment_target)

    add_makefile_extras(makefile, extra_include_dirs, extra_lib_dirs, extra_libs)

    exe, build = makefile.build_command(name)

    if sipcfg.deployment_target:
        envvars = {'MACOSX_DEPLOYMENT_TARGET': '%s' % sipcfg.deployment_target}
    else:
        envvars = None

    # Make sure the executable file doesn't exist.
    remove_file(exe)
    run_command(build, envvars=envvars)

    if not os.access(exe, os.X_OK):
        return None

    if sys.platform != 'win32':
        exe = "./" + exe

    return exe


def add_makefile_extras(makefile, extra_include_dirs, extra_lib_dirs, extra_libs):
    """Add any extra include or library directories or libraries to a makefile.

    makefile is the makefile.
    extra_include_dirs is the list of extra include directories.
    extra_lib_dirs is the list of extra library directories.
    extra_libs is the list of extra libraries.
    """
    if extra_include_dirs:
        makefile.extra_include_dirs.extend(extra_include_dirs)

    if extra_lib_dirs:
        makefile.extra_lib_dirs.extend(extra_lib_dirs)

    if extra_libs:
        makefile.extra_libs.extend(extra_libs)


def check_vendorid():
    """See if the VendorID library and include file can be found.
    """
    if opts.vendorcheck:
        if os.access(os.path.join(opts.vendincdir, "vendorid.h"), os.F_OK):
            if glob.glob(os.path.join(opts.vendlibdir, "*vendorid*")):
                sipconfig.inform("The VendorID package was found.")
            else:
                opts.vendorcheck = 0
                sipconfig.inform("The VendorID library could not be found in "
                                 "%s and so signed interpreter checking will "
                                 "be disabled. If the VendorID library is "
                                 "installed then use the -m argument to "
                                 "explicitly specify the correct "
                                 "directory." % opts.vendlibdir)
        else:
            opts.vendorcheck = 0
            sipconfig.inform("vendorid.h could not be found in %s and so "
                             "signed interpreter checking will be disabled. "
                             "If the VendorID package is installed then use "
                             "the -l argument to explicitly specify the "
                             "correct directory." % opts.vendincdir)


def get_command_stdout(cmd, and_stderr=False, envvars=None):
    """Return stdout (and optionally stderr) from the given command.
    """
    if envvars is not None:
        env = os.environ.copy()
        env.update(envvars)
    else:
        env = None

    try:
        import subprocess
    except ImportError:
        if and_stderr:
            _, sout = os.popen4(cmd)
        else:
            _, sout, _ = os.popen3(cmd)

        return sout

    if and_stderr:
        stderr = subprocess.STDOUT
    else:
        stderr = subprocess.PIPE

    p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
            stdout=subprocess.PIPE, stderr=stderr, env=env)

    return p.stdout


def check_dbus():
    """See if the DBus support module should be built.
    """
    sipconfig.inform("Checking to see if the dbus support module should be built...")

    sout = get_command_stdout("pkg-config --cflags-only-I --libs dbus-1")
    iflags = sout.read().strip()

    if not iflags:
        sipconfig.inform("DBus v1 does not seem to be installed.")
        return

    if sys.hexversion >= 0x03000000:
        iflags = iflags.decode()

    for f in iflags.split():
        if f.startswith("-I"):
            dbusincdirs.append(f[2:])
        elif f.startswith("-L"):
            dbuslibdirs.append(f[2:])
        elif f.startswith("-l"):
            dbuslibs.append(f[2:])

    try:
        import dbus.mainloop
    except:
        sipconfig.inform("The Python dbus module doesn't seem to be installed.")
        return

    global pydbusmoddir
    pydbusmoddir = dbus.mainloop.__path__[0]

    # Try and find dbus-python.h.  We don't use pkg-config because it is broken
    # for dbus-python (at least for versions up to and including v0.81.0).
    # Instead we look where DBus itself is installed - which in most cases will
    # be where dbus-python is also installed.
    if opts.pydbusincdir:
        dlist = [opts.pydbusincdir]
    else:
        dlist = dbusincdirs

    for d in dlist:
        if os.access(os.path.join(d, "dbus", "dbus-python.h"), os.F_OK):
            if d not in dbusincdirs:
                dbusincdirs.append(d)

            break
    else:
        sipconfig.inform("dbus/dbus-python.h could not be found and so the "
                         "DBus support module will be disabled. If "
                         "dbus-python v0.80 or later is installed then use "
                         "the -s argument to explicitly specify the directory "
                         "containing dbus/dbus-python.h.")
        pydbusmoddir = None


def check_module(mname, incfile, test, extra_include_dirs=None, extra_lib_dirs=None, extra_libs=None):
    """See if a module can be built and, if so, add it to the global list of
    modules.

    mname is the name of the module.
    incfile is the name of the include file needed for the test.
    test is a C++ statement being used for the test.
    extra_include_dirs is an optional list of extra include directories.
    extra_lib_dirs is an optional list of extra library directories.
    extra_libs is an optional list of extra libraries.
    """
    # Check that the module is enabled if we are not automatically enabling all
    # modules.
    if len(opts.enabled) > 0 and mname not in opts.enabled:
        return

    # Check the module's main .sip file exists.
    if os.access(os.path.join(src_dir, "sip", mname, mname + "mod.sip"), os.F_OK):
        sipconfig.inform("Checking to see if the %s module should be built..." % mname)

        if check_api(incfile, test, mname, extra_include_dirs=extra_include_dirs, extra_lib_dirs=extra_lib_dirs, extra_libs=extra_libs):
            pyqt_modules.append(mname)


def check_api(incfile, test, mname, extra_include_dirs=None, extra_lib_dirs=None, extra_libs=None):
    """Return non-zero if a class is available.

    incfile is the name of the include file needed for the test.
    test is a C++ statement being used for the test.
    mname is the name of the module.
    extra_include_dirs is an optional list of extra include directories.
    extra_lib_dirs is an optional list of extra library directories.
    extra_libs is an optional list of extra libraries.
    """
    # We use a module specific name to avoid a potential problem on Windows
    # where the operating system doesn't delete previous tests quickly enough.
    cfgtest = "cfgtest_%s.cpp" % mname

    f = open(cfgtest, "w")

    f.write("""#include <%s>

int main(int, char **)
{
    %s;
}
""" % (incfile, test))

    f.close()

    return compile_qt_program(cfgtest, mname,
            extra_include_dirs=extra_include_dirs,
            extra_lib_dirs=extra_lib_dirs, extra_libs=extra_libs)


def set_sip_flags(pyqt):
    """Set the SIP platform, version and feature flags.

    pyqt is the configuration instance.
    """
    sip_flags = []

    # If we don't check for signed interpreters, we exclude the 'VendorID'
    # feature
    if not opts.vendorcheck:
        sip_flags.append("-x")
        sip_flags.append("VendorID")

    if qt_version >= 0x050000 and not opts.with_deprecated:
        sip_flags.append("-x")
        sip_flags.append("PyQt_Deprecated_5_0")

    # Handle the platform tag.
    if sys.platform == 'win32':
        plattag = "WS_WIN"
    elif sys.platform == "darwin":
        if "__USE_WS_X11__" in sipcfg.build_macros()["DEFINES"]:
            plattag = "WS_X11"
        else:
            plattag = "WS_MACX"
    else:
        plattag = "WS_X11"

    sip_flags.append("-t")
    sip_flags.append(plattag)

    # Handle the Qt version tag.
    verstag = sipconfig.version_to_sip_tag(qt_version, pyqt.qt_version_tags(), "Qt")

    # Handle any feature flags.
    for xf in qt_xfeatures:
        sip_flags.append("-x")
        sip_flags.append(xf)

    if verstag:
        sip_flags.append("-t")
        sip_flags.append(verstag)

    # Handle the version specific Python features.
    if sipcfg.py_version < 0x020400:
        sip_flags.append("-x")
        sip_flags.append("Py_DateTime")

    if sipcfg.py_version < 0x030000:
        sip_flags.append("-x")
        sip_flags.append("Py_v3")

    # Generate code for a debug build of Python if needed.
    if hasattr(sys, "gettotalrefcount"):
        sip_flags.append("-D")

    qt_sip_flags.extend(sip_flags)

    # There is an issue creating QObjects while the GIL is held causing
    # deadlocks in multi-threaded applications.  We don't fully understand this
    # yet so we make sure we avoid the problem by always releasing the GIL.
    qt_sip_flags.append("-g")

    # Return the sip flags that sub-modules need to know about.
    return " ".join(sip_flags)


def needed_qt_libs(mname, qt_libs):
    """Add any additional Qt libraries needed by a module to an existing list.

    mname is the name of the module.
    qt_libs is the current list of libraries.
    """

    # The dependencies between the different Qt libraries.  The order within
    # each list is important.  Note that this affects the include directories
    # as well as the libraries.
    LIB_DEPS = {
        "QtCore": [],
        "QtDBus": ["QtCore"],
        "QtDeclarative": ["QtNetwork", "QtGui"],
        "QtGui": ["QtCore"],
        "QtHelp": ["QtGui"],
        "QtMultimedia": ["QtGui"],
        "QtNetwork": ["QtCore"],
        "QtOpenGL": ["QtGui"],
        "QtScript": ["QtCore"],
        "QtScriptTools": ["QtScript", "QtGui", "QtCore"],
        "QtSql": ["QtGui"],
        "QtSvg": ["QtGui"],
        "QtTest": ["QtGui"],
        "QtWebKit": ["QtNetwork", "QtGui"],
        "QtXml": ["QtCore"],
        "QtXmlPatterns": ["QtNetwork", "QtCore"],
        "phonon": ["QtGui"],
        "QtAssistant": ["QtNetwork", "QtGui"],
        "QtDesigner": ["QtGui"],
        "QAxContainer": ["QtGui"]
    }

    # Handle the dependencies first.
    for d in LIB_DEPS[mname]:
        needed_qt_libs(d, qt_libs)

    if mname not in qt_libs:
        qt_libs.insert(0, mname)


def mk_clean_dir(name, clean=1):
    """Create a clean (ie. empty) directory.

    name is the name of the directory.
    """
    if clean:
        try:
            shutil.rmtree(name)
        except:
            pass

    try:
        os.makedirs(name)
    except:
        if clean:
            sipconfig.error("Unable to create the %s directory." % name)


def generate_code(mname, extra_include_dirs=None, extra_lib_dirs=None, extra_libs=None, extra_sip_flags=None):
    """Generate the code for a module.

    mname is the name of the module to generate the code for.
    extra_include_dirs is an optional list of additional directories to add to
    the list of include directories.
    extra_lib_dirs is an optional list of additional directories to add to the
    list of library directories.
    extra_libs is an optional list of additional libraries to add to the list
    of libraries.
    extra_sip_flags is an optional list of additional flags to pass to SIP.
    """
    sipconfig.inform("Generating the C++ source for the %s module..." % mname)

    mk_clean_dir(mname)

    # Work out what Qt libraries need to be linked against and how SIP is
    # supposed to handle the consolidated module and its components.
    cons_args = []

    if opts.bigqt:
        if mname == "_qt":
            qt_libs = []

            for m in pyqt_modules:
                needed_qt_libs(m, qt_libs)
        else:
            if mname != "Qt":
                cons_args.append("-p")
                cons_args.append("PyQt4._qt")

            qt_libs = 0
    else:
        if mname == "Qt":
            qt_libs = 0
        else:
            qt_libs = []
            needed_qt_libs(mname, qt_libs)

    # Build the SIP command line.
    argv = ['"' + sipcfg.sip_bin + '"', '-w']

    if opts.no_timestamp:
        argv.append("-T")

    if not opts.no_docstrings:
        argv.append("-o");

    if opts.prot_is_public:
        argv.append("-P");

    argv.extend(qt_sip_flags)
    argv.extend(cons_args)

    if extra_sip_flags:
        argv.extend(extra_sip_flags)

    if opts.concat:
        argv.append("-j")
        argv.append(str(opts.split))

    if opts.tracing:
        argv.append("-r")

    if mname not in ("Qt", "_qt", "Qsci"):
        argv.append("-a")
        argv.append(mname + ".api")

    # Pass the absolute pathname so that #line files are absolute.
    argv.append("-c")
    argv.append(os.path.abspath(mname))

    buildfile = os.path.join(mname, mname + ".sbf")
    argv.append("-b")
    argv.append(buildfile)

    argv.append("-I")
    argv.append(os.path.join(src_dir, "sip"))

    # Add the name of the .sip file keeping in mind SIP assumes POSIX style
    # path separators.  The Qt module's .sip file is generated by this script
    # and so will be in a different place if this is an out-of-tree build.
    if mname == "Qt":
        argv.append("sip/Qt/Qtmod.sip")
    else:
        drive, path = os.path.splitdrive(src_dir)
        parts = path.split(os.sep)
        parts.extend(["sip", mname, mname + "mod.sip"])
        argv.append(drive + "/".join(parts))

    cmd = " ".join(argv)

    if opts.verbose:
        sys.stdout.write(cmd + "\n")

    os.system(cmd)

    # Check the result.
    if not os.access(buildfile, os.F_OK):
        sipconfig.error("Unable to create the C++ code.")

    # Generate the Makefile.
    sipconfig.inform("Creating the Makefile for the %s module..." % mname)

    installs = []

    if opts.install_sipfiles:
        sipfiles = []

        sipdir = os.path.join("sip", mname)
        if mname != "Qt":
            sipdir = os.path.join(src_dir, sipdir)
            rel_sipdir = sipdir
        else:
            rel_sipdir = os.path.join("..", sipdir)

        for s in glob.glob(os.path.join(sipdir, "*.sip")):
            sipfiles.append(os.path.join(rel_sipdir, os.path.basename(s)))

        installs.append([sipfiles, os.path.join(opts.pyqtsipdir, mname)])

    opengl = (mname == "QtOpenGL")

    makefile = sipconfig.SIPModuleMakefile(
        configuration=sipcfg,
        build_file=mname + ".sbf",
        dir=mname,
        install_dir=pyqt_modroot,
        installs=installs,
        qt=qt_libs,
        opengl=opengl,
        warnings=1,
        static=opts.static,
        debug=opts.debug,
        universal=sipcfg.universal,
        arch=sipcfg.arch,
        prot_is_public=opts.prot_is_public,
        deployment_target=sipcfg.deployment_target
    )

    add_makefile_extras(makefile, extra_include_dirs, extra_lib_dirs, extra_libs)

    if qt_version >= 0x050000 and opts.with_deprecated:
        # The name of this macro is very confusing.
        makefile.extra_defines.append('QT_DISABLE_DEPRECATED_BEFORE=0x04ffff')

    makefile.generate()


def fix_license(name):
    """ Fix the license file, if there is one, so that it conforms to the SIP
    v5 syntax.
    """

    try:
        f = open(name, "r")
    except IOError:
        sipconfig.error("Failed to open license file %s." % name)

    f5 = open(name + "5", "w")

    for line in f:
        if line.startswith("%License"):
            anno_start = line.find("/")
            anno_end = line.rfind("/")

            if anno_start < 0 or anno_end < 0 or anno_start == anno_end:
                sipconfig.error("%s has missing annotations." % name)

            annos = line[anno_start + 1:anno_end].split(", ")
            annos5 = [anno[0].lower() + anno[1:] for anno in annos]

            f5.write("%License(")
            f5.write(", ".join(annos5))
            f5.write(")\n")
        else:
            f5.write(line)

    f5.close()
    f.close()


def check_license():
    """Handle the validation of the PyQt license.
    """
    try:
        import license
        ltype = license.LicenseType
        lname = license.LicenseName

        try:
            lfile = license.LicenseFile
        except AttributeError:
            lfile = None
    except ImportError:
        ltype = None

    if ltype is None:
        ltype = "GPL"
        lname = "GNU General Public License"
        lfile = "pyqt-gpl.sip"

    sipconfig.inform("This is the %s version of PyQt %s (licensed under the %s) for Python %s on %s." % (ltype, pyqt_version_str, lname, sys.version.split()[0], sys.platform))

    # Common checks.
    if qt_licensee and ltype == "GPL":
        sipconfig.error("This version of PyQt and the commercial version of Qt have incompatible licenses.")

    # Confirm the license if not already done.
    if not opts.license_confirmed:
        loptions = """
Type 'L' to view the license.
"""

        sys.stdout.write(loptions)
        sys.stdout.write("""Type 'yes' to accept the terms of the license.
Type 'no' to decline the terms of the license.

""")

        while 1:
            sys.stdout.write("Do you accept the terms of the license? ")
            sys.stdout.flush()

            try:
                resp = sys.stdin.readline()
            except KeyboardInterrupt:
                raise SystemExit
            except:
                resp = ""

            resp = resp.strip().lower()

            if resp == "yes":
                break

            if resp == "no":
                sys.exit(0)

            if resp == "l":
                os.system("more LICENSE")

    # Check that the license file exists and fix its syntax.
    lfile_path = os.path.join(src_dir, "sip", lfile)

    if os.access(lfile_path, os.F_OK):
        sipconfig.inform("Found the license file %s." % lfile)
        fix_license(lfile_path)
    else:
        sipconfig.error("Please copy the license file %s to the sip directory." % lfile)


def get_build_macros(overrides):
    """Return the dictionary of platform specific build macros from the Qt
    installation.  Return None if any of the overrides was invalid.

    overrides is a list of macros overrides from the user.
    """
    global qt_macx_spec

    # Get the name of the qmake configuration file to take the macros from.
    if "QMAKESPEC" in list(os.environ.keys()):
        fname = os.environ["QMAKESPEC"]

        if not os.path.dirname(fname) or fname.startswith("unsupported"):
            qt_macx_spec = fname
            fname = os.path.join(qt_archdatadir, "mkspecs", fname)
    elif sys.platform == "darwin":
        # The Qt Mac binary installer defaults to xcode which we don't want.
        # Use Qt5's macx-clang if it is available, otherwise fall back to
        # macx-g++.
        fname = os.path.join(qt_archdatadir, "mkspecs", "macx-clang")
        if os.path.isdir(fname):
            qt_macx_spec = "macx-clang"
        else:
            fname = os.path.join(qt_archdatadir, "mkspecs", "macx-g++")
            qt_macx_spec = "macx-g++"
    else:
        fname = os.path.join(qt_archdatadir, "mkspecs", "default")

        # Qt5 doesn't have the 'default' link.
        if not os.path.isdir(fname):
            f = get_command_stdout(opts.qmake + " -query QMAKE_SPEC")
            fname = f.read().strip()
            if sys.hexversion >= 0x03000000:
                fname = fname.decode()
            f.close()

            fname = os.path.join(qt_archdatadir, "mkspecs", fname)

    fname = os.path.join(fname, "qmake.conf")

    if not os.access(fname, os.F_OK):
        sipconfig.error("Unable to find the qmake configuration file %s. Use the QMAKESPEC environment variable to specify the correct platform." % fname)

    # Add the Qt specific macros to the default.
    names = list(sipcfg.build_macros().keys())
    names.append("INCDIR_QT")
    names.append("LIBDIR_QT")
    names.append("MOC")

    properties = {
        "QT_INSTALL_BINS":      qt_bindir,
        "QT_INSTALL_HEADERS":   qt_incdir,
        "QT_INSTALL_LIBS":      qt_libdir
    }

    macros = sipconfig.parse_build_macros(fname, names, overrides, properties)

    if macros is None:
        return None

    # Qt5 doesn't seem to support the specific macros so add them if they are
    # missing.
    if macros.get("INCDIR_QT", "") == "":
        macros["INCDIR_QT"] = qt_incdir

    if macros.get("LIBDIR_QT", "") == "":
        macros["LIBDIR_QT"] = qt_libdir

    if macros.get("MOC", "") == "":
        default_moc = os.path.join(qt_bindir, "moc")
        if sys.platform == 'win32':
            default_moc += ".exe"

        macros["MOC"] = default_moc

    return macros


def check_qt_installation(macros):
    """Check the Qt installation and get the version number and edition and
    return the configuration instance.

    macros is the dictionary of build macros.
    """
    # Check the Qt version number.
    if qt_version < 0x040100:
        sipconfig.error("PyQt v4 requires Qt v4.1.0 or later.")

    # Starting with v4.7, Qt (when built with MinGW) assumes that stack frames
    # are 16 byte aligned because it uses SSE.  However the Python Windows
    # installers are built with 4 byte aligned stack frames.  We therefore need
    # to tweak the g++ flags to deal with it.
    if sipcfg.platform == 'win32-g++' and qt_version >= 0x040700:
        macros['CFLAGS'] += ' -mstackrealign'
        macros['CXXFLAGS'] += ' -mstackrealign'

    # Work out how Qt was built on MacOS.
    if sys.platform == "darwin":
        if os.access(os.path.join(qt_libdir, "QtCore.framework"), os.F_OK):
            global qt_framework
            qt_framework = 1

    # Get the Makefile generator.
    generator = macros["MAKEFILE_GENERATOR"]

    # We haven't yet factored out sipconfig's knowledge of how to build Qt
    # binaries and it is expecting to find these in the configuration when it
    # generates the Makefiles.
    sipcfg.qt_version = qt_version
    sipcfg.qt_edition = qt_edition
    sipcfg.qt_winconfig = qt_shared
    sipcfg.qt_framework = qt_framework
    sipcfg.qt_threaded = 1
    sipcfg.qt_dir = qt_dir
    sipcfg.qt_lib_dir = qt_libdir

    return ConfigurePyQt4(generator)


def fix_qmake_args(args=""):
    """Make any platform specific adjustments to the arguments passed to qmake.
    """
    if sys.platform == "darwin":
        # The Qt binary installer has macx-xcode as the default.
        args = "-spec %s %s" % (qt_macx_spec, args)

    return args


def get_qt_configuration():
    """Set the qt_dir, qt_incdir, qt_libdir, qt_bindir, qt_datadir,
    qt_archdatadir, qt_pluginsdir and qt_xfeatures globals for the Qt
    installation.
    """
    sipconfig.inform("Determining the layout of your Qt installation...")

    # The file names we will use to get the directory information.
    app = "qtdirs"
    pro_file = app + ".pro"
    make_file = app + ".mk"
    make_target = ""
    cpp_file = app + ".cpp"
    out_file = app + ".out"
    qmake_args = fix_qmake_args("-o " + make_file)

    if sys.platform == 'win32':
        if opts.debug:
            exe_file = os.path.join("debug", app + ".exe")
            make_target = " debug"
        else:
            exe_file = os.path.join("release", app + ".exe")
            make_target = " release"
    elif sys.platform == "darwin":
        exe_file = os.path.join(app + ".app", "Contents", "MacOS", app)
    else:
        exe_file = os.path.join(".", app)

    # Generate the qmake project file.
    f = open(pro_file, "w")

    if sipcfg.arch:
        f.write(arch_config())

    f.write(
"""QT = core
# This is for certain broken Linux distros and is needed to make sure that
# QT_SHARED is properly defined.
CONFIG += link_prl
TARGET = %s
SOURCES = %s
""" % (app, cpp_file))

    f.close()

    # Generate the source code.
    f = open(cpp_file, "w")

    f.write(
"""#include <QCoreApplication>
#include <QFile>
#include <QLibraryInfo>
#include <QTextStream>

// This is for Qt v5.  The value isn't accurate but triggers the desired
// description of the edition.
#if !defined(QT_EDITION)
#define QT_EDITION      0x200
#endif

int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);
    QFile outf("%s");

    if (!outf.open(QIODevice::WriteOnly|QIODevice::Truncate|QIODevice::Text))
        return 1;

    QTextStream out(&outf);

    out << QLibraryInfo::location(QLibraryInfo::PrefixPath) << '\\n';
    out << QLibraryInfo::location(QLibraryInfo::HeadersPath) << '\\n';
    out << QLibraryInfo::location(QLibraryInfo::LibrariesPath) << '\\n';
    out << QLibraryInfo::location(QLibraryInfo::BinariesPath) << '\\n';
    out << QLibraryInfo::location(QLibraryInfo::DataPath) << '\\n';
#if QT_VERSION >= 0x050000
    out << QLibraryInfo::location(QLibraryInfo::ArchDataPath) << '\\n';
#else
    out << QLibraryInfo::location(QLibraryInfo::DataPath) << '\\n';
#endif
    out << QLibraryInfo::location(QLibraryInfo::PluginsPath) << '\\n';

    out << QT_VERSION << '\\n';
    out << QT_EDITION << '\\n';

    out << QLibraryInfo::licensee() << '\\n';

#if defined(QT_SHARED) || defined(QT_DLL)
    out << "shared\\n";
#else
    out << "static\\n";
#endif

    // Determine which features should be disabled.

#if defined(QT_NO_ACCESSIBILITY)
    out << "PyQt_Accessibility\\n";
#endif

#if defined(QT_NO_SESSIONMANAGER)
    out << "PyQt_SessionManager\\n";
#endif

#if defined(QT_NO_STATUSTIP)
    out << "PyQt_StatusTip\\n";
#endif

#if defined(QT_NO_TOOLTIP)
    out << "PyQt_ToolTip\\n";
#endif

#if defined(QT_NO_WHATSTHIS)
    out << "PyQt_WhatsThis\\n";
#endif

#if defined(QT_NO_OPENSSL)
    out << "PyQt_OpenSSL\\n";
#endif

#if defined(QT_NO_SIZEGRIP)
    out << "PyQt_SizeGrip\\n";
#endif

#if defined(QT_NO_SYSTEMTRAYICON)
    out << "PyQt_SystemTrayIcon\\n";
#endif

#if defined(QT_NO_PRINTDIALOG)
    out << "PyQt_PrintDialog\\n";
#endif

#if defined(QT_NO_PRINTER)
    out << "PyQt_Printer\\n";
#endif

#if defined(QT_NO_PRINTPREVIEWDIALOG)
    out << "PyQt_PrintPreviewDialog\\n";
#endif

#if defined(QT_NO_PRINTPREVIEWWIDGET)
    out << "PyQt_PrintPreviewWidget\\n";
#endif

#if defined(QT_NO_RAWFONT)
    out << "PyQt_RawFont\\n";
#endif

#if !defined(QT3_SUPPORT) || QT_VERSION >= 0x040200
    out << "PyQt_NoPrintRangeBug\\n";
#endif

#if defined(QT_OPENGL_ES)
    out << "PyQt_NoOpenGLES\\n";
#endif

#if defined(QT_NO_FPU) || defined(QT_ARCH_ARM) || defined(QT_ARCH_WINDOWSCE) || defined(QT_ARCH_SYMBIAN) || defined(QT_ARCH_VXWORKS)
    out << "PyQt_qreal_double\\n";
#endif

    return 0;
}
""" % out_file)

    f.close()

    # Create the makefile, first making sure it doesn't already exist.
    remove_file(make_file)
    run_command("%s %s %s" % (opts.qmake, qmake_args, pro_file))

    if not os.access(make_file, os.F_OK):
        sipconfig.error("%s failed to create a makefile. %s" % (opts.qmake, MSG_CHECK_QMAKE))

    # Try and work out the name of make.
    if sipcfg.platform.startswith("win32-msvc"):
        make = "nmake"
    elif sipcfg.platform == "win32-borland":
        make = "bmake"
    elif sipcfg.platform == "win32-g++":
        make = "mingw32-make"
    else:
        make = "make"

    # Create the executable, first making sure it doesn't exist.
    remove_file(exe_file)
    run_command("%s -f %s%s" % (make, make_file, make_target))

    if not os.access(exe_file, os.X_OK):
        sipconfig.error("Failed to determine the layout of your Qt installation. Try again using the --verbose flag to see more detail about the problem.")

    # Create the output file, first making sure it doesn't exist.
    remove_file(out_file)
    run_command(exe_file)

    if not os.access(out_file, os.F_OK):
        sipconfig.error("%s failed to create %s. Make sure your Qt installation is correct." % (exe_file, out_file))

    # Read the directories.
    f = open(out_file, "r")
    lines = f.read().strip().split("\n")
    f.close()

    global qt_dir, qt_incdir, qt_libdir, qt_bindir, qt_datadir, qt_archdatadir
    global qt_pluginsdir
    global qt_version, qt_edition, qt_licensee, qt_shared, qt_xfeatures

    qt_dir = lines[0]
    qt_incdir = lines[1]
    qt_libdir = lines[2]
    qt_bindir = lines[3]
    qt_datadir = lines[4]
    qt_archdatadir = lines[5]
    qt_pluginsdir = lines[6]
    qt_version = lines[7]
    qt_edition = lines[8]
    qt_licensee = lines[9]
    qt_shared = lines[10]
    qt_xfeatures = lines[11:]

    if opts.assume_shared:
        qt_shared = 'shared'
    elif qt_shared == 'static':
        qt_shared = ''

    # 'Nokia' is the value that is used by Maemo's version of Qt.
    if qt_licensee in ('Open Source', 'Nokia'):
        qt_licensee = None

    try:
        qt_version = int(qt_version)
    except ValueError:
        sipconfig.error("QT_VERSION should be a number but \"%s\" was found." % qt_version)

    try:
        qt_edition = int(qt_edition)
    except ValueError:
        sipconfig.error("QT_EDITION should be a number but \"%s\" was found." % qt_edition)

    # Now convert the edition to a descriptive string.  The order of testing is
    # important.
    if qt_edition & 0x200:
        # It has ActiveQt.
        qt_edition = "Desktop"

        # ActiveQt became part of the open source version in v4.5.2.
        if qt_version >= 0x040502 and qt_licensee is None:
            qt_edition = "free"
    elif qt_edition & 0x008:
        # It has OpenGL.
        qt_edition = "free"
    elif qt_edition & 0x002:
        # It has GUI.
        qt_edition = "Desktop Light"
    else:
        qt_edition = "Console"

    if qt_shared:
        if opts.staticplugins:
            sipconfig.inform("Static plugins are disabled because Qt has been built as shared libraries.")
            opts.staticplugins = []
    else:
        if opts.static or opts.bigqt:
            pass
        else:
            sipconfig.error("Qt has been built as static libraries so either the -g or -k argument should be used.")


def main():
    """Create the configuration module module.
    """
    # Check SIP is new enough.
    if sipcfg.sip_version < sip_min_version:
        sipconfig.error("This version of PyQt requires SIP v%s or later" % sipconfig.version_to_string(sip_min_version))

    global opts
    global macros

    # Parse the command line.
    p = create_optparser()
    opts, args = p.parse_args()

    # Provide defaults for platform-specific options.
    if sys.platform == 'win32':
        opts.qmake = find_default_qmake()
        opts.prot_is_public = False

    if not opts.qmake:
        sipconfig.error(MSG_CHECK_QMAKE)

    # Where the modules will be placed.
    global pyqt_modroot
    pyqt_modroot = os.path.join(opts.pyqtmoddir, "PyQt4")

    # When building static libraries, signed interpreter checking makes no
    # sense.
    if opts.vendorcheck and opts.static:
        sipconfig.error("Using the VendorID package when building static libraries makes no sense.")

    # Get the details of the Qt installation.
    get_qt_configuration()

    # Provide some defaults that are based on the Qt configuration.
    if not opts.plugindir:
        opts.plugindir = qt_pluginsdir

    if opts.qscidir:
        # An explicit directory implies installing the API file.
        opts.api = True
    else:
        opts.qscidir = os.path.join(qt_datadir, "qsci")

        if opts.api is None:
            # Install the API file if the default directory exists.
            opts.api = os.path.isdir(opts.qscidir)

    # Replace the existing build macros with the ones from the Qt installation.
    macros = get_build_macros(args)

    if macros is None:
        p.print_help()
        sys.exit(2)

    sipcfg.set_build_macros(macros)

    # Check Qt is what we need.
    pyqt = check_qt_installation(macros)

    # Check the licenses are compatible.
    check_license()

    # Check which modules to build.
    pyqt.check_modules()

    # Check for the VendorID package.
    check_vendorid()

    # Set the SIP platform, version and feature flags.
    sip_flags = set_sip_flags(pyqt)

    # Tell the user what's been found.
    inform_user()

    # Embed the sip flags.
    sipconfig.inform("Embedding sip flags...")
    in_f = open(os.path.join(src_dir, "qpy", "QtCore", "qpycore_post_init.cpp.in"))
    out_f = open(os.path.join(src_dir, "qpy", "QtCore", "qpycore_post_init.cpp"), "wt")

    for line in in_f:
        line = line.replace("@@PYQT_SIP_FLAGS@@", sip_flags)
        out_f.write(line)

    in_f.close()
    out_f.close()

    # Generate the code.
    pyqt.code()

    # Create the additional Makefiles.
    sipconfig.inform("Creating top level Makefile...")

    installs=[(pyqt.module_installs(), pyqt_modroot)]

    if opts.api:
        installs.append(("PyQt4.api", os.path.join(opts.qscidir, "api", "python")))

    xtra_modules = ["Qt"]

    if opts.bigqt:
        xtra_modules.append("_qt")

        if opts.mwg_qsci_dir:
            xtra_modules.append("Qsci")

        if opts.mwg_qwt_dir:
            xtra_modules.append("Qwt5")

    sipconfig.ParentMakefile(
        configuration=sipcfg,
        subdirs=pyqt.qpy_libs() + pyqt_modules + xtra_modules + pyqt.tools(),
        installs=installs
    ).generate()

    # Install the configuration module.
    create_config("pyqtconfig.py", os.path.join(src_dir, "pyqtconfig.py.in"),
            macros)


###############################################################################
# The script starts here.
###############################################################################

if __name__ == "__main__":
    try:
        main()
    except SystemExit:
        raise
    except:
        sys.stderr.write(
"""An internal error occured.  Please report all the output from the program,
including the following traceback, to support@riverbankcomputing.com.
""")
        raise