summaryrefslogtreecommitdiff
path: root/configure
blob: 886eabb404b77dfb9fd1517a292a51165194afc3 (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
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
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
#!/bin/sh
# The purpose of this script is to check for all external tools, headers, and
# libraries VBox OSE depends on.

#
# Copyright (C) 2006-2019 Oracle Corporation
#
# This file is part of VirtualBox Open Source Edition (OSE), as
# available from http://www.virtualbox.org. This file is free software;
# you can redistribute it and/or modify it under the terms of the GNU
# General Public License (GPL) as published by the Free Software
# Foundation, in version 2 as it comes in the "COPYING" file of the
# VirtualBox OSE distribution. VirtualBox OSE is distributed in the
# hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
#

LC_ALL=C
export LC_ALL

# append some extra paths
PATH="$PATH:/opt/gnome/bin"
# Solaris (order of paths important for tr, grep, sed to work)
PATH="/usr/xpg4/bin:$PATH:/usr/local/bin:/usr/sfw/bin:/usr/ccs/bin"
ORGPATH=$PATH

# Wrapper for ancient /usr/bin/which on darwin that always returns 0
which_wrapper()
{
  if [ -z "$have_ancient_which" ]; then
    if which /bin/___cErTaINly_a_nOn_eXisTing_fIle___ 2> /dev/null > /dev/null; then
      have_ancient_which="yes"
    else
      have_ancient_which="no"
    fi
  fi
  if [ "$have_ancient_which" = "yes" ]; then
    retval=`which $* 2>/dev/null`
    echo "$retval"
    test -n "$retval" -a -x "$retval"
    unset retval
  else
    which $* 2> /dev/null
  fi
}

OS=`uname -s | sed -e 's/GNU\/Linux/Linux/g' | tr '[:upper:]' '[:lower:]'`
case "$OS" in
  linux)
    ;;
  darwin)
    ;;
  freebsd)
    ;;
  netbsd)
    ;;
  sunos)
    OS='solaris'
    ;;
  haiku)
    ;;
  *)
    echo "Cannot determine OS!"
    exit 1
    ;;
esac

#
# Defaults
#
OSE=1
ODIR="`pwd`/"
ODIR_OVERRIDE=0
OUT_PATH=""
OUT_PATH_OVERRIDE=0
SETUP_WINE=
ONLY_ADDITIONS=0
TARGET_MACHINE=""
TARGET_CPU=""
WITH_XPCOM=1
WITH_PYTHON=1
WITH_JAVA=1
WITH_VMMRAW=1
WITH_LIBIDL=1
WITH_GSOAP=0
WITH_QT5=1
WITH_SDL=1
WITH_SDL_TTF=1
WITH_X11=1
WITH_ALSA=1
WITH_PULSE=1
WITH_DBUS=1
WITH_DEVMAPPER=1
WITH_KMODS=1
WITH_OPENGL=1
WITH_HARDENING=1
WITH_UDPTUNNEL=1
WITH_VDE=0
WITH_VNC=0
WITH_EXTPACK=1
WITH_DOCS=1
WITH_LIBVPX=1
WITH_LIBOPUS=1
BUILD_LIBXML2=
BUILD_LIBCURL=
BUILD_LIBSSL=
BUILD_LIBVPX=
PASSIVE_MESA=0
CC="gcc"
CC32=""
CC64=""
CXX="g++"
CXX32=""
CXX64=""
YASM="yasm"
IASL="iasl"
XSLTPROC="xsltproc"
GENISOIMAGE="genisoimage"
INCCRYPTO=""
LIBCRYPTO="-lssl -lcrypto"
LIBPTHREAD="-lpthread"
LIBCAP="-lcap"
GSOAP=""
GSOAP_IMPORT=""
INCX11="/usr/local/include"
LIBX11="-L/usr/X11R6/lib -L/usr/X11R6/lib64 -L/usr/local/lib -lXext -lX11"
LIBXCURSOR="-lXcursor"
LIBXMU="-lXmu"
LIBXINERAMA="-lXinerama"
LIBXRANDR="-lXrandr"
MAKESELF="makeself"
MESA="-lGL"
INCZ=""
LIBZ="-lz"
INCVNCSERVER=""
LIBVNCSERVER="-lvncserver"
INCDEVMAPPER=""
LIBDEVMAPPER="-ldevmapper"
CXX_FLAGS=""
if [ "$OS" = "freebsd" ]; then
  INCCURL="-I/usr/local/include"
  LIBCURL="-L/usr/local/lib -lcurl"
  INCPULSE="-I/usr/local/include"
  LIBPULSE="-L/usr/local/lib"
  INCPNG="-I/usr/local/include"
  LIBPNG="-L/usr/local/lib -lpng"
else
  INCCURL=""
  LIBCURL="-lcurl"
  INCPNG=""
  LIBPNG="-lpng"
fi
INCVPX=""
LIBVPX="-lvpx"
PKGCONFIG="`which_wrapper pkg-config`"
PYTHONDIR="/usr /usr/local"
QT5DIR="/usr/lib/qt5 /usr/share/qt5 /usr/lib64/qt5 /usr /usr/local"
QT5DIR_PKGCONFIG=1
QT5MAJ=5
QT5MIN=6
KBUILDDIR="`cd \`dirname $0\`; pwd`/kBuild"
DEVDIR="`cd \`dirname $0\`; pwd`/tools"
if [ -d "/lib/modules/`uname -r`/build" ]; then
  LINUX="/lib/modules/`uname -r`/build"
elif [ "`echo /lib/modules/*`" != "/lib/modules/*" ]; then
  # Get the most recent kernel headers if none match the current kernel.
  for i in /lib/modules/*; do
    if [ -r "$i/build" ]; then
      LINUX="$i/build"
    fi
  done
fi
if [ -z "$LINUX" ]; then
 LINUX="/usr/src/linux"
fi
KCHMVIEWER="kchmviewer"
LOG="configure.log"
CNF="AutoConfig.kmk"
ENV="env.sh"
BUILD_TYPE="release"
# the restricting tool is ar (mri mode).
INVALID_CHARS="[^A-Za-z0-9/\\$:._-]"

if (cd `dirname $0`; pwd)|grep -q "$INVALID_CHARS"; then
  echo "Error: VBox base path contains invalid characters!"
  exit 1
fi

# Posix /bin/sh isn't supporting echo -n. Use printf instead.
ECHO_N="printf"


cleanup()
{
  rm -f $ODIR.tmp_src.cc $ODIR.tmp_src.c $ODIR.tmp_out $ODIR.test_execute.log
}

fail()
{
  if [ -z "$nofatal" -o "x$1" != "x" ]; then
    cleanup
    rm -f $ENV
    echo "Check $LOG for details"
    exit 1
  fi
}

log()
{
  echo "$1"
  echo "$1" >> $LOG
}

log_success()
{
  if [ -n "$1" ]; then $ECHO_N "$1, "; fi
  echo "OK."
  echo "$1" >> $LOG
  echo >> $LOG
  echo >> $LOG
}

log_failure()
{
  echo
  echo "  ** $1!"
  echo "** $1!" >> $LOG
  echo >> $LOG
}

cnf_append()
{
  printf "%-30s := %s\n" "$1" "$2" >> $CNF
}

strip_l()
{
  echo "$1"|$KBUILD_SED 's|-l\([^ ]\+\)|\1|g; s|^-[^l][^ ]*||g; s| -[^l][^ ]*||g; s|^ ||; s| *$||g'
}

strip_L()
{
  echo "$1"|$KBUILD_SED 's|-L\([^ ]\+\)|\1|g; s|^-[^L][^ ]*||g; s| -[^L][^ ]*||g; s|^ ||; s| *$||g'
}

strip_I()
{
  echo "$1"|$KBUILD_SED 's|-I\([^ ]\+\)|\1|g; s|^-[^I][^ ]*||g; s| -[^I][^ ]*||g; s|^ ||; s| *$||g'
}

prefix_I()
{
  echo "$1"|$KBUILD_SED 's|^\/|-I/|g; s| \/| -I/|g'
}

check_avail()
{
   if [ -z "$1" ]; then
     log_failure "$2 is empty"
     fail $3
     return 1
   elif which_wrapper $1 > /dev/null; then
     return 0
   else
     log_failure "$1 (variable $2) not found"
     fail $3
     return 1
   fi
}


# Prepare a test
test_header()
{
  echo "***** Checking $1 *****" >> $LOG
  $ECHO_N "Checking for $1: "
}


# Compile a test
#  $1   compile flags/libs
#  $2   library name
#  $3   package name
#  $4   if this argument is 'nofatal', don't abort
test_compile()
{
  echo "compiling the following source file:" >> $LOG
  cat $ODIR.tmp_src.cc >> $LOG
  echo "using the following command line:" >> $LOG
  echo "$CXX $CXX_FLAGS -fPIC -g -O -Wall -o $ODIR.tmp_out $ODIR.tmp_src.cc \"$1\"" >> $LOG
  $CXX $CXX_FLAGS -fPIC -g -O -Wall -o $ODIR.tmp_out $ODIR.tmp_src.cc $1 >> $LOG 2>&1
  if [ $? -ne 0 ]; then
    if [ -z "$4" ]; then
      echo
      echo "  $2 not found at $1 or $3 headers not found"
      echo "  Check the file $LOG for detailed error information."
      fail
    else
      echo >> $LOG
      echo >> $LOG
    fi
    return 1
  fi
  return 0
}


# Execute a compiled test binary
test_execute()
{
  echo "executing the binary" >> $LOG
  $ODIR.tmp_out > $ODIR.test_execute.log
  rc=$?
  cat $ODIR.test_execute.log | tee -a $LOG
  if [ $rc -ne 0 ]; then
    fail $1
    return 1
  fi
  echo >> $LOG
  echo >> $LOG
  return 0
}


# Execute a compiled test binary
test_execute_path()
{
  ## LD_LIBRARY_PATH to set.
  local_path="${1}"
  ## Set this to non-empty to make this test non-fatal.
  local_nofail="${2}"
  echo "executing the binary (LD_LIBRARY_PATH=$1)" >> $LOG
  LD_LIBRARY_PATH="${local_path}" $ODIR.tmp_out > $ODIR.test_execute.log
  rc=$?
  cat $ODIR.test_execute.log | tee -a $LOG
  if [ $rc -ne 0 ]; then
    test -z "${local_nofail}" && fail
    echo >> $LOG
    echo >> $LOG
    return 1
  fi
  echo >> $LOG
  echo >> $LOG
  return 0
}


#
# Check for OS, MACHINE, CPU
#
check_environment()
{
  test_header environment
  BUILD_CPU=`uname -m`
  [ "$OS" = "solaris" ] && BUILD_CPU=`isainfo | cut -f 1 -d ' '`
  case "$BUILD_CPU" in
    i[3456789]86|x86|i86pc|BePC)
      BUILD_MACHINE='x86'
      LIB='lib'
      ;;
    x86_64|amd64)
      BUILD_MACHINE='amd64'
      BUILD_CPU='k8'
      if [ "$OS" != "solaris" ]; then
        # on AMD64 systems, 64bit libs are usually located in /usr/lib64
        # see http://www.pathname.com/fhs/pub/fhs-2.3.html#LIB64
        LIB='lib64'
      else
        # Solaris doesn't seem to subscribe to fhs, libs are usually in
        # a '64' subdirectory of the standard 'lib' dirs while some 64-bit
        # alternative binaries can be found in 'amd64' subdirs of the 'bin'
        # ones. So, in order to find the right stuff (esp. sdl-config) we'll
        # have to make sure the */bin/amd64 dirs are searched before the */bin
        # ones. (The sed has some sideeffects, but they shouldn't harm us...)
        echo "64-bit Solaris detected, hacking the PATH" >> $LOG
        echo "old PATH: $PATH" >> $LOG
        PATH=`echo ":$PATH:" | sed -e 's,\(:[^:]*/bin\):,\1/amd64:\1:,g' \
                                   -e 's/^:*//' -e 's/:*$//g' -e 's/::*/:/g' `
        export PATH
        echo "new PATH: $PATH" >> $LOG
        LIB='lib/64'
      fi
      ;;
    sparc64|sparcv9)
        BUILD_MACHINE='sparc64'
        BUILD_CPU='blend'
        ;;
    sparc32|sparc|sparcv8|sparcv7|sparcv8e)
        BUILD_MACHINE='sparc32'
        BUILD_CPU='blend'
        ;;
    *)
      log_failure "Cannot determine system"
      exit 1
      ;;
  esac
  [ -z "$TARGET_MACHINE" ] && TARGET_MACHINE=$BUILD_MACHINE
  [ -z "$TARGET_CPU"     ] && TARGET_CPU=$BUILD_CPU
  DEVDIR_BIN="$DEVDIR/$OS.$BUILD_MACHINE/bin"
  log_success "Determined build machine: $OS.$BUILD_MACHINE, target machine: $OS.$TARGET_MACHINE"

  echo "BUILD_PLATFORM=\"$OS\"" >> $ENV
  echo "export BUILD_PLATFORM" >> $ENV
  echo "BUILD_PLATFORM_ARCH=\"$BUILD_MACHINE\"" >> $ENV
  echo "export BUILD_PLATFORM_ARCH" >> $ENV
  echo "BUILD_TARGET=\"$OS\"" >> $ENV
  echo "export BUILD_TARGET" >> $ENV
  echo "BUILD_TARGET_ARCH=\"$TARGET_MACHINE\"" >> $ENV
  echo "export BUILD_TARGET_ARCH" >> $ENV
  echo "BUILD_TARGET_CPU=\"$TARGET_CPU\"" >> $ENV
  echo "export BUILD_TARGET_CPU" >> $ENV
  echo "BUILD_TYPE=\"$BUILD_TYPE\"" >> $ENV
  echo "export BUILD_TYPE" >> $ENV
}

#
# Check for gcc with version >= 3.2.
# We depend on a working gcc, if we fail terminate in every case.
#
check_gcc()
{
  test_header gcc
  if check_avail "$CC" CC really; then
    cc_ver=`$CC -dumpfullversion 2>/dev/null`
    if [ $? -ne 0 ]; then
      cc_ver=`$CC -dumpversion 2>/dev/null`
    fi
    if [ $? -ne 0 ]; then
      log_failure "cannot execute '$CC -dumpversion'"
      fail really
    fi
    if check_avail "$CXX" CXX really; then
      cxx_ver=`$CXX -dumpfullversion 2>/dev/null`
      if [ $? -ne 0 ]; then
        cxx_ver=`$CXX -dumpversion 2>/dev/null`
      fi
      if [ $? -ne 0 ]; then
        log_failure "cannot execute '$CXX -dumpversion'"
        fail really
      fi
      cc_maj=`echo $cc_ver|cut -d. -f1`
      cc_min=`echo $cc_ver|cut -d. -f2`
      if [ "x$cc_ver" != "x$cxx_ver" ]; then
        log_failure "gcc version $cc_ver does not match g++ version $cxx_ver"
        fail really
      elif [ $cc_maj -eq 4 -a $cc_min -eq 0 -a "$OS" = "darwin" ]; then
        log_success "found version $cc_ver"
      # gcc-4.0 is allowed for Darwin only
      elif [ $cc_maj -eq 4 -a $cc_min -eq 2 -a "$OS" = "freebsd" ]; then
        log_success "found version $cc_ver"
      # gcc-4.2 is allowed for FreeBSD only
      elif [ $cc_maj -lt 4 \
             -o \( $cc_maj -eq 4 -a $cc_min -lt 4 -a "$OS" != "darwin" \) \
             -o \( $cc_maj -eq 4 -a $cc_min -lt 2 -a "$OS"  = "darwin" \) ]; then
        log_failure "gcc version $cc_maj.$cc_min found, expected gcc 4.x or later"
        fail really
      else
        log_success "found version $cc_ver"
      fi
      if [ "$BUILD_MACHINE" = "amd64" ]; then
        [ -z "$CC32"  ] && CC32="$CC -m32"
        [ -z "$CXX32" ] && CXX32="$CXX -m32"
      else
        [ -z "$CC32"  ] && CC32="$CC"
        [ -z "$CXX32" ] && CXX32="$CXX"
      fi
      if [ "$BUILD_MACHINE" = "x86" -a "$TARGET_MACHINE" = "amd64" ]; then
        [ -z "$CC64"  ] && CC64="$CC -m64"
        [ -z "$CXX64" ] && CXX64="$CXX -m64"
      fi
      if [ "$TARGET_MACHINE" = "amd64" -a $WITH_VMMRAW -eq 0 ]; then
        CC32="undefined"
        CXX32="undefined"
      fi
      if [ "$CC" != "gcc" ]; then
        cnf_append "TOOL_GCC3_CC"   "$CC"
        cnf_append "TOOL_GCC3_AS"   "$CC"
        cnf_append "TOOL_GCC3_LD"   "$CC"
        cnf_append "TOOL_GXX3_CC"   "$CC"
        cnf_append "TOOL_GXX3_AS"   "$CC"
      fi
      if [ "$CXX" != "g++" ]; then
        cnf_append "TOOL_GCC3_CXX"  "$CXX"
        cnf_append "TOOL_GXX3_CXX"  "$CXX"
        cnf_append "TOOL_GXX3_LD"   "$CXX"
      fi
      if [ "$CC32" != "gcc -m32" -a "$CC32" != "undefined" ]; then
        cnf_append "TOOL_GCC32_CC"  "$CC32"
        cnf_append "TOOL_GCC32_AS"  "$CC32"
        cnf_append "TOOL_GCC32_LD"  "$CC32"
        cnf_append "TOOL_GXX32_CC"  "$CC32"
        cnf_append "TOOL_GXX32_AS"  "$CC32"
      fi
      if [ "$CXX32" != "g++ -m32" -a "$CXX32" != "undefined" ]; then
        cnf_append "TOOL_GCC32_CXX" "$CXX32"
        cnf_append "TOOL_GXX32_CXX" "$CXX32"
        cnf_append "TOOL_GXX32_LD"  "$CXX32"
      fi
      # this isn't not necessary, there is not such tool.
      if [ -n "$CC64" ]; then
        cnf_append "TOOL_GCC64_CC"  "$CC64"
        cnf_append "TOOL_GCC64_AS"  "$CC64"
        cnf_append "TOOL_GCC64_LD"  "$CC64"
        cnf_append "TOOL_GXX64_CC"  "$CC64"
        cnf_append "TOOL_GXX64_AS"  "$CC64"
      fi
      if [ -n "$CXX64" ]; then
        cnf_append "TOOL_GCC64_CXX" "$CXX64"
        cnf_append "TOOL_GXX64_CXX" "$CXX64"
        cnf_append "TOOL_GXX64_LD"  "$CXX64"
      fi
      if [ "$CC" != "gcc" ]; then
        if [ -n "$CC64" ]; then
          cnf_append "TOOL_Bs3Gcc64Elf64_CC"  "$CC64"
        else
          cnf_append "TOOL_Bs3Gcc64Elf64_CC"  "$CC"
        fi
        if [ -n "$CXX64" ]; then
          cnf_append "TOOL_Bs3Gcc64Elf64_CXX" "$CXX64"
        else
          cnf_append "TOOL_Bs3Gcc64Elf64_CXX" "$CXX"
        fi
      fi
      # Solaris sports a 32-bit gcc/g++.
      if [ "$OS" = "solaris" -a "$BUILD_MACHINE" = "amd64" ]; then
        [ "$CC" = "gcc" ] && CC="gcc -m64"
        [ "$CXX" = "g++" ] && CXX="g++ -m64"
      fi
    fi
  fi
}


#
# Check for the OpenWatcom compiler, needed for compiling the BIOS
#
# If the system has Open Watcom installed, WATCOM will be set in the
# environment.  If the user has her/his own Open Watcom install it will be
# pointed to by on the command line, which will set the WATCOM variable.
# The only exception is detecting OpenWatcom in tools/common/openwatcom.
#
check_open_watcom()
{
  test_header "Open Watcom"

  if [ -z "$WATCOM" ]; then
    WATCOM=`/bin/ls -rd1 $PWD/tools/common/openwatcom/* 2> /dev/null | head -1`
    if [ -z "$WATCOM" ]; then
      if [ $OSE -eq 0 -a $OS = "linux" ]; then
        log_failure "Open Watcom was not found"
        exit 1
      fi
      log_failure "Open Watcom was not found, using alternative BIOS sources"
      cnf_append "VBOX_WITH_OPEN_WATCOM" ""
      return 0;
    fi
  fi

  case "$OS" in
    "darwin")   wc_bin="binosx";;  # ??
    "dos")      wc_bin="binw";;
    "freebsd")  wc_bin="binfbsd";; # ??
    "linux")    wc_bin="binl";;
    "netbsd")   wc_bin="binnbsd";; # ??
    "solaris")  wc_bin="binsol";;  # ??
    "os2")      wc_bin="binp";;
    "win")      wc_bin="binnt";;
    *)          wc_bin="binl";;
  esac

  # Check that the tools we use are there.
  for prog in wasm wcc wlink;
  do
    if [ ! -f "$WATCOM/$wc_bin/$prog" ]; then
      log_failure "$WATCOM/$wc_bin/$prog does not exist or is not a regular file."
      fail
    fi
  done

  # Use WASM to get the version.
  wasm_ver=`$WATCOM/$wc_bin/wasm -? 2>&1 | sed -e '1!d' -e 's/Open Watcom Assembler Version *//'`
  if [ -z "$wasm_ver" ]; then
    log_failure "$WATCOM/$wc_bin/wasm -? did not produce the expected response"
    fail
  fi
  log_success "found version $wasm_ver"
  cnf_append "PATH_TOOL_OPENWATCOM" "$WATCOM"
  cnf_append "VBOX_WITH_OPEN_WATCOM" "1"

  unset wasm_ver
  unset wc_wasm
  unset wc_bin
}


#
# Check for yasm, needed to compile assembler files
#
check_yasm()
{
  test_header yasm
  if check_avail "$YASM" YASM; then
    yasm_ver=`$YASM --version|grep "^yasm"|sed 's+^yasm \(.*\)+\1+'`
    if [ $? -ne 0 ]; then
      log_failure "yasm not found"
      fail
    else
      yasm_maj=`echo $yasm_ver|cut -d. -f1`
      yasm_min=`echo $yasm_ver|cut -d. -f2`
      yasm_rev=`echo $yasm_ver|cut -d. -f3`
      yasm_ver_mul=`expr $yasm_maj \* 10000 + $yasm_min \* 100 + $yasm_rev`
      if [ $yasm_ver_mul -lt 501 ]; then
        log_failure "found version $yasm_ver, expected at least 0.5.1"
        fail
      else
        log_success "found version $yasm_ver"
      fi
    fi
  fi
}


#
# Check for the iasl ACPI compiler, needed to compile vbox.dsl
#
check_iasl()
{
  test_header iasl
  if check_avail "$IASL" IASL; then
    iasl_ver=`$IASL|grep "ASL.*version"|sed 's+^ASL.*version \([0-9]*\).*+\1+'`
    if [ $? -ne 0 ]; then
      log_failure "iasl not found"
      fail
    else
      log_success "found version $iasl_ver"
      cnf_append "VBOX_IASLCMD" "`which_wrapper $IASL`"
    fi
  fi
}


#
# Check for xsltproc, needed by Main
#
check_xsltproc()
{
  if [ -n "$BUILD_LIBXSLT" ]; then
      return 0;
  fi
  test_header xslt
  if check_avail "$XSLTPROC" XSLTPROC; then
    xsltproc_ver=`$XSLTPROC --version`
    if [ $? -ne 0 ]; then
      log_failure "xsltproc not found"
      fail
    else
      log_success "found"
      cnf_append "VBOX_XSLTPROC" "`which_wrapper $XSLTPROC`"
    fi
  fi
}


#
# Check for libxml2, needed by the Runtime.
# 2.6.24 is known to NOT work, 2.6.26 is known to work (there is no 2.6.25 release)
#
check_libxml2()
{
  if [ -z "$BUILD_LIBXML2" ]; then
    test_header libxml2
    if which_wrapper pkg-config > /dev/null; then
      libxml2_ver=`pkg-config libxml-2.0 --modversion 2>> $LOG`
      if [ $? -ne 0 ]; then
        log_failure "libxml2 not found"
        fail
      else
        FLGXML2=`pkg-config libxml-2.0 --cflags`
        INCXML2=`strip_I "$FLGXML2"`
        LIBXML2=`pkg-config libxml-2.0 --libs`
        cat > $ODIR.tmp_src.cc << EOF
#include <cstdio>
#include <libxml/xmlversion.h>
extern "C" int main(void)
{
  printf("found version %s", LIBXML_DOTTED_VERSION);
#if LIBXML_VERSION >= 20626
  printf(", OK.\n");
  return 0;
#else
  printf(", expected version 2.6.26 or higher\n");
  return 1;
#endif
}
EOF
        [ -n "$INCXML2" ] && I_INCXML2=`prefix_I "$INCXML2"`
        if test_compile "$LIBXML2 $LIBPTHREAD $I_INCXML2" xml2 xml2; then
          if test_execute; then
            cnf_append "SDK_VBOX_LIBXML2_DEFS" "_REENTRANT" ## @todo get defines from --cflags
            cnf_append "SDK_VBOX_LIBXML2_INCS" "$INCXML2"
            cnf_append "SDK_VBOX_LIBXML2_LIBS" "`strip_l "$LIBXML2"`"
          fi
        fi
      fi
    elif which_wrapper xml2-config; then
      libxml2_ver=`xml2-config --version`
      if [ $? -ne 0 ]; then
        log_failure "xml2-config not found"
        fail
      else
        log_success "found version $libxml2_ver"
        FLGXML2=`xml2-config --cflags`
        INCXML2=`strip_I "$FLGXML2"`
        LIBXML2=`xml2-config --libs`
        cat > $ODIR.tmp_src.cc << EOF
#include <cstdio>
#include <libxml/xmlversion.h>
extern "C" int main(void)
{
  printf("found version %s", LIBXML_DOTTED_VERSION);
#if LIBXML_VERSION >= 20626
  printf(", OK.\n");
  return 0;
#else
  printf(", expected version 2.6.26 or higher\n");
  return 1;
#endif
}
EOF
        [ -n "$INCXML2" ] && I_INCXML2=`prefix_I "$INCXML2"`
        if test_compile "$LIBXML2 $LIBPTHREAD $I_INCXML2" xml2 xml2; then
          if test_execute; then
            cnf_append "SDK_VBOX_LIBXML2_DEFS" "_REENTRANT" ## @todo get defines from --cflags
            cnf_append "SDK_VBOX_LIBXML2_INCS" "$INCXML2"
            cnf_append "SDK_VBOX_LIBXML2_LIBS" "`strip_l "$LIBXML2"`"
          fi
        fi
      fi
    else
      log_failure "neither pkg-config nor xml2-config found"
      fail
    fi
  fi
}


#
# Check for libIDL, needed by xpcom
#
check_libidl()
{
  test_header libIDL

  if which_wrapper libIDL-config-2 > /dev/null; then
    libidl_ver=`libIDL-config-2 --version`
    if [ $? -ne 0 ]; then
      log_failure "libIDL-config-2 not working"
      fail
    else
      log_success "found version $libidl_ver"
      cnf_append "VBOX_LIBIDL_CONFIG" \
        "PKG_CONFIG_PATH=`libIDL-config-2 --prefix`/$LIB/pkgconfig `which_wrapper libIDL-config-2`"
    fi
  elif check_avail "libIDL-config" libIDL-config; then
    libidl_ver=`libIDL-config --version`
    if [ $? -ne 0 ]; then
      log_failure "libIDL-config not working"
      fail
    else
      log_success "found version $libidl_ver"
      cnf_append "VBOX_LIBIDL_CONFIG" "`which_wrapper libIDL-config`"
    fi
  fi
}


#
# Check for libdevmapper, needed by the VBoxVolInfo
#
check_libdevmapper()
{
  test_header libdevmapper
  cat > $ODIR.tmp_src.cc << EOF
#include <cstdio>
extern "C" {
#define private
#include <libdevmapper.h>
int main()
{
  char version[80];

  if (!dm_get_library_version(version, sizeof(version)))
  {
    printf("dm_get_library_version() failed.\n");
    return 1;
  }

  const char* v=version;
  unsigned int major = 0, minor = 0, micro = 0;

  for (; *v !='.' && *v != '\0'; v++) major = major*10 + *v-'0';
  if (*v == '.') v++;
  for (; *v !='.' && *v != '\0'; v++) minor = minor*10 + *v-'0';
  if (*v == '.') v++;
  for (; *v !='.' && *v != '\0'; v++) micro = micro*10 + *v-'0';

  printf("found version %s", version);
  if (major*10000 + minor*100 + micro >= 10200)
  {
    printf(", OK.\n");
    return 0;
  }
  else
  {
    printf(", expected version 1.02 or higher\n");
    return 1;
  }
}
}
EOF
  if test_compile "$LIBDEVMAPPER $INCDEVMAPPER" libdevmapper libdevmapper; then
    if test_execute; then
      cnf_append "VBOX_WITH_DEVMAPPER" "1"
    fi
  fi
}


#
# Check for openssl, needed for RDP and S3
#
check_ssl()
{
  if [ -z "$BUILD_LIBSSL" ]; then
    test_header ssl
    cat > $ODIR.tmp_src.cc << EOF
#include <cstdio>
#include <openssl/opensslv.h>
#include <openssl/ssl.h>
extern "C" int main(void)
{
  printf("found version %s", OPENSSL_VERSION_TEXT);
  SSL_library_init();
#if OPENSSL_VERSION_NUMBER >= 0x10001000
  printf(", OK.\n");
  return 0;
#else
  printf(", expected version 1.0.1 or higher\n");
  return 1;
#endif
}
EOF
    if test_compile "$INCCRYPTO $LIBCRYPTO" libcrypto openssl; then
      if test_execute nofatal; then
        cnf_append "SDK_VBOX_OPENSSL_INCS" "`strip_I "$INCCRYPTO"`"
        cnf_append "SDK_VBOX_OPENSSL_LIBS" "`strip_l "$LIBCRYPTO"`"
        cnf_append "SDK_VBOX_BLD_OPENSSL_LIBS" "`strip_l "$LIBCRYPTO"`"
      fi
    fi
  fi
}


#
# Check for pthread, needed by VBoxSVC, frontends, ...
#
check_pthread()
{
  test_header pthread
  cat > $ODIR.tmp_src.cc << EOF
#include <cstdio>
#include <pthread.h>
extern "C" int main(void)
{
  pthread_mutex_t mutex;
  if (pthread_mutex_init(&mutex, NULL)) {
    printf("pthread_mutex_init() failed\n");
    return 1;
  }
  if (pthread_mutex_lock(&mutex)) {
    printf("pthread_mutex_lock() failed\n");
    return 1;
  }
  if (pthread_mutex_unlock(&mutex)) {
    printf("pthread_mutex_unlock() failed\n");
    return 1;
  }
  printf("found, OK.\n");
}
EOF
  if test_compile $LIBPTHREAD pthread pthread; then
    if test_execute; then
      cnf_append "LIB_PTHREAD" "`strip_l "$LIBPTHREAD"`"
    fi
  fi
}


#
# Check for zlib, needed by VBoxSVC, Runtime, ...
#
check_z()
{
  test_header zlib
  cat > $ODIR.tmp_src.cc << EOF
#include <cstdio>
#include <zlib.h>
extern "C" int main(void)
{
  printf("found version %s", ZLIB_VERSION);
#if ZLIB_VERNUM >= 0x1210
  printf(", OK.\n");
  return 0;
#else
  printf(", expected version 1.2.1 or higher\n");
  return 1;
#endif
}
EOF
  [ -n "$INCZ" ] && I_INCZ=`prefix_I "$INCZ"`
  if test_compile "$LIBZ $I_INCZ" zlib zlib; then
    if test_execute; then
      echo "if1of (\$(KBUILD_TARGET),darwin freebsd haiku linux)" >> $CNF
      cnf_append " SDK_VBOX_ZLIB_LIBS" "`strip_l "$LIBZ"`"
      cnf_append " SDK_VBOX_ZLIB_INCS" "$INCZ"
      echo "endif" >> $CNF
    fi
  fi
}


#
# Check for libpng, needed by kchmviewer
#
check_png()
{
  test_header libpng
  cat > $ODIR.tmp_src.cc << EOF
#include <cstdio>
#include <png.h>
extern "C" int main(void)
{
  printf("found version %s", PNG_LIBPNG_VER_STRING);
#if PNG_LIBPNG_VER >= 10205
  printf(", OK.\n");
  return 0;
#else
  printf(", expected version 1.2.5 or higher\n");
  return 1;
#endif
}
EOF
  [ -n "$INCPNG" ] && I_INCPNG=`prefix_I "$INCPNG"`
  if test_compile "$LIBPNG $I_INCPNG" libpng libpng; then
    if test_execute; then
      cnf_append "SDK_VBOX_LIBPNG_LIBS" "`strip_l "$LIBPNG"`"
      cnf_append "SDK_VBOX_LIBPNG_INCS" "$INCPNG"
    fi
  fi
}

#
# Check for libvncserver, needed for VNC in OSE
#
check_vncserver()
{
  test_header libvncserver
  cat > $ODIR.tmp_src.cc <<EOF
#include <cstdio>
#include <rfb/rfbconfig.h>

extern "C" int main()
{
  const char* v=LIBVNCSERVER_VERSION;
  unsigned int major = 0, minor = 0, micro = 0;

  for (; *v !='.' && *v != '\0'; v++) major = major*10 + *v-'0';
  if (*v == '.') v++;
  for (; *v !='.' && *v != '\0'; v++) minor = minor*10 + *v-'0';
  if (*v == '.') v++;
  for (; *v !='.' && *v != '\0'; v++) micro = micro*10 + *v-'0';

  printf("found version %s", LIBVNCSERVER_PACKAGE_VERSION);
  if (major*10000 + minor*100 + micro >= 900)
  {
    printf(", OK.\n");
    return 0;
  }
  else
  {
    printf(", expected version 0.9 or higher\n");
    return 1;
  }
}
EOF
  if test_compile "$LIBVNCSERVER $INCVNCSERVER" libvncserver libvncserver; then
    if test_execute; then
      cnf_append "VBOX_WITH_EXTPACK_VNC" "1"
    fi
  fi
}

#
# Check for libcurl, needed by S3
#
check_curl()
{
  if [ -z "$BUILD_LIBCURL" ]; then
    test_header libcurl
    cat > $ODIR.tmp_src.cc << EOF
#include <cstdio>
#include <curl/curl.h>
extern "C" int main(void)
{
  printf("found version %s", LIBCURL_VERSION);
#if 10000*LIBCURL_VERSION_MAJOR + 100*LIBCURL_VERSION_MINOR + LIBCURL_VERSION_PATCH >= 71901
  printf(", OK.\n");
  return 0;
#else
  printf(", expected version 7.19.1 or higher\n");
  return 1;
#endif
}
EOF
    [ -n "$INCCURL" ] && I_INCCURL=`prefix_I "$INCCURL"`
    if test_compile "$LIBCURL $I_INCCURL" libcurl libcurl; then
      if test_execute; then
        cnf_append "SDK_VBOX_LIBCURL_LIBS" "`strip_l "$LIBCURL"`"
        cnf_append "SDK_VBOX_LIBCURL_INCS" "$INCCURL"
      fi
    fi
  fi
}


#
# Check for pam, needed by VRDPAuth
# Version 79 was introduced in 9/2005, do we support older versions?
# Debian/sarge uses 76
# OpenSUSE comes with 0.99.xxx where they changed the versioning scheme.
#
check_pam()
{
  test_header pam
  cat > $ODIR.tmp_src.cc << EOF
#include <cstdio>
#include <security/pam_appl.h>
extern "C" int main(void)
{
  printf("found version %d", __LIBPAM_VERSION);
  if (__LIBPAM_VERSION >= 76)
  {
    printf(", OK.\n");
    return 0;
  }
  else
  {
    printf(", expected version 76 or higher\n");
    return 1;
  }
}
EOF
  if test_compile "-lpam" pam pam nofatal; then
    if test_execute nofatal; then
      return 0;
    fi
  fi
  echo "pam0.x not found"
  test_header linux_pam
  cat > $ODIR.tmp_src.cc << EOF
#include <cstdio>
#include <security/pam_appl.h>
extern "C" int main(void)
{
  printf("found version %d.%d", __LINUX_PAM__, __LINUX_PAM_MINOR__);
  if (__LINUX_PAM__ >= 1)
  {
    printf(", OK.\n");
    return 0;
  }
  else
  {
    printf(", expected version 1.0 or higher\n");
    return 1;
  }
}
EOF
  if test_compile "-lpam" pam pam; then
    test_execute
  fi
}


#
# Check for the SDL library, needed by VBoxSDL and VirtualBox
# We depend at least on version 1.2.7
#
check_sdl()
{
  test_header SDL
  if [ "$OS" = "darwin" ]; then
    if [ -f "/System/Library/Frameworks/SDL.framework/SDL" ]; then
      PATH_SDK_LIBSDL="/System/Library/Frameworks/SDL.framework"
    elif [ -f "/Library/Frameworks/SDL.framework/SDL" ]; then
      PATH_SDK_LIBSDL="/Library/Frameworks/SDL.framework"
    fi
    if [ -n "$PATH_SDK_LIBSDL" ]; then
      foundsdl=1
      INCSDL="$PATH_SDK_LIBSDL/Headers"
      FLDSDL="-framework SDL"
    else
      log_failure "SDL framework not found"
      fail
    fi
  else
    if which_wrapper sdl-config > /dev/null; then
      FLGSDL=`sdl-config --cflags`
      INCSDL=`strip_I "$FLGSDL"`
      LIBSDL=`sdl-config --libs`
      LIBSDLMAIN="-lSDLmain"
      FLDSDL=
      foundsdl=1
    fi
  fi
  [ "$OS" = "linux" -o "$OS" = "darwin" -o "$OS" = "solaris" ] && LIBSDLMAIN=""
  if [ -n "$foundsdl" ]; then
    cat > $ODIR.tmp_src.cc << EOF
#include <cstdio>
#include <SDL.h>
#include <SDL_main.h>
#undef main
extern "C" int main(int argc, char** argv)
{
  printf("found version %d.%d.%d",
         SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL);
#if SDL_VERSION_ATLEAST(1,2,7)
  printf(", OK.\n");
  return 0;
#else
  printf(", expected version 1.2.7 or higher\n");
  return 1;
#endif
}
EOF
    [ -n "$INCSDL" ] && I_INCSDL=`prefix_I "$INCSDL"`
    if test_compile "$LIBSDL $LIBSDLMAIN $I_INCSDL $FLDSDL" SDL SDL; then
      if test_execute; then
        cnf_append "LIB_SDK_LIBSDL_SDL"     "`strip_l "$LIBSDL"`"
        cnf_append "SDK_LIBSDL_LIBPATH"     "`strip_L "$LIBSDL"`"
        cnf_append "LIB_SDK_LIBSDL_SDLMAIN" "`strip_l "$LIBSDLMAIN"`"
        [ -n "$INCSDL" ] && cnf_append "SDK_LIBSDL_INCS"    "$INCSDL"
        [ -n "$FLDSDL" ] && cnf_append "SDK_LIBSDL_LDFLAGS" "$FLDSDL"
      fi
    fi
  else
    log_failure "SDL not found (can be disabled using --disable-sdl)"
    fail
  fi
}


#
# Check for the SDL_ttf library, needed by VBoxSDL (secure label)
#
check_sdl_ttf()
{
  test_header SDL_ttf
  cat > $ODIR.tmp_src.cc << EOF
#include <cstdio>
#include <SDL_ttf.h>
#ifndef SDL_TTF_MAJOR_VERSION
#define SDL_TTF_MAJOR_VERSION TTF_MAJOR_VERSION
#define SDL_TTF_MINOR_VERSION TTF_MINOR_VERSION
#define SDL_TTF_PATCHLEVEL    TTF_PATCHLEVEL
#endif
extern "C" int main(void)
{
  printf("found version %d.%d.%d",
         SDL_TTF_MAJOR_VERSION, SDL_TTF_MINOR_VERSION, SDL_TTF_PATCHLEVEL);
#if 10000*SDL_TTF_MAJOR_VERSION + 100*SDL_TTF_MINOR_VERSION + SDL_TTF_PATCHLEVEL >= 20006
  printf(", OK.\n");
  return 0;
#else
  printf(", expected version 2.0.6 or higher\n");
  return 1;
#endif
}
EOF
  if test_compile "-lSDL_ttf $I_INCSDL" SDL_ttf SDL_ttf nofatal; then
    test_execute nofatal || \
      cnf_append "VBOX_WITH_SECURELABEL" ""
  else
    echo "not found -- disabling VBoxSDL secure label."
    cnf_append "VBOX_WITH_SECURELABEL" ""
  fi
}


#
# Check for libasound, needed by the ALSA audio backend
#
check_alsa()
{
  test_header ALSA
  cat > $ODIR.tmp_src.cc << EOF
#include <cstdio>
#include <alsa/asoundlib.h>
extern "C" int main(void)
{
  printf("found version %d.%d.%d",
         SND_LIB_MAJOR, SND_LIB_MINOR, SND_LIB_SUBMINOR);
#if 10000*SND_LIB_MAJOR + 100*SND_LIB_MINOR + SND_LIB_SUBMINOR >= 10014
  printf(", OK.\n");
  return 0;
#else
  printf(", expected version 1.0.14 or higher\n");
  return 1;
#endif
}
EOF
  if test_compile "-lasound" asound asound; then
    test_execute
  fi
}


#
# Check for PulseAudio
#
check_pulse()
{
  test_header "PulseAudio"
  cat > $ODIR.tmp_src.cc << EOF
#include <cstdio>
#include <pulse/version.h>
extern "C" int main(void)
{
  printf("found version %s API version %d", pa_get_headers_version(), PA_API_VERSION);
#if PA_API_VERSION >= 9
  printf(", OK.\n");
  return 0;
#else
  printf(", expected version 0.9.0 (API version 9) or higher\n");
  return 1;
#endif
}
EOF
  if test_compile "$INCPULSE $LIBPULSE -lpulse" pulse pulse; then
    test_execute
  fi
}


#
# Check for the X libraries (Xext, X11)
#
check_x()
{
  test_header "X libraries"
  cat > $ODIR.tmp_src.cc << EOF
#include <cstdio>
#include <X11/Xlib.h>
extern "C" int main(void)
{
  Display *dpy;
  int scrn_num;
  Screen *scrn;
  Window win;

  dpy      = XOpenDisplay(NULL);
  scrn_num = DefaultScreen(dpy);
  scrn     = ScreenOfDisplay(dpy, scrn_num);
  win      = XCreateWindow(dpy, RootWindowOfScreen(scrn), 0, 0, 100, 100,
                           0, 16, InputOutput, CopyFromParent, 0, NULL);
  XDestroyWindow(dpy, win);
  XCloseDisplay(dpy);
}
EOF
  [ -n "$INCX11" ] && I_INCX11=`prefix_I "$INCX11"`
  if test_compile "$LIBX11 $I_INCX11" Xlibs Xlibs; then
    log_success "found"
  fi
}


#
# Check for the Xcursor library, needed by VBoxSDL.
#
check_xcursor()
{
  test_header Xcursor
  cat > $ODIR.tmp_src.cc << EOF
#include <cstdio>
#include <X11/Xlib.h>
#include <X11/Xcursor/Xcursor.h>
extern "C" int main(void)
{
  XcursorImage *cursor = XcursorImageCreate (10, 10);
  XcursorImageDestroy(cursor);
  return 0;
}
EOF
  [ -n "$INCX11" ] && I_INCX11=`prefix_I "$INCX11"`
  if test_compile "$LIBX11 $LIBXCURSOR $I_INCX11" Xcursor Xcursor; then
    log_success "found"
    cnf_append "VBOX_XCURSOR_LIBS" "`strip_l "$LIBXCURSOR"`"
  fi
}


#
# Check for the Xinerama library, needed by the Qt GUI
#
check_xinerama()
{
  test_header Xinerama
  cat > $ODIR.tmp_src.cc << EOF
#include <X11/Xlib.h>
#include <X11/extensions/Xinerama.h>
extern "C" int main(void)
{
  Display *dpy;
  Bool flag;
  dpy = XOpenDisplay(NULL);
  if (dpy)
  {
    flag = XineramaIsActive(dpy);
    XCloseDisplay(dpy);
  }
}
EOF
  [ -n "$INCX11" ] && I_INCX11=`prefix_I "$INCX11"`
  if test_compile "$LIBX11 $LIBXINERAMA $I_INCX11" Xinerama Xinerama; then
    log_success "found"
  fi
}


#
# Check for the Xinerama library, needed by the Qt GUI
#
check_xrandr()
{
  test_header Xrandr
  cat > $ODIR.tmp_src.cc << EOF
#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>
extern "C" int main(void)
{
  Display *dpy;
  Bool flag;
  int major, minor;
  dpy = XOpenDisplay(NULL);
  if (dpy)
  {
    flag = XRRQueryVersion(dpy, &major, &minor);
    XCloseDisplay(dpy);
  }
}
EOF
  [ -n "$INCX11" ] && I_INCX11=`prefix_I "$INCX11"`
  if test_compile "$LIBX11 $LIBXRANDR $I_INCX11" Xrandr Xrandr; then
    log_success "found"
  fi
}


#
# Check for OpenGL
#
check_opengl()
{
  # On darwin this is a on/off decision only
  if [ "$OS" = "darwin" ]; then
    test_header "OpenGL support"
    echo "enabled"
    cnf_append "VBOX_WITH_CROGL"     "1"
  else
    check_xmu
    check_mesa
  fi
}


#
# Check for the Xmu library, needed by OpenGL
#
check_xmu()
{
  test_header Xmu
  cat > $ODIR.tmp_src.cc << EOF
#include <cstdio>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xmu/StdCmap.h>
extern "C" int main(void)
{
  Display *dpy;
  int scrn_num;
  Screen *scrn;

  dpy = XOpenDisplay(NULL);
  if (dpy)
  {
    scrn_num = DefaultScreen(dpy);
    scrn     = ScreenOfDisplay(dpy, scrn_num);
    Status status = XmuLookupStandardColormap(dpy, RootWindowOfScreen(scrn), 0,
                    24, XA_RGB_DEFAULT_MAP, False, True);
    printf("Status = %x\n", status);
    XCloseDisplay(dpy);
  }
  return 0;
}
EOF
  [ -n "$INCX11" ] && I_INCX11=`prefix_I "$INCX11"`
  if test_compile "$LIBX11 $LIBXMU $I_INCX11" Xmu Xmu; then
    log_success "found"
    cnf_append "VBOX_XMU_LIBS" "`strip_l "$LIBXMU"`"
  fi
}

#
# Check for Mesa, needed by OpenGL
#
check_mesa()
{
  test_header "Mesa / GLU"
  cat > $ODIR.tmp_src.cc << EOF
#include <cstdio>
#include <X11/Xlib.h>
#include <GL/glx.h>
#include <GL/glu.h>
extern "C" int main(void)
{
  Display *dpy;
  int major, minor;

  dpy = XOpenDisplay(NULL);
  if (dpy)
  {
    Bool glx_version = glXQueryVersion(dpy, &major, &minor);
    XCloseDisplay(dpy);
    if (glx_version)
    {
      printf("found version %u.%u, OK.\n", major, minor);
      return 0;
    }
  }
  printf("found (inactive), OK.\n");
  return 0;
}
EOF
  [ -n "$INCX11" ] && I_INCX11=`prefix_I "$INCX11"`
  if test_compile "$LIBX11 $MESA $I_INCX11" Mesa Mesa; then
    [ $PASSIVE_MESA -eq 1 ] && unset DISPLAY
    test_execute
  fi
}


#
# Check for the Qt5 library, needed by the VirtualBox frontend
#
# Currently not fatal.
#
check_qt5()
{
  foundqt5=
  test_header Qt5
  cat > $ODIR.tmp_src.cc << EOF
#include <QtGlobal>
extern "C" int main(void)
{
EOF
  echo "#if QT_VERSION >= $(($QT5MAJ*65536+$QT5MIN*256))" >> $ODIR.tmp_src.cc
  cat >> $ODIR.tmp_src.cc << EOF
  return 0;
#else
  return 1;
#endif
}
EOF
  if [ "$OS" = "darwin" ]; then
    # First check if there is the internal version of Qt. If yes nothing else
    # has to be done.
    QT_INTERNAL=`/bin/ls -rd1 $PWD/tools/$BUILD_TARGET.$BUILD_PLATFORM_ARCH/qt/* 2> /dev/null`
    for t in $QT_INTERNAL; do
      if [ -f "$t/Frameworks/QtCoreVBox.framework/QtCoreVBox" ]; then
        cnf_append "VBOX_WITH_ORACLE_QT" "1"
        log_success "use internal version"
        return
      fi
    done
    # Now try the user provided directory and some of the standard directories.
    QT_TRIES="$QT5DIR /System/Library /Library"
    for t in $QT_TRIES; do
      if [ -f "$t/Frameworks/QtCore.framework/QtCore" ]; then
        PATH_SDK_QT5="$t"
        break
      fi
    done
    # Add the necessary params for building the test application
    if [ -n "$PATH_SDK_QT5" ]; then
      foundqt5=1
      INCQT5=-I$PATH_SDK_QT5/Frameworks/QtCore.framework/Headers
      LIBQT5=-F$PATH_SDK_QT5/Frameworks
      FLGQT5="-framework QtCore"
    else
      log_failure "Qt5 framework not found (can be disabled using --disable-qt)"
      fail
    fi
  else # !darwin
    if [ $QT5DIR_PKGCONFIG -eq 1 ]; then
      # Default is to use pkg-config:
      if which_wrapper pkg-config > /dev/null; then
        qt5_ver=`pkg-config Qt5Core --modversion 2>> $LOG`
        if [ $? -eq 0 ]; then
          echo "(Qt5 from pkg-config)" >> $LOG
          FLGQT5=`pkg-config Qt5Core --cflags`
          # gcc 4.8 is able to compile with C++11 (see also VBOX_GCC_std in Config.kmk)
          [ $cc_maj -eq 4 -a $cc_min -eq 8 ] && FLGQT5="$FLGQT5 -std=c++11"
          INCQT5=`strip_I "$FLGQT5"`
          LIBDIR5=`pkg-config Qt5Core --variable=libdir`
          LIBQT5=`pkg-config Qt5Core --libs`
          LIBQT5="-L$LIBDIR5 $LIBQT5"
          TOOLQT5=`pkg-config Qt5Core --variable=prefix`
          TOOLQT5BIN=`pkg-config Qt5Core --variable=host_bins`
          if test_compile "$LIBQT5 $LIBPTHREAD $FLGQT5" qt5 qt5 nofatal; then
            test_execute_path "`strip_L "$LIBQT5"`" nofatal && foundqt5=3 # pkg-config
          fi
        fi
      else
        log_failure "pkg-config not found"
        fail
      fi
    fi
    if [ -z "$foundqt5" ]; then
      # Do it the old way (e.g. user has specified QT5DIR):
      for q in $QT5DIR "$PWD/tools/linux.$TARGET_MACHINE"/qt/v5.*; do
        echo "(Qt5 from '$q')" >> $LOG
        INCQT5="$q/include $q/include/QtCore"
        FLGQT5="-DQT_SHARED"
        I_INCQT5=`prefix_I "$INCQT5"`
        LIBQT5="-L$q/lib -lQt5CoreVBox"
        TOOLQT5="$q"
        if test_compile "$LIBQT5 $LIBPTHREAD $I_INCQT5 $FLGQT5" qt5 qt5 nofatal &&
            test_execute_path "`strip_L "$LIBQT5"`" nofatal; then
          foundqt5=2 # internal
          break;
        fi
        LIBQT5="-L$q/lib -lQt5Core"
        if test_compile "$LIBQT5 $LIBPTHREAD $I_INCQT5 $FLGQT5" qt5 qt5 nofatal &&
            test_execute_path "`strip_L "$LIBQT5"`" nofatal; then
          foundqt5=1 # no pkg-config, Qt directory
          break;
        fi
      done
    fi
  fi
  if [ -n "$foundqt5" ]; then
    # We decided which version of Qt to use, now enforce the version requirement:
    cat > $ODIR.tmp_src.cc << EOF
#include <cstdio>
#include <QtGlobal>
extern "C" int main(void)
{
  printf("found version %s", QT_VERSION_STR);
EOF
  echo "#if QT_VERSION >= $(($QT5MAJ*65536+$QT5MIN*256))" >> $ODIR.tmp_src.cc
    cat >> $ODIR.tmp_src.cc << EOF
  printf(", OK.\n");
  return 0;
#else
EOF
echo "  printf(\", expected version $QT5MAJ.$QT5MIN or higher\\\\n\");" >> $ODIR.tmp_src.cc
    cat >> $ODIR.tmp_src.cc << EOF
  return 1;
#endif
}
EOF
    [ -n "$INCQT5" ] && I_INCQT5=`prefix_I "$INCQT5"`
    if test_compile "$LIBQT5 $LIBPTHREAD $I_INCQT5 $FLGQT5" qt5 qt5 nofatal; then
      if test_execute_path "`strip_L "$LIBQT5"`"; then
        if [ "$OS" = "darwin" ]; then
          # Successful build & run the test application so add the necessary
          # params to AutoConfig.kmk:
          cnf_append "PATH_SDK_QT5_INC" "$PATH_SDK_QT5/Frameworks"
          cnf_append "PATH_SDK_QT5_LIB" "$PATH_SDK_QT5/Frameworks"
          cnf_append "PATH_SDK_QT5" "$PATH_SDK_QT5/Frameworks"
          # Check for the moc tool in the Qt directory found & some standard
          # directories.
          for q in $PATH_SDK_QT5 /usr /Developer/Tools/Qt; do
            if which_wrapper "$q/bin/moc" > /dev/null; then
              cnf_append "PATH_TOOL_QT5_BIN" "$q/bin"
            fi
          done
        else
          # Strip .../QtCore as we add components ourself:
          INCQT5=`echo "$INCQT5"|$KBUILD_SED 's|\([^ ]*\)/QtCore|\1|g; s| $||g'`
          # store only the first path, remove all other paths
          # most likely pkg-config gave us -I/usr/include/qt5 -I/usr/include/qt5/QtCore
          INCQT5=`echo "$INCQT5"|$KBUILD_SED 's|\([^ ]*\) .*|\1|'`
          cnf_append "VBOX_PATH_QT_LIB" "`strip_L "$LIBQT5"`"
          cnf_append "PATH_SDK_QT5_INC" "$INCQT5"
          # This is not quite right since the qt libpath does not have to be first...
          cnf_append "PATH_SDK_QT5_LIB" '$'"(firstword `strip_L "$LIBQT5"`)"
          if [ "$foundqt5" = "2" ]; then
            cnf_append "VBOX_WITH_ORACLE_QT" "1"
          fi
          if [ "$foundqt5" != "3" ]; then
            TOOLQT5BIN="$TOOLQT5/bin"
          fi
          test_header "Qt5 devtools"
          # Try it with a suffix, some platforms use that
          if which_wrapper "$TOOLQT5BIN/moc-qt5" > /dev/null; then
            QT5BINSUFF="-qt5"
          else
            QT5BINSUFF=""
          fi
          moc_ver=`$TOOLQT5BIN/moc$QT5BINSUFF -v 2>&1|sed 's+^.*(Qt \(.*\))+\1+'`
          if [ $? -ne 0 ]; then
            log_failure "moc$QT5BINSUFF not working"
            fail
          else
            log_success "found version $moc_ver"
            cnf_append "VBOX_PATH_QT" "$TOOLQT5"
            cnf_append "PATH_SDK_QT5" "$TOOLQT5"
            cnf_append "PATH_TOOL_QT5_BIN" "$TOOLQT5BIN"
            [ -n "$QT5BINSUFF" ] && cnf_append "TOOL_QT5_BIN_SUFF" "$QT5BINSUFF"
          fi
        fi
      fi
    else
      log_failure "qt5 not working"
      fail
    fi
  else
    log_failure "qt5 not found"
    fail
  fi
}


#
# Check for libopus
#
check_libopus()
{
  if [ -z "$BUILD_LIBOPUS" ]; then
    test_header libopus
    if which_wrapper pkg-config > /dev/null; then
      libopus_ver=`pkg-config opus --modversion 2>> $LOG`
      if [ $? -eq 0 ]; then
        FLGOPUS=`pkg-config opus --cflags`
        INCOPUS=`strip_I "$FLGOPUS"`
        LIBOPUS=`pkg-config opus --libs`
      fi
      cat > $ODIR.tmp_src.cc << EOF
#include <cstdio>
#include <opus/opus.h>
extern "C" int main(void)
{
  OpusEncoder *test;
  printf("found, OK.\n");
}
EOF
      [ -n "$INCOPUS" ] && I_INCOPUS=`prefix_I "$INCOPUS"`
      if test_compile "$LIBOPUS $I_INCOPUS" opus opus; then
        if test_execute; then
          cnf_append "SDK_VBOX_OPUS_INCS" "$INCOPUS"
          cnf_append "SDK_VBOX_OPUS_LIBS" "`strip_l "$LIBOPUS"`"
          fi
      fi
    fi
  fi
}


#
# Check for libvpx
#
check_vpx()
{
  if [ -z "$BUILD_LIBVPX" ]; then
    test_header libvpx
    if which_wrapper pkg-config > /dev/null; then
      libvpx_ver=`pkg-config vpx --modversion 2>> $LOG`
      if [ $? -eq 0 ]; then
        FLGVPX=`pkg-config vpx --cflags`
        INCVPX=`strip_I "$FLGVPX"`
        LIBVPX=`pkg-config vpx --libs`
      fi
      cat > $ODIR.tmp_src.cc << EOF
#include <cstdio>
#include <vpx/vpx_codec.h>
extern "C" int main(void)
{
  int version = vpx_codec_version();
  int verMajor = VPX_VERSION_MAJOR(version);
  int verMinor = VPX_VERSION_MINOR(version);
  int verPatch = VPX_VERSION_PATCH(version);
  printf("found version %d.%d.%d", verMajor, verMinor, verPatch);
  if (   verMajor == 1 && verMinor >= 0
      || verMajor == 0 && verMinor == 9 && verPatch >= 5)
  {
      printf(", OK.\n");
      return 0;
  }
  else
  {
      printf(", expected version 0.9.5 or higher\n");
      return 1;
   }
}
EOF
      [ -n "$INCVPX" ] && I_INCVPX=`prefix_I "$INCVPX"`
      if test_compile "$LIBVPX $I_INCVPX" vpx vpx; then
        if test_execute; then
          cnf_append "SDK_VBOX_VPX_INCS" "$INCVPX"
          cnf_append "SDK_VBOX_VPX_LIBS" "`strip_l "$LIBVPX"`"
          fi
      fi
    fi
  fi
}


#
# Check whether static libstdc++ is installed. This library is required
# for the Linux guest additions.
#
check_staticlibstdcxx()
{
  test_header "static stc++ library"
  libstdcxx=`$CXX -print-file-name=libstdc++.a`
  cat > $ODIR.tmp_src.cc << EOF
#include <string>

extern "C" int main(void)
{
  std::string s = "test";
  return 0;
}
EOF
  if test_compile "$libstdcxx" libstdc++ libstdc++; then
    log_success "found"
  fi
}


#
# Check for Linux sources
#
check_linux()
{
  test_header "Linux kernel sources"
  cat > $ODIR.tmp_src.c << EOF
#include <linux/version.h>
int printf(const char *format, ...);
int main(void)
{
  printf("found version %d.%d.%d",  LINUX_VERSION_CODE / 65536,
                                   (LINUX_VERSION_CODE % 65536) / 256,
                                    LINUX_VERSION_CODE          % 256);
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,0)
  printf(", OK.\n");
  return 0;
#else
  printf(", expected version 2.4.0 or higher\n");
  return 1;
#endif
}
EOF
  echo "compiling the following source file:" >> $LOG
  cat $ODIR.tmp_src.c >> $LOG
  echo "using the following command line:" >> $LOG
  echo "$CC -O -Wall -o $ODIR.tmp_out $ODIR.tmp_src.c -nostdinc -I$LINUX/include " \
      "-I$LINUX/include/generated/uapi" >> $LOG
  $CC -O -Wall -o $ODIR.tmp_out $ODIR.tmp_src.c -nostdinc -I$LINUX/include \
      -I$LINUX/include/generated/uapi >> $LOG 2>&1
  if [ $? -ne 0 ]; then
    echo
    echo "  Linux kernel headers not found at $LINUX"
    echo "  Check the file $LOG for detailed error information."
    fail
  else
    if test_execute; then
      cnf_append "VBOX_LINUX_SRC" "`cd $LINUX ; pwd`"
    fi
  fi
}

#
# Check for kchmviewer, needed to display the online help
# (unused as we ship kchmviewer)
#
check_kchmviewer()
{
  test_header kchmviewer
  if check_avail "$KCHMVIEWER" KCHMVIEWER; then
    kchmviewer_ver=`$KCHMVIEWER --version|grep "^KchmViewer:"|sed 's+^KchmViewer: \(.*\)+\1+'`
    if [ $? -ne 0 ]; then
      log_failure "kchmviewer not working"
      fail
    else
      log_success "found version $kchmviewer_ver"
    fi
  fi
}


#
# Check for the kBuild tools, we don't support GNU make
#
check_kbuild()
{
  test_header kBuild
  if which_wrapper "$KBUILDDIR/bin/$OS.$BUILD_MACHINE/kmk" > /dev/null; then
    KBUILDDIR_BIN="$KBUILDDIR/bin/$OS.$BUILD_MACHINE"
    echo "PATH_KBUILD=\"`cd $KBUILDDIR ; pwd`\"" >> $ENV
    echo "export PATH_KBUILD" >> $ENV
    echo "PATH_DEVTOOLS=\"$DEVDIR\"" >> $ENV
    echo "export PATH_DEVTOOLS" >> $ENV
    echo "path_kbuild_bin=\"\$PATH_KBUILD/bin/\$BUILD_TARGET.\$BUILD_PLATFORM_ARCH\"" >> $ENV
    echo "export PATH_KBUILD_BIN" >> $ENV
    echo "path_dev_bin=\"\$PATH_DEVTOOLS/\$BUILD_TARGET.\$BUILD_PLATFORM_ARCH\"/bin" >> $ENV
    if [ "$OS" = "solaris" ]; then
      # Because of sh being non-default shell in Solaris we need to export PATH again when
      # sourcing env.sh. Simply exporting from ./configure does not export PATH correctly.
      echo "PATH=\"$ORGPATH\"" >> $ENV
      echo "echo \"\$PATH\" | /usr/sfw/bin/ggrep -q \"\$path_kbuild_bin\" || PATH=\"\$path_kbuild_bin:\$PATH\"" >> $ENV
      echo "echo \"\$PATH\" | /usr/sfw/bin/ggrep -q \"\$path_dev_bin\" || PATH=\"\$path_dev_bin:\$PATH\"" >> $ENV
    else
      echo "echo \"\$PATH\" | grep -q \"\$path_kbuild_bin\" || PATH=\"\$path_kbuild_bin:\$PATH\"" >> $ENV
      echo "echo \"\$PATH\" | grep -q \"\$path_dev_bin\" || PATH=\"\$path_dev_bin:\$PATH\"" >> $ENV
    fi
    echo "export PATH" >> $ENV
    echo "unset path_kbuild_bin path_dev_bin" >> $ENV
    KBUILD_SED="$KBUILDDIR_BIN/kmk_sed"
  elif [ "$OS.$BUILD_MACHINE" = "darwin.amd64" ]; then
    # Currently there are no amd64 kBuild bins. So use the x86 variant in any case.
    KBUILDDIR_BIN="$KBUILDDIR/bin/$OS.x86"
    echo "PATH_KBUILD=\"`cd $KBUILDDIR ; pwd`\"" >> $ENV
    echo "export PATH_KBUILD" >> $ENV
    echo "PATH_DEVTOOLS=\"$DEVDIR\"" >> $ENV
    echo "export PATH_DEVTOOLS" >> $ENV
    echo "path_kbuild_bin=\"\$PATH_KBUILD/bin/\$BUILD_TARGET.x86\"" >> $ENV
    echo "PATH_KBUILD_BIN=\"\$path_kbuild_bin\"" >> $ENV
    echo "export PATH_KBUILD_BIN" >> $ENV
    echo "path_dev_bin=\"\$PATH_DEVTOOLS/\$BUILD_TARGET.\$BUILD_PLATFORM_ARCH\"/bin" >> $ENV
    echo "echo \"\$PATH\" | grep -q \"\$path_kbuild_bin\" || PATH=\"\$path_kbuild_bin:\$PATH\"" >> $ENV
    echo "echo \"\$PATH\" | grep -q \"\$path_dev_bin\" || PATH=\"\$path_dev_bin:\$PATH\"" >> $ENV
    echo "export PATH" >> $ENV
    echo "unset path_kbuild_bin path_dev_bin" >> $ENV
    KBUILD_SED="$KBUILDDIR_BIN/kmk_sed"
  elif check_avail "kmk" KBUILDDIR really; then
    # check for installed kBuild
    KBUILD_SED="`which_wrapper kmk_sed`"
  else
    fail
  fi
  log_success "found"
}


#
# Check for compiler.h
# Some Linux distributions include "compiler.h" in their libc linux
# headers package, some don't.  Most don't need it, building might (!)
# not succeed on openSUSE without it.
#
# See http://www.mail-archive.com/qemu-devel%40nongnu.org/msg07980.html
#
check_compiler_h()
{
  test_header compiler.h
  if test ! -f "/usr/include/linux/compiler.h"; then
    log_success "compiler.h not found"
  else
    cnf_append "VBOX_WITH_LINUX_COMPILER_H" "1"
    log_success "compiler.h found"
  fi
}

#
# Check for libcap.
# Required to pass CAP_NET_RAW to our binaries to allow to open SOCK_RAW
# sockets for doing ICMP requests.
#
check_libcap()
{
  test_header "libcap library"
  cat > $ODIR.tmp_src.cc << EOF
#include <cstdio>
#include <sys/types.h>
#include <linux/types.h>
#include <sys/capability.h>

extern "C" int main(void)
{
  char buf[1024];
  cap_t caps = cap_get_proc();
  snprintf(buf, sizeof(buf), "Current caps are '%s'\n", cap_to_text(caps, NULL));
  return 0;
}
EOF
  if test_compile $LIBCAP libcap libcap; then
    if test_execute; then
      log_success "found"
    fi
  fi
}

#
# Check if we are able to build 32-bit applications (needed for the guest additions)
#
check_32bit()
{
  test_header "32-bit support"
  cat > $ODIR.tmp_src.c << EOF
#include <stdint.h>
int main(void)
{
  return 0;
}
EOF
  echo "compiling the following source file:" >> $LOG
  cat $ODIR.tmp_src.c >> $LOG
  echo "using the following command line:" >> $LOG
  echo "$CC -m32 -O -Wall -o $ODIR.tmp_out $ODIR.tmp_src.c" >> $LOG
  $CC -m32 -O -Wall -o $ODIR.tmp_out $ODIR.tmp_src.c >> $LOG 2>&1
  if [ $? -ne 0 ]; then
    echo
    echo "  Cannot compile 32-bit applications (missing headers and/or libraries)!"
    echo "  Check the file $LOG for detailed error information."
    fail
  else
    echo "executing the binary" >> $LOG
    $ODIR.tmp_out 2> $ODIR.test_execute.log
    rc=$?
    cat $ODIR.test_execute.log >> $LOG
    if [ $rc -ne 0 ]; then
      echo
      echo "  Cannot execute 32-bit applications! Either enable 32-bit support in the"
      echo "  kernel configuration or use --disable-vmmraw to disable 32-bit guests."
      fail
      return 1
    fi
  fi
  log_success ""
}


#
# Check for Python
#
check_python()
{
  test_header "Python support"

  # On darwin this is a on/off decision only
  if [ "$OS" = "darwin" ]; then
    echo "enabled"
    cnf_append "VBOX_WITH_PYTHON"     "1"
    return
  fi

  cat > $ODIR.tmp_src.cc << EOF
#include <cstdio>
#include <Python.h>
extern "C" int main(void)
{
  Py_Initialize();
  printf("found version %s", PY_VERSION);
#if PY_VERSION_HEX >= 0x02060000
  printf(", OK.\n");
  return 0;
#else
  printf(", expected version 2.6 or higher\n");
  return 1;
#endif
}
EOF
  found=
  SUPPYTHONLIBS="python2.7 python2.6 python3.1 python3.2 python3.3 python3.4 python3.4m python3.5 python3.5m python3.6 python3.6m"
  for p in $PYTHONDIR; do
    for d in $SUPPYTHONLIBS; do
      for b in lib/x86_64-linux-gnu lib/i386-linux-gnu lib64 lib/64 lib; do
        echo "compiling the following source file:" >> $LOG
        cat $ODIR.tmp_src.cc >> $LOG
        echo "using the following command line:" >> $LOG
        echo "$CXX -O -Wall -o $ODIR.tmp_out $ODIR.tmp_src.cc -I$p/include/$d $p/$b/lib$d.so" >> $LOG
        $CXX -O -Wall -o $ODIR.tmp_out $ODIR.tmp_src.cc -I$p/include/$d $p/$b/lib$d.so >> $LOG 2>&1
        if [ $? -eq 0 ]; then
          found=1
          break
        fi
      done
      if [ -n "$found" ]; then break; fi
    done
    if [ -n "$found" ]; then break; fi
  done
  if [ -n "$found" ]; then
    if test_execute; then
      cnf_append "VBOX_WITH_PYTHON"     "1"
      cnf_append "VBOX_PATH_PYTHON_INC" "$p/include/$d"
      cnf_append "VBOX_LIB_PYTHON"      "$p/$b/lib$d.so"
    else
      log_failure "Python not working"
      fail
    fi
  else
    log_failure "Python not found"
    fail
  fi
}


#
# Check for Java
#
check_java()
{
  test_header "Java support"
  log_success
}


#
# Setup wine
#
setup_wine()
{
  test_header "Wine support"
  if ! which_wrapper wine > /dev/null; then
    echo "  wine binary not found"
    fail
  fi
  if ! which_wrapper wine > /dev/null; then
    echo "  wine not found"
    fail
  fi
  wine_version="`wine --version`"
  case "`expr "$wine_version" : 'wine-\([0-9.]*\)' '>' 1.1.43`" in
    "0")
      if ! which_wrapper wineprefixcreate > /dev/null; then
        echo "  wineprefixcreate not found"
        fail
      fi
    ;;
    *) eval "wineprefixcreate() { true ; }" ;; # now created automatically
  esac
  export WINEPREFIX="${ODIR}wine.$BUILD_MACHINE"
  echo "WINEPREFIX=\"${ODIR}wine.$BUILD_MACHINE\"" >> $ENV
  echo "export WINEPREFIX" >> $ENV
  rm -rf $WINEPREFIX
  mkdir -p $WINEPREFIX
  touch $WINEPREFIX/.no_prelaunch_window_flag
  if ! wineprefixcreate -q > /dev/null 2>&1; then
    echo "  wineprefixcreate failed"
    fail
  fi
  tmp=.tmp.wine.reg
  rm -f $tmp
  echo 'REGEDIT4' > $tmp
  echo '' >> $tmp
  echo '[HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment]' >> $tmp
  echo "\"PATH\"=\"c:\\\\\\\\windows\\\\\\\\system32;c:\\\\\\\\windows;z:$DEVDIR/win.x86/vcc/v8/bin/Microsoft.VC80.CRT;z:$DEVDIR/win.x86/HTML_Help_Workshop/v1.3\"" >> $tmp
  echo '' >> $tmp
  echo '[HKEY_CURRENT_USER\Software\Wine\AppDefaults\hhc.exe\DllOverrides]' >> $tmp
  echo '"itss"="native"' >> $tmp
  echo '' >> $tmp
  echo '[HKEY_CURRENT_USER\Software\Wine\AppDefaults\hhw.exe\DllOverrides]' >> $tmp
  echo '"itss"="native"' >> $tmp
  echo '' >> $tmp
  if ! wine regedit $tmp > /dev/null 2>&1; then
    rm -f $tmp
    echo "  failed to load registry changes (path)."
    fail
  fi
  rm -f $tmp
  log_success "found"
}


#
# Check for gSOAP.
#
check_gsoap()
{
  test_header "GSOAP compiler"
  if [ -z "$GSOAP" -a -z "$GSOAP_IMPORT" ]; then
    if which_wrapper pkg-config > /dev/null; then
      GSOAP_CXX_LIBS=`pkg-config gsoapssl++ --libs 2>> $LOG`
      if [ $? -eq 0 ]; then
        GSOAP=`pkg-config gsoapssl++ --variable=exec_prefix`
        GSOAP_IMPORT="$GSOAP/share/gsoap/import"
        if [ ! -d "$GSOAP_IMPORT"  -a  -d "$GSOAP/include/gsoap" ]; then
          GSOAP_IMPORT="$GSOAP/include/gsoap"
        fi
        cnf_append "VBOX_GSOAP_INSTALLED"   "1"
        cnf_append "VBOX_PATH_GSOAP"        "$GSOAP"
        cnf_append "VBOX_PATH_GSOAP_IMPORT" "$GSOAP_IMPORT"
        if [ -f "$GSOAP/share/gsoap/stdsoap2.cpp" ]; then
          cnf_append "VBOX_GSOAP_CXX_SOURCES" "$GSOAP/share/gsoap/stdsoap2.cpp"
        else
          cnf_append "VBOX_GSOAP_CXX_SOURCES" ""
        fi
        cnf_append "VBOX_GSOAP_CXX_LIBS"    "`strip_l "$GSOAP_CXX_LIBS"`"
        gsoap_version=`pkg-config gsoapssl++ --modversion`
        log_success "found version $gsoap_version"
        return
      fi
    fi
  fi
  if [ -z "$GSOAP" ]; then
    GSOAP="/usr"
  fi
  if which_wrapper "$GSOAP/bin/soapcpp2" > /dev/null; then
    if which_wrapper "$GSOAP/bin/wsdl2h" > /dev/null; then
      if [ -f "$GSOAP/include/stdsoap2.h" ]; then
        # TODO: Check for libgsoap++.a/so

        if [ -z "$GSOAP_IMPORT" ]; then
          GSOAP_IMPORT="$GSOAP/share/gsoap/import"
          if [ ! -d "$GSOAP_IMPORT"  -a  -d "$GSOAP/include/gsoap" ]; then
            GSOAP_IMPORT="$GSOAP/include/gsoap"
          fi
        fi
        if [ -f "$GSOAP_IMPORT/stlvector.h" ]; then
          cnf_append "VBOX_GSOAP_INSTALLED"   "1"
          cnf_append "VBOX_PATH_GSOAP"        "$GSOAP"
          cnf_append "VBOX_PATH_GSOAP_IMPORT" "$GSOAP_IMPORT"
          if [ -f "$GSOAP/share/gsoap/stdsoap2.cpp" ]; then
            cnf_append "VBOX_GSOAP_CXX_SOURCES" "$GSOAP/share/gsoap/stdsoap2.cpp"
          else
            cnf_append "VBOX_GSOAP_CXX_SOURCES" ""
          fi
          cnf_append "VBOX_GSOAP_CXX_LIBS"    "libgsoapssl++"
          log_success "found"
        else
          log_failure "stlvector.h not found -- disabling webservice"
          cnf_append "VBOX_WITH_WEBSERVICES" ""
        fi
      else
        log_failure "stdsoap2.h not found -- disabling webservice"
        cnf_append "VBOX_WITH_WEBSERVICES" ""
      fi
    else
      log_failure "wsdl2h not found -- disabling webservice"
      cnf_append "VBOX_WITH_WEBSERVICES" ""
    fi
  else
    log_failure "soapcpp2 not found -- disabling webservice"
    cnf_append "VBOX_WITH_WEBSERVICES" ""
  fi
}


#
# Check Xcode path
#
check_xcode_sdk_path()
{
    # Check if WITH_XCODE_DIR is set.
    if [ -z "$WITH_XCODE_DIR" ]; then
        echo "Please specify --with-xcode-dir option."
        return 0
    fi

    # Check if specified path exists and is a directory.
    if [ -d "$1" ]; then
        return 1
    else
        echo "Xcode path [$1] not found."
        return 0
    fi
}

#
# Determines the Darwin version.
# @todo This should really check the Xcode/SDK version.
#
check_darwinversion()
{
  test_header "Darwin version"
  darwin_ver=`uname -r`
  case "$darwin_ver" in
    17\.*)
      check_xcode_sdk_path "$WITH_XCODE_DIR"
      [ $? -eq 1 ] || fail
      darwin_ver="10.13" # High Sierra
      sdk=$WITH_XCODE_DIR/Developer/SDKs/MacOSX10.6.sdk
      cnf_append "VBOX_WITH_MACOSX_COMPILERS_FROM_DEVEL" "1"
      cnf_append "VBOX_PATH_MACOSX_DEVEL_ROOT" "$WITH_XCODE_DIR/Developer"
      ;;
    16\.*)
      check_xcode_sdk_path "$WITH_XCODE_DIR"
      [ $? -eq 1 ] || fail
      darwin_ver="10.12" # Sierra
      sdk=$WITH_XCODE_DIR/Developer/SDKs/MacOSX10.6.sdk
      cnf_append "VBOX_WITH_MACOSX_COMPILERS_FROM_DEVEL" "1"
      cnf_append "VBOX_PATH_MACOSX_DEVEL_ROOT" "$WITH_XCODE_DIR/Developer"
      ;;
    15\.*)
      check_xcode_sdk_path "$WITH_XCODE_DIR"
      [ $? -eq 1 ] || fail
      darwin_ver="10.11" # El Capitan
      sdk=$WITH_XCODE_DIR/Developer/SDKs/MacOSX10.6.sdk
      cnf_append "VBOX_WITH_MACOSX_COMPILERS_FROM_DEVEL" "1"
      cnf_append "VBOX_PATH_MACOSX_DEVEL_ROOT" "$WITH_XCODE_DIR/Developer"
      ;;
    14\.*)
      check_xcode_sdk_path "$WITH_XCODE_DIR"
      [ $? -eq 1 ] || fail
      darwin_ver="10.10" # Yosemite
      sdk=$WITH_XCODE_DIR/Developer/SDKs/MacOSX10.6.sdk
      cnf_append "VBOX_WITH_MACOSX_COMPILERS_FROM_DEVEL" "1"
      cnf_append "VBOX_PATH_MACOSX_DEVEL_ROOT" "$WITH_XCODE_DIR/Developer"
      ;;
    13\.*)
      check_xcode_sdk_path "$WITH_XCODE_DIR"
      [ $? -eq 1 ] || fail
      darwin_ver="10.9" # Mavericks
      sdk=$WITH_XCODE_DIR/Developer/SDKs/MacOSX10.6.sdk
      cnf_append "VBOX_WITH_MACOSX_COMPILERS_FROM_DEVEL" "1"
      cnf_append "VBOX_PATH_MACOSX_DEVEL_ROOT" "$WITH_XCODE_DIR/Developer"
      ;;
    12\.*)
      darwin_ver="10.8" # Mountain Lion
      if [ ! -z "$WITH_XCODE_DIR" ]; then
        sdk=$WITH_XCODE_DIR/Developer/SDKs/MacOSX10.6.sdk
        cnf_append "VBOX_WITH_MACOSX_COMPILERS_FROM_DEVEL" "1"
        cnf_append "VBOX_PATH_MACOSX_DEVEL_ROOT" "$WITH_XCODE_DIR/Developer"
      else
        sdk=/Developer/SDKs/MacOSX10.6.sdk
      fi
      CXX_FLAGS="-mmacosx-version-min=10.6 -isysroot $sdk -Wl,-syslibroot,$sdk"
      ;;
    11\.*)
      darwin_ver="10.7" # Lion
      sdk=/Developer/SDKs/MacOSX10.6.sdk
      CXX_FLAGS="-mmacosx-version-min=10.6 -isysroot $sdk -Wl,-syslibroot,$sdk"
      ;;
    10\.*)
      darwin_ver="10.6" # Snow Leopard
      if [ "$BUILD_MACHINE" = "x86" ]; then
          sdk=/Developer/SDKs/MacOSX10.5.sdk
          CXX_FLAGS="-mmacosx-version-min=10.5 -isysroot $sdk -Wl,-syslibroot,$sdk"
          cnf_append "VBOX_MACOS_10_5_WORKAROUND" "1"
      else
          sdk=/Developer/SDKs/MacOSX10.6.sdk
          CXX_FLAGS="-mmacosx-version-min=10.6 -isysroot $sdk -Wl,-syslibroot,$sdk"
      fi
#      test "$CC" = "gcc" && CC="gcc-4.0"
#      test "$CXX" = "g++" && CXX="g++-4.0"
      cnf_append "VBOX_WITHOUT_VBOXPYTHON_FOR_OSX_10_7" "1"
      ;;
    9\.*)
      darwin_ver="10.5" # Leopard
      sdk=/Developer/SDKs/MacOSX10.5.sdk
      CXX_FLAGS="-mmacosx-version-min=10.5 -isysroot $sdk -Wl,-syslibroot,$sdk"
#      test "$CC" = "gcc" && CC="gcc-4.0"
#      test "$CXX" = "g++" && CXX="g++-4.0"
      cnf_append "VBOX_WITHOUT_VBOXPYTHON_FOR_OSX_10_6" "1"
      cnf_append "VBOX_WITHOUT_VBOXPYTHON_FOR_OSX_10_7" "1"
      ;;
    8\.*)
      darwin_ver="10.4" # Tiger
      sdk=/Developer/SDKs/MacOSX10.4u.sdk
      CXX_FLAGS="-mmacosx-version-min=10.4 -isysroot $sdk -Wl,-syslibroot,$sdk"
#      test "$CC" = "gcc" && CC="gcc-4.0"
#      test "$CXX" = "g++" && CXX="g++-4.0"
      cnf_append "VBOX_WITH_COCOA_QT" ""
      cnf_append "VBOX_WITHOUT_VBOXPYTHON_FOR_OSX_10_6" "1"
      cnf_append "VBOX_WITHOUT_VBOXPYTHON_FOR_OSX_10_7" "1"
      ;;
    *)
      echo "  failed to determine Darwin version. (uname -r: $darwin_ver)"
      fail
      darwin_ver="unknown"
      ;;
  esac
  log_success "found version $darwin_ver (SDK: $sdk)"
}


check_makeself()
{
  test_header "makeself"
  if check_avail "$MAKESELF" makeself; then
    makeself_ver=`$MAKESELF --version|grep version|sed 's+^Makeself.*version \([0-9\.]*\).*+\1+'`
    if [ $? -ne 0 ]; then
      log_failure "makeself not working"
      fail
    else
      log_success "found version $makeself_ver"
      cnf_append "VBOX_MAKESELF" "`which_wrapper $MAKESELF`"
    fi
  fi
}


#
# Show help
#
show_help()
{
cat << EOF
Usage: ./configure [OPTIONS]...

Configuration:
  -h, --help               display this help and exit
  --nofatal                don't abort on errors
EOF
[ $WITH_XPCOM   -eq 1 ] && echo "  --disable-xpcom          disable XPCOM and related stuff"
[ $WITH_PYTHON  -eq 1 ] && echo "  --disable-python         disable python bindings"
[ $WITH_JAVA    -eq 1 ] && echo "  --disable-java           disable java bindings"
[ $WITH_VMMRAW  -eq 1 ] && echo "  --disable-vmmraw         disable VMM raw mode (VT-x/AMD-V mandatory!)"
[ $WITH_SDL_TTF -eq 1 ] && echo "  --disable-sdl-ttf        disable SDL_ttf detection"
[ $WITH_QT5     -eq 1 ] && echo "  --disable-qt             disable Qt detection"
[ $WITH_ALSA    -eq 1 ] && echo "  --disable-alsa           disable the ALSA sound backend"
[ $WITH_PULSE   -eq 1 ] && echo "  --disable-pulse          disable the PulseAudio backend"
[ $WITH_DBUS    -eq 1 ] && echo "  --disable-dbus           don't use DBus and hal for hardware detection"
[ $WITH_KMODS   -eq 1 ] && echo "  --disable-kmods          don't build Linux kernel modules (host and guest)"
[ $WITH_OPENGL  -eq 1 ] && echo "  --disable-opengl         disable OpenGL support (2D & 3D)"
[ $WITH_QT5     -eq 0 ] && echo "  --enable-qt5             enable Qt5 detection"
[ $WITH_GSOAP   -eq 0 ] && echo "  --enable-webservice      enable the webservice stuff"
[ $OSE          -eq 1 ] && echo "  --enable-vnc             enable the VNC server"
[ $OSE          -eq 0 ] && echo "  --disable-extpack        don't build the extpack"
[ $WITH_DOCS    -eq 1 ] && echo "  --disable-docs           don't build the documentation"
[ $WITH_LIBVPX  -eq 1 ] && echo "  --disable-libvpx         don't use libvpx for video capturing"
[ $WITH_LIBOPUS -eq 1 ] && echo "  --disable-libopus        don't use libopus for audio capturing"
[ "$OS" = "linux" -o "$OS" = "freebsd" ] && echo "  --enable-vde             enable VDE networking"
cat << EOF
  --disable-udptunnel      disable UDP tunnel networking
  --disable-devmapper      disable device mapper library access
  --disable-hardening      don't be strict about /dev/vboxdrv access
  --build-libxml2          build libxml2 from sources
EOF
[ $OSE -eq 0 ] && cat << EOF
  --build-libssl           build openssl from sources
  --build-libcurl          build libcurl from sources
  --build-libvpx           build libvpx from sources
EOF
[ "$OS"   != "darwin" ] && echo "  --setup-wine             setup a Wine directory and register the hhc hack"
cat << EOF
  --only-additions         only build the Guest Additions

Paths:
  --with-gcc=PATH          location of the gcc compiler [$CC]
  --with-g++=PATH          location of the g++ compiler [$CXX]
  --with-kbuild=DIR        kbuild directory [$KBUILDDIR]
  --with-iasl=PATH         location of the iasl compiler [$IASL]
  --with-makeself=PATH     location of makeself [$MAKESELF]
EOF
[ "$OS"   = "darwin" ] && echo "  --with-xcode-dir=DIR     custom path to Xcode root directory; it is assumed that Xcode"
[ "$OS"   = "darwin" ] && echo "                           contains OS X 10.6 SDK (required for Mountain Lion and newer hosts"
[ "$OS"   = "darwin" ] && echo "                           only, ignored for the rest)"
[ "$OS"    = "linux" ] && echo "  --with-linux=DIR         Linux kernel source directory [$LINUX]"
[ $WITH_QT5    -eq 1 ] && echo "  --with-qt-dir=DIR        directory for Qt headers/libraries [pkgconfig]"
[ $WITH_GSOAP  -eq 1 ] && echo "  --with-gsoap-dir=PATH    directory for gSOAP compiler/headers/libraries"
[ $WITH_GSOAP  -eq 1 ] && echo "                           (soapcpp2 and wsdl2h, soapstd2.h, libgsoap++.a/so)"
[ $WITH_GSOAP  -eq 1 ] && echo "  --with-gsoap-import=PATH directory for gSOAP import files (stlvector.h)"
cat << EOF
  --with-openssl-dir=DIR   directory for OpenSSL headers/libraries
  --with-ow-dir=DIR        directory where Open Watcom can be found [$WATCOM]
  --out-path=PATH          the folder to which configuration and build output
                           should go

Build type:
  -d, --build-debug        build with debugging symbols and assertions
  --build-profile          build with profiling support
  --build-headless         build headless (without any GUI frontend)
EOF
  exit 0
}


#
# The body.
#

# test if we are OSE
if [ $OSE -eq 1 -a -r "`cd \`dirname $0\`; pwd`/src/VBox/RDP/server/server.cpp" ]; then
  OSE=0
  # Set this as a reminder to print a log message once we know the path of the
  # log file
  NOT_OSE=1
fi

# Change OS specific defaults; must be before all other stuff
if [ "$OS" = "darwin" ]; then
  WITH_SDL=0
  WITH_SDL_TTF=0
  WITH_X11=0
  WITH_ALSA=0
  WITH_PULSE=0
  WITH_DBUS=0
  WITH_KMODS=0
  BUILD_LIBXML2=1
  BUILD_LIBVPX=1
  [ $OSE -eq 1 ] || BUILD_LIBCURL=1
  [ $OSE -eq 1 ] && WITH_LIBVPX=0
  [ $OSE -eq 1 ] && WITH_LIBOPUS=0
  WITH_XCODE_DIR=""
elif [ "$OS" = "haiku" ]; then
  #WITH_SDL=0
  WITH_SDL_TTF=0
  WITH_X11=0
  WITH_ALSA=0
  WITH_PULSE=0
  WITH_DBUS=0
  WITH_KMODS=0
  WITH_LIBIDL=0
  WITH_XPCOM=0
  BUILD_LIBXSLT=1
  BUILD_LIBXML2=1
  WITH_LIBVPX=0
  WITH_LIBOPUS=0
  # it is part of libroot, which is linked by default,
  # but the script wants something
  LIBPTHREAD="-lroot"
  #[ $OSE -eq 1 ] || BUILD_LIBCURL=1
  [ $OSE -eq 1 ] || BUILD_LIBSSL=1
elif [ "$OS" = "solaris" ]; then
  [ $OSE -eq 1 ] && WITH_LIBVPX=0
  [ $OSE -eq 1 ] && WITH_LIBOPUS=0
fi

# scan command line options
for option in "$@"; do
  case "$option" in
    --help|-help|-h)
      show_help
      ;;
    --nofatal)
      nofatal=1
      ;;
    --env-only)
      ENV_ONLY=1
      ;;
    --with-gcc=*)
      CC=`echo $option | cut -d'=' -f2`
      ;;
    --with-g++=*)
      CXX=`echo $option | cut -d'=' -f2`
      ;;
    --with-kbuild=*)
      KBUILDDIR=`echo $option | cut -d'=' -f2`
      if echo $KBUILDDIR|grep -q "$INVALID_CHARS"; then
        echo "Error: KBUILDDIR contains invalid characters!"
        exit 1
      fi
      ;;
    --with-qt-dir=*)
      [ $WITH_QT5 -eq 1 ] && QT5DIR=`echo $option | cut -d'=' -f2`
      [ $WITH_QT5 -eq 1 ] && QT5DIR_PKGCONFIG=0
      ;;
    --with-qt5-minor=*)
      QT5MIN=`echo $option | cut -d'=' -f2`
      ;;
    --with-qt5-major=*)
      QT5MAJ=`echo $option | cut -d'=' -f2`
      ;;
    --with-openssl-dir=*)
      OPENSSLDIR=`echo $option | cut -d'=' -f2`
      INCCRYPTO="-I${OPENSSLDIR}/include"
      LIBCRYPTO="${OPENSSLDIR}/lib/libcrypto.a ${OPENSSLDIR}/lib/libssl.a"
      ;;
    --with-ow-dir=*)
      WATCOM=`echo $option | cut -d'=' -f2`
      ;;
    --with-gsoap-dir=*)
      GSOAP=`echo $option | cut -d'=' -f2`
      ;;
    --with-gsoap-import=*)
      GSOAP_IMPORT=`echo $option | cut -d'=' -f2`
      ;;
    --with-iasl=*)
      IASL=`echo $option | cut -d'=' -f2`
      ;;
    --with-xcode-dir=*)
      WITH_XCODE_DIR=`echo $option | cut -d'=' -f2`
      echo $option
      ;;
    --with-linux=*)
      LINUX=`echo $option | cut -d'=' -f2`
      ;;
    --with-makeself=*)
      MAKESELF=`echo $option | cut -d'=' -f2`
      ;;
    --target-arch=*)
      TARGET_MACHINE=`echo $option | cut -d'=' -f2`
      ;;
    --disable-xpcom)
      [ $WITH_XPCOM -eq 1 ] && WITH_XPCOM=0
      ;;
    --disable-python)
      [ $WITH_PYTHON -eq 1 ] && WITH_PYTHON=0
      ;;
    --disable-java)
      [ $WITH_JAVA -eq 1 ] && WITH_JAVA=0
      ;;
    --disable-vmmraw)
      [ $WITH_VMMRAW -eq 1 ] && WITH_VMMRAW=0
      ;;
    --disable-sdl-ttf)
      [ $WITH_SDL_TTF -eq 1 ] && WITH_SDL_TTF=0
      ;;
    --disable-qt)
      [ $WITH_QT5 -eq 1 ] && WITH_QT5=0
      ;;
    --enable-qt5)
      [ $WITH_QT5 -eq 0 ] && WITH_QT5=1
      ;;
    --passive-mesa)
      PASSIVE_MESA=1
      ;;
    --disable-alsa)
      [ $WITH_ALSA -eq 1 ] && WITH_ALSA=0
      ;;
    --disable-pulse)
      [ $WITH_PULSE -eq 1 ] && WITH_PULSE=0
      ;;
    --enable-pulse)
      WITH_PULSE=2
      ;;
    --disable-dbus)
      [ $WITH_DBUS -eq 1 ] && WITH_DBUS=0
      ;;
    --disable-kmods)
      [ $WITH_KMODS -eq 1 ] && WITH_KMODS=0
      ;;
    --disable-opengl)
      [ $WITH_OPENGL -eq 1 ] && WITH_OPENGL=0
      ;;
    --enable-webservice)
      [ $WITH_GSOAP -eq 0 ] && WITH_GSOAP=1
      ;;
    --enable-vnc)
      WITH_VNC=1
      ;;
    --disable-hardening)
      WITH_HARDENING=0
      ;;
    --disable-extpack)
      WITH_EXTPACK=0
      ;;
    --disable-docs)
      WITH_DOCS=0
      ;;
    --enable-hardening)
      WITH_HARDENING=2
      ;;
    --build-libopus)
      BUILD_LIBOPUS=1
      ;;
    --disable-udptunnel)
      WITH_UDPTUNNEL=0
      ;;
    --enable-vde)
      WITH_VDE=1
      ;;
    --disable-devmapper)
      WITH_DEVMAPPER=0
      ;;
    --disable-libvpx)
      WITH_LIBVPX=0
      ;;
    --disable-libopus)
      WITH_LIBOPUS=0
      ;;
    --disable-sdl)
      WITH_SDL=0
      ;;
    --build-debug|-d)
      BUILD_TYPE=debug
      ;;
    --build-profile)
      BUILD_TYPE=profile
      ;;
    --build-libxml2)
      BUILD_LIBXML2=1
      ;;
    --build-libssl)
      BUILD_LIBSSL=1
      ;;
    --build-libcurl)
      BUILD_LIBCURL=1
      ;;
    --build-libvpx)
      BUILD_LIBVPX=1
      ;;
    --build-headless)
      HEADLESS=1
      WITH_SDL=0
      WITH_SDL_TTF=0
      WITH_X11=0
      WITH_OPENGL=0
      WITH_QT5=0
      ;;
    --ose)
      OSE=2
      ;;
    --odir=*)
      ODIR="`echo $option | cut -d'=' -f2`/"
      ODIR_OVERRIDE=1
      ;;
    --out-path=*)
      out_path="`echo $option | cut -d'=' -f2`/"
      if [ -d $out_path ]; then
        saved_path="`pwd`"
        cd $out_path
        OUT_PATH="`pwd`"
        cd $saved_path
        OUT_PATH_OVERRIDE=1
        if [ $ODIR_OVERRIDE -eq 0 ]; then
          # This variable has not *yet* been overridden.  That can still happen.
          ODIR=$OUT_PATH/
        fi
      else
        echo "Error: invalid folder \"$out_path\" in option \"$option\""
        exit 1
      fi
      ;;
    --setup-wine)
      [ "$OS" != "darwin" ] && SETUP_WINE=1
      ;;
    --only-additions)
      ONLY_ADDITIONS=1
      ;;
    *)
      echo
      echo "Unrecognized option \"$option\""
      echo
      show_help
      ;;
  esac
done

LOG="$ODIR$LOG"
ENV="$ODIR$ENV"
CNF="$ODIR$CNF"

# initialize output files
cat > $LOG << EOF
# Log file generated by
#
#   '$0 $*'
#

EOF
cat > $CNF << EOF
# -*- Makefile -*-
#
# automatically generated by
#
#   '$0 $*'
#
# It will be completely overwritten if configure is executed again.
#

EOF
cat > $ENV << EOF
#!/bin/bash
#
# automatically generated by
#
#   '$0 $*'
#
# It will be completely overwritten if configure is executed again.
# Make sure you source this file once before you start to build VBox.
#

EOF

# Print log warning about OSE if necessary
if [ -n "$NOT_OSE" ]; then
  echo "Found RDP server, assuming VBOX_OSE = FALSE" >> $LOG
  echo >> $LOG
fi


if [ "$BUILD_TYPE" = "debug" ]; then
  echo "Creating DEBUG build!" >> $LOG
elif [ "$BUILD_TYPE" = "profile" ]; then
  echo "Creating PROFILE build!" >> $LOG
fi

# first determine our environment
check_environment
check_kbuild

[ -n "$ENV_ONLY" ] && exit 0

# append the tools directory to the default search path
echo "$PATH" | grep -q "$DEVDIR_BIN" || PATH="$PATH:$DEVDIR_BIN"
export PATH

# if we will be writing to a different out directory then set this up now
if [ $OUT_PATH_OVERRIDE -eq 1 ]; then
  echo "AUTOCFG=$OUT_PATH/AutoConfig.kmk" >> $ENV
  echo "export AUTOCFG" >> $ENV
  echo "LOCALCFG=$OUT_PATH/LocalConfig.kmk" >> $ENV
  echo "export LOCALCFG" >> $ENV
  echo "PATH_OUT_BASE=$OUT_PATH" >> $ENV
  echo "export PATH_OUT_BASE" >> $ENV
fi

# don't bother people with -Werror
cnf_append "VBOX_GCC_WERR" "\$(NO_SUCH_VARIABLE)"

# some things are not available in for OSE
if [ $OSE -ge 1 ]; then
  cnf_append "VBOX_OSE" "1"
  cnf_append "VBOX_WITH_VALIDATIONKIT" ""
  cnf_append "VBOX_WITH_WIN32_ADDITIONS" ""

  if [ "$OS" = "linux" ]; then
    cnf_append "VBOX_WITH_LINUX_ADDITIONS" "1"
  else
    cnf_append "VBOX_WITH_LINUX_ADDITIONS" ""
  fi
  echo >> $CNF
fi

# extpack
if [ $ONLY_ADDITIONS -eq 1 ]; then
  cnf_append "VBOX_WITH_EXTPACK_PUEL_BUILD" ""
elif [ $OSE -eq 0 ]; then
  if [ $WITH_EXTPACK -eq 1 ]; then
    BUILD_LIBSSL=1
  else
    cnf_append "VBOX_WITH_EXTPACK_PUEL_BUILD" ""
  fi
fi

# headless
if [ -n "$HEADLESS" ]; then
  cnf_append "VBOX_HEADLESS" "1"
fi

# emit disable directives corresponding to any --disable-xxx options.
if [ $WITH_OPENGL -eq 0 ]; then
  cnf_append "VBOX_WITH_CROGL" ""
  cnf_append "VBOX_WITH_VIDEOHWACCEL" ""
  cnf_append "VBOX_GUI_USE_QGL" ""
fi
[ $WITH_XPCOM     -eq 0 ] && cnf_append "VBOX_WITH_MAIN" ""
[ $WITH_QT5       -eq 0 ] && cnf_append "VBOX_WITH_QTGUI" ""
[ $WITH_SDL_TTF   -eq 0 ] && cnf_append "VBOX_WITH_SECURELABEL" ""
[ $WITH_PYTHON    -eq 0 ] && cnf_append "VBOX_WITH_PYTHON" ""
[ $WITH_JAVA      -eq 0 ] && cnf_append "VBOX_WITH_JXPCOM" ""
[ $WITH_JAVA      -eq 0 ] && cnf_append "VBOX_WITH_JWS" ""
[ $WITH_HARDENING -eq 0 ] && cnf_append "VBOX_WITHOUT_HARDENING" "1"
[ $WITH_HARDENING -eq 2 ] && cnf_append "VBOX_WITH_HARDENING" "2"
[ $WITH_VMMRAW    -eq 0 ] && cnf_append "VBOX_WITH_RAW_MODE" ""
if [ $WITH_LIBVPX -eq 0 ]; then
  cnf_append "VBOX_WITH_LIBVPX" ""
  cnf_append "VBOX_WITH_RECORDING" ""
fi
if [ $WITH_LIBOPUS -eq 0 ]; then
  cnf_append "VBOX_WITH_LIBOPUS" ""
  cnf_append "VBOX_WITH_AUDIO_RECORDING" ""
fi

# Darwin-specific
if [ "$OS" = "darwin" ]; then
  check_darwinversion
fi
# the tools
check_gcc
if [ $ONLY_ADDITIONS -eq 0 ]; then
  check_open_watcom
  [ "$OS" != "darwin"  ] && check_iasl
  # don't check for yasm for the time beeing as 0.40 and 0.50 both have known bugs
  # [ "$OS" != "darwin" ] && check_yasm
  [ "$OS" != "darwin"  ] && check_xsltproc
fi

# the libraries
if [ $ONLY_ADDITIONS -eq 0 ]; then
  [ "$OS" != "darwin"  ] && check_pthread
  check_libxml2
  [ $WITH_LIBIDL -eq 1 ] && check_libidl
  check_ssl
  check_curl
  [ $WITH_LIBVPX -eq 1 ] && check_vpx
  [ $WITH_LIBOPUS -eq 1 ] && check_libopus
  [ "$OS" != "darwin"  ] && check_z
  [ "$OS" != "darwin"  ] && check_png
  [ $OSE -eq 0 -a "$OS" = "linux" ] && check_pam
  if [ $WITH_SDL -eq 1 ]; then
    check_sdl
  else
    cnf_append "VBOX_WITH_VBOXSDL" ""
  fi
  [ $WITH_SDL_TTF -eq 1 -a $OSE -eq 0 ] && check_sdl_ttf
  [ $WITH_X11    -eq 1 ] && check_x
  # TODO check for xcomposite-dev (X11/extensions/Xcomposite.h, additions only)
  # TODO check for libxdamange-dev (X11/extensions/Xdamage.h, additions only)
  [ $WITH_X11    -eq 1 ] && check_xcursor
  [ $WITH_X11    -eq 1 ] && check_xinerama
  [ $WITH_X11    -eq 1 ] && check_xrandr
  [ $WITH_OPENGL -eq 1 ] && check_opengl
  [ $WITH_QT5    -eq 1 ] && check_qt5
  [ $WITH_PYTHON -eq 1 ] && check_python
  [ $WITH_JAVA   -eq 1 ] && check_java

  # PulseAudio
  if [ "$OS" = "linux" -o "$OS" = "freebsd" -o "$OS" = "netbsd" ]; then
    if [ $WITH_PULSE -eq 1 ]; then
      check_pulse
    elif [ $WITH_PULSE -eq 0 ]; then
      cnf_append "VBOX_WITH_AUDIO_PULSE" ""
    fi
 fi
fi

# Linux-specific
if [ "$OS" = "linux" ]; then
  # don't check for the static libstdc++ in the PUEL version as we build the
  # additions at a dedicated box
  [ $OSE -ge 1 ] && check_staticlibstdcxx
  if [ $WITH_KMODS -eq 1 ]; then
    check_linux
  else
    cnf_append "VBOX_LINUX_SRC" ""
    cnf_append "VBOX_WITHOUT_LINUX_TEST_BUILDS" "1"
  fi
  if [ $ONLY_ADDITIONS -eq 0 ]; then
    if [ $WITH_ALSA -eq 1 ]; then
      check_alsa
    else
      cnf_append "VBOX_WITH_AUDIO_ALSA" ""
    fi
    if [ $WITH_DBUS -eq 0 ]; then
      cnf_append "VBOX_WITH_DBUS" ""
    fi
    if [ $WITH_DEVMAPPER -eq 1 ]; then
      check_libdevmapper
    else
      cnf_append "VBOX_WITH_DEVMAPPER" ""
    fi
    check_libcap
  fi
  check_compiler_h
  [ $ONLY_ADDITIONS -eq 0 -a "$BUILD_MACHINE" = "amd64" -a $WITH_VMMRAW -eq 1 ] && check_32bit
  # tools/common/makeself*
  [ $OSE -ge 1 ] && check_makeself
fi

[ -n "$SETUP_WINE" ] && setup_wine

if [ $ONLY_ADDITIONS -eq 0 -a $WITH_GSOAP -eq 1 ]; then
  check_gsoap
else
  if [ $OSE -ge 1 ]; then
    cnf_append "VBOX_WITH_WEBSERVICES" ""
  fi
fi

# UDPTUNNEL
if [ $ONLY_ADDITIONS -eq 0 -a $WITH_UDPTUNNEL -eq 0 ]; then
  cnf_append "VBOX_WITH_UDPTUNNEL" ""
fi

# VDE
if [ $ONLY_ADDITIONS -eq 0 -a "$OS" = "linux" -o "$OS" = "freebsd" ]; then
  if [ $WITH_VDE -eq 1 ]; then
    cnf_append "VBOX_WITH_VDE" "1"
  fi
fi

# DOCS
if [ $ONLY_ADDITIONS -eq 1 -o $WITH_DOCS -eq 0 ]; then
  cnf_append "VBOX_WITH_DOCS" ""
  cnf_append "VBOX_WITH_DOCS_PACKING" ""
fi

# VNC server support
if [ $ONLY_ADDITIONS -eq 0 -a $OSE -ge 1 ]; then
  if [ $WITH_VNC = 1 ]; then
    check_vncserver
  else
    cnf_append "VBOX_WITH_EXTPACK_VNC" ""
  fi
fi

if [ $ONLY_ADDITIONS -eq 1 ]; then
  cnf_append "VBOX_ONLY_ADDITIONS" "1"
fi

# success!
echo
echo "Successfully generated '$CNF' and '$ENV'."
echo "Source '$ENV' once before you start to build VBox:"
echo ""
echo "  source $ENV"
echo "  kmk"
echo ""
if [ "$OS" = "linux" ]; then
    if [ $OUT_PATH_OVERRIDE -eq 1 ]; then
      vbox_out_path=$OUT_PATH
    else
      vbox_out_path=./out
    fi
    echo "To compile the kernel modules, do:"
    echo ""
    echo "  cd $vbox_out_path/$OS.$TARGET_MACHINE/$BUILD_TYPE/bin/src"
    echo "  make"
    echo ""
fi
if [ $ONLY_ADDITIONS -eq 1 ]; then
    echo ""
    echo "  Tree configured to build only the Guest Additions"
    echo ""
elif [ $WITH_HARDENING -gt 0 ]; then
    echo ""
    echo "  +++ WARNING +++ WARNING +++ WARNING +++ WARNING +++ WARNING +++ WARNING +++"
    echo "  Hardening is enabled which means that the VBox binaries will not run from"
    echo "  the binary directory. The binaries have to be installed suid root and some"
    echo "  more prerequisites have to be fulfilled which is normally done by installing"
    echo "  the final package. For development, the hardening feature can be disabled"
    echo "  by specifying the --disable-hardening parameter. Please never disable that"
    echo "  feature for the final distribution!"
    echo "  +++ WARNING +++ WARNING +++ WARNING +++ WARNING +++ WARNING +++ WARNING +++"
    echo ""
else
    echo ""
    echo "  +++ WARNING +++ WARNING +++ WARNING +++ WARNING +++ WARNING +++ WARNING +++"
    echo "  Hardening is disabled. Please do NOT build packages for distribution with"
    echo "  disabled hardening!"
    echo "  +++ WARNING +++ WARNING +++ WARNING +++ WARNING +++ WARNING +++ WARNING +++"
    echo ""
fi
echo "Enjoy!"
cleanup