File: SConstruct

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (3859 lines) | stat: -rwxr-xr-x 147,194 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
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
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
#! -*- python -*-
# Copyright (c) 2012 The Native Client Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import atexit
import json
import os
import platform
import re
import subprocess
import sys
import zlib
sys.path.append("./common")
sys.path.append('../third_party')

from SCons.Errors import UserError
from SCons.Script import GetBuildFailures

import SCons.Warnings
import SCons.Util

SCons.Warnings.warningAsException()

sys.path.append("tools")
import command_tester
import test_lib

import pynacl.platform

# turning garbage collection off reduces startup time by 10%
import gc
gc.disable()

# REPORT
CMD_COUNTER = {}
ENV_COUNTER = {}
def PrintFinalReport():
  """This function is run just before scons exits and dumps various reports.
  """
  # Note, these global declarations are not strictly necessary
  global pre_base_env
  global CMD_COUNTER
  global ENV_COUNTER

  if pre_base_env.Bit('target_stats'):
    print
    print '*' * 70
    print 'COMMAND EXECUTION REPORT'
    print '*' * 70
    for k in sorted(CMD_COUNTER.keys()):
      print "%4d %s" % (CMD_COUNTER[k], k)

    print
    print '*' * 70
    print 'ENVIRONMENT USAGE REPORT'
    print '*' * 70
    for k in sorted(ENV_COUNTER.keys()):
      print "%4d  %s" % (ENV_COUNTER[k], k)

  failures = []
  for failure in GetBuildFailures():
    for node in Flatten(failure.node):
      failures.append({
          # If this wasn't a test, "GetTestName" will return raw_name.
          'test_name': GetTestName(node),
          'raw_name': str(node.path),
          'errstr': failure.errstr
      })

  json_path = ARGUMENTS.get('json_build_results_output_file')
  if json_path:
    with open(json_path, 'w') as f:
      json.dump(failures, f, sort_keys=True, indent=2)

  if not failures:
    return

  print
  print '*' * 70
  print 'ERROR REPORT: %d failures' % len(failures)
  print '*' * 70
  print
  for failure in failures:
    test_name = failure['test_name']
    if test_name != failure['raw_name']:
      test_name = '%s (%s)' % (test_name, failure['raw_name'])
    print "%s failed: %s\n" % (test_name, failure['errstr'])


def VerboseConfigInfo(env):
  "Should we print verbose config information useful for bug reports"
  if '--help' in sys.argv: return False
  if env.Bit('prebuilt') or env.Bit('built_elsewhere'): return False
  return env.Bit('sysinfo')


# SANITY CHECKS

# NOTE BitFromArgument(...) implicitly defines additional ACCEPTABLE_ARGUMENTS.
ACCEPTABLE_ARGUMENTS = set([
    # TODO: add comments what these mean
    # TODO: check which ones are obsolete
    ####  ASCII SORTED ####
    # Use a destination directory other than the default "scons-out".
    'DESTINATION_ROOT',
    'MODE',
    'SILENT',
    # Limit bandwidth of browser tester
    'browser_tester_bw',
    # Location to download Chromium binaries to and/or read them from.
    'chrome_binaries_dir',
    # used for chrome_browser_tests: path to the browser
    'chrome_browser_path',
    # A comma-separated list of test names to disable by excluding the
    # tests from a test suite.  For example, 'small_tests
    # disable_tests=run_hello_world_test' will run small_tests without
    # including hello_world_test.  Note that if a test listed here
    # does not exist you will not get an error or a warning.
    'disable_tests',
    # used for chrome_browser_tests: path to a pre-built browser plugin.
    'force_ppapi_plugin',
    # force emulator use by tests
    'force_emulator',
    # force sel_ldr use by tests
    'force_sel_ldr',
    # force nacl_helper_bootstrap used by tests
    'force_bootstrap',
    # force irt image used by tests
    'force_irt',
    # generate_ninja=FILE enables a Ninja backend for SCons.  This writes a
    # .ninja build file to FILE describing all of SCons' build targets.
    'generate_ninja',
    # Path to a JSON file for machine-readable output.
    'json_build_results_output_file',
    # Replacement memcheck command for overriding the DEPS-in memcheck
    # script.  May have commas to separate separate shell args.  There
    # is no quoting, so this implies that this mechanism will fail if
    # the args actually need to have commas.  See
    # http://code.google.com/p/nativeclient/issues/detail?id=3158 for
    # the discussion of why this argument is needed.
    'memcheck_command',
    # If the replacement memcheck command only works for trusted code,
    # set memcheck_trusted_only to non-zero.
    'memcheck_trusted_only',
    # When building with MSan, this can be set to values 0 (fastest, least
    # useful reports) through 2 (slowest, most useful reports). Default is 1.
    'msan_track_origins',
    # colon-separated list of linker flags, e.g. "-lfoo:-Wl,-u,bar".
    'nacl_linkflags',
    # prefix to add in-front of perf tracking trace labels.
    'perf_prefix',
    # colon-separated list of pnacl bcld flags, e.g. "-lfoo:-Wl,-u,bar".
    # Not using nacl_linkflags since that gets clobbered in some tests.
    'pnacl_bcldflags',
    'platform',
    # Run tests under this tool (e.g. valgrind, tsan, strace, etc).
    # If the tool has options, pass them after comma: 'tool,--opt1,--opt2'.
    # NB: no way to use tools the names or the args of
    # which contains a comma.
    'run_under',
    # More args for the tool.
    'run_under_extra_args',
    # Multiply timeout values by this number.
    'scale_timeout',
    # test_wrapper specifies a wrapper program such as
    # tools/run_test_via_ssh.py, which runs tests on a remote host
    # using rsync and SSH.  Example usage:
    #   ./scons run_hello_world_test platform=arm force_emulator= \
    #     test_wrapper="./tools/run_test_via_ssh.py --host=armbox --subdir=tmp"
    'test_wrapper',
    # Replacement tsan command for overriding the DEPS-in tsan
    # script.  May have commas to separate separate shell args.  There
    # is no quoting, so this implies that this mechanism will fail if
    # the args actually need to have commas.  See
    # http://code.google.com/p/nativeclient/issues/detail?id=3158 for
    # the discussion of why this argument is needed.
    'tsan_command',
    # Run browser tests under this tool. See
    # tools/browser_tester/browsertester/browserlauncher.py for tool names.
    'browser_test_tool',
    # Where to install header files for public consumption.
    'includedir',
    # Where to install libraries for public consumption.
    'libdir',
    # Where to install trusted-code binaries for public (SDK) consumption.
    'bindir',
    # Where a Breakpad build output directory is for optional Breakpad testing.
    'breakpad_tools_dir',
    # Allows overriding of the nacl newlib toolchain directory.
    'nacl_newlib_dir',
    # Allows override of the nacl glibc toolchain directory.
    'nacl_glibc_dir',
    # Allows override of the pnacl newlib toolchain directory.
    'pnacl_newlib_dir',
    # Allows overriding the version number in the toolchain's
    # FEATURE_VERSION file.  This is used for PNaCl ABI compatibility
    # testing.
    'toolchain_feature_version',
  ])


# Overly general to provide compatibility with existing build bots, etc.
# In the future it might be worth restricting the values that are accepted.
_TRUE_STRINGS = set(['1', 'true', 'yes'])
_FALSE_STRINGS = set(['0', 'false', 'no'])


# Converts a string representing a Boolean value, of some sort, into an actual
# Boolean value. Python's built in type coercion does not work because
# bool('False') == True
def StringValueToBoolean(value):
  # ExpandArguments may stick non-string values in ARGUMENTS. Be accommodating.
  if isinstance(value, bool):
    return value

  if not isinstance(value, basestring):
    raise Exception("Expecting a string but got a %s" % repr(type(value)))

  if value.lower() in _TRUE_STRINGS:
    return True
  elif value.lower() in _FALSE_STRINGS:
    return False
  else:
    raise Exception("Cannot convert '%s' to a Boolean value" % value)


def GetBinaryArgumentValue(arg_name, default):
  if not isinstance(default, bool):
    raise Exception("Default value for '%s' must be a Boolean" % arg_name)
  if arg_name not in ARGUMENTS:
    return default
  return StringValueToBoolean(ARGUMENTS[arg_name])


# name is the name of the bit
# arg_name is the name of the command-line argument, if it differs from the bit
def BitFromArgument(env, name, default, desc, arg_name=None):
  # In most cases the bit name matches the argument name
  if arg_name is None:
    arg_name = name

  DeclareBit(name, desc)
  assert arg_name not in ACCEPTABLE_ARGUMENTS, repr(arg_name)
  ACCEPTABLE_ARGUMENTS.add(arg_name)

  if GetBinaryArgumentValue(arg_name, default):
    env.SetBits(name)
  else:
    env.ClearBits(name)


# SetUpArgumentBits declares binary command-line arguments and converts them to
# bits. For example, one of the existing declarations would result in the
# argument "bitcode=1" causing env.Bit('bitcode') to evaluate to true.
# NOTE Command-line arguments are a SCons-ism that is separate from
# command-line options.  Options are prefixed by "-" or "--" whereas arguments
# are not.  The function SetBitFromOption can be used for options.
# NOTE This function must be called before the bits are used
# NOTE This function must be called after all modifications of ARGUMENTS have
# been performed. See: ExpandArguments
def SetUpArgumentBits(env):
  BitFromArgument(env, 'bitcode', default=False,
    desc='We are building bitcode')

  BitFromArgument(env, 'pnacl_native_clang_driver', default=False,
    desc='Use the (experimental) native PNaCl Clang driver')

  BitFromArgument(env, 'nacl_clang', default=(not env.Bit('bitcode') and
                                              not env.Bit('nacl_glibc')),
    desc='Use the native nacl-clang newlib compiler instead of nacl-gcc')

  BitFromArgument(env, 'translate_fast', default=False,
    desc='When using pnacl TC (bitcode=1) use accelerated translation step')

  BitFromArgument(env, 'use_sz', default=False,
    desc='When using pnacl TC (bitcode=1) use Subzero for fast translation')

  BitFromArgument(env, 'built_elsewhere', default=False,
    desc='The programs have already been built by another system')

  BitFromArgument(env, 'skip_trusted_tests', default=False,
    desc='Only run untrusted tests - useful for translator testing'
      ' (also skips tests of the IRT itself')

  BitFromArgument(env, 'nacl_pic', default=False,
    desc='generate position indepent code for (P)NaCl modules')

  BitFromArgument(env, 'nacl_static_link', default=not env.Bit('nacl_glibc'),
    desc='Whether to use static linking instead of dynamic linking '
      'for building NaCl executables during tests. '
      'For nacl-newlib, the default is 1 (static linking). '
      'For nacl-glibc, the default is 0 (dynamic linking).')

  BitFromArgument(env, 'nacl_disable_shared', default=not env.Bit('nacl_glibc'),
    desc='Do not build shared versions of libraries. '
      'For nacl-newlib, the default is 1 (static libraries only). '
      'For nacl-glibc, the default is 0 (both static and shared libraries).')

  # Defaults on when --verbose is specified.
  # --verbose sets 'brief_comstr' to False, so this looks a little strange
  BitFromArgument(env, 'target_stats', default=not GetOption('brief_comstr'),
    desc='Collect and display information about which commands are executed '
      'during the build process')

  BitFromArgument(env, 'werror', default=True,
    desc='Treat warnings as errors (-Werror)')

  BitFromArgument(env, 'disable_nosys_linker_warnings', default=False,
    desc='Disable warning mechanism in src/untrusted/nosys/warning.h')

  BitFromArgument(env, 'naclsdk_validate', default=True,
    desc='Verify the presence of the SDK')

  # TODO(mseaborn): Remove this, since this is always False -- Valgrind is
  # no longer supported.  This will require removing some Chromium-side
  # references.
  BitFromArgument(env, 'running_on_valgrind', default=False,
    desc='Compile and test using valgrind')

  BitFromArgument(env, 'pp', default=False,
    desc='Enable pretty printing')

  # Defaults on when --verbose is specified
  # --verbose sets 'brief_comstr' to False, so this looks a little strange
  BitFromArgument(env, 'sysinfo', default=not GetOption('brief_comstr'),
    desc='Print verbose system information')

  BitFromArgument(env, 'disable_flaky_tests', default=False,
    desc='Do not run potentially flaky tests - used on Chrome bots')

  BitFromArgument(env, 'use_sandboxed_translator', default=False,
    desc='use pnacl sandboxed translator for linking (not available for arm)')

  BitFromArgument(env, 'pnacl_generate_pexe', default=env.Bit('bitcode'),
    desc='use pnacl to generate pexes and translate in a separate step')

  BitFromArgument(env, 'translate_in_build_step', default=True,
    desc='Run translation during build phase (e.g. if do_not_run_tests=1)')

  BitFromArgument(env, 'pnacl_unsandboxed', default=False,
    desc='Translate pexe to an unsandboxed, host executable')

  BitFromArgument(env, 'nonsfi_nacl', default=False,
    desc='Use Non-SFI Mode instead of the original SFI Mode.  This uses '
      'nonsfi_loader instead of sel_ldr, and it tells the PNaCl toolchain '
      'to translate pexes to Non-SFI nexes.')

  BitFromArgument(env, 'use_newlib_nonsfi_loader', default=True,
    desc='Test nonsfi_loader linked against NaCl newlib instead of the one '
      'linked against host libc. This flag makes sense only with '
      'nonsfi_nacl=1.')

  BitFromArgument(env, 'browser_headless', default=False,
    desc='Where possible, set up a dummy display to run the browser on '
      'when running browser tests.  On Linux, this runs the browser through '
      'xvfb-run.  This Scons does not need to be run with an X11 display '
      'and we do not open a browser window on the user\'s desktop.  '
      'Unfortunately there is no equivalent on Mac OS X.')

  BitFromArgument(env, 'disable_crash_dialog', default=True,
    desc='Disable Windows\' crash dialog box, which Windows pops up when a '
      'process exits with an unhandled fault.  Windows enables this by '
      'default for processes launched from the command line or from the '
      'GUI.  Our default is to disable it, because the dialog turns crashes '
      'into hangs on Buildbot, and our test suite includes various crash '
      'tests.')

  BitFromArgument(env, 'do_not_run_tests', default=False,
    desc='Prevents tests from running.  This lets SCons build the files needed '
      'to run the specified test(s) without actually running them.  This '
      'argument is a counterpart to built_elsewhere.')

  BitFromArgument(env, 'no_gdb_tests', default=False,
    desc='Prevents GDB tests from running.  If GDB is not available, you can '
      'test everything else by specifying this flag.')

  # TODO(shcherbina): add support for other golden-based tests, not only
  # run_x86_*_validator_testdata_tests.
  BitFromArgument(env, 'regenerate_golden', default=False,
    desc='When running golden-based tests, instead of comparing results '
         'save actual output as golden data.')

  BitFromArgument(env, 'x86_64_zero_based_sandbox', default=False,
    desc='Use the zero-address-based x86-64 sandbox model instead of '
      'the r15-based model.')

  BitFromArgument(env, 'android', default=False,
                  desc='Build for Android target')

  BitFromArgument(env, 'skip_nonstable_bitcode', default=False,
                  desc='Skip tests involving non-stable bitcode')

  #########################################################################
  # EXPERIMENTAL
  # This is for generating a testing library for use within private test
  # enuminsts, where we want to compare and test different validators.
  #
  BitFromArgument(env, 'ncval_testing', default=False,
    desc='EXPERIMENTAL: Compile validator code for testing within enuminsts')

  # PNaCl sanity checks
  if not env.Bit('bitcode'):
    pnacl_only_flags = ('nonsfi_nacl',
                        'pnacl_generate_pexe',
                        'pnacl_unsandboxed',
                        'skip_nonstable_bitcode',
                        'translate_fast',
                        'use_sz',
                        'use_sandboxed_translator')

    for flag_name in pnacl_only_flags:
      if env.Bit(flag_name):
        raise UserError('The option %r only makes sense when using the '
                        'PNaCl toolchain (i.e. with bitcode=1)'
                        % flag_name)
  else:
    pnacl_incompatible_flags = ('nacl_clang',
                                'nacl_glibc')
    for flag_name in pnacl_incompatible_flags:
      if env.Bit(flag_name):
        raise UserError('The option %r cannot be used when building with '
                        'PNaCl (i.e. with bitcode=1)' % flag_name)

def CheckArguments():
  for key in ARGUMENTS:
    if key not in ACCEPTABLE_ARGUMENTS:
      raise UserError('bad argument: %s' % key)


def GetTargetPlatform():
  return pynacl.platform.GetArch3264(ARGUMENTS.get('platform', 'x86-32'))

def GetBuildPlatform():
  return pynacl.platform.GetArch3264()


environment_list = []

# Base environment for both nacl and non-nacl variants.
kwargs = {}
if ARGUMENTS.get('DESTINATION_ROOT') is not None:
  kwargs['DESTINATION_ROOT'] = ARGUMENTS.get('DESTINATION_ROOT')
pre_base_env = Environment(
    # Use the environment that scons was run in to run scons invoked commands.
    # This allows in things like externally provided PATH, PYTHONPATH.
    ENV = os.environ.copy(),
    tools = ['component_setup'],
    # SOURCE_ROOT is one leave above the native_client directory.
    SOURCE_ROOT = Dir('#/..').abspath,
    # Publish dlls as final products (to staging).
    COMPONENT_LIBRARY_PUBLISH = True,

    # Use workaround in special scons version.
    LIBS_STRICT = True,
    LIBS_DO_SUBST = True,

    # Select where to find coverage tools.
    COVERAGE_MCOV = '../third_party/lcov/bin/mcov',
    COVERAGE_GENHTML = '../third_party/lcov/bin/genhtml',
    **kwargs
)


if 'generate_ninja' in ARGUMENTS:
  import pynacl.scons_to_ninja
  pynacl.scons_to_ninja.GenerateNinjaFile(
      pre_base_env, dest_file=ARGUMENTS['generate_ninja'])


breakpad_tools_dir = ARGUMENTS.get('breakpad_tools_dir')
if breakpad_tools_dir is not None:
  pre_base_env['BREAKPAD_TOOLS_DIR'] = pre_base_env.Dir(
      os.path.abspath(breakpad_tools_dir))


# CLANG
DeclareBit('clang', 'Use clang to build trusted code')
pre_base_env.SetBitFromOption('clang', False)

DeclareBit('asan',
           'Use AddressSanitizer to build trusted code (implies --clang)')
pre_base_env.SetBitFromOption('asan', False)
if pre_base_env.Bit('asan'):
  pre_base_env.SetBits('clang')

DeclareBit('msan',
           'Use MemorySanitizer to build trusted code (implies --clang)')
pre_base_env.SetBitFromOption('msan', False)
if pre_base_env.Bit('msan'):
  pre_base_env.SetBits('clang')

# CODE COVERAGE
DeclareBit('coverage_enabled', 'The build should be instrumented to generate'
           'coverage information')

# If the environment variable BUILDBOT_BUILDERNAME is set, we can determine
# if we are running in a VM by the lack of a '-bare-' (aka bare metal) in the
# bot name.  Otherwise if the builder name is not set, then assume real HW.
DeclareBit('running_on_vm', 'Returns true when environment is running in a VM')
builder = os.environ.get('BUILDBOT_BUILDERNAME')
if builder and builder.find('-bare-') == -1:
  pre_base_env.SetBits('running_on_vm')
else:
  pre_base_env.ClearBits('running_on_vm')

DeclareBit('nacl_glibc', 'Use nacl-glibc for building untrusted code')
pre_base_env.SetBitFromOption('nacl_glibc', False)

# This function should be called ASAP after the environment is created, but
# after ExpandArguments.
SetUpArgumentBits(pre_base_env)

# Register PrintFinalReport only after SetUpArgumentBits since it references
# bits that get declared in SetUpArgumentBits
atexit.register(PrintFinalReport)

def DisableCrashDialog():
  if sys.platform == 'win32':
    import win32api
    import win32con
    # The double call is to preserve existing flags, as discussed at
    # http://blogs.msdn.com/oldnewthing/archive/2004/07/27/198410.aspx
    new_flags = win32con.SEM_NOGPFAULTERRORBOX
    existing_flags = win32api.SetErrorMode(new_flags)
    win32api.SetErrorMode(existing_flags | new_flags)

if pre_base_env.Bit('disable_crash_dialog'):
  DisableCrashDialog()

# We want to pull CYGWIN setup in our environment or at least set flag
# nodosfilewarning. It does not do anything when CYGWIN is not involved
# so let's do it in all cases.
pre_base_env['ENV']['CYGWIN'] = os.environ.get('CYGWIN', 'nodosfilewarning')

# Note: QEMU_PREFIX_HOOK may influence test runs and sb translator invocations
pre_base_env['ENV']['QEMU_PREFIX_HOOK'] = os.environ.get('QEMU_PREFIX_HOOK', '')

# Allow the zero-based sandbox model to run insecurely.
# TODO(arbenson): remove this once binutils bug is fixed (see
# src/trusted/service_runtime/arch/x86_64/sel_addrspace_posix_x86_64.c)
if pre_base_env.Bit('x86_64_zero_based_sandbox'):
  pre_base_env['ENV']['NACL_ENABLE_INSECURE_ZERO_BASED_SANDBOX'] = 1

if pre_base_env.Bit('werror'):
  werror_flags = ['-Werror']
else:
  werror_flags = []

# Allow variadic macros
werror_flags = werror_flags + ['-Wno-variadic-macros']

if pre_base_env.Bit('clang'):
  # Allow 'default' label in switch even when all enumeration cases
  # have been covered.
  werror_flags += ['-Wno-covered-switch-default']
  # Allow C++11 extensions (for "override")
  werror_flags += ['-Wno-c++11-extensions']


# Method to make sure -pedantic, etc, are not stripped from the
# default env, since occasionally an engineer will be tempted down the
# dark -- but wide and well-trodden -- path of expediency and stray
# from the path of correctness.

def EnsureRequiredBuildWarnings(env):
  if (env.Bit('linux') or env.Bit('mac')) and not env.Bit('android'):
    required_env_flags = set(['-pedantic', '-Wall'] + werror_flags)
    ccflags = set(env.get('CCFLAGS'))

    if not required_env_flags.issubset(ccflags):
      raise UserError('required build flags missing: '
                      + ' '.join(required_env_flags.difference(ccflags)))
  else:
    # windows get a pass for now
    pass

pre_base_env.AddMethod(EnsureRequiredBuildWarnings)

# Expose MakeTempDir and MakeTempFile to scons scripts
def MakeEmptyFile(env, **kwargs):
  fd, path = test_lib.MakeTempFile(env, **kwargs)
  os.close(fd)
  return path

pre_base_env.AddMethod(test_lib.MakeTempDir)
pre_base_env.AddMethod(MakeEmptyFile)

# Method to add target suffix to name.
def NaClTargetArchSuffix(env, name):
  return name + '_' + env['TARGET_FULLARCH'].replace('-', '_')

pre_base_env.AddMethod(NaClTargetArchSuffix)


# Generic Test Wrapper

# Add list of Flaky or Bad tests to skip per platform.  A
# platform is defined as build type
# <BUILD_TYPE>-<SUBARCH>
bad_build_lists = {
    'arm': [],
}

# This is a list of tests that do not yet pass when using nacl-glibc.
# TODO(mseaborn): Enable more of these tests!
nacl_glibc_skiplist = set([
    # Struct layouts differ.
    'run_abi_test',
    # Syscall wrappers not implemented yet.
    'run_sysbasic_test',
    'run_sysbrk_test',
    # Fails because clock() is not hooked up.
    'run_timefuncs_test',
    # Needs further investigation.
    'sdk_minimal_test',
    # This test fails with nacl-glibc: glibc reports an internal
    # sanity check failure in free().
    # TODO(robertm): This needs further investigation.
    'run_ppapi_event_test',
    'run_ppapi_geturl_valid_test',
    'run_ppapi_geturl_invalid_test',
    # http://code.google.com/p/chromium/issues/detail?id=108131
    # we would need to list all of the glibc components as
    # web accessible resources in the extensions's manifest.json,
    # not just the nexe and nmf file.
    'run_ppapi_extension_mime_handler_browser_test',
    ])
nacl_glibc_skiplist.update(['%s_irt' % test for test in nacl_glibc_skiplist])

# Whitelist of tests to run for Non-SFI Mode.  Note that typos here will
# not be caught automatically!
# TODO(mseaborn): Eventually we should run all of small_tests instead of
# this whitelist.
nonsfi_test_whitelist = set([
    'run_arm_float_abi_test',
    'run_clock_get_test',
    'run_clone_test',
    'run_directory_test',
    'run_dup_test',
    'run_example_irt_caller_test',
    'run_exception_test',
    'run_fcntl_test',
    'run_file_descriptor_test',
    'run_float_test',
    'run_fork_test',
    'run_getpid_test',
    'run_hello_world_test',
    'run_icache_test',
    'run_irt_futex_test',
    'run_malloc_realloc_calloc_free_test',
    'run_mmap_test',
    'run_nanosleep_test',
    'run_nonsfi_syscall_test',
    'run_prctl_test',
    'run_printf_test',
    'run_pwrite_test',
    'run_random_test',
    'run_rlimit_test',
    'run_sigaction_test',
    'run_signal_sigbus_test',
    'run_signal_test',
    'run_socket_test',
    'run_stack_alignment_asm_test',
    'run_stack_alignment_test',
    'run_syscall_test',
    'run_thread_test',
    'run_user_async_signal_test',
    ])


# If a test is not in one of these suites, it will probally not be run on a
# regular basis.  These are the suites that will be run by the try bot or that
# a large number of users may run by hand.
MAJOR_TEST_SUITES = set([
  'small_tests',
  'medium_tests',
  'large_tests',
  # Tests using the pepper plugin, only run with chrome
  # TODO(ncbray): migrate pepper_browser_tests to chrome_browser_tests
  'pepper_browser_tests',
  # Lightweight browser tests
  'chrome_browser_tests',
  'huge_tests',
  'memcheck_bot_tests',
  'tsan_bot_tests',
  # Special testing environment for testing comparing x86 validators.
  'ncval_testing',
  # Environment for validator difference testing
  'validator_diff_tests',
  # Subset of tests enabled for Non-SFI Mode.
  'nonsfi_tests',
])

# These are the test suites we know exist, but aren't run on a regular basis.
# These test suites are essentially shortcuts that run a specific subset of the
# test cases.
ACCEPTABLE_TEST_SUITES = set([
  'barebones_tests',
  'dynamic_load_tests',
  'eh_tests',  # Tests for C++ exception handling
  'exception_tests',  # Tests for hardware exception handling
  'exit_status_tests',
  'gdb_tests',
  'mmap_race_tests',
  'nonpexe_tests',
  'pnacl_abi_tests',
  'sel_ldr_sled_tests',
  'sel_ldr_tests',
  'toolchain_tests',
  'validator_modeling',
  'validator_tests',
  # Special testing of the decoder for the ARM validator.
  'arm_decoder_tests',
])

# Under --mode=nacl_irt_test we build variants of numerous tests normally
# built under --mode=nacl.  The test names and suite names for these
# variants are set (in IrtTestAddNodeToTestSuite, below) by appending _irt
# to the names used for the --mode=nacl version of the same tests.
MAJOR_TEST_SUITES |= set([name + '_irt'
                          for name in MAJOR_TEST_SUITES])
ACCEPTABLE_TEST_SUITES |= set([name + '_irt'
                               for name in ACCEPTABLE_TEST_SUITES])

# The major test suites are also acceptable names.  Suite names are checked
# against this set in order to catch typos.
ACCEPTABLE_TEST_SUITES.update(MAJOR_TEST_SUITES)


def ValidateTestSuiteNames(suite_name, node_name):
  if node_name is None:
    node_name = '<unknown>'

  # Prevent a silent failiure - strings are iterable!
  if not isinstance(suite_name, (list, tuple)):
    raise Exception("Test suites for %s should be specified as a list, "
      "not as a %s: %s" % (node_name, type(suite_name).__name__,
      repr(suite_name)))

  if not suite_name:
    raise Exception("No test suites are specified for %s. Set the 'broken' "
      "parameter on AddNodeToTestSuite in the cases where there's a known "
      "issue and you don't want the test to run" % (node_name,))

  # Make sure each test is in at least one test suite we know will run
  major_suites = set(suite_name).intersection(MAJOR_TEST_SUITES)
  if not major_suites:
    raise Exception("None of the test suites %s for %s are run on a "
    "regular basis" % (repr(suite_name), node_name))

  # Make sure a wierd test suite hasn't been inadvertantly specified
  for s in suite_name:
    if s not in ACCEPTABLE_TEST_SUITES:
      raise Exception("\"%s\" is not a known test suite. Either this is "
      "a typo for %s, or it should be added to ACCEPTABLE_TEST_SUITES in "
      "SConstruct" % (s, node_name))

BROKEN_TEST_COUNT = 0


def GetPlatformString(env):
  build = env['BUILD_TYPE']

  # If we are testing 'NACL' we really need the trusted info
  if build=='nacl' and 'TRUSTED_ENV' in env:
    trusted_env = env['TRUSTED_ENV']
    build = trusted_env['BUILD_TYPE']
    subarch = trusted_env['BUILD_SUBARCH']
  else:
    subarch = env['BUILD_SUBARCH']

  # Build the test platform string
  return build + '-' + subarch

pre_base_env.AddMethod(GetPlatformString)


tests_to_disable_qemu = set([
    # These tests do not work under QEMU but do work on ARM hardware.
    #
    # You should use the is_broken argument in preference to adding
    # tests to this list.
    #
    # See: http://code.google.com/p/nativeclient/issues/detail?id=2437
    # Note, for now these tests disable both the irt and non-irt variants
    'run_egyptian_cotton_test',
    'run_many_threads_sequential_test',
    # subprocess needs to also have qemu prefix, which isn't supported
    'run_subprocess_test',
    'run_thread_suspension_test',
    'run_dynamic_modify_test',
])

tests_to_disable = set()
if ARGUMENTS.get('disable_tests', '') != '':
  tests_to_disable.update(ARGUMENTS['disable_tests'].split(','))


def ShouldSkipTest(env, node_name):
  if (env.Bit('skip_trusted_tests')
      and (env['NACL_BUILD_FAMILY'] == 'TRUSTED'
           or env['NACL_BUILD_FAMILY'] == 'UNTRUSTED_IRT')):
    return True

  if env.Bit('do_not_run_tests'):
    # This hack is used for pnacl testing where we might build tests
    # without running them on one bot and then transfer and run them on another.
    # The skip logic only takes the first bot into account e.g. qemu
    # restrictions, while it really should be skipping based on the second
    # bot. By simply disabling the skipping completely we work around this.
    return False

  # There are no known-to-fail tests any more, but this code is left
  # in so that if/when we port to a new architecture or add a test
  # that is known to fail on some platform(s), we can continue to have
  # a central location to disable tests from running.  NB: tests that
  # don't *build* on some platforms need to be omitted in another way.

  if node_name in tests_to_disable:
    return True

  if env.UsingEmulator():
    if node_name in tests_to_disable_qemu:
      return True
    # For now also disable the irt variant
    if node_name.endswith('_irt') and node_name[:-4] in tests_to_disable_qemu:
      return True

  # Retrieve list of tests to skip on this platform
  skiplist = bad_build_lists.get(env.GetPlatformString(), [])
  if node_name in skiplist:
    return True

  if env.Bit('nacl_glibc') and node_name in nacl_glibc_skiplist:
    return True

  return False

pre_base_env.AddMethod(ShouldSkipTest)


def AddImplicitTestSuites(suite_list, node_name):
  if node_name in nonsfi_test_whitelist:
    suite_list = suite_list + ['nonsfi_tests']
  return suite_list


def AddNodeToTestSuite(env, node, suite_name, node_name, is_broken=False,
                       is_flaky=False):
  global BROKEN_TEST_COUNT

  # CommandTest can return an empty list when it silently discards a test
  if not node:
    return

  assert node_name is not None
  test_name_regex = r'run_.*_(unit)?test.*$'
  assert re.match(test_name_regex, node_name), (
      'test %r does not match "run_..._test" naming convention '
      '(precise regex is %s)' % (node_name, test_name_regex))

  suite_name = AddImplicitTestSuites(suite_name, node_name)
  ValidateTestSuiteNames(suite_name, node_name)

  AlwaysBuild(node)

  if is_broken or is_flaky and env.Bit('disable_flaky_tests'):
    # Only print if --verbose is specified
    if not GetOption('brief_comstr'):
      print '*** BROKEN ', node_name
    BROKEN_TEST_COUNT += 1
    env.Alias('broken_tests', node)
  elif env.ShouldSkipTest(node_name):
    print '*** SKIPPING ', env.GetPlatformString(), ':', node_name
    env.Alias('broken_tests', node)
  else:
    env.Alias('all_tests', node)

    for s in suite_name:
      env.Alias(s, node)

  if node_name:
    env.ComponentTestOutput(node_name, node)
    test_name = node_name
  else:
    # This is rather shady, but the tests need a name without dots so they match
    # what gtest does.
    # TODO(ncbray) node_name should not be optional.
    test_name = os.path.basename(str(node[0].path))
    if test_name.endswith('.out'):
      test_name = test_name[:-4]
    test_name = test_name.replace('.', '_')
  SetTestName(node, test_name)

pre_base_env.AddMethod(AddNodeToTestSuite)


def TestBindsFixedTcpPort(env, node):
  # This tells Scons that tests that bind a fixed TCP port should not
  # run concurrently, because they would interfere with each other.
  # These tests are typically tests for NaCl's GDB debug stub.  The
  # dummy filename used below is an arbitrary token that just has to
  # match across the tests.
  SideEffect(env.File('${SCONSTRUCT_DIR}/test_binds_fixed_tcp_port'), node)

pre_base_env.AddMethod(TestBindsFixedTcpPort)


# Convenient testing aliases
# NOTE: work around for scons non-determinism in the following two lines
Alias('sel_ldr_sled_tests', [])

Alias('small_tests', [])
Alias('medium_tests', [])
Alias('large_tests', [])

Alias('small_tests_irt', [])
Alias('medium_tests_irt', [])
Alias('large_tests_irt', [])

Alias('pepper_browser_tests', [])
Alias('chrome_browser_tests', [])

Alias('unit_tests', 'small_tests')
Alias('smoke_tests', ['small_tests', 'medium_tests'])

if pre_base_env.Bit('nacl_glibc'):
  Alias('memcheck_bot_tests', ['small_tests'])
  Alias('tsan_bot_tests', ['small_tests'])
else:
  Alias('memcheck_bot_tests', ['small_tests', 'medium_tests', 'large_tests'])
  Alias('tsan_bot_tests', [])


def Banner(text):
  print '=' * 70
  print text
  print '=' * 70

pre_base_env.AddMethod(Banner)


# PLATFORM LOGIC
# Define the platforms, and use them to define the path for the
# scons-out directory (aka TARGET_ROOT)
#
# Various variables in the scons environment are related to this, e.g.
#
# BUILD_ARCH: (arm, mips, x86)
# BUILD_SUBARCH: (32, 64)
#

DeclareBit('build_x86_32', 'Building binaries for the x86-32 architecture',
           exclusive_groups='build_arch')
DeclareBit('build_x86_64', 'Building binaries for the x86-64 architecture',
           exclusive_groups='build_arch')
DeclareBit('build_mips32', 'Building binaries for the MIPS architecture',
           exclusive_groups='build_arch')
DeclareBit('build_arm_arm', 'Building binaries for the ARM architecture',
           exclusive_groups='build_arch')

# Shorthand for either the 32 or 64 bit version of x86.
DeclareBit('build_x86', 'Building binaries for the x86 architecture')

DeclareBit('build_arm', 'Building binaries for the arm architecture')


def MakeArchSpecificEnv(platform=None):
  env = pre_base_env.Clone()
  if platform is None:
    platform = GetTargetPlatform()

  arch = pynacl.platform.GetArch(platform)
  if pynacl.platform.IsArch64Bit(platform):
    subarch = '64'
  else:
    subarch = '32'

  env.Replace(BUILD_FULLARCH=platform)
  env.Replace(BUILD_ARCHITECTURE=arch)
  env.Replace(BUILD_SUBARCH=subarch)
  env.Replace(TARGET_FULLARCH=platform)
  env.Replace(TARGET_ARCHITECTURE=arch)
  env.Replace(TARGET_SUBARCH=subarch)

  env.SetBits('build_%s' % platform.replace('-', '_'))

  if env.Bit('build_x86_32') or env.Bit('build_x86_64'):
    env.SetBits('build_x86')
  if env.Bit('build_arm_arm'):
    env.SetBits('build_arm')

  env.Replace(BUILD_ISA_NAME=platform)

  # Determine where the object files go
  env.Replace(BUILD_TARGET_NAME=platform)
  # This may be changed later; see target_variant_map, below.
  env.Replace(TARGET_VARIANT='')
  env.Replace(TARGET_ROOT=
      '${DESTINATION_ROOT}/${BUILD_TYPE}-${BUILD_TARGET_NAME}${TARGET_VARIANT}')
  return env


# Valgrind
pre_base_env.AddMethod(lambda self: ARGUMENTS.get('running_on_valgrind'),
                       'IsRunningUnderValgrind')

DeclareBit('with_leakcheck', 'Running under Valgrind leak checker')

def RunningUnderLeakCheck():
  run_under = ARGUMENTS.get('run_under')
  if run_under:
    extra_args = ARGUMENTS.get('run_under_extra_args')
    if extra_args:
      run_under += extra_args
    if run_under.find('leak-check=full') > 0:
      return True
  return False

if RunningUnderLeakCheck():
  pre_base_env.SetBits('with_leakcheck')


def StripSuffix(string, suffix):
  assert string.endswith(suffix)
  return string[:-len(suffix)]


# TODO(mseaborn): Change code to use ComponentLibrary() directly.
# DualLibrary() is left over from when we built libraries twice, with and
# without "-fPIC", for building plugins as DSOs.
def DualLibrary(env, lib_name, *args, **kwargs):
  return env.ComponentLibrary(lib_name, *args, **kwargs)

def DualObject(env, *args, **kwargs):
  return env.ComponentObject(*args, **kwargs)


def AddDualLibrary(env):
  env.AddMethod(DualLibrary)
  env.AddMethod(DualObject)


# In prebuild mode we ignore the dependencies so that stuff does
# NOT get build again
# Optionally ignore the build process.
DeclareBit('prebuilt', 'Disable all build steps, only support install steps')
pre_base_env.SetBitFromOption('prebuilt', False)


# HELPERS FOR TEST INVOLVING TRUSTED AND UNTRUSTED ENV
def GetEmulator(env):
  emulator = ARGUMENTS.get('force_emulator')
  if emulator is None and 'TRUSTED_ENV' in env:
    emulator = env['TRUSTED_ENV'].get('EMULATOR')
  return emulator

pre_base_env.AddMethod(GetEmulator)

def UsingEmulator(env):
  return bool(env.GetEmulator())

pre_base_env.AddMethod(UsingEmulator)


def GetValidator(env, validator):
  # NOTE: that the variable TRUSTED_ENV is set by ExportSpecialFamilyVars()
  if 'TRUSTED_ENV' not in env:
    return None

  # TODO(shyamsundarr): rename ncval_new to ncval.
  if validator is None:
    if env.Bit('build_arm'):
      validator = 'arm-ncval-core'
    elif env.Bit('build_mips32'):
      validator = 'mips-ncval-core'
    else:
      validator = 'ncval_new'

  trusted_env = env['TRUSTED_ENV']
  return trusted_env.File('${STAGING_DIR}/${PROGPREFIX}%s${PROGSUFFIX}' %
                    validator)

pre_base_env.AddMethod(GetValidator)


# Perform os.path.abspath rooted at the directory SConstruct resides in.
def SConstructAbsPath(env, path):
  return os.path.normpath(os.path.join(env['MAIN_DIR'], path))

pre_base_env.AddMethod(SConstructAbsPath)


def GetPlatformBuildTargetDir(env):
  # Currently we do not support any cross OS compiles, eventually the OS name
  # will probably be passed in through arguments.
  os_name = pynacl.platform.GetOS()

  # Currently 32/64 share the same tool build target directory. When we have
  # separate toolchains for each the architectures will probably have to use
  # the Arch3264() variant.
  build_arch = pynacl.platform.GetArch(GetBuildPlatform())

  return '%s_%s' % (os_name, build_arch)

pre_base_env.AddMethod(GetPlatformBuildTargetDir)


def GetToolchainDir(env, platform_build_dir=None, toolchain_name=None,
                    target_arch=None, is_pnacl=None, lib_name=None):
  if platform_build_dir is None:
    platform_build_dir = env.GetPlatformBuildTargetDir()

  if toolchain_name is None:
    # Fill in default arguments based on environment.
    if is_pnacl is None:
      # For the purposes of finding the toolchain dir, nacl_clang is PNaCl.
      is_pnacl = env.Bit('bitcode') or env.Bit('nacl_clang')
      if lib_name is None:
        if is_pnacl or not env.Bit('nacl_glibc'):
          lib_name = 'newlib'
        else:
          lib_name = 'glibc'

    if target_arch is None:
      target_arch = pynacl.platform.GetArch(GetTargetPlatform())

    if is_pnacl:
      target_env = 'pnacl'
    else:
      target_env = 'nacl_%s' % target_arch

    # See if we have a custom toolchain directory set.
    if is_pnacl:
      toolchain_arg = 'pnacl_%s_dir' % lib_name
    else:
      assert lib_name == 'glibc'
      toolchain_arg = 'nacl_%s_dir' % lib_name

    custom_toolchain_dir = ARGUMENTS.get(toolchain_arg, None)
    if custom_toolchain_dir:
      return env.SConstructAbsPath(custom_toolchain_dir)

    # Get the standard toolchain name since no directory custom was found.
    if is_pnacl:
      target_env = 'pnacl'
    else:
      target_env = 'nacl_%s' % target_arch
    toolchain_name = '%s_%s_raw' % (target_env, lib_name)

  # Get the absolute path for the platform build directory and toolchain.
  toolchain_sub_dir = os.path.join('toolchain',
                                   platform_build_dir,
                                   toolchain_name)
  return env.SConstructAbsPath(toolchain_sub_dir)

pre_base_env.AddMethod(GetToolchainDir)


def GetSelLdr(env):
  sel_ldr = ARGUMENTS.get('force_sel_ldr')
  if sel_ldr:
    return env.File(env.SConstructAbsPath(sel_ldr))

  # NOTE: that the variable TRUSTED_ENV is set by ExportSpecialFamilyVars()
  if 'TRUSTED_ENV' not in env:
    return None

  trusted_env = env['TRUSTED_ENV']
  return trusted_env.File('${STAGING_DIR}/${PROGPREFIX}sel_ldr${PROGSUFFIX}')

pre_base_env.AddMethod(GetSelLdr)


def GetSelLdrSeccomp(env):
  # NOTE: that the variable TRUSTED_ENV is set by ExportSpecialFamilyVars()
  if 'TRUSTED_ENV' not in env:
    return None

  if not (env.Bit('linux') and env.Bit('build_x86_64')):
    return None

  trusted_env = env['TRUSTED_ENV']
  return trusted_env.File('${STAGING_DIR}/${PROGPREFIX}'
                          'sel_ldr_seccomp${PROGSUFFIX}')

pre_base_env.AddMethod(GetSelLdrSeccomp)


def SupportsSeccompBpfSandbox(env):
  if not (env.Bit('linux') and env.Bit('build_x86_64')):
    return False

  # The gcov runtime does some extra calls (such as 'access') that
  # are not permitted by the policy.
  if env.Bit('coverage_enabled'):
    return False

  # This is a lame detection if seccomp bpf filters are supported by the kernel.
  # We suppose that any Linux kernel v3.2+ supports it, but it is only true
  # for Ubuntu kernels. Seccomp BPF filters reached the mainline at 3.5,
  # so this check will be wrong on some relatively old non-Ubuntu Linux distros.
  kernel_version = map(int, platform.release().split('.', 2)[:2])
  return kernel_version >= [3, 2]

pre_base_env.AddMethod(SupportsSeccompBpfSandbox)


def GetBootstrap(env):
  if env.Bit('msan'):
    # Bootstrap doens't currently work with MSan. However, MSan is only
    # available on x86_64 where we don't need bootstrap anyway.
    return None, None
  bootstrap = ARGUMENTS.get('force_bootstrap')
  if bootstrap:
    bootstrap = env.File(env.SConstructAbsPath(bootstrap))
  else:
    if 'TRUSTED_ENV' in env:
      trusted_env = env['TRUSTED_ENV']
      if trusted_env.Bit('linux'):
        bootstrap = trusted_env.File('${STAGING_DIR}/nacl_helper_bootstrap')
  if bootstrap:
    template_digits = 'X' * 16
    return (bootstrap,
            ['--r_debug=0x' + template_digits,
             '--reserved_at_zero=0x' + template_digits])
  return None, None

pre_base_env.AddMethod(GetBootstrap)

def AddBootstrap(env, executable, args):
  bootstrap, bootstrap_args = env.GetBootstrap()
  if bootstrap is None:
    return [executable] + args
  else:
    return [bootstrap, executable] + bootstrap_args + args

pre_base_env.AddMethod(AddBootstrap)


def GetNonSfiLoader(env):
  if env.Bit('use_newlib_nonsfi_loader'):
    return nacl_env.GetTranslatedNexe(nacl_env.File(
        '${STAGING_DIR}/${PROGPREFIX}nonsfi_loader${PROGSUFFIX}'))

  if 'TRUSTED_ENV' not in env:
    return None
  return env['TRUSTED_ENV'].File(
      '${STAGING_DIR}/${PROGPREFIX}nonsfi_loader${PROGSUFFIX}')

pre_base_env.AddMethod(GetNonSfiLoader)


def GetIrtNexe(env, chrome_irt=False):
  image = ARGUMENTS.get('force_irt')
  if image:
    return env.SConstructAbsPath(image)

  if chrome_irt:
    return nacl_irt_env.File('${STAGING_DIR}/irt.nexe')
  else:
    return nacl_irt_env.File('${STAGING_DIR}/irt_core.nexe')

pre_base_env.AddMethod(GetIrtNexe)


# Note that we build elf_loader in the nacl_irt_env, not because it is
# actually built like the IRT per se, but just because we need it always to
# be built against newlib.
def GetElfLoaderNexe(env):
  elf_loader_env = nacl_env
  if env.Bit('nacl_glibc'):
    elf_loader_env = nacl_irt_env
  return elf_loader_env.File('${STAGING_DIR}/elf_loader.nexe')

pre_base_env.AddMethod(GetElfLoaderNexe)


def ApplyTLSEdit(env, nexe_name, raw_nexe):
  # If the environment was built elsewhere, we do not need to apply tls_edit
  # since it only needs to be done during building.
  if env.Bit('built_elsewhere'):
    return env.File(nexe_name)

  tls_edit_exe = env['BUILD_ENV'].File('${STAGING_DIR}/tls_edit${PROGSUFFIX}')
  return env.Command(
      nexe_name,
      [tls_edit_exe, raw_nexe],
      '${SOURCES[0]} --verbose ${SOURCES[1:]} ${TARGET}')

pre_base_env.AddMethod(ApplyTLSEdit)

def CommandValidatorTestNacl(env, name, image,
                             validator_flags=None,
                             validator=None,
                             size='medium',
                             **extra):
  validator = env.GetValidator(validator)
  if validator is None:
    print 'WARNING: no validator found. Skipping test %s' % name
    return []

  if validator_flags is None:
    validator_flags = []

  if env.Bit('pnacl_generate_pexe'):
    return []

  command = [validator] + validator_flags + [image]
  return env.CommandTest(name, command, size, **extra)

pre_base_env.AddMethod(CommandValidatorTestNacl)


def ExtractPublishedFiles(env, target_name):
  run_files = ['$STAGING_DIR/' + os.path.basename(published_file.path)
               for published_file in env.GetPublished(target_name, 'run')]
  nexe = '$STAGING_DIR/%s${PROGSUFFIX}' % target_name
  return [env.File(file) for file in run_files + [nexe]]

pre_base_env.AddMethod(ExtractPublishedFiles)


# Only include the chrome side of the build if present.
if os.path.exists(pre_base_env.File(
    '#/../ppapi/native_client/chrome_main.scons').abspath):
  SConscript('#/../ppapi/native_client/chrome_main.scons',
      exports=['pre_base_env'])
  enable_chrome = True
else:
  def AddChromeFilesFromGroup(env, file_group):
    pass
  pre_base_env.AddMethod(AddChromeFilesFromGroup)
  enable_chrome = False
DeclareBit('enable_chrome_side',
           'Is the chrome side present.')
pre_base_env.SetBitFromOption('enable_chrome_side', enable_chrome)

def ProgramNameForNmf(env, basename):
  """ Create an architecture-specific filename that can be used in an NMF URL.
  """
  if env.Bit('pnacl_generate_pexe'):
    return basename
  else:
    return '%s_%s' % (basename, env.get('TARGET_FULLARCH'))

pre_base_env.AddMethod(ProgramNameForNmf)


def MakeNaClLogOption(env, target):
  """ Make up a filename related to the [target], for use with NACLLOG.
  The file should end up in the build directory (scons-out/...).
  """
  # NOTE: to log to the source directory use file.srcnode().abspath instead.
  # See http://www.scons.org/wiki/File%28%29
  return env.File(target + '.nacllog').abspath

pre_base_env.AddMethod(MakeNaClLogOption)

def MakeVerboseExtraOptions(env, target, log_verbosity, extra):
  """ Generates **extra options that will give access to service runtime logs,
  at a given log_verbosity. Slips the options into the given extra dict. """
  log_file = env.MakeNaClLogOption(target)
  extra['log_file'] = log_file
  extra_env = ['NACLLOG=%s' % log_file,
               'NACLVERBOSITY=%d' % log_verbosity]
  extra['osenv'] = extra.get('osenv', []) + extra_env

pre_base_env.AddMethod(MakeVerboseExtraOptions)

def ShouldUseVerboseOptions(env, extra):
  """ Heuristic for setting up Verbose NACLLOG options. """
  return ('process_output_single' in extra or
          'log_golden' in extra)

pre_base_env.AddMethod(ShouldUseVerboseOptions)


DeclareBit('tests_use_irt', 'Non-browser tests also load the IRT image', False)

# Bit to be set by individual test/nacl.scons files that need to opt out.
DeclareBit('nonstable_bitcode', 'Tests use non-stable bitcode features', False)


def GetFinalizedPexe(env, pexe):
  """ Prep and finalize the ABI for a given pexe if needed.
  """
  if not env.Bit('pnacl_generate_pexe') or env.Bit('nonstable_bitcode'):
    return pexe

  # We can remove this once we move all CommandSelLdrTestNacl to a nacl.scons
  # file instead.  There are currently some canned nexe tests in build.scons.
  if env['NACL_BUILD_FAMILY'] == 'TRUSTED':
    return pexe

  # Otherwise, finalize during the build step, since there is no finalize tool
  # that can run on triggered bots such as the ARM HW bots.
  pexe_name = pexe.abspath
  final_name = StripSuffix(pexe_name, '.nonfinal.pexe') + '.final.pexe'
  # Make sure the pexe doesn't get removed by the fake builders when
  # built_elsewhere=1
  env.Precious(pexe)
  node = env.Command(target=final_name, source=[pexe_name],
                     action=[Action('${PNACLFINALIZECOM}',
                                    '${PNACLFINALIZECOMSTR}')])
  env.Alias('all_programs', node)
  assert len(node) == 1, node
  return node[0]

pre_base_env.AddMethod(GetFinalizedPexe)


# Translate the given pexe.
def GetTranslatedNexe(env, pexe):
  # First finalize the pexe.
  pexe = GetFinalizedPexe(env, pexe)

  # Then check if we need to translate.
  # Check if we started with a pexe, so there is actually a translation step.
  if not env.Bit('pnacl_generate_pexe'):
    return pexe

  # We can remove this once we move all CommandSelLdrTestNacl to a nacl.scons
  # file instead.  There are currently some canned nexe tests in build.scons.
  if env['NACL_BUILD_FAMILY'] == 'TRUSTED':
    return pexe

  # Often there is a build step (do_not_run_tests=1) and a test step
  # (which is run with -j1). Normally we want to translate in the build step
  # so we can translate in parallel. However when we do sandboxed translation
  # on arm hw, we do the build step on x86 and translation on arm, so we have
  # to force the translation to be done in the test step. Hence,
  # we check the bit 'translate_in_build_step' / check if we are
  # in the test step.
  if not env.Bit('translate_in_build_step') and env.Bit('do_not_run_tests'):
    return pexe

  pexe_name = pexe.abspath
  # Tidy up the suffix (remove the .final.pexe or .nonfinal.pexe),
  # depending on whether or not the pexe was finalized.
  suffix_to_strip = '.final.pexe'
  if not pexe_name.endswith(suffix_to_strip):
    suffix_to_strip = '.nonfinal.pexe'
  nexe_name = StripSuffix(pexe_name, suffix_to_strip) + '.nexe'
  # Make sure the pexe doesn't get removed by the fake builders when
  # built_elsewhere=1
  env.Precious(pexe)
  command = '${TRANSLATECOM}'
  if env.Bit('nonstable_bitcode'):
    command += ' --allow-llvm-bitcode-input'
  node = env.Command(target=nexe_name, source=[pexe_name],
                     action=[Action(command, '${TRANSLATECOMSTR}')])
  env.Alias('all_programs', node)
  assert len(node) == 1, node
  return node[0]

pre_base_env.AddMethod(GetTranslatedNexe)


def CommandTestFileDumpCheck(env,
                             name,
                             target,
                             check_file,
                             objdump_flags):
  """Create a test that disassembles a binary (|target|) and checks for
  patterns in the |check_file|.  Disassembly is done using |objdump_flags|.
  """

  # Do not try to run OBJDUMP if 'built_elsewhere', since that *might* mean
  # that a toolchain is not even present.  E.g., the arm hw buildbots do
  # not have the pnacl toolchain. We should be able to look for the host
  # ARM objdump though... a TODO(jvoung) for when there is time.
  if env.Bit('built_elsewhere'):
    return []
  target = env.GetTranslatedNexe(target)
  return env.CommandTestFileCheck(name,
                                  ['${OBJDUMP}', objdump_flags, target],
                                  check_file)

pre_base_env.AddMethod(CommandTestFileDumpCheck)

def CommandTestFileCheck(env, name, cmd, check_file):
  """Create a test that runs a |cmd| (array of strings),
  which is expected to print to stdout.  The results
  of stdout will then be piped to the file_check.py tool which
  will search for the regexes specified in |check_file|. """

  return env.CommandTest(
          name,
          ['${PYTHON}',
          env.File('${SCONSTRUCT_DIR}/tools/llvm_file_check_wrapper.py'),
          '${FILECHECK}',
          check_file] + cmd,
          direct_emulation=False)

pre_base_env.AddMethod(CommandTestFileCheck)

def CommandSelLdrTestNacl(env, name, nexe,
                          args = None,
                          log_verbosity=2,
                          sel_ldr_flags=None,
                          loader=None,
                          size='medium',
                          # True for *.nexe statically linked with glibc
                          glibc_static=False,
                          skip_bootstrap=False,
                          wrapper_program_prefix=None,
                          # e.g., [ 'python', 'time_check.py', '--' ]
                          **extra):
  # Disable all sel_ldr tests for windows under coverage.
  # Currently several .S files block sel_ldr from being instrumented.
  # See http://code.google.com/p/nativeclient/issues/detail?id=831
  if ('TRUSTED_ENV' in env and
      env['TRUSTED_ENV'].Bit('coverage_enabled') and
      env['TRUSTED_ENV'].Bit('windows')):
    return []

  # The nexe might be a pexe that needs finalization, and translation.
  nexe = env.GetTranslatedNexe(nexe)

  command = [nexe]
  if args is not None:
    command += args

  if env.Bit('pnacl_unsandboxed') or (env.Bit('nonsfi_nacl') and
                                      not env.Bit('tests_use_irt')):
    # Run unsandboxed executable directly, without sel_ldr.
    return env.CommandTest(name, command, size, **extra)

  if loader is None:
    if env.Bit('nonsfi_nacl'):
      loader = env.GetNonSfiLoader()
    else:
      loader = env.GetSelLdr()
    if loader is None:
      print 'WARNING: no sel_ldr found. Skipping test %s' % name
      return []

  # Avoid problems with [] as default arguments
  if sel_ldr_flags is None:
    sel_ldr_flags = []
  else:
    # Avoid modifying original list
    sel_ldr_flags = list(sel_ldr_flags)

  # Disable the validator if running a GLibC test under Valgrind.
  # http://code.google.com/p/nativeclient/issues/detail?id=1799
  if env.IsRunningUnderValgrind() and env.Bit('nacl_glibc'):
    sel_ldr_flags += ['-cc']
    # https://code.google.com/p/nativeclient/issues/detail?id=3158
    # We don't currently have valgrind.so for LD_PRELOAD to use.  That .so
    # is not used for newlib.
    # TODO(sehr): add valgrind.so built for NaCl.
    return []

  # Skip platform qualification checks on configurations with known issues.
  if env.GetEmulator() or env.IsRunningUnderValgrind():
    sel_ldr_flags += ['-Q']

  # Skip validation if we are using the x86-64 zero-based sandbox.
  # TODO(arbenson): remove this once the validator supports the x86-64
  # zero-based sandbox model.
  if env.Bit('x86_64_zero_based_sandbox'):
    sel_ldr_flags += ['-c']

  # The glibc modifications only make sense for nacl_env tests.
  # But this function gets used by some base_env (i.e. src/trusted/...)
  # tests too.  Don't add the --nacl_glibc changes to the command
  # line for those cases.
  if env.Bit('nacl_glibc') and env['NACL_BUILD_FAMILY'] != 'TRUSTED':
    if not glibc_static and not env.Bit('nacl_static_link'):
      # Enable file access so shared libraries can be loaded.
      sel_ldr_flags.append('-a')
      # Locally-built shared libraries come from ${LIB_DIR} while
      # toolchain-provided ones come from ${NACL_SDK_LIB}.
      library_path = '${LIB_DIR}:${NACL_SDK_LIB}'
      if env.Bit('build_x86'):
        # In the old glibc, we run via runnable-ld.so (the dynamic linker).
        command = ['${NACL_SDK_LIB}/runnable-ld.so',
                   '--library-path', library_path] + command
      else:
        # In the new glibc, we run via elf_loader and direct it where to
        # find the dynamic linker in the toolchain.
        command = [env.GetElfLoaderNexe(),
                   '--interp-prefix',
                   os.path.dirname(env.subst('${NACL_SDK_LIB}'))] + command
        sel_ldr_flags.extend(['-E', 'LD_LIBRARY_PATH=' + library_path])

  # Turn off sandbox for mac so coverage files can be written out.
  if ('TRUSTED_ENV' in env and
      env['TRUSTED_ENV'].Bit('coverage_enabled') and
      env.Bit('host_mac') and
      '-a' not in sel_ldr_flags):
    sel_ldr_flags += ['-a']

  if env.Bit('tests_use_irt'):
    sel_ldr_flags += ['-B', nacl_env.GetIrtNexe()]

  if skip_bootstrap:
    loader_cmd = [loader]
  else:
    loader_cmd = env.AddBootstrap(loader, [])

  if env.Bit('nonsfi_nacl'):
    # nonsfi_loader does not accept the same flags as sel_ldr yet, so
    # we ignore sel_ldr_flags here.
    command = [loader] + command
  else:
    command = loader_cmd + sel_ldr_flags + ['--'] + command

  if env.Bit('host_linux'):
    extra['using_nacl_signal_handler'] = True

  if env.ShouldUseVerboseOptions(extra):
    env.MakeVerboseExtraOptions(name, log_verbosity, extra)

  node = env.CommandTest(name, command, size, posix_path=True,
                         wrapper_program_prefix=wrapper_program_prefix, **extra)
  if env.Bit('tests_use_irt'):
    env.Alias('irt_tests', node)
  return node

pre_base_env.AddMethod(CommandSelLdrTestNacl)


TEST_EXTRA_ARGS = ['stdin', 'log_file',
                   'stdout_golden', 'stderr_golden', 'log_golden',
                   'filter_regex', 'filter_inverse', 'filter_group_only',
                   'osenv', 'arch', 'subarch', 'exit_status',
                   'num_runs', 'process_output_single',
                   'process_output_combined', 'using_nacl_signal_handler',
                   'declares_exit_status', 'time_warning', 'time_error']

TEST_TIME_THRESHOLD = {
    'small':   2,
    'medium': 10,
    'large':  60,
    'huge': 1800,
    }

# Valgrind handles SIGSEGV in a way our testing tools do not expect.
UNSUPPORTED_VALGRIND_EXIT_STATUS = ['trusted_sigabrt',
                                    'untrusted_sigill' ,
                                    'untrusted_segfault',
                                    'untrusted_sigsegv_or_equivalent',
                                    'trusted_segfault',
                                    'trusted_sigsegv_or_equivalent']


def GetPerfEnvDescription(env):
  """Return a string describing architecture, library, etc. options.

  This function attempts to gather a string that might inform why a performance
  change has occurred.
  """
  if env['NACL_BUILD_FAMILY'] == 'TRUSTED':
    # Trusted tests do not depend on the untrusted toolchain, untrusted libc,
    # whether or not the IRT is used, etc.
    description_list = ['trusted',
                        env['TARGET_PLATFORM'].lower(),
                        env['TARGET_FULLARCH']]
    return ARGUMENTS.get('perf_prefix', '') + '_'.join(description_list)
  description_list = [env['TARGET_FULLARCH']]
  # Using a list to keep the order consistent.
  bit_to_description = [ ('tests_use_irt', ('with_irt', '')),
                         ('bitcode', ('pnacl', 'nnacl')),
                         ('translate_fast', ('fast', '')),
                         ('use_sz', ('sz', '')),
                         ('nacl_glibc', ('glibc', 'newlib')),
                         ('nacl_static_link', ('static', 'dynamic')),
                         ]
  for (bit, (descr_yes, descr_no)) in bit_to_description:
    if env.Bit(bit):
      additional = descr_yes
    else:
      additional = descr_no
    if additional:
      description_list.append(additional)
  return ARGUMENTS.get('perf_prefix', '') + '_'.join(description_list)

pre_base_env.AddMethod(GetPerfEnvDescription)


TEST_NAME_MAP = {}

def GetTestName(target):
  key = str(target.path)
  return TEST_NAME_MAP.get(key, key)

pre_base_env['GetTestName'] = GetTestName


def SetTestName(node, name):
  for target in Flatten(node):
    TEST_NAME_MAP[str(target.path)] = name


def ApplyTestWrapperCommand(command_args, extra_deps):
  new_args = ARGUMENTS['test_wrapper'].split()
  for input_file in extra_deps:
    new_args.extend(['-F', input_file])
  for arg in command_args:
    if isinstance(arg, str):
      new_args.extend(['-a', arg])
    else:
      new_args.extend(['-f', arg])
  return new_args


def CommandTest(env, name, command, size='small', direct_emulation=True,
                extra_deps=[], posix_path=False, capture_output=True,
                capture_stderr=True, wrapper_program_prefix=None,
                scale_timeout=None, **extra):
  if not name.endswith('.out') or name.startswith('$'):
    raise Exception('ERROR: bad test filename for test output %r' % name)

  if env.IsRunningUnderValgrind():
    skip = 'Valgrind'
  elif env.Bit('asan'):
    skip = 'AddressSanitizer'
  else:
    skip = None
  # Valgrind tends to break crash tests by changing the exit status.
  # So far, tests using declares_exit_status are crash tests.  If this
  # changes, we will have to find a way to make declares_exit_status
  # work with Valgrind.
  if (skip is not None and
      (extra.get('exit_status') in UNSUPPORTED_VALGRIND_EXIT_STATUS or
       bool(int(extra.get('declares_exit_status', 0))))):
    print 'Skipping death test "%s" under %s' % (name, skip)
    return []

  if env.Bit('asan'):
    extra.setdefault('osenv', [])
    # Ensure that 'osenv' is a list.
    if isinstance(extra['osenv'], str):
      extra['osenv'] = [extra['osenv']]
    # ASan normally intercepts SIGSEGV and SIGFPE and disables our signal
    # handlers, which interferes with various NaCl tests, including the
    # platform qualification test built into sel_ldr.  We fix this by telling
    # ASan not to mess with SIGSEGV and SIGFPE.
    asan_options = ['handle_segv=0', 'handle_sigfpe=0']
    # ASan aborts on errors rather than exits. This changes the expected exit
    # codes for some tests.
    asan_options.append('abort_on_error=0')

    if env.Bit('host_mac') and int(platform.mac_ver()[0].split('.')[1]) < 7:
      # MacOS 10.6 has a bug in the libsandbox system library where it
      # makes a memcmp call that reads off the end of a malloc'd block.
      # The bug appears to be harmless, but trips an ASan report.  So
      # tell ASan to suppress memcmp checks.
      asan_options.append('strict_memcmp=0')
    # TODO(mcgrathr): Remove this when we clean up all the crufty old
    # code to be leak-free.
    # https://code.google.com/p/nativeclient/issues/detail?id=3874
    asan_options.append('detect_leaks=0')
    # Note that the ASan runtime doesn't use : specifically as a separator.
    # It actually just looks for "foo=" anywhere in the string with strstr,
    # so any separator will do.  The most obvious choices, ' ', ',', and ';'
    # all cause command_tester.py to split things up and get confused.
    extra['osenv'].append('ASAN_OPTIONS=' + ':'.join(asan_options))

  name = '${TARGET_ROOT}/test_results/' + name
  # NOTE: using the long version of 'name' helps distinguish opt vs dbg
  max_time = TEST_TIME_THRESHOLD[size]
  if 'scale_timeout' in ARGUMENTS:
    max_time = max_time * int(ARGUMENTS['scale_timeout'])
  if scale_timeout:
    max_time = max_time * scale_timeout

  if env.Bit('nacl_glibc'):
    suite = 'nacl_glibc'
  else:
    suite = 'nacl_newlib'
  if env.Bit('bitcode'):
    suite = 'p' + suite

  script_flags = ['--name', '%s.${GetTestName(TARGET)}' % suite,
                  '--time_warning', str(max_time),
                  '--time_error', str(10 * max_time),
                  ]

  run_under = ARGUMENTS.get('run_under')
  if run_under:
    run_under_extra_args = ARGUMENTS.get('run_under_extra_args')
    if run_under_extra_args:
      run_under = run_under + ',' + run_under_extra_args
    script_flags.append('--run_under')
    script_flags.append(run_under)

  emulator = env.GetEmulator()
  if emulator and direct_emulation:
    command = [emulator] + command

  # test wrapper should go outside of emulators like qemu, since the
  # test wrapper code is not emulated.
  if wrapper_program_prefix is not None:
    command = wrapper_program_prefix + command

  script_flags.append('--perf_env_description')
  script_flags.append(env.GetPerfEnvDescription())

  # Add architecture info.
  extra['arch'] = env['BUILD_ARCHITECTURE']
  extra['subarch'] = env['BUILD_SUBARCH']

  for flag_name, flag_value in extra.iteritems():
    assert flag_name in TEST_EXTRA_ARGS, repr(flag_name)
    if isinstance(flag_value, list):
      # Options to command_tester.py which are actually lists must not be
      # separated by whitespace. This stringifies the lists with a separator
      # char to satisfy command_tester.
      flag_value =  command_tester.StringifyList(flag_value)
    # do not add --flag + |flag_name| |flag_value| if
    # |flag_value| is false (empty).
    if flag_value:
      script_flags.append('--' + flag_name)
      # Make sure flag values are strings (or SCons objects) when building
      # up the command. Right now, this only means convert ints to strings.
      if isinstance(flag_value, int):
        flag_value = str(flag_value)
      script_flags.append(flag_value)

  # Other extra flags
  if not capture_output:
    script_flags.extend(['--capture_output', '0'])
  if not capture_stderr:
    script_flags.extend(['--capture_stderr', '0'])

  # Set command_tester.py's output filename.  We skip this when using
  # test_wrapper because the run_test_via_ssh.py wrapper does not have
  # the ability to copy result files back from the remote host.
  if 'test_wrapper' not in ARGUMENTS:
    script_flags.extend(['--output_stamp', name])

  test_script = env.File('${SCONSTRUCT_DIR}/tools/command_tester.py')
  extra_deps = extra_deps + [env.File('${SCONSTRUCT_DIR}/tools/test_lib.py')]
  command = ['${PYTHON}', test_script] + script_flags + command
  if 'test_wrapper' in ARGUMENTS:
    command = ApplyTestWrapperCommand(command, extra_deps)
  return env.AutoDepsCommand(name, command,
                             extra_deps=extra_deps, posix_path=posix_path,
                             disabled=env.Bit('do_not_run_tests'))

pre_base_env.AddMethod(CommandTest)


def FileSizeTest(env, name, envFile, max_size=None):
  """FileSizeTest() returns a scons node like the other XYZTest generators.
  It logs the file size of envFile in a perf-buildbot-recognizable format.
  Optionally, it can cause a test failure if the file is larger than max_size.
  """
  def doSizeCheck(target, source, env):
    filepath = source[0].abspath
    actual_size = os.stat(filepath).st_size
    command_tester.LogPerfResult(name,
                                 env.GetPerfEnvDescription(),
                                 '%.3f' % (actual_size / 1024.0),
                                 'KB')
    # Also get zipped size.
    nexe_file = open(filepath, 'rb')
    zipped_size = len(zlib.compress(nexe_file.read()))
    nexe_file.close()
    command_tester.LogPerfResult(name,
                                 'ZIPPED_' + env.GetPerfEnvDescription(),
                                 '%.3f' % (zipped_size / 1024.0),
                                 'KB')
    # Finally, do the size check.
    if max_size is not None and actual_size > max_size:
      # NOTE: this exception only triggers a failure for this particular test,
      # just like any other test failure.
      raise Exception("File %s larger than expected: expected up to %i, got %i"
                      % (filepath, max_size, actual_size))
  # If 'built_elsewhere', the file should should have already been built.
  # Do not try to built it and/or its pieces.
  if env.Bit('built_elsewhere'):
    env.Ignore(name, envFile)
  return env.Command(name, envFile, doSizeCheck)

pre_base_env.AddMethod(FileSizeTest)

def StripExecutable(env, name, exe):
  """StripExecutable returns a node representing the stripped version of |exe|.
     The stripped version will be given the basename |name|.
     NOTE: for now this only works with the untrusted toolchain.
     STRIP does not appear to be a first-class citizen in SCons and
     STRIP has only been set to point at the untrusted toolchain.
  """
  return env.Command(
      target=name,
      source=[exe],
      action=[Action('${STRIPCOM} ${SOURCES} -o ${TARGET}', '${STRIPCOMSTR}')])

pre_base_env.AddMethod(StripExecutable)


# TODO(ncbray): pretty up the log output when running this builder.
def DisabledCommand(target, source, env):
  pass

pre_base_env['BUILDERS']['DisabledCommand'] = Builder(action=DisabledCommand)


def AutoDepsCommand(env, name, command, extra_deps=[], posix_path=False,
                    disabled=False):
  """AutoDepsCommand() takes a command as an array of arguments.  Each
  argument may either be:

   * a string, or
   * a Scons file object, e.g. one created with env.File() or as the
     result of another build target.

  In the second case, the file is automatically declared as a
  dependency of this command.
  """
  command = list(command)
  deps = []
  for index, arg in enumerate(command):
    if not isinstance(arg, str):
      if len(Flatten(arg)) != 1:
        # Do not allow this, because it would cause "deps" to get out
        # of sync with the indexes in "command".
        # See http://code.google.com/p/nativeclient/issues/detail?id=1086
        raise AssertionError('Argument to AutoDepsCommand() actually contains '
                             'multiple (or zero) arguments: %r' % arg)
      if posix_path:
        command[index] = '${SOURCES[%d].posix}' % len(deps)
      else:
        command[index] = '${SOURCES[%d].abspath}' % len(deps)
      deps.append(arg)

  # If built_elsewhere, build commands are replaced by no-ops, so make sure
  # the targets don't get removed first
  if env.Bit('built_elsewhere'):
    env.Precious(deps)
  env.Depends(name, extra_deps)

  if disabled:
    return env.DisabledCommand(name, deps)
  else:
    return env.Command(name, deps, ' '.join(command))


pre_base_env.AddMethod(AutoDepsCommand)


def GetPrintableCommandName(cmd):
  """Look at the first few elements of cmd to derive a suitable command name."""
  cmd_tokens = cmd.split()
  if "python" in cmd_tokens[0] and len(cmd_tokens) >= 2:
    cmd_name = cmd_tokens[1]
  else:
    cmd_name = cmd_tokens[0].split('(')[0]

  # undo some pretty printing damage done by hammer
  cmd_name = cmd_name.replace('________','')
  # use file name part of a path
  return cmd_name.split('/')[-1]


def GetPrintableEnvironmentName(env):
  # use file name part of a obj root path as env name
  return env.subst('${TARGET_ROOT}').split('/')[-1]

pre_base_env.AddMethod(GetPrintableEnvironmentName)


def CustomCommandPrinter(cmd, targets, source, env):
  # Abuse the print hook to count the commands that are executed
  if env.Bit('target_stats'):
    cmd_name = GetPrintableCommandName(cmd)
    env_name = env.GetPrintableEnvironmentName()
    CMD_COUNTER[cmd_name] = CMD_COUNTER.get(cmd_name, 0) + 1
    ENV_COUNTER[env_name] = ENV_COUNTER.get(env_name, 0) + 1

  if env.Bit('pp'):
    # Our pretty printer
    if targets:
      cmd_name = GetPrintableCommandName(cmd)
      env_name = env.GetPrintableEnvironmentName()
      sys.stdout.write('[%s] [%s] %s\n' % (cmd_name, env_name,
                                           targets[0].get_path()))
  else:
    # The SCons default (copied from print_cmd_line in Action.py)
    sys.stdout.write(cmd + u'\n')

if 'generate_ninja' not in ARGUMENTS:
  pre_base_env.Append(PRINT_CMD_LINE_FUNC=CustomCommandPrinter)


def GetAbsDirArg(env, argument, target):
  """Fetch the named command-line argument and turn it into an absolute
directory name.  If the argument is missing, raise a UserError saying
that the given target requires that argument be given."""
  dir = ARGUMENTS.get(argument)
  if not dir:
    raise UserError('%s must be set when invoking %s' % (argument, target))
  return os.path.join(env.Dir('$MAIN_DIR').abspath, dir)

pre_base_env.AddMethod(GetAbsDirArg)


def MakeGTestEnv(env):
  # Create an environment to run unit tests using Gtest.
  gtest_env = env.Clone()

  if gtest_env['NACL_BUILD_FAMILY'] != 'TRUSTED' or not gtest_env.Bit('mac'):
    # This became necessary for the arm cross TC v4.6 but probable applies
    # to all new gcc TCs.  MacOS does not have this switch.
    gtest_env.Append(LINKFLAGS=['-pthread'])

  # Define compile-time flag that communicates that we are compiling in the test
  # environment (rather than for the TCB).
  if gtest_env['NACL_BUILD_FAMILY'] == 'TRUSTED':
    gtest_env.Append(CCFLAGS=['-DNACL_TRUSTED_BUT_NOT_TCB'])

  # This is necessary for unittest_main.c which includes gtest/gtest.h
  # The problem is that gtest.h includes other files expecting the
  # include path to be set.
  gtest_env.Prepend(CPPPATH=['${SOURCE_ROOT}/testing/gtest/include'])

  # gtest does not compile with our stringent settings.
  if gtest_env.Bit('linux') or gtest_env.Bit('mac'):
    # "-pedantic" is because of: gtest-typed-test.h:236:46: error:
    # anonymous variadic macros were introduced in C99
    # Also, gtest does not compile successfully with "-Wundef".
    gtest_env.FilterOut(CCFLAGS=['-pedantic', '-Wundef'])
  gtest_env.FilterOut(CXXFLAGS=['-fno-rtti', '-Weffc++'])

  # gtest is incompatible with static linking due to obscure libstdc++
  # linking interactions.
  # See http://code.google.com/p/nativeclient/issues/detail?id=1987
  gtest_env.FilterOut(LINKFLAGS=['-static'])

  gtest_env.Prepend(LIBS=['gtest'])
  return gtest_env

pre_base_env.AddMethod(MakeGTestEnv)

def MakeUntrustedNativeEnv(env):
  native_env = nacl_env.Clone()
  if native_env.Bit('bitcode') and not native_env.Bit('build_mips32'):
    native_env = native_env.PNaClGetNNaClEnv()
  return native_env

pre_base_env.AddMethod(MakeUntrustedNativeEnv)

def MakeBaseTrustedEnv(platform=None):
  base_env = MakeArchSpecificEnv(platform)
  base_env.Append(
    IS_BUILD_ENV = False,
    BUILD_SUBTYPE = '',
    CPPPATH = [
      '${SOURCE_ROOT}',
    ],

    EXTRA_CFLAGS = [],
    EXTRA_CXXFLAGS = [],
    EXTRA_LIBS = [],
    CFLAGS = ['${EXTRA_CFLAGS}'],
    CXXFLAGS = ['${EXTRA_CXXFLAGS}'],
  )
  if base_env.Bit('ncval_testing'):
    base_env.Append(CPPDEFINES = ['NCVAL_TESTING'])

  base_env.Append(BUILD_SCONSCRIPTS = [
      # KEEP THIS SORTED PLEASE
      'build/package_version/build.scons',
      'pynacl/build.scons',
      'src/nonsfi/irt/build.scons',
      'src/nonsfi/loader/build.scons',
      'src/shared/gio/build.scons',
      'src/shared/imc/build.scons',
      'src/shared/platform/build.scons',
      'src/third_party/gtest/build.scons',
      'src/trusted/cpu_features/build.scons',
      'src/trusted/debug_stub/build.scons',
      'src/trusted/desc/build.scons',
      'src/trusted/fault_injection/build.scons',
      'src/trusted/interval_multiset/build.scons',
      'src/trusted/nacl_base/build.scons',
      'src/trusted/perf_counter/build.scons',
      'src/trusted/platform_qualify/build.scons',
      'src/trusted/seccomp_bpf/build.scons',
      'src/trusted/service_runtime/build.scons',
      'src/trusted/validator/build.scons',
      'src/trusted/validator/driver/build.scons',
      'src/trusted/validator_arm/build.scons',
      'src/trusted/validator_ragel/build.scons',
      'src/trusted/validator_x86/build.scons',
      'tests/common/build.scons',
      'tests/lock_manager/build.scons',
      'tests/performance/build.scons',
      'tests/python_version/build.scons',
      'tests/sel_ldr_seccomp/build.scons',
      'tests/tools/build.scons',
      'tests/unittests/shared/imc/build.scons',
      'tests/unittests/shared/platform/build.scons',
      'tests/unittests/trusted/asan/build.scons',
      'tests/unittests/trusted/bits/build.scons',
      'tests/unittests/trusted/platform_qualify/build.scons',
      'tests/unittests/trusted/service_runtime/build.scons',
      'toolchain_build/build.scons',
  ])

  base_env.AddMethod(SDKInstallBin)

  # The ARM and MIPS validators can be built for any target that doesn't use
  # ELFCLASS64.
  if not base_env.Bit('build_x86_64'):
    base_env.Append(
        BUILD_SCONSCRIPTS = [
          'src/trusted/validator_mips/build.scons',
        ])

  base_env.AddChromeFilesFromGroup('trusted_scons_files')

  base_env.Replace(
      NACL_BUILD_FAMILY = 'TRUSTED',
  )

  # Add optional scons files if present in the directory tree.
  if os.path.exists(pre_base_env.subst('${MAIN_DIR}/supplement/build.scons')):
    base_env.Append(BUILD_SCONSCRIPTS=['${MAIN_DIR}/supplement/build.scons'])

  return base_env


# Select tests to run under coverage build.
pre_base_env['COVERAGE_TARGETS'] = [
    'small_tests', 'medium_tests', 'large_tests',
    'chrome_browser_tests']


pre_base_env.Help("""\
======================================================================
Help for NaCl
======================================================================

Common tasks:
-------------

* cleaning:           scons -c
* building:           scons
* smoke test:         scons --mode=nacl,opt-linux -k pp=1 smoke_tests

* sel_ldr:            scons --mode=opt-linux sel_ldr

Targets to build trusted code destined for the SDK:
* build trusted-code tools:     scons build_bin
* install trusted-code tools:   scons install_bin bindir=...
* These default to opt build, or add --mode=dbg-host for debug build.

Targets to build untrusted code destined for the SDK:
* build just libraries:         scons build_lib
* install just headers:         scons install_headers includedir=...
* install just libraries:       scons install_lib libdir=...
* install headers and libraries:scons install includedir=... libdir=...

* dump system info:   scons --mode=nacl,opt-linux dummy

Options:
--------

--prebuilt          Do not build things, just do install steps

--verbose           Full command line logging before command execution

pp=1                Use command line pretty printing (more concise output)

sysinfo=1           Verbose system info printing

naclsdk_validate=0  Suppress presence check of sdk



Automagically generated help:
-----------------------------
""")


def SetUpClang(env):
  env['CLANG_DIR'] = '${SOURCE_ROOT}/third_party/llvm-build/Release+Asserts/bin'
  env['CLANG_OPTS'] = []
  if env.Bit('asan'):
    if not (env.Bit('host_linux') or env.Bit('host_mac')):
      raise UserError("ERROR: ASan is only available for Linux and Mac")
    env.Append(CLANG_OPTS=['-fsanitize=address',
                           '-gline-tables-only',
                           '-fno-omit-frame-pointer',
                           '-DADDRESS_SANITIZER'])
    if env.Bit('host_mac'):
      # The built executables will try to find this library at runtime
      # in the directory containing the executable itself.  In the
      # Chromium build, the library just gets copied into that
      # directory.  Here, there isn't a single directory from which
      # all the test binaries are run (sel_ldr is run from staging/
      # but other trusted test binaries are run from their respective
      # obj/.../ directories).  So instead just point the dynamic linker
      # at the right directory using an environment variable.
      # Be sure to check and update clang_lib_version whenever updating
      # tools/clang revs in DEPS.
      clang_lib_version = '4.0.0'
      clang_lib_dir = str(env.Dir('${CLANG_DIR}/../lib/clang/%s/lib/darwin' %
                                  clang_lib_version).abspath)
      env['ENV']['DYLD_LIBRARY_PATH'] = clang_lib_dir
      if 'PROPAGATE_ENV' not in env:
        env['PROPAGATE_ENV'] = []
      env['PROPAGATE_ENV'].append('DYLD_LIBRARY_PATH')

  if env.Bit('msan'):
    if not env.Bit('host_linux') or not env.Bit('build_x86_64'):
      raise UserError('ERROR: MSan is only available for x86-64 Linux')
    track_origins = ARGUMENTS.get('msan_track_origins', '1')
    env.Append(CLANG_OPTS=['-fsanitize=memory',
                           '-fsanitize-memory-track-origins=%s' % track_origins,
                           '-gline-tables-only',
                           '-fno-omit-frame-pointer',
                           '-DMEMORY_SANITIZER'])

  env['CC'] = '${CLANG_DIR}/clang ${CLANG_OPTS}'
  env['CXX'] = '${CLANG_DIR}/clang++ ${CLANG_OPTS}'
  # Make sure we find Clang-supplied libraries like -lprofile_rt
  # in the Clang build we use, rather than from the system.
  # The system-installed versions go with the system-installed Clang
  # and might not be compatible with the Clang we're running.
  env.Append(LIBPATH=['${CLANG_DIR}/../lib'])

def GenerateOptimizationLevels(env):
  if env.Bit('clang'):
    SetUpClang(env)

  # Generate debug variant.
  debug_env = env.Clone(tools = ['target_debug'])
  debug_env['OPTIMIZATION_LEVEL'] = 'dbg'
  debug_env['BUILD_TYPE'] = debug_env.subst('$BUILD_TYPE')
  debug_env['BUILD_DESCRIPTION'] = debug_env.subst('$BUILD_DESCRIPTION')
  AddDualLibrary(debug_env)
  # Add to the list of fully described environments.
  environment_list.append(debug_env)

  # Generate opt variant.
  opt_env = env.Clone(tools = ['target_optimized'])
  opt_env['OPTIMIZATION_LEVEL'] = 'opt'
  opt_env['BUILD_TYPE'] = opt_env.subst('$BUILD_TYPE')
  opt_env['BUILD_DESCRIPTION'] = opt_env.subst('$BUILD_DESCRIPTION')
  AddDualLibrary(opt_env)
  # Add to the list of fully described environments.
  environment_list.append(opt_env)

  return (debug_env, opt_env)


def SDKInstallBin(env, name, node, target=None):
  """Add the given node to the build_bin and install_bin targets.
It will be installed under the given name with the build target appended.
The optional target argument overrides the setting of what that target is."""
  env.Alias('build_bin', node)
  if 'install_bin' in COMMAND_LINE_TARGETS:
    dir = env.GetAbsDirArg('bindir', 'install_bin')
    if target is None:
      target = env['TARGET_FULLARCH'].replace('-', '_')
    file_name, file_ext = os.path.splitext(name)
    output_name = file_name + '_' + target + file_ext
    install_node = env.InstallAs(os.path.join(dir, output_name), node)
    env.Alias('install_bin', install_node)


def MakeWindowsEnv(platform=None):
  base_env = MakeBaseTrustedEnv(platform)
  windows_env = base_env.Clone(
      BUILD_TYPE = '${OPTIMIZATION_LEVEL}-win',
      BUILD_TYPE_DESCRIPTION = 'Windows ${OPTIMIZATION_LEVEL} build',
      tools = ['target_platform_windows'],
      # Windows /SAFESEH linking requires either an .sxdata section be
      # present or that @feat.00 be defined as a local, absolute symbol
      # with an odd value.
      ASCOM = ('$ASPPCOM /E /D__ASSEMBLER__ | '
               '$WINASM -defsym @feat.00=1 -o $TARGET'),
      PDB = '${TARGET.base}.pdb',
      # Strict doesn't currently work for Windows since some of the system
      # libraries like wsock32 are magical.
      LIBS_STRICT = False,
      TARGET_ARCH='x86_64' if base_env.Bit('build_x86_64') else 'x86',
  )

  windows_env.Append(
      CPPDEFINES = [
          ['_WIN32_WINNT', '0x0501'],
          ['__STDC_LIMIT_MACROS', '1'],
          ['NOMINMAX', '1'],
          # WIN32 is used by ppapi
          ['WIN32', '1'],
          # WIN32_LEAN_AND_MEAN tells windows.h to omit obsolete and rarely
          # used #include files. This allows use of Winsock 2.0 which otherwise
          # would conflict with Winsock 1.x included by windows.h.
          ['WIN32_LEAN_AND_MEAN', ''],
      ],
      LIBS = ['ws2_32', 'advapi32'],
      # TODO(bsy) remove 4355 once cross-repo
      # NACL_ALLOW_THIS_IN_INITIALIZER_LIST changes go in.
      CCFLAGS = ['/EHsc', '/WX', '/wd4355', '/wd4800']
  )

  # This linker option allows us to ensure our builds are compatible with
  # Chromium, which uses it.
  if windows_env.Bit('build_x86_32'):
    windows_env.Append(LINKFLAGS = "/safeseh")

  # We use the GNU assembler (gas) on Windows so that we can use the
  # same .S assembly files on all platforms.  Microsoft's assembler uses
  # a completely different syntax for x86 code.
  if windows_env.Bit('build_x86_64'):
    # This assembler only works for x86-64 code.
    windows_env['WINASM'] = \
        windows_env.File('$SOURCE_ROOT/third_party/mingw-w64/mingw/bin/'
                         'x86_64-w64-mingw32-as.exe').abspath
  else:
    # This assembler only works for x86-32 code.
    windows_env['WINASM'] = \
        windows_env.File('$SOURCE_ROOT/third_party/gnu_binutils/files/'
                         'as').abspath
  return windows_env

(windows_debug_env,
 windows_optimized_env) = GenerateOptimizationLevels(MakeWindowsEnv())

def MakeUnixLikeEnv(platform=None):
  unix_like_env = MakeBaseTrustedEnv(platform)
  # -Wdeclaration-after-statement is desirable because MS studio does
  # not allow declarations after statements in a block, and since much
  # of our code is portable and primarily initially tested on Linux,
  # it'd be nice to get the build error earlier rather than later
  # (building and testing on Linux is faster).
  # TODO(nfullagar): should we consider switching to -std=c99 ?
  unix_like_env.Prepend(
    CFLAGS = [
        '-std=gnu99',
        '-Wdeclaration-after-statement',
        # Require defining functions as "foo(void)" rather than
        # "foo()" because, in C (but not C++), the latter defines a
        # function with unspecified arguments rather than no
        # arguments.
        '-Wstrict-prototypes',
        ],
    CCFLAGS = [
        # '-malign-double',
        '-Wall',
        '-pedantic',
        '-Wextra',
        '-Wno-long-long',
        '-Wswitch-enum',
        '-Wsign-compare',
        '-Wundef',
        '-fdiagnostics-show-option',
        '-fvisibility=hidden',
        '-fstack-protector',
        ] + werror_flags,
    # NOTE: pthread is only neeeded for libppNaClPlugin.so and on arm
    LIBS = ['pthread'],
    CPPDEFINES = [['__STDC_LIMIT_MACROS', '1'],
                  ['__STDC_FORMAT_MACROS', '1'],
                  ],
  )
  # Android's stlport uses __STRICT_ANSI__ to exclude "long long".
  # This breaks basically all C++ code that uses stlport.
  if not unix_like_env.Bit('android'):
    unix_like_env.Prepend(CXXFLAGS=['-std=c++98'])

  if not unix_like_env.Bit('clang'):
    unix_like_env.Append(CCFLAGS=['--param', 'ssp-buffer-size=4'])

  if unix_like_env.Bit('werror'):
    unix_like_env.Append(LINKFLAGS=['-Werror'])

  return unix_like_env


def MakeMacEnv(platform=None):
  mac_env = MakeUnixLikeEnv(platform).Clone(
      BUILD_TYPE = '${OPTIMIZATION_LEVEL}-mac',
      BUILD_TYPE_DESCRIPTION = 'MacOS ${OPTIMIZATION_LEVEL} build',
      tools = ['target_platform_mac'],
      # TODO(bradnelson): this should really be able to live in unix_like_env
      #                   but can't due to what the target_platform_x module is
      #                   doing.
      LINK = '$CXX',
      PLUGIN_SUFFIX = '.bundle',
  )
  # On Mac, only the newer clang toolchains can parse some of the trusted
  # code's assembly syntax, so turn clang on by default.
  mac_env.SetBits('clang')

  # For no good reason, this all gets instantiated on every platform,
  # and then only actually used on Mac.  But the find_sdk.py script
  # will barf if run on a non-Mac.
  if pynacl.platform.IsMac():
    # mac_sdk_min must be kept in synch with mac_sdk_min in
    # chromium/src/build/config/mac/mac_sdk.gni.
    mac_sdk_min = '10.10'
    # Find the Mac SDK to use as sysroot.
    # This invocation matches the model in //build/config/mac/mac_sdk.gni.
    mac_sdk_sysroot, mac_sdk_version = subprocess.check_output([
        sys.executable,
        os.path.join(os.path.pardir, 'build', 'mac', 'find_sdk.py'),
        '--print_sdk_path',
        mac_sdk_min
        ]).splitlines()
  else:
    mac_sdk_sysroot = 'ThisIsNotAMac'

  # This should be kept in synch with mac_deployment_target
  # in build/common.gypi, which in turn should be kept in synch
  # with chromium/src/build/common.gypi.
  mac_deployment_target = '10.6'

  sdk_flags = ['-isysroot', mac_sdk_sysroot,
               '-mmacosx-version-min=' + mac_deployment_target]
  mac_env.Append(CCFLAGS=sdk_flags, ASFLAGS=sdk_flags, LINKFLAGS=sdk_flags)

  subarch_flag = '-m%s' % mac_env['BUILD_SUBARCH']
  mac_env.Append(
      # '-Wno-gnu' is required for the statement expression defining dirfd
      # for OSX -- otherwise, a warning is generated.
      CCFLAGS=[subarch_flag, '-fPIC', '-Wno-gnu'],
      ASFLAGS=[subarch_flag],
      LINKFLAGS=[subarch_flag, '-fPIC'],
      CPPDEFINES = [# defining _DARWIN_C_SOURCE breaks 10.4
                    #['_DARWIN_C_SOURCE', '1'],
                    #['__STDC_LIMIT_MACROS', '1']
                    ],
  )

  return mac_env

(mac_debug_env, mac_optimized_env) = GenerateOptimizationLevels(MakeMacEnv())


def which(cmd, paths=os.environ.get('PATH', '').split(os.pathsep)):
  for p in paths:
     if os.access(os.path.join(p, cmd), os.X_OK):
       return True
  return False


def SetUpLinuxEnvArm(env):
  jail = env.GetToolchainDir(toolchain_name='arm_trusted')
  if not platform.machine().startswith('arm'):
    # Allow emulation on non-ARM hosts.
    env.Replace(EMULATOR=jail + '/run_under_qemu_arm')
  if env.Bit('built_elsewhere'):
    def FakeInstall(dest, source, env):
      print 'Not installing', dest
      # Replace build commands with no-ops
    env.Replace(CC='true', CXX='true', LD='true',
                AR='true', RANLIB='true', INSTALL=FakeInstall)
  else:
    env.Replace(CC='arm-linux-gnueabihf-gcc',
                CXX='arm-linux-gnueabihf-g++',
                LD='arm-linux-gnueabihf-ld',
                ASFLAGS=[],
                # The -rpath-link argument is needed on Ubuntu/Precise to
                # avoid linker warnings about missing ld.linux.so.3.
                # TODO(sbc): remove this once we stop supporting Precise
                # as a build environment.
                LINKFLAGS=['-Wl,-rpath-link=' + jail +
                           '/lib/arm-linux-gnueabihf']
                )
    # Note we let the compiler choose whether it's -marm or -mthumb by
    # default.  The hope is this will have the best chance of testing
    # the prevailing compilation mode used for Chromium et al.
    env.Prepend(CCFLAGS=['-march=armv7-a'])

  # get_plugin_dirname.cc has a dependency on dladdr
  env.Append(LIBS=['dl'])

def SetUpAndroidEnv(env):
  env.FilterOut(CPPDEFINES=[['_LARGEFILE64_SOURCE', '1']])
  android_ndk_root = os.path.join('${SOURCE_ROOT}', 'third_party',
                                  'android_tools', 'ndk')
  android_ndk_experimental_root = os.path.join('${SOURCE_ROOT}',
                                               'third_party', 'android_tools',
                                               'ndk_experimental')
  android_sdk_root = os.path.join('${SOURCE_ROOT}', 'third_party',
                                  'android_tools', 'sdk')
  android_sdk_version = 21
  android_stlport_root = os.path.join(android_ndk_root, 'sources', 'cxx-stl',
                                      'stlport')
  ndk_host_os_map = {
      pynacl.platform.OS_WIN : 'win',
      pynacl.platform.OS_MAC: 'darwin',
      pynacl.platform.OS_LINUX : 'linux'
      }
  host_os = ndk_host_os_map[pynacl.platform.GetOS()]
  android_sdk = os.path.join(android_sdk_root, 'platforms',
                             'android-%s' % android_sdk_version)
  arch_cflags = []
  if env.Bit('build_arm'):
    android_ndk_target_prefix = 'arm-linux-androideabi'
    android_ndk_version = '4.8'
    android_app_abi = 'armeabi-v7a'
    android_ndk_sysroot = os.path.join(android_ndk_root, 'platforms',
                                       'android-14', 'arch-arm')
    android_ndk_lib_dir = os.path.join('usr', 'lib')
    android_toolchain = os.path.join(android_ndk_root, 'toolchains',
                                     'arm-linux-androideabi-4.8', 'prebuilt',
                                     '%s-x86_64' % host_os, 'bin')
    arch_cflags += ['-march=armv7-a', '-mfloat-abi=softfp']
  elif env.Bit('build_x86_32'):
    android_ndk_target_prefix = 'i686-linux-android'
    android_ndk_version = '4.8'
    android_app_abi = 'x86'
    android_ndk_sysroot = os.path.join(android_ndk_root, 'platforms',
                                       'android-14', 'arch-x86')
    android_ndk_lib_dir = os.path.join('usr', 'lib')
    android_toolchain = os.path.join(android_ndk_root, 'toolchains',
                                     'x86-4.8', 'prebuilt',
                                     '%s-x86_64' % host_os, 'bin')
    arch_cflags += ['-m32', '-msse2']
  # TODO(sehr): add other android architecture platform settings here.
  android_ndk_include = os.path.join(android_ndk_sysroot, 'usr', 'include')
  android_ndk_lib = os.path.join(android_ndk_sysroot, android_ndk_lib_dir)
  android_sdk_jar = os.path.join(android_sdk, 'android.jar')
  android_stlport_include = os.path.join(android_stlport_root, 'stlport')
  android_stlport_libs_dir = os.path.join(android_stlport_root, 'libs',
                                          android_app_abi)
  android_ndk_libgcc_path = os.path.join(android_toolchain, '..', 'lib', 'gcc',
                                         android_ndk_target_prefix,
                                         android_ndk_version)
  env.Replace(CC=os.path.join(android_toolchain,
                              '%s-gcc' % android_ndk_target_prefix),
              CXX=os.path.join(android_toolchain,
                              '%s-g++' % android_ndk_target_prefix),
              LD=os.path.join(android_toolchain,
                              '%s-g++' % android_ndk_target_prefix),
              AR=os.path.join(android_toolchain,
                              '%s-ar' % android_ndk_target_prefix),
              RANLIB=os.path.join(android_toolchain,
                                  '%s-ranlib' % android_ndk_target_prefix),
              READELF=os.path.join(android_toolchain,
                                   '%s-readelf' % android_ndk_target_prefix),
              STRIP=os.path.join(android_toolchain,
                                 '%s-strip' % android_ndk_target_prefix),
              EMULATOR=os.path.join(android_sdk_root, 'tools', 'emulator'),
              LIBPATH=['${LIB_DIR}',
                       android_ndk_lib,
                       android_ndk_libgcc_path,
                       os.path.join(android_stlport_root, 'libs',
                                    android_app_abi),
                       ],
              LIBS=['stlport_shared',
                    'gcc',
                    'c',
                    'dl',
                    'm',
                    ],
              )
  # SHLINKFLAGS should not inherit options from LINKFLAGS.
  env.FilterOut(SHLINKFLAGS=['$LINKFLAGS'])
  env.Append(CCFLAGS=['--sysroot=' + android_ndk_sysroot,
                      '-isystem=' + os.path.join(android_ndk_sysroot, 'usr',
                                                 'include'),
                      '-I%s' % android_stlport_include,
                      '-ffunction-sections',
                      '-g',
                      '-fstack-protector',
                      '-fno-short-enums',
                      '-finline-limit=64',
                      '-Wa,--noexecstack',
                      '-DANDROID',
                      '-D__ANDROID__',
                      # Due to bogus warnings on uintptr_t formats.
                      '-Wno-format',
                      ] + arch_cflags,
             CXXFLAGS=['-I%s' % android_stlport_include,
                       '-I%s' % android_ndk_include,
                       '-fno-exceptions',
                       ],
             LINKFLAGS=['--sysroot=' + android_ndk_sysroot,
                        '-nostdlib',
                        '-Wl,--no-undefined',
                        # Don't export symbols from statically linked libraries.
                        '-Wl,--exclude-libs=ALL',
                        # crtbegin_dynamic.o should be the last item in ldflags.
                        os.path.join(android_ndk_lib, 'crtbegin_dynamic.o'),
                        ],
             LINKCOM=' $ANDROID_EXTRA_LIBS',
             ANDROID_EXTRA_LIBS=os.path.join(android_ndk_lib,
                                             'crtend_android.o'),
             SHLINKFLAGS=['--sysroot=' + android_ndk_sysroot,
                          '-nostdlib',
                          # crtbegin_so.o should be the last item in ldflags.
                          os.path.join(android_ndk_lib, 'crtbegin_so.o'),
                          ],
             SHLINKCOM=' $ANDROID_EXTRA_SHLIBS',
             ANDROID_EXTRA_SHLIBS=os.path.join(android_ndk_lib,
                                               'crtend_so.o'),
             )
  return env

def SetUpLinuxEnvMips(env):
  jail = env.GetToolchainDir(toolchain_name='mips_trusted')
  if not platform.machine().startswith('mips'):
    # Allow emulation on non-MIPS hosts.
    env.Replace(EMULATOR=jail + '/run_under_qemu_mips32')
  if env.Bit('built_elsewhere'):
    def FakeInstall(dest, source, env):
      print 'Not installing', dest
      # Replace build commands with no-ops
    env.Replace(CC='true', CXX='true', LD='true',
                AR='true', RANLIB='true', INSTALL=FakeInstall)
  else:
    tc_dir = os.path.join(jail, 'bin')
    if not which(os.path.join(tc_dir, 'mipsel-linux-gnu-gcc')):
      print ("WARNING: "
          "MIPS trusted toolchain not found - try running:\n"
          "  build/package_version/package_version.py --packages"
          " linux_x86/mips_trusted sync -x\n"
          "Or build it yourself with:\n"
          "  tools/trusted_cross_toolchains/trusted-toolchain-creator"
          ".mipsel.debian.sh nacl_sdk")
    env.Replace(CC=os.path.join(tc_dir, 'mipsel-linux-gnu-gcc'),
                CXX=os.path.join(tc_dir, 'mipsel-linux-gnu-g++'),
                LD=os.path.join(tc_dir, 'mipsel-linux-gnu-ld'),
                ASFLAGS=[],
                LIBPATH=['${LIB_DIR}',
                         jail + '/sysroot/usr/lib']
                )

    env.Append(LIBS=['rt', 'dl', 'pthread'],
                     CCFLAGS=['-march=mips32r2'])

# Makes a generic Linux development environment.
# Linux development environments are used in two different ways.
# 1) To produce trusted tools (e.g., sel_ldr), called TRUSTED_ENV
# 2) To produce build tools (e.g., tls_edit), called BUILD_ENV
def MakeGenericLinuxEnv(platform=None):
  linux_env = MakeUnixLikeEnv(platform).Clone(
      BUILD_TYPE = '${OPTIMIZATION_LEVEL}-linux',
      BUILD_TYPE_DESCRIPTION = 'Linux ${OPTIMIZATION_LEVEL} build',
      tools = ['target_platform_linux'],
      # TODO(bradnelson): this should really be able to live in unix_like_env
      #                   but can't due to what the target_platform_x module is
      #                   doing.
      LINK = '$CXX',
  )

  # Prepend so we can disable warnings via Append
  linux_env.Prepend(
      CPPDEFINES = [['_POSIX_C_SOURCE', '199506'],
                    ['_XOPEN_SOURCE', '600'],
                    ['_GNU_SOURCE', '1'],
                    ['_LARGEFILE64_SOURCE', '1'],
                    ],
      LIBS = ['rt'],
      )

  if linux_env.Bit('build_x86_32'):
    linux_env.Prepend(
        CCFLAGS = ['-m32'],
        LINKFLAGS = ['-m32'],
        )
  elif linux_env.Bit('build_x86_64'):
    linux_env.Prepend(
        CCFLAGS = ['-m64'],
        LINKFLAGS = ['-m64'],
        )
  elif linux_env.Bit('build_arm'):
    SetUpLinuxEnvArm(linux_env)
  elif linux_env.Bit('build_mips32'):
    SetUpLinuxEnvMips(linux_env)
  else:
    Banner('Strange platform: %s' % GetTargetPlatform())

  # These are desireable options for every Linux platform:
  # _FORTIFY_SOURCE: general paranoia "hardening" option for library functions
  # -fPIE/-pie: create a position-independent executable
  # relro/now: "hardening" options for linking
  # noexecstack: ensure that the executable does not get a PT_GNU_STACK
  #              header that causes the kernel to set the READ_IMPLIES_EXEC
  #              personality flag, which disables NX page protection.
  linux_env.Prepend(CPPDEFINES=[['-D_FORTIFY_SOURCE', '2']])
  # By default SHLINKFLAGS uses $LINKFLAGS, but we do not want -pie
  # in $SHLINKFLAGS, only in $LINKFLAGS.  So move LINKFLAGS over to
  # COMMON_LINKFLAGS, and add the "hardening" options there.  Then
  # make both LINKFLAGS and SHLINKFLAGS refer to that, and add -pie
  # only to LINKFLAGS.
  linux_env.Replace(COMMON_LINKFLAGS=linux_env['LINKFLAGS'],
                    LINKFLAGS=['$COMMON_LINKFLAGS'])
  linux_env.FilterOut(SHLINKFLAGS=['$LINKFLAGS'])
  linux_env.Prepend(SHLINKFLAGS=['$COMMON_LINKFLAGS'])
  linux_env.Prepend(COMMON_LINKFLAGS=['-Wl,-z,relro',
                                      '-Wl,-z,now',
                                      '-Wl,-z,noexecstack'])
  linux_env.Prepend(LINKFLAGS=['-pie'])
  # The ARM toolchain has a linker that doesn't handle the code its
  # compiler generates under -fPIE.
  if linux_env.Bit('build_arm') or linux_env.Bit('build_mips32'):
    linux_env.Prepend(CCFLAGS=['-fPIC'])
    # TODO(mcgrathr): Temporarily punt _FORTIFY_SOURCE for ARM because
    # it causes a libc dependency newer than the old bots have installed.
    linux_env.FilterOut(CPPDEFINES=[['-D_FORTIFY_SOURCE', '2']])
  else:
    linux_env.Prepend(CCFLAGS=['-fPIE'])

  # We always want to use the same flags for .S as for .c because
  # code-generation flags affect the predefines we might test there.
  linux_env.Replace(ASFLAGS=['${CCFLAGS}'])

  return linux_env

# Specializes a generic Linux development environment to be a trusted
# environment.
def MakeTrustedLinuxEnv(platform=None):
  linux_env = MakeGenericLinuxEnv(platform)
  if linux_env.Bit('android'):
    SetUpAndroidEnv(linux_env)
  return linux_env

(linux_debug_env, linux_optimized_env) = \
    GenerateOptimizationLevels(MakeTrustedLinuxEnv())


def BiasedBitcodeFlags(env):
  """ Return clang flags to use biased bitcode and generate native-ABI-compliant
      code. Does not imply pre-translation.
  """
  if env.Bit('build_x86_32'):
    return ['--target=i686-unknown-nacl']
  if env.Bit('build_x86_64'):
    return ['--target=x86_64-unknown-nacl']
  if env.Bit('build_arm'):
    return ['--target=armv7-unknown-nacl-gnueabihf', '-mfloat-abi=hard']
  if env.Bit('build_mips32'):
    return []
  raise UserError('No known target bits set')

pre_base_env.AddMethod(BiasedBitcodeFlags)

# Do this before the site_scons/site_tools/naclsdk.py stuff to pass it along.
pre_base_env.Append(
    PNACL_BCLDFLAGS = ARGUMENTS.get('pnacl_bcldflags', '').split(':'))


# The nacl_env is used to build native_client modules
# using a special tool chain which produces platform
# independent binaries
# NOTE: this loads stuff from: site_scons/site_tools/naclsdk.py
nacl_env = MakeArchSpecificEnv()
# See comment below about libc++ and libpthread in NONIRT_LIBS.
using_nacl_libcxx = nacl_env.Bit('bitcode') or nacl_env.Bit('nacl_clang')
nacl_env = nacl_env.Clone(
    tools = ['naclsdk'],
    NACL_BUILD_FAMILY = 'UNTRUSTED',
    BUILD_TYPE = 'nacl',
    BUILD_TYPE_DESCRIPTION = 'NaCl module build',

    ARFLAGS = 'rc',

    # ${SOURCE_ROOT} for #include <ppapi/...>
    CPPPATH = [
      '${SOURCE_ROOT}',
    ],

    EXTRA_CFLAGS = [],
    EXTRA_CXXFLAGS = [],
    EXTRA_LIBS = [],
    EXTRA_LINKFLAGS = ARGUMENTS.get('nacl_linkflags', '').split(':'),

    # always optimize binaries
    CCFLAGS = ['-O2',
               '-g',
               '-fomit-frame-pointer',
               # This makes sure unwind/backtrace info is available for
               # all code locations.  Note build/untrusted.gypi uses it too.
               '-fasynchronous-unwind-tables',
               '-Wall',
               '-Wundef',
               '-fdiagnostics-show-option',
               '-pedantic',
               ] +
              werror_flags,

    CFLAGS = ['-std=gnu99',
              ],
    CXXFLAGS = ['-std=gnu++98',
                '-Wno-long-long',
                ],

    # This magic is copied from scons-2.0.1/engine/SCons/Defaults.py
    # where this pattern is used for _LIBDIRFLAGS, which produces -L
    # switches.  Here we are producing a -Wl,-rpath-link,DIR for each
    # element of LIBPATH, i.e. for each -LDIR produced.
    RPATH_LINK_FLAGS = '$( ${_concat(RPATHLINKPREFIX, LIBPATH, RPATHLINKSUFFIX,'
                       '__env__, RDirs, TARGET, SOURCE)} $)',
    RPATHLINKPREFIX = '-Wl,-rpath-link,',
    RPATHLINKSUFFIX = '',

    LIBS = [],
    LINKFLAGS = ['${RPATH_LINK_FLAGS}'] + (['-Werror'] if nacl_env.Bit('werror')
                                           else []),

    # These are settings for in-tree, non-browser tests to use.
    # They use libraries that circumvent the IRT-based implementations
    # in the public libraries.
    # Note that pthread_private is part of NONIRT_LIBS for clang because
    # libc++ depends on libpthread. However we can't just add
    # libpthread_private to the link line because those libs get added before
    # the standard libs, so the references that come from libc++ itself will
    # still get satisfied from libpthread instead of libpthread_private (and
    # that code will crash because it requires the IRT). So put libc++ on the
    # user link line before libpthread_private to ensure that its references
    # to libpthread also get satisfied by libpthread_private.
    # TODO(dschuff): Also remove the hack in pnacl-ld and use this for pnacl.
    NONIRT_LIBS = (['nacl_sys_private'] +
                   (['c++','pthread_private'] if using_nacl_libcxx else [])),
    PTHREAD_LIBS = ['pthread_private'],
    DYNCODE_LIBS = ['nacl_dyncode_private'],
    EXCEPTION_LIBS = ['nacl_exception_private'],
    LIST_MAPPINGS_LIBS = ['nacl_list_mappings_private'],
    RANDOM_LIBS = ['nacl_random_private'],
    )

def UsesAbiNote(env):
  """Return True if using a new-style GCC with .note.NaCl.ABI.* notes.
This means there will always be an RODATA segment, even if just for the note."""
  return env.Bit('build_arm') and not env.Bit('bitcode')

nacl_env.AddMethod(UsesAbiNote)

def UnderWindowsCoverage(env):
  """Return True if using running on coverage under windows."""
  if 'TRUSTED_ENV' not in env:
    return False
  return env['TRUSTED_ENV'].Bit('coverage_enabled') and env.Bit('host_windows')

nacl_env.AddMethod(UnderWindowsCoverage)

def SetNonStableBitcodeIfAllowed(env, allow_sb_translator=False):
  """ This modifies the environment to allow features that aren't part
      of PNaCl's stable ABI.  If tests using these features should be
      skipped entirely, this returns False.  Otherwise, on success, it
      returns True.
  """
  if env.Bit('bitcode') and env.Bit('skip_nonstable_bitcode'):
    return False
  # The PNaCl sandboxed translator (for the most part) only accepts stable
  # bitcode, so in most cases we skip building non-stable tests.
  # However, there are some limited cases like debug information which
  # we support but do not guarantee stability. Tests targeting such cases
  # can opt-in to testing w/ allow_sb_translator=True.
  if env.Bit('use_sandboxed_translator') and not allow_sb_translator:
    return False
  # Change environment to skip finalization step.
  env.SetBits('nonstable_bitcode')
  return True

nacl_env.AddMethod(SetNonStableBitcodeIfAllowed)


def AllowInlineAssembly(env):
  """ This modifies the environment to allow inline assembly in
      untrusted code.  If the environment cannot be modified to allow
      inline assembly, it returns False.  Otherwise, on success, it
      returns True.
  """
  if env.Bit('bitcode'):
    # For each architecture, we only attempt to make our inline
    # assembly code work with one untrusted-code toolchain.  For x86,
    # we target GCC, but not PNaCl/Clang, because the latter's
    # assembly support has various quirks that we don't want to have
    # to debug.  For ARM, we target PNaCl/Clang, because that is the
    # only current ARM toolchain.  One day, we will have an ARM GCC
    # toolchain, and we will no longer need to use inline assembly
    # with PNaCl/Clang at all.
    #
    # For Non-SFI NaCl we use inline assembly in PNaCl/Clang.
    if not (env.Bit('build_arm') or env.Bit('build_mips32')
            or env.Bit('nonsfi_nacl')):
      return False
    # Inline assembly does not work in pexes.
    if env.Bit('pnacl_generate_pexe'):
      return False
    env.AddBiasForPNaCl()
    env.PNaClForceNative()

    if env.Bit('build_x86_32'):
      env.AppendUnique(CCFLAGS=['--target=i686-unknown-nacl'])
    elif env.Bit('build_x86_64'):
      env.AppendUnique(CCFLAGS=['--target=x86_64-unknown-nacl'])
    elif env.Bit('build_arm'):
      env.AppendUnique(CCFLAGS=['--target=armv7a-unknown-nacl-gnueabihf',
                                '-mfloat-abi=hard'])
    # Enable the use of inline assembly.
    env.Append(CCFLAGS=['-fgnu-inline-asm'])
  return True

nacl_env.AddMethod(AllowInlineAssembly)


# TODO(mseaborn): Enable this unconditionally once the C code on the
# Chromium side compiles successfully with this warning.
if not enable_chrome:
  nacl_env.Append(CFLAGS=['-Wstrict-prototypes'])

# This is the address at which a user executable is expected to place its
# data segment in order to be compatible with the integrated runtime (IRT)
# library.  This address should not be changed lightly.
irt_compatible_rodata_addr = 0x10000000
# This is the address at which the IRT's own code will be located.
# It must be below irt_compatible_rodata and leave enough space for
# the code segment of the IRT.  It should be as close as possible to
# irt_compatible_rodata so as to leave the maximum contiguous area
# available for the dynamic code loading area that falls below it.
# This can be adjusted as necessary for the actual size of the IRT code.
irt_code_addr = irt_compatible_rodata_addr - (6 << 20) # max 6M IRT code
# This is the address at which the IRT's own data will be located.  The
# 32-bit sandboxes limit the address space to 1GB; the initial thread's
# stack sits at the top of the address space and extends down for
# NACL_DEFAULT_STACK_MAX (src/trusted/service_runtime/sel_ldr.h) below.
# So this must be below there, and leave enough space for the IRT's own
# data segment.  It should be as high as possible so as to leave the
# maximum contiguous area available for the user's data and break below.
# This can be adjusted as necessary for the actual size of the IRT data
# (that is RODATA, rounded up to 64k, plus writable data).
# 1G (address space) - 16M (NACL_DEFAULT_STACK_MAX) - 1MB (IRT rodata+data)
irt_data_addr = (1 << 30) - (16 << 20) - (1 << 20)

nacl_env.Replace(
    IRT_DATA_REGION_START = '%#.8x' % irt_compatible_rodata_addr,
    # Load addresses of the IRT's code and data segments.
    IRT_BLOB_CODE_START = '%#.8x' % irt_code_addr,
    IRT_BLOB_DATA_START = '%#.8x' % irt_data_addr,
    )

def TestsUsePublicListMappingsLib(env):
  """Use the public list_mappings library for in-tree tests."""
  env.Replace(LIST_MAPPINGS_LIBS=['nacl_list_mappings'])

def TestsUsePublicLibs(env):
  """Change the environment so it uses public libraries for in-tree tests."""
  env.Replace(NONIRT_LIBS=['pthread'] if env.Bit('bitcode') else [],
              PTHREAD_LIBS=['pthread'],
              DYNCODE_LIBS=['nacl_dyncode', 'nacl'],
              EXCEPTION_LIBS=['nacl_exception', 'nacl'],
              RANDOM_LIBS=['nacl'])

# glibc is incompatible with libpthread_private and libnacl_sys_private.
if nacl_env.Bit('nacl_glibc'):
  nacl_env.Replace(NONIRT_LIBS=[],
                   PTHREAD_LIBS=['pthread'])

# These add on to those set in pre_base_env, above.
nacl_env.Append(
    CPPDEFINES = [
        # This ensures that UINT32_MAX gets defined.
        ['__STDC_LIMIT_MACROS', '1'],
        # This ensures that PRId64 etc. get defined.
        ['__STDC_FORMAT_MACROS', '1'],
        # _GNU_SOURCE ensures that strtof() gets declared.
        ['_GNU_SOURCE', 1],
        ['_POSIX_C_SOURCE', '199506'],
        ['_XOPEN_SOURCE', '600'],

        ['DYNAMIC_ANNOTATIONS_ENABLED', '1' ],
        ['DYNAMIC_ANNOTATIONS_PREFIX', 'NACL_' ],

        ['NACL_BUILD_ARCH', '${BUILD_ARCHITECTURE}'],
        ['NACL_BUILD_SUBARCH', '${BUILD_SUBARCH}'],
        ],
    )

def FixWindowsAssembler(env):
  if env.Bit('host_windows'):
    # ASCOM is the rule used by .s files and ASPPCOM is the rule used by .S
    # files; the latter uses the C preprocessor.  This is needed because Windows
    # builds are case-insensitive, so they all appear as .s files.
    env.Replace(ASCOM='${ASPPCOM}')

FixWindowsAssembler(nacl_env)

# Look in the local include and lib directories before the toolchain's.
nacl_env['INCLUDE_DIR'] = '${TARGET_ROOT}/include'
# Remove the default $LIB_DIR element so that we prepend it without duplication.
# Using PrependUnique alone would let it stay last, where we want it first.
nacl_env.FilterOut(LIBPATH=['${LIB_DIR}'])
nacl_env.PrependUnique(
    CPPPATH = ['${INCLUDE_DIR}'],
    LIBPATH = ['${LIB_DIR}'],
    )

if nacl_env.Bit('bitcode'):
  # passing -O when linking requests LTO, which does additional global
  # optimizations at link time
  nacl_env.Append(LINKFLAGS=['-O3'])
  if not nacl_env.Bit('nacl_glibc'):
    nacl_env.Append(LINKFLAGS=['-static'])

  if nacl_env.Bit('translate_fast'):
    nacl_env.Append(LINKFLAGS=['-Xlinker', '-translate-fast'])
    nacl_env.Append(TRANSLATEFLAGS=['-translate-fast'])
  if nacl_env.Bit('use_sz'):
    nacl_env.Append(TRANSLATEFLAGS=['--use-sz'])

  # With pnacl's clang base/ code uses the "override" keyword.
  nacl_env.Append(CXXFLAGS=['-Wno-c++11-extensions'])
  # Allow extraneous semicolons.  (Until these are removed.)
  # http://code.google.com/p/nativeclient/issues/detail?id=2861
  nacl_env.Append(CCFLAGS=['-Wno-extra-semi'])
  # Allow unused private fields.  (Until these are removed.)
  # http://code.google.com/p/nativeclient/issues/detail?id=2861
  nacl_env.Append(CCFLAGS=['-Wno-unused-private-field'])
  # native_client/src/nonsfi/linux/linux_syscall_structs.h uses designated
  # initializers, which causes a warning when included from c++98 code.
  nacl_env.Append(CXXFLAGS=['-Wno-c99-extensions'])

if nacl_env.Bit('nacl_clang'):
  # third_party/valgrind/nacl_valgrind.h uses asm instead of __asm__
  # https://code.google.com/p/nativeclient/issues/detail?id=3974
  # TODO(dschuff): change it to __asm__ and remove this suppression.
  nacl_env.Append(CCFLAGS=['-Wno-language-extension-token'])

# We use a special environment for building the IRT image because it must
# always use the newlib toolchain, regardless of --nacl_glibc.  We clone
# it from nacl_env here, before too much other cruft has been added.
# We do some more magic below to instantiate it the way we need it.
nacl_irt_env = nacl_env.Clone(
    BUILD_TYPE = 'nacl_irt',
    BUILD_TYPE_DESCRIPTION = 'NaCl IRT build',
    NACL_BUILD_FAMILY = 'UNTRUSTED_IRT',
)

# Provide access to the IRT build environment from the default environment
# which is needed when compiling custom IRT for testing purposes.
nacl_env['NACL_IRT_ENV'] = nacl_irt_env

# Since we don't build src/untrusted/pthread/nacl.scons in
# nacl_irt_env, we must tell the IRT how to find the pthread.h header.
nacl_irt_env.Append(CPPPATH='${MAIN_DIR}/src/untrusted/pthread')

# Map certain flag bits to suffices on the build output.  This needs to
# happen pretty early, because it affects any concretized directory names.
target_variant_map = [
    ('bitcode', 'pnacl'),
    ('translate_fast', 'fast'),
    ('use_sz', 'subzero'),
    ('nacl_pic', 'pic'),
    ('use_sandboxed_translator', 'sbtc'),
    ('nacl_glibc', 'glibc'),
    ('pnacl_generate_pexe', 'pexe'),
    ('nonsfi_nacl', 'nonsfi'),
    ]
for variant_bit, variant_suffix in target_variant_map:
  if nacl_env.Bit(variant_bit):
    nacl_env['TARGET_VARIANT'] += '-' + variant_suffix

if nacl_env.Bit('bitcode'):
  nacl_env['TARGET_VARIANT'] += '-clang'

nacl_env.Replace(TESTRUNNER_LIBS=['testrunner'])

# TODO(mseaborn): Make nacl-glibc-based static linking work with just
# "-static", without specifying a linker script.
# See http://code.google.com/p/nativeclient/issues/detail?id=1298
def GetLinkerScriptBaseName(env):
  if env.Bit('build_x86_64'):
    return 'elf_x86_64_nacl'
  else:
    return 'elf_i386_nacl'

if (nacl_env.Bit('nacl_glibc') and
    nacl_env.Bit('nacl_static_link')):
  nacl_env.Append(LINKFLAGS=['-static'])
  if nacl_env.Bit('build_x86'):
    # The "-lc" is necessary because libgcc_eh depends on libc but for
    # some reason nacl-gcc is not linking with "--start-group/--end-group".
    nacl_env.Append(LINKFLAGS=[
        '-T', 'ldscripts/%s.x.static' % GetLinkerScriptBaseName(nacl_env),
        '-lc'])

if nacl_env.Bit('running_on_valgrind'):
  nacl_env.Append(CCFLAGS = ['-g', '-Wno-overlength-strings',
                             '-fno-optimize-sibling-calls'],
                  CPPDEFINES = [['DYNAMIC_ANNOTATIONS_ENABLED', '1' ],
                                ['DYNAMIC_ANNOTATIONS_PREFIX', 'NACL_' ]])
  # With GLibC, libvalgrind.so is preloaded at runtime.
  # With Newlib, it has to be linked in.
  if not nacl_env.Bit('nacl_glibc'):
    nacl_env.Append(LINKFLAGS = ['-Wl,-u,have_nacl_valgrind_interceptors'],
                    LIBS = ['valgrind'])

environment_list.append(nacl_env)

if not nacl_env.Bit('nacl_glibc'):
  # These are all specific to nacl-newlib so we do not include them
  # when building against nacl-glibc.  The functionality of
  # pthread/startup/stubs/nosys is provided by glibc.  The valgrind
  # code currently assumes nc_threads.
  nacl_env.Append(
      BUILD_SCONSCRIPTS = [
        ####  ALPHABETICALLY SORTED ####
        'src/untrusted/elf_loader/nacl.scons',
        'src/untrusted/pthread/nacl.scons',
        'src/untrusted/stubs/nacl.scons',
        'src/untrusted/nosys/nacl.scons',
        ####  ALPHABETICALLY SORTED ####
      ])
nacl_env.Append(
    BUILD_SCONSCRIPTS = [
    ####  ALPHABETICALLY SORTED ####
    'src/nonsfi/irt/build.scons',
    'src/nonsfi/linux/nacl.scons',
    'src/nonsfi/loader/build.scons',
    'src/shared/gio/nacl.scons',
    'src/shared/imc/nacl.scons',
    'src/shared/platform/nacl.scons',
    'src/trusted/service_runtime/nacl.scons',
    'src/trusted/validator/nacl.scons',
    'src/untrusted/irt/nacl_headers.scons',
    'src/untrusted/minidump_generator/nacl.scons',
    'src/untrusted/nacl/nacl.scons',
    'src/untrusted/pll_loader/nacl.scons',
    'src/untrusted/pnacl_dynloader/nacl.scons',
    'src/untrusted/valgrind/nacl.scons',
    ####  ALPHABETICALLY SORTED ####
])
nacl_env.AddChromeFilesFromGroup('untrusted_scons_files')

# These are tests that are worthwhile to run in IRT variant only.
irt_only_tests = [
    #### ALPHABETICALLY SORTED ####
    'tests/elf_loader/nacl.scons',
    'tests/irt/nacl.scons',
    'tests/irt_compatibility/nacl.scons',
    'tests/irt_entry_alignment/nacl.scons',
    'tests/irt_ext/nacl.scons',
    'tests/irt_stack_alignment/nacl.scons',
    'tests/sbrk/nacl.scons',
    'tests/translator_size_limits/nacl.scons',
    ]

# These are tests that are worthwhile to run in both IRT and non-IRT variants.
# The nacl_irt_test mode runs them in the IRT variants.
irt_variant_tests = [
    #### ALPHABETICALLY SORTED ####
    'tests/app_lib/nacl.scons',
    'tests/benchmark/nacl.scons',
    'tests/bigalloc/nacl.scons',
    'tests/callingconv/nacl.scons',
    'tests/callingconv_ppapi/nacl.scons',
    'tests/callingconv_case_by_case/nacl.scons',
    'tests/clock/nacl.scons',
    'tests/common/nacl.scons',
    'tests/compiler_thread_suspension/nacl.scons',
    'tests/computed_gotos/nacl.scons',
    'tests/data_below_data_start/nacl.scons',
    'tests/data_not_executable/nacl.scons',
    'tests/debug_stub/nacl.scons',
    'tests/dup/nacl.scons',
    'tests/dynamic_code_loading/nacl.scons',
    'tests/dynamic_linking/nacl.scons',
    'tests/egyptian_cotton/nacl.scons',
    'tests/environment_variables/nacl.scons',
    'tests/exception_test/nacl.scons',
    'tests/fdopen_test/nacl.scons',
    'tests/file/nacl.scons',
    'tests/futexes/nacl.scons',
    'tests/gc_instrumentation/nacl.scons',
    'tests/gdb/nacl.scons',
    'tests/glibc_file64_test/nacl.scons',
    'tests/glibc_static_test/nacl.scons',
    'tests/glibc_syscall_wrappers/nacl.scons',
    'tests/glibc_socket_wrappers/nacl.scons',
    'tests/hello_world/nacl.scons',
    'tests/imc_shm_mmap/nacl.scons',
    'tests/includability/nacl.scons',
    'tests/infoleak/nacl.scons',
    'tests/libc/nacl.scons',
    'tests/libc_free_hello_world/nacl.scons',
    'tests/limited_file_access/nacl.scons',
    'tests/list_mappings/nacl.scons',
    'tests/longjmp/nacl.scons',
    'tests/loop/nacl.scons',
    'tests/math/nacl.scons',
    'tests/memcheck_test/nacl.scons',
    'tests/mmap/nacl.scons',
    'tests/mmap_main_nexe/nacl.scons',
    'tests/mmap_prot_exec/nacl.scons',
    'tests/mmap_race_protect/nacl.scons',
    'tests/nacl_log/nacl.scons',
    'tests/nanosleep/nacl.scons',
    'tests/nonsfi/nacl.scons',
    'tests/noop/nacl.scons',
    'tests/nrd_xfer/nacl.scons',
    'tests/nthread_nice/nacl.scons',
    'tests/null/nacl.scons',
    'tests/nullptr/nacl.scons',
    'tests/pagesize/nacl.scons',
    'tests/performance/nacl.scons',
    'tests/pnacl_abi/nacl.scons',
    'tests/pnacl_dynamic_loading/nacl.scons',
    'tests/pnacl_native_objects/nacl.scons',
    'tests/random/nacl.scons',
    'tests/redir/nacl.scons',
    'tests/rodata_not_writable/nacl.scons',
    'tests/run_py/nacl.scons',
    'tests/sel_ldr/nacl.scons',
    'tests/sel_ldr_seccomp/nacl.scons',
    'tests/sel_main_chrome/nacl.scons',
    'tests/signal_handler/nacl.scons',
    'tests/simd/nacl.scons',
    'tests/sleep/nacl.scons',
    'tests/stack_alignment/nacl.scons',
    'tests/stubout_mode/nacl.scons',
    'tests/sysbasic/nacl.scons',
    'tests/syscall_return_regs/nacl.scons',
    'tests/syscall_return_sandboxing/nacl.scons',
    'tests/syscalls/nacl.scons',
    'tests/thread_capture/nacl.scons',
    'tests/threads/nacl.scons',
    'tests/time/nacl.scons',
    'tests/tls/nacl.scons',
    'tests/tls_segment_x86_32/nacl.scons',
    'tests/toolchain/nacl.scons',
    'tests/toolchain/arm/nacl.scons',
    'tests/toolchain/mips/nacl.scons',
    'tests/unittests/shared/platform/nacl.scons',
    'tests/untrusted_check/nacl.scons',
    'tests/unwind_restores_regs/nacl.scons',
    'tests/validator/nacl.scons',
    #### ALPHABETICALLY SORTED ####
    # NOTE: The following tests are really IRT-only tests, but they
    # are in this category so that they can generate libraries (which
    # works in nacl_env but not in nacl_irt_test_env) while also
    # adding tests to nacl_irt_test_env.
    'tests/inbrowser_test_runner/nacl.scons',
    'tests/untrusted_minidump/nacl.scons',
]

# These are tests that are NOT worthwhile to run in an IRT variant.
# In some cases, that's because they are browser tests which always
# use the IRT.  In others, it's because they are special-case tests
# that are incompatible with having an IRT loaded.
nonvariant_tests = [
    #### ALPHABETICALLY SORTED ####
    'tests/barebones/nacl.scons',
    'tests/chrome_extension/nacl.scons',
    'tests/custom_desc/nacl.scons',
    'tests/faulted_thread_queue/nacl.scons',
    'tests/gold_plugin/nacl.scons',
    'tests/imc_sockets/nacl.scons',
    'tests/minnacl/nacl.scons',
    'tests/multiple_sandboxes/nacl.scons',
    # Potential issue with running them:
    # http://code.google.com/p/nativeclient/issues/detail?id=2092
    # See also the comment in "buildbot/buildbot_standard.py"
    'tests/pnacl_shared_lib_test/nacl.scons',
    'tests/pwrite/nacl.scons',
    'tests/signal_handler_single_step/nacl.scons',
    'tests/thread_suspension/nacl.scons',
    'tests/trusted_crash/crash_in_syscall/nacl.scons',
    'tests/trusted_crash/osx_crash_filter/nacl.scons',
    'tests/trusted_crash/osx_crash_forwarding/nacl.scons',
    'tests/unittests/shared/imc/nacl.scons',
    #### ALPHABETICALLY SORTED ####
]

nacl_env.Append(BUILD_SCONSCRIPTS=nonvariant_tests)
nacl_env.AddChromeFilesFromGroup('nonvariant_test_scons_files')
nacl_env.Append(BUILD_SCONSCRIPTS=irt_variant_tests)
nacl_env.AddChromeFilesFromGroup('irt_variant_test_scons_files')

# Defines TESTS_TO_RUN_INBROWSER.
SConscript('tests/inbrowser_test_runner/selection.scons',
           exports=['nacl_env'])

# Possibly install a toolchain by downloading it
# TODO: explore using a less heavy weight mechanism
# NOTE: this uses stuff from: site_scons/site_tools/naclsdk.py
import SCons.Script

SCons.Script.AddOption('--download',
                       dest='download',
                       metavar='DOWNLOAD',
                       default=False,
                       action='store_true',
                       help='deprecated - allow tools to download')

if nacl_env.GetOption('download'):
  print '@@@@ --download is deprecated, use gclient runhooks --force'
  nacl_sync_env = nacl_env.Clone()
  nacl_sync_env['ENV'] = os.environ
  nacl_sync_env.Execute('gclient runhooks --force')


def NaClSharedLibrary(env, lib_name, *args, **kwargs):
  env_shared = env.Clone(COMPONENT_STATIC=False)
  soname = SCons.Util.adjustixes(lib_name, 'lib', '.so')
  env_shared.AppendUnique(SHLINKFLAGS=['-Wl,-soname,%s' % (soname)])
  return env_shared.ComponentLibrary(lib_name, *args, **kwargs)

nacl_env.AddMethod(NaClSharedLibrary)

def NaClSdkLibrary(env, lib_name, *args, **kwargs):
  n = [env.ComponentLibrary(lib_name, *args, **kwargs)]
  if not env.Bit('nacl_disable_shared'):
    n.append(env.NaClSharedLibrary(lib_name, *args, **kwargs))
  return n

nacl_env.AddMethod(NaClSdkLibrary)


# Special environment for untrusted test binaries that use raw syscalls
def RawSyscallObjects(env, sources):
  raw_syscall_env = env.Clone()
  raw_syscall_env.Append(
    CPPDEFINES = [
      ['USE_RAW_SYSCALLS', '1'],
      ],
  )
  objects = []
  for source_file in sources:
    target_name = 'raw_' + os.path.basename(source_file).rstrip('.c')
    object = raw_syscall_env.ComponentObject(target_name,
                                             source_file)
    objects.append(object)
  return objects

nacl_env.AddMethod(RawSyscallObjects)


# The IRT-building environment was cloned from nacl_env, but it should
# ignore the --nacl_glibc, nacl_pic=1 and bitcode=1 switches.
# We have to reinstantiate the naclsdk.py magic after clearing those flags,
# so it regenerates the tool paths right.
# TODO(mcgrathr,bradnelson): could get cleaner if naclsdk.py got folded back in.
nacl_irt_env.ClearBits('nacl_glibc')
nacl_irt_env.ClearBits('nacl_pic')
nacl_irt_env.ClearBits('pnacl_generate_pexe')
nacl_irt_env.ClearBits('use_sandboxed_translator')
nacl_irt_env.ClearBits('bitcode')
# The choice of toolchain used to build the IRT does not depend on the toolchain
# used to build user/test code. nacl-clang is used everywhere for the IRT.
nacl_irt_env.SetBits('nacl_clang')

nacl_irt_env.Tool('naclsdk')
# These are unfortunately clobbered by running Tool, which
# we needed to do to get the destination directory reset.
# We want all the same values from nacl_env.
nacl_irt_env.Replace(EXTRA_CFLAGS=nacl_env['EXTRA_CFLAGS'],
                     EXTRA_CXXFLAGS=nacl_env['EXTRA_CXXFLAGS'],
                     CCFLAGS=nacl_env['CCFLAGS'],
                     CFLAGS=nacl_env['CFLAGS'],
                     CXXFLAGS=nacl_env['CXXFLAGS'])
FixWindowsAssembler(nacl_irt_env)
# Make it find the libraries it builds, rather than the SDK ones.
nacl_irt_env.Replace(LIBPATH='${LIB_DIR}')

# The IRT must be built using LLVM's assembler on x86-64 to preserve sandbox
# base address hiding. It's also used on x86-32 for consistency.
if nacl_irt_env.Bit('build_x86_64') or nacl_irt_env.Bit('build_x86_32'):
  nacl_irt_env.Append(CCFLAGS=['-integrated-as'])
if nacl_irt_env.Bit('build_x86_32'):
  # The x86-32 IRT needs to be callable with an under-aligned stack.
  # See  https://code.google.com/p/nativeclient/issues/detail?id=3935
  nacl_irt_env.Append(CCFLAGS=['-mstackrealign', '-mno-sse'])

# The IRT is C only, don't link with the C++ linker so that it doesn't
# start depending on the C++ standard library and (in the case of
# libc++) pthread.
nacl_irt_env.Replace(LINK=(nacl_irt_env['LINK'].
                           replace('nacl-clang++', 'nacl-clang')))

# TODO(mcgrathr): Clean up uses of these methods.
def AddLibraryDummy(env, nodes):
  return nodes
nacl_irt_env.AddMethod(AddLibraryDummy, 'AddLibraryToSdk')

def AddObjectInternal(env, nodes):
  return env.Replicate('${LIB_DIR}', nodes)
nacl_env.AddMethod(AddObjectInternal, 'AddObjectToSdk')
nacl_irt_env.AddMethod(AddObjectInternal, 'AddObjectToSdk')

def IrtNaClSdkLibrary(env, lib_name, *args, **kwargs):
  env.ComponentLibrary(lib_name, *args, **kwargs)
nacl_irt_env.AddMethod(IrtNaClSdkLibrary, 'NaClSdkLibrary')

nacl_irt_env.AddMethod(SDKInstallBin)

# Populate the internal include directory when AddHeaderToSdk
# is used inside nacl_env.
def AddHeaderInternal(env, nodes, subdir='nacl'):
  dir = '${INCLUDE_DIR}'
  if subdir is not None:
    dir += '/' + subdir
  n = env.Replicate(dir, nodes)
  return n

nacl_irt_env.AddMethod(AddHeaderInternal, 'AddHeaderToSdk')

def PublishHeader(env, nodes, subdir):
  if ('install' in COMMAND_LINE_TARGETS or
      'install_headers' in COMMAND_LINE_TARGETS):
    dir = env.GetAbsDirArg('includedir', 'install_headers')
    if subdir is not None:
      dir += '/' + subdir
    n = env.Install(dir, nodes)
    env.Alias('install', env.Alias('install_headers', n))
    return n

def PublishLibrary(env, nodes):
  env.Alias('build_lib', nodes)

  if ('install' in COMMAND_LINE_TARGETS or
      'install_lib' in COMMAND_LINE_TARGETS):
    dir = env.GetAbsDirArg('libdir', 'install_lib')
    n = env.Install(dir, nodes)
    env.Alias('install', env.Alias('install_lib', n))
    return n

def NaClAddHeader(env, nodes, subdir='nacl'):
  n = AddHeaderInternal(env, nodes, subdir)
  PublishHeader(env, n, subdir)
  return n
nacl_env.AddMethod(NaClAddHeader, 'AddHeaderToSdk')

def NaClAddLibrary(env, nodes):
  nodes = env.Replicate('${LIB_DIR}', nodes)
  PublishLibrary(env, nodes)
  return nodes
nacl_env.AddMethod(NaClAddLibrary, 'AddLibraryToSdk')

def NaClAddObject(env, nodes):
  lib_nodes = env.Replicate('${LIB_DIR}', nodes)
  PublishLibrary(env, lib_nodes)
  return lib_nodes
nacl_env.AddMethod(NaClAddObject, 'AddObjectToSdk')

# We want to do this for nacl_env when not under --nacl_glibc,
# but for nacl_irt_env whether or not under --nacl_glibc, so
# we do it separately for each after making nacl_irt_env and
# clearing its Bit('nacl_glibc').
def AddImplicitLibs(env):
  implicit_libs = []

  # Require the pnacl_irt_shim for pnacl x86-64 and arm.
  # Use -B to have the compiler look for the fresh libpnacl_irt_shim.a.
  if ( env.Bit('bitcode') and
       (env.Bit('build_x86_64') or env.Bit('build_arm'))
       and env['NACL_BUILD_FAMILY'] != 'UNTRUSTED_IRT'):
    # Note: without this hack ibpnacl_irt_shim.a will be deleted
    #       when "built_elsewhere=1"
    #       Since we force the build in a previous step the dependency
    #       is not really needed.
    #       Note: the "precious" mechanism did not work in this case
    if not env.Bit('built_elsewhere'):
      if env.Bit('enable_chrome_side'):
        implicit_libs += ['libpnacl_irt_shim.a']

  if not env.Bit('nacl_glibc'):
    # These are automatically linked in by the compiler, either directly
    # or via the linker script that is -lc.  In the non-glibc build, we
    # are the ones providing these files, so we need dependencies.
    # The ComponentProgram method (site_scons/site_tools/component_builders.py)
    # adds dependencies on env['IMPLICIT_LIBS'] if that's set.
    if env.Bit('bitcode'):
      implicit_libs += ['libnacl.a']
    else:
      implicit_libs += ['crt1.o',
                        'libnacl.a',
                        'crti.o',
                        'crtn.o']
      # TODO(mcgrathr): multilib nonsense defeats -B!  figure out a better way.
      if GetTargetPlatform() == 'x86-32':
        implicit_libs.append(os.path.join('32', 'crt1.o'))
    # libc++ depends on libpthread, and because PPAPI applications always need
    # threads anyway, nacl-clang just includes -lpthread unconditionally.
    if using_nacl_libcxx and env['NACL_BUILD_FAMILY'] != 'UNTRUSTED_IRT':
      implicit_libs += ['libpthread.a']

  if implicit_libs != []:
    env['IMPLICIT_LIBS'] = [env.File(os.path.join('${LIB_DIR}', file))
                            for file in implicit_libs]
    # The -B<dir>/ flag is necessary to tell gcc to look for crt[1in].o there.
    env.Prepend(LINKFLAGS=['-B${LIB_DIR}/'])

AddImplicitLibs(nacl_env)
AddImplicitLibs(nacl_irt_env)

nacl_irt_env.Append(
    BUILD_SCONSCRIPTS = [
        'src/shared/gio/nacl.scons',
        'src/shared/platform/nacl.scons',
        'src/tools/tls_edit/build.scons',
        'src/untrusted/elf_loader/nacl.scons',
        'src/untrusted/irt/nacl.scons',
        'src/untrusted/nacl/nacl.scons',
        'src/untrusted/stubs/nacl.scons',
        'tests/irt_private_pthread/nacl.scons',
    ])
nacl_irt_env.AddChromeFilesFromGroup('untrusted_irt_scons_files')

environment_list.append(nacl_irt_env)

# Since browser_tests already use the IRT normally, those are fully covered
# in nacl_env.  But the non_browser_tests don't use the IRT in nacl_env.
# We want additional variants of those tests with the IRT, so we make
# another environment and repeat them with that adjustment.
nacl_irt_test_env = nacl_env.Clone(
    BUILD_TYPE = 'nacl_irt_test',
    BUILD_TYPE_DESCRIPTION = 'NaCl tests build with IRT',
    NACL_BUILD_FAMILY = 'UNTRUSTED_IRT_TESTS',
    NACL_ENV = nacl_env,

    INCLUDE_DIR = nacl_env.Dir('${INCLUDE_DIR}'),
    LIB_DIR = nacl_env.Dir('${LIB_DIR}'),
    BUILD_SCONSCRIPTS = [],
    )
nacl_irt_test_env.SetBits('tests_use_irt')
if nacl_irt_test_env.Bit('enable_chrome_side'):
  nacl_irt_test_env.Replace(TESTRUNNER_LIBS=['testrunner_browser'])

nacl_irt_test_env.Append(BUILD_SCONSCRIPTS=irt_variant_tests)
nacl_irt_test_env.AddChromeFilesFromGroup('irt_variant_test_scons_files')
nacl_irt_test_env.Append(BUILD_SCONSCRIPTS=irt_only_tests)
TestsUsePublicLibs(nacl_irt_test_env)
TestsUsePublicListMappingsLib(nacl_irt_test_env)

# We add the following settings after creating nacl_irt_test_env because we
# don't want them to be inherited by nacl_irt_test_env.
if nacl_env.Bit('nonsfi_nacl'):
  if nacl_env.Bit('pnacl_generate_pexe'):
    # Not-IRT-using non-SFI code uses Linux syscalls directly.  Since this
    # involves using inline assembly, this requires turning off the PNaCl ABI
    # checker.
    nacl_env.SetBits('nonstable_bitcode')
    nacl_env.Append(LINKFLAGS=['--pnacl-disable-abi-check'])
    # Tell the PNaCl translator to link a Linux executable.
    nacl_env.Append(TRANSLATEFLAGS=['--noirt'])
  else:
    nacl_env.Append(LINKFLAGS=['--pnacl-allow-native', '-Wt,--noirt'])

# If a tests/.../nacl.scons file builds a library, we will just use
# the one already built in nacl_env instead.
def IrtTestDummyLibrary(*args, **kwargs):
  pass
nacl_irt_test_env.AddMethod(IrtTestDummyLibrary, 'ComponentLibrary')

def IrtTestAddNodeToTestSuite(env, node, suite_name, node_name=None,
                              is_broken=False, is_flaky=False,
                              disable_irt_suffix=False):
  # The disable_irt_suffix argument is there for allowing tests
  # defined in nacl_irt_test_env to be part of chrome_browser_tests
  # (rather than part of chrome_browser_tests_irt).
  # TODO(mseaborn): But really, all of chrome_browser_tests should be
  # placed in nacl_irt_test_env rather than in nacl_env.
  suite_name = AddImplicitTestSuites(suite_name, node_name)
  if not disable_irt_suffix:
    if node_name is not None:
      node_name += '_irt'
    suite_name = [name + '_irt' for name in suite_name]
  # NOTE: This needs to be called directly to as we're overriding the
  #       prior version.
  return AddNodeToTestSuite(env, node, suite_name, node_name,
                            is_broken, is_flaky)
nacl_irt_test_env.AddMethod(IrtTestAddNodeToTestSuite, 'AddNodeToTestSuite')

environment_list.append(nacl_irt_test_env)


windows_coverage_env = windows_debug_env.Clone(
    tools = ['code_coverage'],
    BUILD_TYPE = 'coverage-win',
    BUILD_TYPE_DESCRIPTION = 'Windows code coverage build',
    # TODO(bradnelson): switch nacl to common testing process so this won't be
    #    needed.
    MANIFEST_FILE = None,
    COVERAGE_ANALYZER_DIR=r'..\third_party\coverage_analyzer\bin',
    COVERAGE_ANALYZER='$COVERAGE_ANALYZER_DIR\coverage_analyzer.exe',
)
# TODO(bradnelson): Switch nacl to common testing process so this won't be
#                   needed. Ignoring instrumentation failure as that's easier
#                   than trying to gate out the ones with asm we can't handle.
windows_coverage_env['LINKCOM'] = windows_coverage_env.Action([
    windows_coverage_env.get('LINKCOM', []),
    '-$COVERAGE_VSINSTR /COVERAGE ${TARGET}'])
windows_coverage_env.Append(LINKFLAGS = ['/NODEFAULTLIB:msvcrt'])
AddDualLibrary(windows_coverage_env)
environment_list.append(windows_coverage_env)

mac_coverage_env = mac_debug_env.Clone(
    tools = ['code_coverage'],
    BUILD_TYPE = 'coverage-mac',
    BUILD_TYPE_DESCRIPTION = 'MacOS code coverage build',
    # Strict doesnt't currently work for coverage because the path to gcov is
    # magically baked into the compiler.
    LIBS_STRICT = False,
    )
AddDualLibrary(mac_coverage_env)
environment_list.append(mac_coverage_env)

linux_coverage_env = linux_debug_env.Clone(
    tools = ['code_coverage'],
    BUILD_TYPE = 'coverage-linux',
    BUILD_TYPE_DESCRIPTION = 'Linux code coverage build',
    # Strict doesnt't currently work for coverage because the path to gcov is
    # magically baked into the compiler.
    LIBS_STRICT = False,
)

linux_coverage_env.FilterOut(CCFLAGS=['-fPIE'])
linux_coverage_env.Append(CCFLAGS=['-fPIC'])

linux_coverage_env['OPTIONAL_COVERAGE_LIBS'] = '$COVERAGE_LIBS'
AddDualLibrary(linux_coverage_env)
environment_list.append(linux_coverage_env)


# Environment Massaging
RELEVANT_CONFIG = ['NACL_BUILD_FAMILY',
                   'BUILD_TYPE',
                   'TARGET_ROOT',
                   'OBJ_ROOT',
                   'BUILD_TYPE_DESCRIPTION',
                   ]

MAYBE_RELEVANT_CONFIG = ['BUILD_OS',
                         'BUILD_ARCHITECTURE',
                         'BUILD_SUBARCH',
                         'TARGET_OS',
                         'TARGET_ARCHITECTURE',
                         'TARGET_SUBARCH',
                         ]

def DumpCompilerVersion(cc, env):
  if 'gcc' in cc:
    env.Execute(env.Action('set'))
    env.Execute(env.Action('${CC} -v -c'))
    env.Execute(env.Action('${CC} -print-search-dirs'))
    env.Execute(env.Action('${CC} -print-libgcc-file-name'))
  elif cc.startswith('cl'):
    import subprocess
    try:
      p = subprocess.Popen(env.subst('${CC} /V'),
                           bufsize=1000*1000,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE)
      stdout, stderr = p.communicate()
      print stderr[0:stderr.find("\r")]
    except WindowsError:
      # If vcvars was not run before running SCons, we won't be able to find
      # the compiler at this point.  SCons has built in functions for finding
      # the compiler, but they haven't run yet.
      print 'Can not find the compiler, assuming SCons will find it later.'
  else:
    print "UNKNOWN COMPILER"


def SanityCheckEnvironments(all_envs):
  # simple completeness check
  for env in all_envs:
    for tag in RELEVANT_CONFIG:
      assert tag in env, repr(tag)
      assert env[tag], repr(env[tag])


def LinkTrustedEnv(selected_envs):
  # Collect build families and ensure that we have only one env per family.
  family_map = {}
  for env in selected_envs:
    family = env['NACL_BUILD_FAMILY']
    if family not in family_map:
      family_map[family] = env
    else:
      msg = 'You are using incompatible environments simultaneously\n'
      msg += '%s vs %s\n' % (env['BUILD_TYPE'],
                             family_map[family]['BUILD_TYPE'])
      msg += ('Please specfy the exact environments you require, e.g. '
              'MODE=dbg-host,nacl')
      raise Exception(msg)

  # Set TRUSTED_ENV so that tests of untrusted code can locate sel_ldr
  # etc.  We set this on trusted envs too because some tests on
  # trusted envs run sel_ldr (e.g. using checked-in binaries).
  if 'TRUSTED' in family_map:
    for env in selected_envs:
      env['TRUSTED_ENV'] = family_map['TRUSTED']
      # Propagate some environment variables from the trusted environment,
      # in case some (e.g. Mac's DYLD_LIBRARY_PATH) are necessary for
      # running sel_ldr et al in untrusted environments' tests.
      for var in env['TRUSTED_ENV'].get('PROPAGATE_ENV', []):
        env['ENV'][var] = env['TRUSTED_ENV']['ENV'][var]
  if 'TRUSTED' not in family_map or 'UNTRUSTED' not in family_map:
    Banner('Warning: "--mode" did not specify both trusted and untrusted '
           'build environments.  As a result, many tests will not be run.')

def MakeBuildEnv():
  build_platform = GetBuildPlatform()

  # Build Platform Base Function
  platform_func_map = {
      'win32' : MakeWindowsEnv,
      'cygwin': MakeWindowsEnv,
      'linux' : MakeGenericLinuxEnv,
      'linux2': MakeGenericLinuxEnv,
      'darwin': MakeMacEnv,
      }
  if sys.platform not in platform_func_map:
    raise UserError('Unrecognized host platform: %s', sys.platform)
  make_env_func = platform_func_map[sys.platform]

  build_env = make_env_func(build_platform)
  build_env['IS_BUILD_ENV'] = True

  # Building tls_edit depends on gio, platform, and validator_ragel.
  build_env['BUILD_SCONSCRIPTS'] = [
    # KEEP THIS SORTED PLEASE
    'src/shared/gio/build.scons',
    'src/shared/platform/build.scons',
    'src/trusted/validator_ragel/build.scons',
    ]

  # The build environment is only used for intermediate steps and should
  # not be creating any targets. Aliases are used as means to add targets
  # to builds (IE, all_programs, all_libraries...etc.). Since we want to
  # share all of our build scripts but not define any aliases, we should
  # override the alias function and essentially stub it out.
  build_env.Alias = lambda env, target, source=[], actions=None, **kw : []

  return build_env

def LinkBuildEnv(selected_envs):
  build_env_map = {
    'opt': opt_build_env,
    'dbg': dbg_build_env,
    }

  # We need to find the optimization level in order to know which
  # build environment we want to use
  opt_level = None
  for env in selected_envs:
    if env.get('OPTIMIZATION_LEVEL', None):
      opt_level = env['OPTIMIZATION_LEVEL']
      break

  build_env = build_env_map.get(opt_level, opt_build_env)
  for env in selected_envs:
    env['BUILD_ENV'] = build_env

  # If the build environment is different from all the selected environments,
  # we will need to also append it to the selected environments so the targets
  # can be built.
  build_env_root = build_env.subst('${TARGET_ROOT}')
  for env in selected_envs:
    if build_env_root == env.subst('${TARGET_ROOT}'):
      break
  else:
    # Did not find a matching environment, append the build environment now.
    selected_envs.append(build_env)

def DumpEnvironmentInfo(selected_envs):
  if VerboseConfigInfo(pre_base_env):
    Banner("The following environments have been configured")
    for env in selected_envs:
      for tag in RELEVANT_CONFIG:
        assert tag in env, repr(tag)
        print "%s:  %s" % (tag, env.subst(env.get(tag)))
      for tag in MAYBE_RELEVANT_CONFIG:
        print "%s:  %s" % (tag, env.subst(env.get(tag)))
      cc = env.subst('${CC}')
      print 'CC:', cc
      asppcom = env.subst('${ASPPCOM}')
      print 'ASPPCOM:', asppcom
      DumpCompilerVersion(cc, env)
      print
    rev_file = 'toolchain/linux_x86/pnacl_newlib_raw/REV'
    if os.path.exists(rev_file):
      for line in open(rev_file).read().split('\n'):
        if "Revision:" in line:
          print "PNACL : %s" % line

def PnaclSetEmulatorForSandboxedTranslator(selected_envs):
  # Slip in emulator flags if necessary, for the sandboxed pnacl translator
  # on ARM, once emulator is actually known (vs in naclsdk.py, where it
  # is not yet known).
  for env in selected_envs:
    if (env.Bit('bitcode')
        and env.Bit('use_sandboxed_translator')
        and env.UsingEmulator()):
      # This must modify the LINK command itself, since LINKFLAGS may
      # be filtered (e.g., in barebones tests).
      env.Append(LINK=' --pnacl-use-emulator')
      env.Append(TRANSLATE=' --pnacl-use-emulator')


# Blank out defaults.
Default(None)

# Apply optional supplement if present in the directory tree.
if os.path.exists(pre_base_env.subst('$MAIN_DIR/supplement/supplement.scons')):
  SConscript('supplement/supplement.scons', exports=['environment_list'])

# print sytem info (optionally)
if VerboseConfigInfo(pre_base_env):
  Banner('SCONS ARGS:' + str(sys.argv))
  os.system(pre_base_env.subst('${PYTHON} tools/sysinfo.py'))

CheckArguments()

SanityCheckEnvironments(environment_list)
selected_envs = FilterEnvironments(environment_list)

# If we are building NaCl, build nacl_irt too.  This works around it being
# a separate mode due to the vagaries of scons when we'd really rather it
# not be, while not requiring that every bot command line using --mode be
# changed to list '...,nacl,nacl_irt' explicitly.
if nacl_env in selected_envs:
  selected_envs.append(nacl_irt_env)

# The nacl_irt_test_env requires nacl_env to build things correctly.
if nacl_irt_test_env in selected_envs and nacl_env not in selected_envs:
  selected_envs.append(nacl_env)

DumpEnvironmentInfo(selected_envs)
LinkTrustedEnv(selected_envs)

# When building NaCl, any intermediate build tool that is used during the
# build process must be built using the current build environment, not the
# target. Create a build environment for this purpose and link it into
# the selected environments
dbg_build_env, opt_build_env = GenerateOptimizationLevels(MakeBuildEnv())
LinkBuildEnv(selected_envs)

# This must happen after LinkTrustedEnv, since that is where TRUSTED_ENV
# is finally set, and env.UsingEmulator() checks TRUSTED_ENV for the emulator.
# This must also happen before BuildEnvironments.
PnaclSetEmulatorForSandboxedTranslator(selected_envs)

BuildEnvironments(selected_envs)

# Change default to build everything, but not run tests.
Default(['all_programs', 'all_bundles', 'all_test_programs', 'all_libraries'])


# Sanity check whether we are ready to build nacl modules
# NOTE: this uses stuff from: site_scons/site_tools/naclsdk.py
if nacl_env.Bit('naclsdk_validate') and (nacl_env in selected_envs or
                                         nacl_irt_env in selected_envs):
  nacl_env.ValidateSdk()

if BROKEN_TEST_COUNT > 0:
  msg = "There are %d broken tests." % BROKEN_TEST_COUNT
  if GetOption('brief_comstr'):
    msg += " Add --verbose to the command line for more information."
  print msg

# separate warnings from actual build output
Banner('B U I L D - O U T P U T:')