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
|
// ccache -- a fast C/C++ compiler cache
//
// Copyright (C) 2002-2007 Andrew Tridgell
// Copyright (C) 2009-2018 Joel Rosdahl
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc., 51
// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include "ccache.h"
#include "compopt.h"
#ifdef HAVE_GETOPT_LONG
#include <getopt.h>
#else
#include "getopt_long.h"
#endif
#include "hashtable.h"
#include "hashtable_itr.h"
#include "hashutil.h"
#include "language.h"
#include "manifest.h"
#define STRINGIFY(x) #x
#define TO_STRING(x) STRINGIFY(x)
static const char VERSION_TEXT[] =
MYNAME " version %s\n"
"\n"
"Copyright (C) 2002-2007 Andrew Tridgell\n"
"Copyright (C) 2009-2018 Joel Rosdahl\n"
"\n"
"This program is free software; you can redistribute it and/or modify it under\n"
"the terms of the GNU General Public License as published by the Free Software\n"
"Foundation; either version 3 of the License, or (at your option) any later\n"
"version.\n";
static const char USAGE_TEXT[] =
"Usage:\n"
" " MYNAME " [options]\n"
" " MYNAME " compiler [compiler options]\n"
" compiler [compiler options] (via symbolic link)\n"
"\n"
"Options:\n"
" -c, --cleanup delete old files and recalculate size counters\n"
" (normally not needed as this is done automatically)\n"
" -C, --clear clear the cache completely (except configuration)\n"
" -F, --max-files=N set maximum number of files in cache to N (use 0 for\n"
" no limit)\n"
" -M, --max-size=SIZE set maximum size of cache to SIZE (use 0 for no\n"
" limit); available suffixes: k, M, G, T (decimal) and\n"
" Ki, Mi, Gi, Ti (binary); default suffix: G\n"
" -o, --set-config=K=V set configuration key K to value V\n"
" -p, --print-config print current configuration options\n"
" -s, --show-stats show statistics summary\n"
" -z, --zero-stats zero statistics counters\n"
"\n"
" -h, --help print this help text\n"
" -V, --version print version and copyright information\n"
"\n"
"See also <https://ccache.samba.org>.\n";
// Global configuration data.
struct conf *conf = NULL;
// Where to write configuration changes.
char *primary_config_path = NULL;
// Secondary, read-only configuration file (if any).
char *secondary_config_path = NULL;
// Current working directory taken from $PWD, or getcwd() if $PWD is bad.
char *current_working_dir = NULL;
// The original argument list.
static struct args *orig_args;
// The source file.
static char *input_file;
// The output file being compiled to.
static char *output_obj;
// The path to the dependency file (implicit or specified with -MF).
static char *output_dep;
// The path to the coverage file (implicit when using -ftest-coverage).
static char *output_cov;
// The path to the stack usage (implicit when using -fstack-usage).
static char *output_su;
// Diagnostic generation information (clang). Contains pathname if not NULL.
static char *output_dia;
// Split dwarf information (GCC 4.8 and up). Contains pathname if not NULL.
static char *output_dwo;
// Language to use for the compilation target (see language.c).
static const char *actual_language;
// Array for storing -arch options.
#define MAX_ARCH_ARGS 10
static size_t arch_args_size = 0;
static char *arch_args[MAX_ARCH_ARGS] = {NULL};
// Name (represented as a struct file_hash) of the file containing the cached
// object code.
static struct file_hash *cached_obj_hash;
// Full path to the file containing the cached object code
// (cachedir/a/b/cdef[...]-size.o).
static char *cached_obj;
// Full path to the file containing the standard error output
// (cachedir/a/b/cdef[...]-size.stderr).
static char *cached_stderr;
// Full path to the file containing the dependency information
// (cachedir/a/b/cdef[...]-size.d).
static char *cached_dep;
// Full path to the file containing the coverage information
// (cachedir/a/b/cdef[...]-size.gcno).
static char *cached_cov;
// Full path to the file containing the stack usage
// (cachedir/a/b/cdef[...]-size.su).
static char *cached_su;
// Full path to the file containing the diagnostic information (for clang)
// (cachedir/a/b/cdef[...]-size.dia).
static char *cached_dia;
// Full path to the file containing the split dwarf (for GCC 4.8 and above)
// (cachedir/a/b/cdef[...]-size.dwo).
//
// Contains NULL if -gsplit-dwarf is not given.
static char *cached_dwo;
// Full path to the file containing the manifest
// (cachedir/a/b/cdef[...]-size.manifest).
static char *manifest_path;
// Time of compilation. Used to see if include files have changed after
// compilation.
time_t time_of_compilation;
// Files included by the preprocessor and their hashes/sizes. Key: file path.
// Value: struct file_hash.
static struct hashtable *included_files = NULL;
// Uses absolute path for some include files.
static bool has_absolute_include_headers = false;
// List of headers to ignore.
static char **ignore_headers;
// Size of headers to ignore list.
static size_t ignore_headers_len;
// Is the compiler being asked to output debug info?
static bool generating_debuginfo;
// Is the compiler being asked to output dependencies?
static bool generating_dependencies;
// Is the compiler being asked to output coverage?
static bool generating_coverage;
// Is the compiler being asked to output stack usage?
static bool generating_stackusage;
// Us the compiler being asked to generate diagnostics
// (--serialize-diagnostics)?
static bool generating_diagnostics;
// Is the compiler being asked to separate dwarf debug info into a separate
// file (-gsplit-dwarf)"?
static bool using_split_dwarf;
// Relocating debuginfo in the format old=new.
static char **debug_prefix_maps = NULL;
// Size of debug_prefix_maps list.
static size_t debug_prefix_maps_len = 0;
// Is the compiler being asked to output coverage data (.gcda) at runtime?
static bool profile_arcs;
// Name of the custom profile directory (default: object dirname).
static char *profile_dir;
// The name of the temporary preprocessed file.
static char *i_tmpfile;
// Are we compiling a .i or .ii file directly?
static bool direct_i_file;
// The name of the cpp stderr file.
static char *cpp_stderr;
// Full path to the statistics file in the subdirectory where the cached result
// belongs (<cache_dir>/<x>/stats).
char *stats_file = NULL;
// Whether the output is a precompiled header.
bool output_is_precompiled_header = false;
// Compiler guessing is currently only based on the compiler name, so nothing
// should hard-depend on it if possible.
enum guessed_compiler guessed_compiler = GUESSED_UNKNOWN;
// Profile generation / usage information.
static char *profile_dir = NULL;
static bool profile_use = false;
static bool profile_generate = false;
// Sanitize blacklist
static char *sanitize_blacklist = NULL;
// Whether we are using a precompiled header (either via -include, #include or
// clang's -include-pch or -include-pth).
static bool using_precompiled_header = false;
// The .gch/.pch/.pth file used for compilation.
static char *included_pch_file = NULL;
// How long (in microseconds) to wait before breaking a stale lock.
unsigned lock_staleness_limit = 2000000;
enum fromcache_call_mode {
FROMCACHE_DIRECT_MODE,
FROMCACHE_CPP_MODE
};
struct pending_tmp_file {
char *path;
struct pending_tmp_file *next;
};
// Temporary files to remove at program exit.
static struct pending_tmp_file *pending_tmp_files = NULL;
#ifndef _WIN32
static sigset_t fatal_signal_set;
// PID of currently executing compiler that we have started, if any. 0 means no
// ongoing compilation.
static pid_t compiler_pid = 0;
#endif
// This is a string that identifies the current "version" of the hash sum
// computed by ccache. If, for any reason, we want to force the hash sum to be
// different for the same input in a new ccache version, we can just change
// this string. A typical example would be if the format of one of the files
// stored in the cache changes in a backwards-incompatible way.
static const char HASH_PREFIX[] = "3";
static void
add_prefix(struct args *args, char *prefix_command)
{
if (str_eq(prefix_command, "")) {
return;
}
struct args *prefix = args_init(0, NULL);
char *e = x_strdup(prefix_command);
char *saveptr = NULL;
for (char *tok = strtok_r(e, " ", &saveptr);
tok;
tok = strtok_r(NULL, " ", &saveptr)) {
char *p;
p = find_executable(tok, MYNAME);
if (!p) {
fatal("%s: %s", tok, strerror(errno));
}
args_add(prefix, p);
free(p);
}
free(e);
cc_log("Using command-line prefix %s", prefix_command);
for (int i = prefix->argc; i != 0; i--) {
args_add_prefix(args, prefix->argv[i-1]);
}
args_free(prefix);
}
// Something went badly wrong - just execute the real compiler.
static void
failed(void)
{
assert(orig_args);
args_strip(orig_args, "--ccache-");
add_prefix(orig_args, conf->prefix_command);
cc_log("Failed; falling back to running the real compiler");
cc_log_argv("Executing ", orig_args->argv);
exitfn_call();
execv(orig_args->argv[0], orig_args->argv);
fatal("execv of %s failed: %s", orig_args->argv[0], strerror(errno));
}
static const char *
temp_dir()
{
static char *path = NULL;
if (path) {
return path; // Memoize
}
path = conf->temporary_dir;
if (str_eq(path, "")) {
path = format("%s/tmp", conf->cache_dir);
}
return path;
}
void
block_signals(void)
{
#ifndef _WIN32
sigprocmask(SIG_BLOCK, &fatal_signal_set, NULL);
#endif
}
void
unblock_signals(void)
{
#ifndef _WIN32
sigset_t empty;
sigemptyset(&empty);
sigprocmask(SIG_SETMASK, &empty, NULL);
#endif
}
static void
add_pending_tmp_file(const char *path)
{
block_signals();
struct pending_tmp_file *e = x_malloc(sizeof(*e));
e->path = x_strdup(path);
e->next = pending_tmp_files;
pending_tmp_files = e;
unblock_signals();
}
static void
do_clean_up_pending_tmp_files(void)
{
struct pending_tmp_file *p = pending_tmp_files;
while (p) {
// Can't call tmp_unlink here since its cc_log calls aren't signal safe.
unlink(p->path);
p = p->next;
// Leak p->path and p here because clean_up_pending_tmp_files needs to be
// signal safe.
}
}
static void
clean_up_pending_tmp_files(void)
{
block_signals();
do_clean_up_pending_tmp_files();
unblock_signals();
}
#ifndef _WIN32
static void
signal_handler(int signum)
{
// Unregister handler for this signal so that we can send the signal to
// ourselves at the end of the handler.
signal(signum, SIG_DFL);
// If ccache was killed explicitly, then bring the compiler subprocess (if
// any) with us as well.
if (signum == SIGTERM
&& compiler_pid != 0
&& waitpid(compiler_pid, NULL, WNOHANG) == 0) {
kill(compiler_pid, signum);
}
do_clean_up_pending_tmp_files();
if (compiler_pid != 0) {
// Wait for compiler subprocess to exit before we snuff it.
waitpid(compiler_pid, NULL, 0);
}
// Resend signal to ourselves to exit properly after returning from the
// handler.
kill(getpid(), signum);
}
static void
register_signal_handler(int signum)
{
struct sigaction act;
memset(&act, 0, sizeof(act));
act.sa_handler = signal_handler;
act.sa_mask = fatal_signal_set;
#ifdef SA_RESTART
act.sa_flags = SA_RESTART;
#endif
sigaction(signum, &act, NULL);
}
static void
set_up_signal_handlers(void)
{
sigemptyset(&fatal_signal_set);
sigaddset(&fatal_signal_set, SIGINT);
sigaddset(&fatal_signal_set, SIGTERM);
#ifdef SIGHUP
sigaddset(&fatal_signal_set, SIGHUP);
#endif
#ifdef SIGQUIT
sigaddset(&fatal_signal_set, SIGQUIT);
#endif
register_signal_handler(SIGINT);
register_signal_handler(SIGTERM);
#ifdef SIGHUP
register_signal_handler(SIGHUP);
#endif
#ifdef SIGQUIT
register_signal_handler(SIGQUIT);
#endif
}
#endif // _WIN32
static void
clean_up_internal_tempdir(void)
{
time_t now = time(NULL);
struct stat st;
if (x_stat(conf->cache_dir, &st) != 0 || st.st_mtime + 3600 >= now) {
// No cleanup needed.
return;
}
update_mtime(conf->cache_dir);
DIR *dir = opendir(temp_dir());
if (!dir) {
return;
}
struct dirent *entry;
while ((entry = readdir(dir))) {
if (str_eq(entry->d_name, ".") || str_eq(entry->d_name, "..")) {
continue;
}
char *path = format("%s/%s", temp_dir(), entry->d_name);
if (x_lstat(path, &st) == 0 && st.st_mtime + 3600 < now) {
tmp_unlink(path);
}
free(path);
}
closedir(dir);
}
static enum guessed_compiler
guess_compiler(const char *path)
{
char *name = basename(path);
enum guessed_compiler result = GUESSED_UNKNOWN;
if (strstr(name, "clang")) {
result = GUESSED_CLANG;
} else if (strstr(name, "gcc") || strstr(name, "g++")) {
result = GUESSED_GCC;
} else if (strstr(name, "nvcc")) {
result = GUESSED_NVCC;
} else if (str_eq(name, "pump") || str_eq(name, "distcc-pump")) {
result = GUESSED_PUMP;
}
free(name);
return result;
}
static char *
get_current_working_dir(void)
{
if (!current_working_dir) {
char *cwd = get_cwd();
if (cwd) {
current_working_dir = x_realpath(cwd);
free(cwd);
}
if (!current_working_dir) {
cc_log("Unable to determine current working directory: %s",
strerror(errno));
failed();
}
}
return current_working_dir;
}
// Transform a name to a full path into the cache directory, creating needed
// sublevels if needed. Caller frees.
static char *
get_path_in_cache(const char *name, const char *suffix)
{
char *path = x_strdup(conf->cache_dir);
for (unsigned i = 0; i < conf->cache_dir_levels; ++i) {
char *p = format("%s/%c", path, name[i]);
free(path);
path = p;
}
char *result =
format("%s/%s%s", path, name + conf->cache_dir_levels, suffix);
free(path);
return result;
}
// This function hashes an include file and stores the path and hash in the
// global included_files variable. If the include file is a PCH, cpp_hash is
// also updated. Takes over ownership of path.
static void
remember_include_file(char *path, struct mdfour *cpp_hash, bool system)
{
size_t path_len = strlen(path);
if (path_len >= 2 && (path[0] == '<' && path[path_len - 1] == '>')) {
// Typically <built-in> or <command-line>.
goto ignore;
}
if (str_eq(path, input_file)) {
// Don't remember the input file.
goto ignore;
}
if (system && (conf->sloppiness & SLOPPY_NO_SYSTEM_HEADERS)) {
// Don't remember this system header.
goto ignore;
}
if (hashtable_search(included_files, path)) {
// Already known include file.
goto ignore;
}
#ifdef _WIN32
// stat fails on directories on win32.
DWORD attributes = GetFileAttributes(path);
if (attributes != INVALID_FILE_ATTRIBUTES &&
attributes & FILE_ATTRIBUTE_DIRECTORY) {
goto ignore;
}
#endif
struct stat st;
if (x_stat(path, &st) != 0) {
goto failure;
}
if (S_ISDIR(st.st_mode)) {
// Ignore directory, typically $PWD.
goto ignore;
}
if (!S_ISREG(st.st_mode)) {
// Device, pipe, socket or other strange creature.
cc_log("Non-regular include file %s", path);
goto failure;
}
// Canonicalize path for comparison; clang uses ./header.h.
char *canonical = path;
size_t canonical_len = path_len;
if (canonical[0] == '.' && canonical[1] == '/') {
canonical += 2;
canonical_len -= 2;
}
for (size_t i = 0; i < ignore_headers_len; i++) {
char *ignore = ignore_headers[i];
size_t ignore_len = strlen(ignore);
if (ignore_len > canonical_len) {
continue;
}
if (strncmp(canonical, ignore, ignore_len) == 0
&& (ignore[ignore_len-1] == DIR_DELIM_CH
|| canonical[ignore_len] == DIR_DELIM_CH
|| canonical[ignore_len] == '\0')) {
goto ignore;
}
}
// The comparison using >= is intentional, due to a possible race between
// starting compilation and writing the include file. See also the notes
// under "Performance" in doc/MANUAL.adoc.
if (!(conf->sloppiness & SLOPPY_INCLUDE_FILE_MTIME)
&& st.st_mtime >= time_of_compilation) {
cc_log("Include file %s too new", path);
goto failure;
}
// The same >= logic as above applies to the change time of the file.
if (!(conf->sloppiness & SLOPPY_INCLUDE_FILE_CTIME)
&& st.st_ctime >= time_of_compilation) {
cc_log("Include file %s ctime too new", path);
goto failure;
}
// Let's hash the include file content.
struct mdfour fhash;
hash_start(&fhash);
bool is_pch = is_precompiled_header(path);
if (is_pch) {
bool using_pch_sum = false;
if (conf->pch_external_checksum) {
// hash pch.sum instead of pch when it exists
// to prevent hashing a very large .pch file every time
char *pch_sum_path = format("%s.sum", path);
if (x_stat(pch_sum_path, &st) == 0) {
char *old_path = path;
path = pch_sum_path;
pch_sum_path = old_path;
using_pch_sum = true;
cc_log("Using pch.sum file %s", path);
}
free(pch_sum_path);
}
if (!hash_file(&fhash, path)) {
goto failure;
}
struct file_hash pch_hash;
hash_result_as_bytes(&fhash, pch_hash.hash);
pch_hash.size = fhash.totalN;
hash_delimiter(cpp_hash, using_pch_sum ? "pch_sum_hash" : "pch_hash");
hash_buffer(cpp_hash, pch_hash.hash, sizeof(pch_hash.hash));
}
if (conf->direct_mode) {
if (!is_pch) { // else: the file has already been hashed.
char *source = NULL;
size_t size;
if (st.st_size > 0) {
if (!read_file(path, st.st_size, &source, &size)) {
goto failure;
}
} else {
source = x_strdup("");
size = 0;
}
int result = hash_source_code_string(conf, &fhash, source, size, path);
free(source);
if (result & HASH_SOURCE_CODE_ERROR
|| result & HASH_SOURCE_CODE_FOUND_TIME) {
goto failure;
}
}
struct file_hash *h = x_malloc(sizeof(*h));
hash_result_as_bytes(&fhash, h->hash);
h->size = fhash.totalN;
hashtable_insert(included_files, path, h);
} else {
free(path);
}
return;
failure:
if (conf->direct_mode) {
cc_log("Disabling direct mode");
conf->direct_mode = false;
}
// Fall through.
ignore:
free(path);
}
// Make a relative path from current working directory to path if path is under
// the base directory. Takes over ownership of path. Caller frees.
static char *
make_relative_path(char *path)
{
if (str_eq(conf->base_dir, "") || !str_startswith(path, conf->base_dir)) {
return path;
}
#ifdef _WIN32
if (path[0] == '/') {
path++; // Skip leading slash.
}
#endif
// x_realpath only works for existing paths, so if path doesn't exist, try
// dirname(path) and assemble the path afterwards. We only bother to try
// canonicalizing one of these two paths since a compiler path argument
// typically only makes sense if path or dirname(path) exists.
char *path_suffix = NULL;
struct stat st;
if (stat(path, &st) != 0) {
// path doesn't exist.
char *dir = dirname(path);
if (stat(dir, &st) != 0) {
// And neither does its parent directory, so no action to take.
free(dir);
return path;
}
free(dir);
path_suffix = basename(path);
char *p = path;
path = dirname(path);
free(p);
}
char *canon_path = x_realpath(path);
if (canon_path) {
free(path);
char *relpath = get_relative_path(get_current_working_dir(), canon_path);
free(canon_path);
if (path_suffix) {
path = format("%s/%s", relpath, path_suffix);
free(relpath);
free(path_suffix);
return path;
} else {
return relpath;
}
} else {
// path doesn't exist, so leave it as it is.
free(path_suffix);
return path;
}
}
// This function reads and hashes a file. While doing this, it also does these
// things:
//
// - Makes include file paths for which the base directory is a prefix relative
// when computing the hash sum.
// - Stores the paths and hashes of included files in the global variable
// included_files.
static bool
process_preprocessed_file(struct mdfour *hash, const char *path, bool pump)
{
char *data;
size_t size;
if (!read_file(path, 0, &data, &size)) {
return false;
}
ignore_headers = NULL;
ignore_headers_len = 0;
if (!str_eq(conf->ignore_headers_in_manifest, "")) {
char *header, *p, *q, *saveptr = NULL;
p = x_strdup(conf->ignore_headers_in_manifest);
q = p;
while ((header = strtok_r(q, PATH_DELIM, &saveptr))) {
ignore_headers = x_realloc(ignore_headers,
(ignore_headers_len+1) * sizeof(char *));
ignore_headers[ignore_headers_len++] = x_strdup(header);
q = NULL;
}
free(p);
}
if (!included_files) {
included_files = create_hashtable(1000, hash_from_string, strings_equal);
}
// Bytes between p and q are pending to be hashed.
char *p = data;
char *q = data;
char *end = data + size;
// There must be at least 7 characters (# 1 "x") left to potentially find an
// include file path.
while (q < end - 7) {
// Check if we look at a line containing the file name of an included file.
// At least the following formats exist (where N is a positive integer):
//
// GCC:
//
// # N "file"
// # N "file" N
// #pragma GCC pch_preprocess "file"
//
// HP's compiler:
//
// #line N "file"
//
// AIX's compiler:
//
// #line N "file"
// #line N
//
// Note that there may be other lines starting with '#' left after
// preprocessing as well, for instance "# pragma".
if (q[0] == '#'
// GCC:
&& ((q[1] == ' ' && q[2] >= '0' && q[2] <= '9')
// GCC precompiled header:
|| (q[1] == 'p'
&& str_startswith(&q[2], "ragma GCC pch_preprocess "))
// HP/AIX:
|| (q[1] == 'l' && q[2] == 'i' && q[3] == 'n' && q[4] == 'e'
&& q[5] == ' '))
&& (q == data || q[-1] == '\n')) {
// Workarounds for preprocessor linemarker bugs in GCC version 6.
if (q[2] == '3') {
if (str_startswith(q, "# 31 \"<command-line>\"\n")) {
// Bogus extra line with #31, after the regular #1: Ignore the whole
// line, and continue parsing.
hash_buffer(hash, p, q - p);
while (q < end && *q != '\n') {
q++;
}
q++;
p = q;
continue;
} else if (str_startswith(q, "# 32 \"<command-line>\" 2\n")) {
// Bogus wrong line with #32, instead of regular #1: Replace the line
// number with the usual one.
hash_buffer(hash, p, q - p);
q += 1;
q[0] = '#';
q[1] = ' ';
q[2] = '1';
p = q;
}
}
while (q < end && *q != '"' && *q != '\n') {
q++;
}
if (q < end && *q == '\n') {
// A newline before the quotation mark -> no match.
continue;
}
q++;
if (q >= end) {
cc_log("Failed to parse included file path");
free(data);
return false;
}
// q points to the beginning of an include file path
hash_buffer(hash, p, q - p);
p = q;
while (q < end && *q != '"') {
q++;
}
// Look for preprocessor flags, after the "filename".
bool system = false;
char *r = q + 1;
while (r < end && *r != '\n') {
if (*r == '3') { // System header.
system = true;
}
r++;
}
// p and q span the include file path.
char *inc_path = x_strndup(p, q - p);
if (!has_absolute_include_headers) {
has_absolute_include_headers = is_absolute_path(inc_path);
}
inc_path = make_relative_path(inc_path);
bool should_hash_inc_path = true;
if (!conf->hash_dir) {
char *cwd = gnu_getcwd();
if (str_startswith(inc_path, cwd) && str_endswith(inc_path, "//")) {
// When compiling with -g or similar, GCC adds the absolute path to
// CWD like this:
//
// # 1 "CWD//"
//
// If the user has opted out of including the CWD in the hash, don't
// hash it. See also how debug_prefix_map is handled.
should_hash_inc_path = false;
}
free(cwd);
}
if (should_hash_inc_path) {
hash_string(hash, inc_path);
}
remember_include_file(inc_path, hash, system);
p = q; // Everything of interest between p and q has been hashed now.
} else if (q[0] == '.' && q[1] == 'i' && q[2] == 'n' && q[3] == 'c'
&& q[4] == 'b' && q[5] == 'i' && q[6] == 'n') {
// An assembler .inc bin (without the space) statement, which could be
// part of inline assembly, refers to an external file. If the file
// changes, the hash should change as well, but finding out what file to
// hash is too hard for ccache, so just bail out.
cc_log("Found unsupported .inc" "bin directive in source code");
stats_update(STATS_UNSUPPORTED_DIRECTIVE);
failed();
} else if (pump && strncmp(q, "_________", 9) == 0) {
// Unfortunately the distcc-pump wrapper outputs standard output lines:
// __________Using distcc-pump from /usr/bin
// __________Using # distcc servers in pump mode
// __________Shutting down distcc-pump include server
while (q < end && *q != '\n') {
q++;
}
if (*q == '\n') {
q++;
}
p = q;
continue;
} else {
q++;
}
}
hash_buffer(hash, p, (end - p));
free(data);
// Explicitly check the .gch/.pch/.pth file, Clang does not include any
// mention of it in the preprocessed output.
if (included_pch_file) {
char *path = x_strdup(included_pch_file);
path = make_relative_path(path);
hash_string(hash, path);
remember_include_file(path, hash, false);
}
return true;
}
// Replace absolute paths with relative paths in the provided dependency file.
static void
use_relative_paths_in_depfile(const char *depfile)
{
if (str_eq(conf->base_dir, "")) {
cc_log("Base dir not set, skip using relative paths");
return; // nothing to do
}
if (!has_absolute_include_headers) {
cc_log("No absolute path for included files found, skip using relative"
" paths");
return; // nothing to do
}
FILE *f;
f = fopen(depfile, "r");
if (!f) {
cc_log("Cannot open dependency file: %s (%s)", depfile, strerror(errno));
return;
}
char *tmp_file = format("%s.tmp", depfile);
FILE *tmpf = create_tmp_file(&tmp_file, "w");
bool result = false;
char buf[10000];
while (fgets(buf, sizeof(buf), f) && !ferror(tmpf)) {
char *saveptr;
char *token = strtok_r(buf, " \t", &saveptr);
while (token) {
char *relpath;
if (is_absolute_path(token) && str_startswith(token, conf->base_dir)) {
relpath = make_relative_path(x_strdup(token));
result = true;
} else {
relpath = token;
}
if (token != buf) { // This is a dependency file.
fputc(' ', tmpf);
}
fputs(relpath, tmpf);
if (relpath != token) {
free(relpath);
}
token = strtok_r(NULL, " \t", &saveptr);
}
}
if (ferror(f)) {
cc_log("Error reading dependency file: %s, skip relative path usage",
depfile);
result = false;
goto out;
}
if (ferror(tmpf)) {
cc_log("Error writing temporary dependency file: %s, skip relative path"
" usage", tmp_file);
result = false;
goto out;
}
out:
fclose(tmpf);
fclose(f);
if (result) {
if (x_rename(tmp_file, depfile) != 0) {
cc_log("Error renaming dependency file: %s -> %s (%s), skip relative"
" path usage", tmp_file, depfile, strerror(errno));
result = false;
} else {
cc_log("Renamed dependency file: %s -> %s", tmp_file, depfile);
}
}
if (!result) {
cc_log("Removing temporary dependency file: %s", tmp_file);
x_unlink(tmp_file);
}
free(tmp_file);
}
// Helper method for copy_file_to_cache and move_file_to_cache_same_fs.
static void
do_copy_or_move_file_to_cache(const char *source, const char *dest, bool copy)
{
assert(!conf->read_only);
assert(!conf->read_only_direct);
struct stat orig_dest_st;
bool orig_dest_existed = stat(dest, &orig_dest_st) == 0;
int compression_level = conf->compression ? conf->compression_level : 0;
bool do_move = !copy && !conf->compression;
bool do_link = copy && conf->hard_link && !conf->compression;
if (do_move) {
move_uncompressed_file(source, dest, compression_level);
} else {
if (do_link) {
x_unlink(dest);
int ret = link(source, dest);
if (ret == 0) {
} else {
cc_log("Failed to link %s to %s: %s", source, dest, strerror(errno));
cc_log("Falling back to copying");
do_link = false;
}
}
if (!do_link) {
int ret = copy_file(source, dest, compression_level);
if (ret != 0) {
cc_log("Failed to copy %s to %s: %s", source, dest, strerror(errno));
stats_update(STATS_ERROR);
failed();
}
}
}
if (!copy && conf->compression) {
// We fell back to copying since dest should be compressed, so clean up.
x_unlink(source);
}
cc_log("Stored in cache: %s -> %s (%s)",
source,
dest,
do_move ? "moved" : (do_link ? "linked" : "copied"));
struct stat st;
if (x_stat(dest, &st) != 0) {
stats_update(STATS_ERROR);
failed();
}
stats_update_size(
file_size(&st) - (orig_dest_existed ? file_size(&orig_dest_st) : 0),
orig_dest_existed ? 0 : 1);
}
// Copy a file into the cache.
//
// dest must be a path in the cache (see get_path_in_cache). source does not
// have to be on the same file system as dest.
//
// An attempt will be made to hard link source to dest if conf->hard_link is
// true and conf->compression is false, otherwise copy. dest will be compressed
// if conf->compression is true.
static void
copy_file_to_cache(const char *source, const char *dest)
{
do_copy_or_move_file_to_cache(source, dest, true);
}
// Move a file into the cache.
//
// dest must be a path in the cache (see get_path_in_cache). source must be on
// the same file system as dest. dest will be compressed if conf->compression
// is true.
static void
move_file_to_cache_same_fs(const char *source, const char *dest)
{
do_copy_or_move_file_to_cache(source, dest, false);
}
// Copy or link a file from the cache.
static void
get_file_from_cache(const char *source, const char *dest)
{
int ret;
bool do_link = conf->hard_link && !file_is_compressed(source);
if (do_link) {
x_unlink(dest);
ret = link(source, dest);
} else {
ret = copy_file(source, dest, 0);
}
if (ret == -1) {
if (errno == ENOENT || errno == ESTALE) {
cc_log("File missing in cache: %s", source);
stats_update(STATS_MISSING);
} else {
cc_log("Failed to %s %s to %s: %s",
do_link ? "link" : "copy",
source,
dest,
strerror(errno));
stats_update(STATS_ERROR);
}
// If there was trouble getting a file from the cached result, wipe the
// whole cached result for consistency.
x_unlink(cached_stderr);
x_unlink(cached_obj);
x_unlink(cached_dep);
x_unlink(cached_cov);
x_unlink(cached_su);
x_unlink(cached_dia);
x_unlink(cached_dwo);
failed();
}
cc_log("Created from cache: %s -> %s", source, dest);
}
// Send cached stderr, if any, to stderr.
static void
send_cached_stderr(void)
{
int fd_stderr = open(cached_stderr, O_RDONLY | O_BINARY);
if (fd_stderr != -1) {
copy_fd(fd_stderr, 2);
close(fd_stderr);
}
}
// Create or update the manifest file.
void update_manifest_file(void)
{
if (!conf->direct_mode
|| !included_files
|| conf->read_only
|| conf->read_only_direct) {
return;
}
struct stat st;
size_t old_size = 0; // in bytes
if (stat(manifest_path, &st) == 0) {
old_size = file_size(&st);
}
if (manifest_put(manifest_path, cached_obj_hash, included_files)) {
cc_log("Added object file hash to %s", manifest_path);
update_mtime(manifest_path);
if (x_stat(manifest_path, &st) == 0) {
stats_update_size(file_size(&st) - old_size, old_size == 0 ? 1 : 0);
}
} else {
cc_log("Failed to add object file hash to %s", manifest_path);
}
}
// Run the real compiler and put the result in cache.
static void
to_cache(struct args *args)
{
char *tmp_stdout = format("%s.tmp.stdout", cached_obj);
int tmp_stdout_fd = create_tmp_fd(&tmp_stdout);
char *tmp_stderr = format("%s.tmp.stderr", cached_obj);
int tmp_stderr_fd = create_tmp_fd(&tmp_stderr);
args_add(args, "-o");
args_add(args, output_obj);
if (generating_diagnostics) {
args_add(args, "--serialize-diagnostics");
args_add(args, output_dia);
}
// Turn off DEPENDENCIES_OUTPUT when running cc1, because otherwise it will
// emit a line like this:
//
// tmp.stdout.vexed.732.o: /home/mbp/.ccache/tmp.stdout.vexed.732.i
x_unsetenv("DEPENDENCIES_OUTPUT");
if (conf->run_second_cpp) {
args_add(args, input_file);
} else {
args_add(args, i_tmpfile);
}
cc_log("Running real compiler");
int status =
execute(args->argv, tmp_stdout_fd, tmp_stderr_fd, &compiler_pid);
args_pop(args, 3);
struct stat st;
if (x_stat(tmp_stdout, &st) != 0) {
// The stdout file was removed - cleanup in progress? Better bail out.
stats_update(STATS_MISSING);
tmp_unlink(tmp_stdout);
tmp_unlink(tmp_stderr);
failed();
}
// distcc-pump outputs lines like this:
// __________Using # distcc servers in pump mode
if (st.st_size != 0 && guessed_compiler != GUESSED_PUMP) {
cc_log("Compiler produced stdout");
stats_update(STATS_STDOUT);
tmp_unlink(tmp_stdout);
tmp_unlink(tmp_stderr);
failed();
}
tmp_unlink(tmp_stdout);
// Merge stderr from the preprocessor (if any) and stderr from the real
// compiler into tmp_stderr.
if (cpp_stderr) {
char *tmp_stderr2 = format("%s.2", tmp_stderr);
if (x_rename(tmp_stderr, tmp_stderr2)) {
cc_log("Failed to rename %s to %s: %s", tmp_stderr, tmp_stderr2,
strerror(errno));
failed();
}
int fd_cpp_stderr = open(cpp_stderr, O_RDONLY | O_BINARY);
if (fd_cpp_stderr == -1) {
cc_log("Failed opening %s: %s", cpp_stderr, strerror(errno));
failed();
}
int fd_real_stderr = open(tmp_stderr2, O_RDONLY | O_BINARY);
if (fd_real_stderr == -1) {
cc_log("Failed opening %s: %s", tmp_stderr2, strerror(errno));
failed();
}
int fd_result =
open(tmp_stderr, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
if (fd_result == -1) {
cc_log("Failed opening %s: %s", tmp_stderr, strerror(errno));
failed();
}
copy_fd(fd_cpp_stderr, fd_result);
copy_fd(fd_real_stderr, fd_result);
close(fd_cpp_stderr);
close(fd_real_stderr);
close(fd_result);
tmp_unlink(tmp_stderr2);
free(tmp_stderr2);
}
if (status != 0) {
cc_log("Compiler gave exit status %d", status);
stats_update(STATS_STATUS);
int fd = open(tmp_stderr, O_RDONLY | O_BINARY);
if (fd != -1) {
// We can output stderr immediately instead of rerunning the compiler.
copy_fd(fd, 2);
close(fd);
tmp_unlink(tmp_stderr);
x_exit(status);
}
tmp_unlink(tmp_stderr);
failed();
}
if (stat(output_obj, &st) != 0) {
cc_log("Compiler didn't produce an object file");
stats_update(STATS_NOOUTPUT);
failed();
}
if (st.st_size == 0) {
cc_log("Compiler produced an empty object file");
stats_update(STATS_EMPTYOUTPUT);
failed();
}
if (x_stat(tmp_stderr, &st) != 0) {
stats_update(STATS_ERROR);
failed();
}
if (st.st_size > 0) {
move_file_to_cache_same_fs(tmp_stderr, cached_stderr);
} else {
tmp_unlink(tmp_stderr);
if (conf->recache) {
// If recaching, we need to remove any previous .stderr.
x_unlink(cached_stderr);
}
}
copy_file_to_cache(output_obj, cached_obj);
if (generating_dependencies) {
use_relative_paths_in_depfile(output_dep);
copy_file_to_cache(output_dep, cached_dep);
}
if (generating_coverage) {
copy_file_to_cache(output_cov, cached_cov);
}
if (generating_stackusage) {
copy_file_to_cache(output_su, cached_su);
}
if (generating_diagnostics) {
copy_file_to_cache(output_dia, cached_dia);
}
if (using_split_dwarf) {
copy_file_to_cache(output_dwo, cached_dwo);
}
stats_update(STATS_TOCACHE);
// Make sure we have a CACHEDIR.TAG in the cache part of cache_dir. This can
// be done almost anywhere, but we might as well do it near the end as we
// save the stat call if we exit early.
{
char *first_level_dir = dirname(stats_file);
if (create_cachedirtag(first_level_dir) != 0) {
cc_log("Failed to create %s/CACHEDIR.TAG (%s)\n",
first_level_dir, strerror(errno));
stats_update(STATS_ERROR);
failed();
}
free(first_level_dir);
// Remove any CACHEDIR.TAG on the cache_dir level where it was located in
// previous ccache versions.
if (getpid() % 1000 == 0) {
char *path = format("%s/CACHEDIR.TAG", conf->cache_dir);
x_unlink(path);
free(path);
}
}
// Everything OK.
send_cached_stderr();
update_manifest_file();
free(tmp_stderr);
free(tmp_stdout);
}
// Find the object file name by running the compiler in preprocessor mode.
// Returns the hash as a heap-allocated hex string.
static struct file_hash *
get_object_name_from_cpp(struct args *args, struct mdfour *hash)
{
time_of_compilation = time(NULL);
char *path_stderr = NULL;
char *path_stdout;
int status;
if (direct_i_file) {
// We are compiling a .i or .ii file - that means we can skip the cpp stage
// and directly form the correct i_tmpfile.
path_stdout = input_file;
status = 0;
} else {
// Run cpp on the input file to obtain the .i.
// Limit the basename to 10 characters in order to cope with filesystem with
// small maximum filename length limits.
char *input_base = basename(input_file);
char *tmp = strchr(input_base, '.');
if (tmp) {
*tmp = 0;
}
if (strlen(input_base) > 10) {
input_base[10] = 0;
}
path_stdout = format("%s/%s.stdout", temp_dir(), input_base);
int path_stdout_fd = create_tmp_fd(&path_stdout);
add_pending_tmp_file(path_stdout);
path_stderr = format("%s/tmp.cpp_stderr", temp_dir());
int path_stderr_fd = create_tmp_fd(&path_stderr);
add_pending_tmp_file(path_stderr);
int args_added = 2;
args_add(args, "-E");
if (conf->keep_comments_cpp) {
args_add(args, "-C");
args_added = 3;
}
args_add(args, input_file);
add_prefix(args, conf->prefix_command_cpp);
cc_log("Running preprocessor");
status = execute(args->argv, path_stdout_fd, path_stderr_fd, &compiler_pid);
args_pop(args, args_added);
}
if (status != 0) {
cc_log("Preprocessor gave exit status %d", status);
stats_update(STATS_PREPROCESSOR);
failed();
}
if (conf->unify) {
// When we are doing the unifying tricks we need to include the input file
// name in the hash to get the warnings right.
hash_delimiter(hash, "unifyfilename");
hash_string(hash, input_file);
hash_delimiter(hash, "unifycpp");
bool debug_unify = getenv("CCACHE_DEBUG_UNIFY");
if (unify_hash(hash, path_stdout, debug_unify) != 0) {
stats_update(STATS_ERROR);
cc_log("Failed to unify %s", path_stdout);
failed();
}
} else {
hash_delimiter(hash, "cpp");
if (!process_preprocessed_file(hash, path_stdout,
guessed_compiler == GUESSED_PUMP)) {
stats_update(STATS_ERROR);
failed();
}
}
hash_delimiter(hash, "cppstderr");
if (!direct_i_file && !hash_file(hash, path_stderr)) {
fatal("Failed to open %s: %s", path_stderr, strerror(errno));
}
if (direct_i_file) {
i_tmpfile = input_file;
} else {
// i_tmpfile needs the proper cpp_extension for the compiler to do its
// thing correctly
i_tmpfile = format("%s.%s", path_stdout, conf->cpp_extension);
x_rename(path_stdout, i_tmpfile);
add_pending_tmp_file(i_tmpfile);
}
if (conf->run_second_cpp) {
free(path_stderr);
} else {
// If we are using the CPP trick, we need to remember this stderr data and
// output it just before the main stderr from the compiler pass.
cpp_stderr = path_stderr;
hash_delimiter(hash, "runsecondcpp");
hash_string(hash, "false");
}
struct file_hash *result = x_malloc(sizeof(*result));
hash_result_as_bytes(hash, result->hash);
result->size = hash->totalN;
return result;
}
static void
update_cached_result_globals(struct file_hash *hash)
{
char *object_name = format_hash_as_string(hash->hash, hash->size);
cached_obj_hash = hash;
cached_obj = get_path_in_cache(object_name, ".o");
cached_stderr = get_path_in_cache(object_name, ".stderr");
cached_dep = get_path_in_cache(object_name, ".d");
cached_cov = get_path_in_cache(object_name, ".gcno");
cached_su = get_path_in_cache(object_name, ".su");
cached_dia = get_path_in_cache(object_name, ".dia");
cached_dwo = get_path_in_cache(object_name, ".dwo");
stats_file = format("%s/%c/stats", conf->cache_dir, object_name[0]);
free(object_name);
}
// Hash mtime or content of a file, or the output of a command, according to
// the CCACHE_COMPILERCHECK setting.
static void
hash_compiler(struct mdfour *hash, struct stat *st, const char *path,
bool allow_command)
{
if (str_eq(conf->compiler_check, "none")) {
// Do nothing.
} else if (str_eq(conf->compiler_check, "mtime")) {
hash_delimiter(hash, "cc_mtime");
hash_int(hash, st->st_size);
hash_int(hash, st->st_mtime);
} else if (str_startswith(conf->compiler_check, "string:")) {
hash_delimiter(hash, "cc_hash");
hash_string(hash, conf->compiler_check + strlen("string:"));
} else if (str_eq(conf->compiler_check, "content") || !allow_command) {
hash_delimiter(hash, "cc_content");
hash_file(hash, path);
} else { // command string
if (!hash_multicommand_output(
hash, conf->compiler_check, orig_args->argv[0])) {
fatal("Failure running compiler check command: %s", conf->compiler_check);
}
}
}
// Hash the host compiler(s) invoked by nvcc.
//
// If ccbin_st and ccbin are set, they refer to a directory or compiler set
// with -ccbin/--compiler-bindir. If they are NULL, the compilers are looked up
// in PATH instead.
static void
hash_nvcc_host_compiler(struct mdfour *hash, struct stat *ccbin_st,
const char *ccbin)
{
// From <http://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html>:
//
// "[...] Specify the directory in which the compiler executable resides.
// The host compiler executable name can be also specified to ensure that
// the correct host compiler is selected."
//
// and
//
// "On all platforms, the default host compiler executable (gcc and g++ on
// Linux, clang and clang++ on Mac OS X, and cl.exe on Windows) found in
// the current execution search path will be used".
if (!ccbin || S_ISDIR(ccbin_st->st_mode)) {
#if defined(__APPLE__)
const char *compilers[] = {"clang", "clang++"};
#elif defined(_WIN32)
const char *compilers[] = {"cl.exe"};
#else
const char *compilers[] = {"gcc", "g++"};
#endif
for (size_t i = 0; i < ARRAY_SIZE(compilers); i++) {
if (ccbin) {
char *path = format("%s/%s", ccbin, compilers[i]);
struct stat st;
if (stat(path, &st) == 0) {
hash_compiler(hash, &st, path, false);
}
free(path);
} else {
char *path = find_executable(compilers[i], MYNAME);
if (path) {
struct stat st;
x_stat(path, &st);
hash_compiler(hash, &st, ccbin, false);
free(path);
}
}
}
} else {
hash_compiler(hash, ccbin_st, ccbin, false);
}
}
// Update a hash sum with information common for the direct and preprocessor
// modes.
static void
calculate_common_hash(struct args *args, struct mdfour *hash)
{
hash_string(hash, HASH_PREFIX);
// We have to hash the extension, as a .i file isn't treated the same by the
// compiler as a .ii file.
hash_delimiter(hash, "ext");
hash_string(hash, conf->cpp_extension);
#ifdef _WIN32
const char *ext = strrchr(args->argv[0], '.');
char full_path_win_ext[MAX_PATH + 1] = {0};
add_exe_ext_if_no_to_fullpath(full_path_win_ext, MAX_PATH, ext,
args->argv[0]);
const char *full_path = full_path_win_ext;
#else
const char *full_path = args->argv[0];
#endif
struct stat st;
if (x_stat(full_path, &st) != 0) {
stats_update(STATS_COMPILER);
failed();
}
// Hash information about the compiler.
hash_compiler(hash, &st, args->argv[0], true);
// Also hash the compiler name as some compilers use hard links and behave
// differently depending on the real name.
hash_delimiter(hash, "cc_name");
char *p = basename(args->argv[0]);
hash_string(hash, p);
free(p);
// Possibly hash the current working directory.
if (generating_debuginfo && conf->hash_dir) {
char *cwd = gnu_getcwd();
for (size_t i = 0; i < debug_prefix_maps_len; i++) {
char *map = debug_prefix_maps[i];
char *sep = strchr(map, '=');
if (sep) {
char *old = x_strndup(map, sep - map);
char *new = x_strdup(sep + 1);
cc_log("Relocating debuginfo CWD %s from %s to %s", cwd, old, new);
if (str_startswith(cwd, old)) {
char *dir = format("%s%s", new, cwd + strlen(old));
free(cwd);
cwd = dir;
}
free(old);
free(new);
}
}
if (cwd) {
cc_log("Hashing CWD %s", cwd);
hash_delimiter(hash, "cwd");
hash_string(hash, cwd);
free(cwd);
}
}
// Possibly hash the coverage data file path.
if (generating_coverage && profile_arcs) {
char *dir = dirname(output_obj);
if (profile_dir) {
dir = x_strdup(profile_dir);
} else {
char *real_dir = x_realpath(dir);
free(dir);
dir = real_dir;
}
if (dir) {
char *base_name = basename(output_obj);
p = remove_extension(base_name);
free(base_name);
char *gcda_path = format("%s/%s.gcda", dir, p);
cc_log("Hashing coverage path %s", gcda_path);
free(p);
hash_delimiter(hash, "gcda");
hash_string(hash, gcda_path);
free(dir);
}
}
// Possibly hash the sanitize blacklist file path.
if (sanitize_blacklist) {
cc_log("Hashing sanitize blacklist %s", sanitize_blacklist);
hash_delimiter(hash, "sanitizeblacklist");
if (!hash_file(hash, sanitize_blacklist)) {
stats_update(STATS_BADEXTRAFILE);
failed();
}
}
if (!str_eq(conf->extra_files_to_hash, "")) {
char *p = x_strdup(conf->extra_files_to_hash);
char *q = p;
char *path;
char *saveptr = NULL;
while ((path = strtok_r(q, PATH_DELIM, &saveptr))) {
cc_log("Hashing extra file %s", path);
hash_delimiter(hash, "extrafile");
if (!hash_file(hash, path)) {
stats_update(STATS_BADEXTRAFILE);
failed();
}
q = NULL;
}
free(p);
}
// Possibly hash GCC_COLORS (for color diagnostics).
if (guessed_compiler == GUESSED_GCC) {
const char *gcc_colors = getenv("GCC_COLORS");
if (gcc_colors) {
hash_delimiter(hash, "gcccolors");
hash_string(hash, gcc_colors);
}
}
}
// Update a hash sum with information specific to the direct and preprocessor
// modes and calculate the object hash. Returns the object hash on success,
// otherwise NULL. Caller frees.
static struct file_hash *
calculate_object_hash(struct args *args, struct mdfour *hash, int direct_mode)
{
bool found_ccbin = false;
if (direct_mode) {
hash_delimiter(hash, "manifest version");
hash_int(hash, MANIFEST_VERSION);
}
// clang will emit warnings for unused linker flags, so we shouldn't skip
// those arguments.
int is_clang = (guessed_compiler == GUESSED_CLANG || guessed_compiler == GUESSED_UNKNOWN);
// First the arguments.
for (int i = 1; i < args->argc; i++) {
// -L doesn't affect compilation (except for clang).
if (i < args->argc-1 && str_eq(args->argv[i], "-L") && !is_clang) {
i++;
continue;
}
if (str_startswith(args->argv[i], "-L") && !is_clang) {
continue;
}
// -Wl,... doesn't affect compilation (except for clang).
if (str_startswith(args->argv[i], "-Wl,") && !is_clang) {
continue;
}
// The -fdebug-prefix-map option may be used in combination with
// CCACHE_BASEDIR to reuse results across different directories. Skip it
// from hashing.
if (str_startswith(args->argv[i], "-fdebug-prefix-map=")) {
continue;
}
// When using the preprocessor, some arguments don't contribute to the
// hash. The theory is that these arguments will change the output of -E if
// they are going to have any effect at all. For precompiled headers this
// might not be the case.
if (!direct_mode && !output_is_precompiled_header
&& !using_precompiled_header) {
if (compopt_affects_cpp(args->argv[i])) {
if (compopt_takes_arg(args->argv[i])) {
i++;
}
continue;
}
if (compopt_short(compopt_affects_cpp, args->argv[i])) {
continue;
}
}
// If we're generating dependencies, we make sure to skip the filename of
// the dependency file, since it doesn't impact the output.
if (generating_dependencies) {
if (str_startswith(args->argv[i], "-Wp,")) {
if (str_startswith(args->argv[i], "-Wp,-MD,")
&& !strchr(args->argv[i] + 8, ',')) {
hash_string_length(hash, args->argv[i], 8);
continue;
} else if (str_startswith(args->argv[i], "-Wp,-MMD,")
&& !strchr(args->argv[i] + 9, ',')) {
hash_string_length(hash, args->argv[i], 9);
continue;
}
} else if (str_startswith(args->argv[i], "-MF")) {
// In either case, hash the "-MF" part.
hash_delimiter(hash, "arg");
hash_string_length(hash, args->argv[i], 3);
bool separate_argument = (strlen(args->argv[i]) == 3);
if (separate_argument) {
// Next argument is dependency name, so skip it.
i++;
}
continue;
}
}
char *p = NULL;
if (str_startswith(args->argv[i], "-specs=")) {
p = args->argv[i] + 7;
} else if (str_startswith(args->argv[i], "--specs=")) {
p = args->argv[i] + 8;
}
struct stat st;
if (p && x_stat(p, &st) == 0) {
// If given an explicit specs file, then hash that file, but don't
// include the path to it in the hash.
hash_delimiter(hash, "specs");
hash_compiler(hash, &st, p, false);
continue;
}
if (str_startswith(args->argv[i], "-fplugin=")
&& x_stat(args->argv[i] + 9, &st) == 0) {
hash_delimiter(hash, "plugin");
hash_compiler(hash, &st, args->argv[i] + 9, false);
continue;
}
if (str_eq(args->argv[i], "-Xclang")
&& i + 3 < args->argc
&& str_eq(args->argv[i+1], "-load")
&& str_eq(args->argv[i+2], "-Xclang")
&& x_stat(args->argv[i+3], &st) == 0) {
hash_delimiter(hash, "plugin");
hash_compiler(hash, &st, args->argv[i+3], false);
i += 3;
continue;
}
if ((str_eq(args->argv[i], "-ccbin")
|| str_eq(args->argv[i], "--compiler-bindir"))
&& i + 1 < args->argc
&& x_stat(args->argv[i+1], &st) == 0) {
found_ccbin = true;
hash_delimiter(hash, "ccbin");
hash_nvcc_host_compiler(hash, &st, args->argv[i+1]);
i++;
continue;
}
// All other arguments are included in the hash.
hash_delimiter(hash, "arg");
hash_string(hash, args->argv[i]);
if (i + 1 < args->argc && compopt_takes_arg(args->argv[i])) {
i++;
hash_delimiter(hash, "arg");
hash_string(hash, args->argv[i]);
}
}
if (!found_ccbin && str_eq(actual_language, "cuda")) {
hash_nvcc_host_compiler(hash, NULL, NULL);
}
// For profile generation (-fprofile-arcs, -fprofile-generate):
// - hash profile directory
//
// For profile usage (-fprofile-use):
// - hash profile data
//
// -fbranch-probabilities and -fvpt usage is covered by
// -fprofile-generate/-fprofile-use.
//
// The profile directory can be specified as an argument to
// -fprofile-generate=, -fprofile-use= or -fprofile-dir=.
if (profile_generate) {
if (!profile_dir) {
profile_dir = get_cwd();
}
cc_log("Adding profile directory %s to our hash", profile_dir);
hash_delimiter(hash, "-fprofile-dir");
hash_string(hash, profile_dir);
}
if (profile_use) {
// Calculate gcda name.
if (!profile_dir) {
profile_dir = get_cwd();
}
char *base_name = remove_extension(output_obj);
char *gcda_name = format("%s/%s.gcda", profile_dir, base_name);
cc_log("Adding profile data %s to our hash", gcda_name);
// Add the gcda to our hash.
hash_delimiter(hash, "-fprofile-use");
hash_file(hash, gcda_name);
free(base_name);
free(gcda_name);
}
// Adding -arch to hash since cpp output is affected.
for (size_t i = 0; i < arch_args_size; ++i) {
hash_delimiter(hash, "-arch");
hash_string(hash, arch_args[i]);
}
struct file_hash *object_hash = NULL;
if (direct_mode) {
// Hash environment variables that affect the preprocessor output.
const char *envvars[] = {
"CPATH",
"C_INCLUDE_PATH",
"CPLUS_INCLUDE_PATH",
"OBJC_INCLUDE_PATH",
"OBJCPLUS_INCLUDE_PATH", // clang
NULL
};
for (const char **p = envvars; *p; ++p) {
char *v = getenv(*p);
if (v) {
hash_delimiter(hash, *p);
hash_string(hash, v);
}
}
if (!(conf->sloppiness & SLOPPY_FILE_MACRO)) {
// The source code file or an include file may contain __FILE__, so make
// sure that the hash is unique for the file name.
hash_delimiter(hash, "inputfile");
hash_string(hash, input_file);
}
hash_delimiter(hash, "sourcecode");
int result = hash_source_code_file(conf, hash, input_file);
if (result & HASH_SOURCE_CODE_ERROR) {
failed();
}
if (result & HASH_SOURCE_CODE_FOUND_TIME) {
cc_log("Disabling direct mode");
conf->direct_mode = false;
return NULL;
}
char *manifest_name = hash_result(hash);
manifest_path = get_path_in_cache(manifest_name, ".manifest");
free(manifest_name);
cc_log("Looking for object file hash in %s", manifest_path);
object_hash = manifest_get(conf, manifest_path);
if (object_hash) {
cc_log("Got object file hash from manifest");
} else {
cc_log("Did not find object file hash in manifest");
}
} else {
if (arch_args_size == 0) {
object_hash = get_object_name_from_cpp(args, hash);
cc_log("Got object file hash from preprocessor");
} else {
args_add(args, "-arch");
for (size_t i = 0; i < arch_args_size; ++i) {
args_add(args, arch_args[i]);
object_hash = get_object_name_from_cpp(args, hash);
cc_log("Got object file hash from preprocessor with -arch %s",
arch_args[i]);
if (i != arch_args_size - 1) {
free(object_hash);
object_hash = NULL;
}
args_pop(args, 1);
}
args_pop(args, 1);
}
if (generating_dependencies) {
cc_log("Preprocessor created %s", output_dep);
}
}
return object_hash;
}
// Try to return the compile result from cache. If we can return from cache
// then this function exits with the correct status code, otherwise it returns.
static void
from_cache(enum fromcache_call_mode mode, bool put_object_in_manifest)
{
// The user might be disabling cache hits.
if (conf->recache) {
return;
}
// If we're using Clang, we can't trust a precompiled header object based on
// running the preprocessor since clang will produce a fatal error when the
// precompiled header is used and one of the included files has an updated
// timestamp:
//
// file 'foo.h' has been modified since the precompiled header 'foo.pch'
// was built
if ((guessed_compiler == GUESSED_CLANG || guessed_compiler == GUESSED_UNKNOWN)
&& output_is_precompiled_header
&& mode == FROMCACHE_CPP_MODE) {
cc_log("Not considering cached precompiled header in preprocessor mode");
return;
}
// Occasionally, e.g. on hard reset, our cache ends up as just filesystem
// meta-data with no content. Catch an easy case of this.
struct stat st;
if (stat(cached_obj, &st) != 0) {
cc_log("Object file %s not in cache", cached_obj);
return;
}
if (st.st_size == 0) {
cc_log("Invalid (empty) object file %s in cache", cached_obj);
x_unlink(cached_obj);
return;
}
// (If mode != FROMCACHE_DIRECT_MODE, the dependency file is created by gcc.)
bool produce_dep_file =
generating_dependencies && mode == FROMCACHE_DIRECT_MODE;
// Get result from cache.
if (!str_eq(output_obj, "/dev/null")) {
get_file_from_cache(cached_obj, output_obj);
if (using_split_dwarf) {
get_file_from_cache(cached_dwo, output_dwo);
}
}
if (produce_dep_file) {
get_file_from_cache(cached_dep, output_dep);
}
if (generating_coverage) {
get_file_from_cache(cached_cov, output_cov);
}
if (generating_stackusage) {
get_file_from_cache(cached_su, output_su);
}
if (generating_diagnostics) {
get_file_from_cache(cached_dia, output_dia);
}
// Update modification timestamps to save files from LRU cleanup. Also gives
// files a sensible mtime when hard-linking.
update_mtime(cached_obj);
update_mtime(cached_stderr);
if (produce_dep_file) {
update_mtime(cached_dep);
}
if (generating_coverage) {
update_mtime(cached_cov);
}
if (generating_stackusage) {
update_mtime(cached_su);
}
if (generating_diagnostics) {
update_mtime(cached_dia);
}
if (cached_dwo) {
update_mtime(cached_dwo);
}
send_cached_stderr();
if (put_object_in_manifest) {
update_manifest_file();
}
// Log the cache hit.
switch (mode) {
case FROMCACHE_DIRECT_MODE:
cc_log("Succeeded getting cached result");
stats_update(STATS_CACHEHIT_DIR);
break;
case FROMCACHE_CPP_MODE:
cc_log("Succeeded getting cached result");
stats_update(STATS_CACHEHIT_CPP);
break;
}
// And exit with the right status code.
x_exit(0);
}
// Find the real compiler. We just search the PATH to find an executable of the
// same name that isn't a link to ourselves.
static void
find_compiler(char **argv)
{
// We might be being invoked like "ccache gcc -c foo.c".
char *base = basename(argv[0]);
if (same_executable_name(base, MYNAME)) {
args_remove_first(orig_args);
free(base);
if (is_full_path(orig_args->argv[0])) {
// A full path was given.
return;
}
base = basename(orig_args->argv[0]);
}
// Support user override of the compiler.
if (!str_eq(conf->compiler, "")) {
base = conf->compiler;
}
char *compiler = find_executable(base, MYNAME);
if (!compiler) {
stats_update(STATS_COMPILER);
fatal("Could not find compiler \"%s\" in PATH", base);
}
if (str_eq(compiler, argv[0])) {
fatal("Recursive invocation (the name of the ccache binary must be \"%s\")",
MYNAME);
}
orig_args->argv[0] = compiler;
}
bool
is_precompiled_header(const char *path)
{
const char *ext = get_extension(path);
char *dir = dirname(path);
const char *dir_ext = get_extension(dir);
bool result =
str_eq(ext, ".gch")
|| str_eq(ext, ".pch")
|| str_eq(ext, ".pth")
|| str_eq(dir_ext, ".gch"); // See "Precompiled Headers" in GCC docs.
free(dir);
return result;
}
static bool
color_output_possible(void)
{
const char *term_env = getenv("TERM");
return isatty(STDERR_FILENO) && term_env && strcasecmp(term_env, "DUMB") != 0;
}
static bool
detect_pch(const char *option, const char *arg, bool *found_pch)
{
struct stat st;
// Try to be smart about detecting precompiled headers.
char *pch_file = NULL;
if (str_eq(option, "-include-pch") || str_eq(option, "-include-pth")) {
if (stat(arg, &st) == 0) {
cc_log("Detected use of precompiled header: %s", arg);
pch_file = x_strdup(arg);
}
} else {
char *gchpath = format("%s.gch", arg);
if (stat(gchpath, &st) == 0) {
cc_log("Detected use of precompiled header: %s", gchpath);
pch_file = x_strdup(gchpath);
} else {
char *pchpath = format("%s.pch", arg);
if (stat(pchpath, &st) == 0) {
cc_log("Detected use of precompiled header: %s", pchpath);
pch_file = x_strdup(pchpath);
} else {
// clang may use pretokenized headers.
char *pthpath = format("%s.pth", arg);
if (stat(pthpath, &st) == 0) {
cc_log("Detected use of pretokenized header: %s", pthpath);
pch_file = x_strdup(pthpath);
}
free(pthpath);
}
free(pchpath);
}
free(gchpath);
}
if (pch_file) {
if (included_pch_file) {
cc_log("Multiple precompiled headers used: %s and %s\n",
included_pch_file, pch_file);
stats_update(STATS_ARGS);
return false;
}
included_pch_file = pch_file;
*found_pch = true;
}
return true;
}
// Process the compiler options into options suitable for passing to the
// preprocessor and the real compiler. The preprocessor options don't include
// -E; this is added later. Returns true on success, otherwise false.
bool
cc_process_args(struct args *args, struct args **preprocessor_args,
struct args **compiler_args)
{
bool found_c_opt = false;
bool found_S_opt = false;
bool found_pch = false;
bool found_fpch_preprocess = false;
const char *explicit_language = NULL; // As specified with -x.
const char *file_language; // As deduced from file extension.
const char *input_charset = NULL;
// Is the dependency makefile name overridden with -MF?
bool dependency_filename_specified = false;
// Is the dependency makefile target name specified with -MT or -MQ?
bool dependency_target_specified = false;
// expanded_args is a copy of the original arguments given to the compiler
// but with arguments from @file and similar constructs expanded. It's only
// used as a temporary data structure to loop over.
struct args *expanded_args = args_copy(args);
// stripped_args essentially contains all original arguments except those
// that only should be passed to the preprocessor (if run_second_cpp is
// false) and except dependency options (like -MD and friends).
struct args *stripped_args = args_init(0, NULL);
// cpp_args contains arguments that were not added to stripped_args, i.e.
// those that should only be passed to the preprocessor if run_second_cpp is
// false. If run_second_cpp is true, they will be passed to the compiler as
// well.
struct args *cpp_args = args_init(0, NULL);
// dep_args contains dependency options like -MD. They only passed to the
// preprocessor, never to the compiler.
struct args *dep_args = args_init(0, NULL);
bool found_color_diagnostics = false;
bool found_directives_only = false;
bool found_rewrite_includes = false;
int argc = expanded_args->argc;
char **argv = expanded_args->argv;
args_add(stripped_args, argv[0]);
bool result = true;
for (int i = 1; i < argc; i++) {
// The user knows best: just swallow the next arg.
if (str_eq(argv[i], "--ccache-skip")) {
i++;
if (i == argc) {
cc_log("--ccache-skip lacks an argument");
result = false;
goto out;
}
args_add(stripped_args, argv[i]);
continue;
}
// Special case for -E.
if (str_eq(argv[i], "-E")) {
stats_update(STATS_PREPROCESSING);
result = false;
goto out;
}
// Handle "@file" argument.
if (str_startswith(argv[i], "@") || str_startswith(argv[i], "-@")) {
char *argpath = argv[i] + 1;
if (argpath[-1] == '-') {
++argpath;
}
struct args *file_args = args_init_from_gcc_atfile(argpath);
if (!file_args) {
cc_log("Couldn't read arg file %s", argpath);
stats_update(STATS_ARGS);
result = false;
goto out;
}
args_insert(expanded_args, i, file_args, true);
argc = expanded_args->argc;
argv = expanded_args->argv;
i--;
continue;
}
// Handle cuda "-optf" and "--options-file" argument.
if (guessed_compiler == GUESSED_NVCC
&& (str_eq(argv[i], "-optf") || str_eq(argv[i], "--options-file"))) {
if (i == argc - 1) {
cc_log("Expected argument after %s", argv[i]);
stats_update(STATS_ARGS);
result = false;
goto out;
}
++i;
// Argument is a comma-separated list of files.
char *str_start = argv[i];
char *str_end = strchr(str_start, ',');
int index = i + 1;
if (!str_end) {
str_end = str_start + strlen(str_start);
}
while (str_end) {
*str_end = '\0';
struct args *file_args = args_init_from_gcc_atfile(str_start);
if (!file_args) {
cc_log("Couldn't read cuda options file %s", str_start);
stats_update(STATS_ARGS);
result = false;
goto out;
}
int new_index = file_args->argc + index;
args_insert(expanded_args, index, file_args, false);
index = new_index;
str_start = str_end;
str_end = strchr(str_start, ',');
}
argc = expanded_args->argc;
argv = expanded_args->argv;
continue;
}
// These are always too hard.
if (compopt_too_hard(argv[i]) || str_startswith(argv[i], "-fdump-")) {
cc_log("Compiler option %s is unsupported", argv[i]);
stats_update(STATS_UNSUPPORTED_OPTION);
result = false;
goto out;
}
// These are too hard in direct mode.
if (conf->direct_mode && compopt_too_hard_for_direct_mode(argv[i])) {
cc_log("Unsupported compiler option for direct mode: %s", argv[i]);
conf->direct_mode = false;
}
// -Xarch_* options are too hard.
if (str_startswith(argv[i], "-Xarch_")) {
cc_log("Unsupported compiler option :%s", argv[i]);
stats_update(STATS_UNSUPPORTED_OPTION);
result = false;
goto out;
}
// Handle -arch options.
if (str_eq(argv[i], "-arch")) {
if (arch_args_size == MAX_ARCH_ARGS - 1) {
cc_log("Too many -arch compiler options; ccache supports at most %d",
MAX_ARCH_ARGS);
stats_update(STATS_UNSUPPORTED_OPTION);
result = false;
goto out;
}
++i;
arch_args[arch_args_size] = x_strdup(argv[i]); // It will leak.
++arch_args_size;
if (arch_args_size == 2) {
conf->run_second_cpp = true;
}
continue;
}
if (str_eq(argv[i], "-fpch-preprocess")
|| str_eq(argv[i], "-emit-pch")
|| str_eq(argv[i], "-emit-pth")) {
found_fpch_preprocess = true;
}
// We must have -c.
if (str_eq(argv[i], "-c")) {
found_c_opt = true;
continue;
}
// -S changes the default extension.
if (str_eq(argv[i], "-S")) {
args_add(stripped_args, argv[i]);
found_S_opt = true;
continue;
}
// Special handling for -x: remember the last specified language before the
// input file and strip all -x options from the arguments.
if (str_eq(argv[i], "-x")) {
if (i == argc - 1) {
cc_log("Missing argument to %s", argv[i]);
stats_update(STATS_ARGS);
result = false;
goto out;
}
if (!input_file) {
explicit_language = argv[i+1];
}
i++;
continue;
}
if (str_startswith(argv[i], "-x")) {
if (!input_file) {
explicit_language = &argv[i][2];
}
continue;
}
// We need to work out where the output was meant to go.
if (str_eq(argv[i], "-o")) {
if (i == argc - 1) {
cc_log("Missing argument to %s", argv[i]);
stats_update(STATS_ARGS);
result = false;
goto out;
}
output_obj = make_relative_path(x_strdup(argv[i+1]));
i++;
continue;
}
// Alternate form of -o with no space. Nvcc does not support this.
if (str_startswith(argv[i], "-o") && guessed_compiler != GUESSED_NVCC) {
output_obj = make_relative_path(x_strdup(&argv[i][2]));
continue;
}
if (str_eq(argv[i], "-gsplit-dwarf")) {
cc_log("Enabling caching of dwarf files since -gsplit-dwarf is used");
using_split_dwarf = true;
args_add(stripped_args, argv[i]);
continue;
}
if (str_startswith(argv[i], "-fdebug-prefix-map=")) {
debug_prefix_maps = x_realloc(
debug_prefix_maps,
(debug_prefix_maps_len + 1) * sizeof(char *));
debug_prefix_maps[debug_prefix_maps_len++] = x_strdup(argv[i] + 19);
args_add(stripped_args, argv[i]);
continue;
}
// Debugging is handled specially, so that we know if we can strip line
// number info.
if (str_startswith(argv[i], "-g")) {
generating_debuginfo = true;
args_add(stripped_args, argv[i]);
if (conf->unify && !str_eq(argv[i], "-g0")) {
cc_log("%s used; disabling unify mode", argv[i]);
conf->unify = false;
}
if (str_eq(argv[i], "-g3")) {
cc_log("%s used; not compiling preprocessed code", argv[i]);
conf->run_second_cpp = true;
}
continue;
}
// These options require special handling, because they behave differently
// with gcc -E, when the output file is not specified.
if (str_eq(argv[i], "-MD") || str_eq(argv[i], "-MMD")) {
generating_dependencies = true;
args_add(dep_args, argv[i]);
continue;
}
if (str_startswith(argv[i], "-MF")) {
dependency_filename_specified = true;
free(output_dep);
char *arg;
bool separate_argument = (strlen(argv[i]) == 3);
if (separate_argument) {
// -MF arg
if (i == argc - 1) {
cc_log("Missing argument to %s", argv[i]);
stats_update(STATS_ARGS);
result = false;
goto out;
}
arg = argv[i + 1];
i++;
} else {
// -MFarg
arg = &argv[i][3];
}
output_dep = make_relative_path(x_strdup(arg));
// Keep the format of the args the same.
if (separate_argument) {
args_add(dep_args, "-MF");
args_add(dep_args, output_dep);
} else {
char *option = format("-MF%s", output_dep);
args_add(dep_args, option);
free(option);
}
continue;
}
if (str_startswith(argv[i], "-MQ") || str_startswith(argv[i], "-MT")) {
dependency_target_specified = true;
char *relpath;
if (strlen(argv[i]) == 3) {
// -MQ arg or -MT arg
if (i == argc - 1) {
cc_log("Missing argument to %s", argv[i]);
stats_update(STATS_ARGS);
result = false;
goto out;
}
args_add(dep_args, argv[i]);
relpath = make_relative_path(x_strdup(argv[i + 1]));
args_add(dep_args, relpath);
free(relpath);
i++;
} else {
char *arg_opt = x_strndup(argv[i], 3);
relpath = make_relative_path(x_strdup(argv[i] + 3));
char *option = format("%s%s", arg_opt, relpath);
args_add(dep_args, option);
free(arg_opt);
free(relpath);
free(option);
}
continue;
}
if (str_eq(argv[i], "-fprofile-arcs")) {
profile_arcs = true;
args_add(stripped_args, argv[i]);
continue;
}
if (str_eq(argv[i], "-ftest-coverage")) {
generating_coverage = true;
args_add(stripped_args, argv[i]);
continue;
}
if (str_eq(argv[i], "-fstack-usage")) {
generating_stackusage = true;
args_add(stripped_args, argv[i]);
continue;
}
if (str_eq(argv[i], "--coverage") // = -fprofile-arcs -ftest-coverage
|| str_eq(argv[i], "-coverage")) { // Undocumented but still works.
profile_arcs = true;
generating_coverage = true;
args_add(stripped_args, argv[i]);
continue;
}
if (str_startswith(argv[i], "-fprofile-dir=")) {
profile_dir = x_strdup(argv[i] + 14);
args_add(stripped_args, argv[i]);
continue;
}
if (str_startswith(argv[i], "-fsanitize-blacklist=")) {
sanitize_blacklist = x_strdup(argv[i] + 21);
args_add(stripped_args, argv[i]);
continue;
}
if (str_startswith(argv[i], "--sysroot=")) {
char *relpath = make_relative_path(x_strdup(argv[i] + 10));
char *option = format("--sysroot=%s", relpath);
args_add(stripped_args, option);
free(relpath);
free(option);
continue;
}
// Alternate form of specifying sysroot without =
if (str_eq(argv[i], "--sysroot")) {
if (i == argc-1) {
cc_log("Missing argument to %s", argv[i]);
stats_update(STATS_ARGS);
result = false;
goto out;
}
args_add(stripped_args, argv[i]);
char *relpath = make_relative_path(x_strdup(argv[i+1]));
args_add(stripped_args, relpath);
i++;
free(relpath);
continue;
}
// Alternate form of specifying target without =
if (str_eq(argv[i], "-target")) {
if (i == argc-1) {
cc_log("Missing argument to %s", argv[i]);
stats_update(STATS_ARGS);
result = false;
goto out;
}
args_add(stripped_args, argv[i]);
args_add(stripped_args, argv[i+1]);
i++;
continue;
}
if (str_startswith(argv[i], "-Wp,")) {
if (str_eq(argv[i], "-Wp,-P")
|| strstr(argv[i], ",-P,")
|| str_endswith(argv[i], ",-P")) {
// -P removes preprocessor information in such a way that the object
// file from compiling the preprocessed file will not be equal to the
// object file produced when compiling without ccache.
cc_log("Too hard option -Wp,-P detected");
stats_update(STATS_UNSUPPORTED_OPTION);
failed();
} else if (str_startswith(argv[i], "-Wp,-MD,")
&& !strchr(argv[i] + 8, ',')) {
generating_dependencies = true;
dependency_filename_specified = true;
free(output_dep);
output_dep = make_relative_path(x_strdup(argv[i] + 8));
args_add(dep_args, argv[i]);
continue;
} else if (str_startswith(argv[i], "-Wp,-MMD,")
&& !strchr(argv[i] + 9, ',')) {
generating_dependencies = true;
dependency_filename_specified = true;
free(output_dep);
output_dep = make_relative_path(x_strdup(argv[i] + 9));
args_add(dep_args, argv[i]);
continue;
} else if (str_startswith(argv[i], "-Wp,-D")
&& !strchr(argv[i] + 6, ',')) {
// Treat it like -D.
args_add(cpp_args, argv[i] + 4);
continue;
} else if (str_eq(argv[i], "-Wp,-MP")
|| (strlen(argv[i]) > 8
&& str_startswith(argv[i], "-Wp,-M")
&& argv[i][7] == ','
&& (argv[i][6] == 'F'
|| argv[i][6] == 'Q'
|| argv[i][6] == 'T')
&& !strchr(argv[i] + 8, ','))) {
// TODO: Make argument to MF/MQ/MT relative.
args_add(dep_args, argv[i]);
continue;
} else if (conf->direct_mode) {
// -Wp, can be used to pass too hard options to the preprocessor.
// Hence, disable direct mode.
cc_log("Unsupported compiler option for direct mode: %s", argv[i]);
conf->direct_mode = false;
}
// Any other -Wp,* arguments are only relevant for the preprocessor.
args_add(cpp_args, argv[i]);
continue;
}
if (str_eq(argv[i], "-MP")) {
args_add(dep_args, argv[i]);
continue;
}
// Input charset needs to be handled specially.
if (str_startswith(argv[i], "-finput-charset=")) {
input_charset = argv[i];
continue;
}
if (str_eq(argv[i], "--serialize-diagnostics")) {
if (i == argc - 1) {
cc_log("Missing argument to %s", argv[i]);
stats_update(STATS_ARGS);
result = false;
goto out;
}
generating_diagnostics = true;
output_dia = make_relative_path(x_strdup(argv[i+1]));
i++;
continue;
}
if (str_startswith(argv[i], "-fprofile-")) {
char *arg = x_strdup(argv[i]);
const char *arg_profile_dir = strchr(argv[i], '=');
if (arg_profile_dir) {
// Convert to absolute path.
char *dir = x_realpath(arg_profile_dir + 1);
if (!dir) {
// Directory doesn't exist.
dir = x_strdup(arg_profile_dir + 1);
}
// We can get a better hit rate by using the real path here.
free(arg);
char *option = x_strndup(argv[i], arg_profile_dir - argv[i]);
arg = format("%s=%s", option, dir);
cc_log("Rewriting %s to %s", argv[i], arg);
free(option);
free(dir);
}
bool supported_profile_option = false;
if (str_startswith(argv[i], "-fprofile-generate")
|| str_eq(argv[i], "-fprofile-arcs")) {
profile_generate = true;
supported_profile_option = true;
} else if (str_startswith(argv[i], "-fprofile-use")
|| str_eq(argv[i], "-fbranch-probabilities")) {
profile_use = true;
supported_profile_option = true;
} else if (str_eq(argv[i], "-fprofile-dir")) {
supported_profile_option = true;
}
if (supported_profile_option) {
args_add(stripped_args, arg);
free(arg);
// If the profile directory has already been set, give up... Hard to
// know what the user means, and what the compiler will do.
if (arg_profile_dir && profile_dir) {
cc_log("Profile directory already set; giving up");
result = false;
goto out;
} else if (arg_profile_dir) {
cc_log("Setting profile directory to %s", profile_dir);
profile_dir = x_strdup(arg_profile_dir);
}
continue;
}
cc_log("Unknown profile option: %s", argv[i]);
free(arg);
}
if (str_eq(argv[i], "-fcolor-diagnostics")
|| str_eq(argv[i], "-fno-color-diagnostics")
|| str_eq(argv[i], "-fdiagnostics-color")
|| str_eq(argv[i], "-fdiagnostics-color=always")
|| str_eq(argv[i], "-fno-diagnostics-color")
|| str_eq(argv[i], "-fdiagnostics-color=never")) {
args_add(stripped_args, argv[i]);
found_color_diagnostics = true;
continue;
}
if (str_eq(argv[i], "-fdiagnostics-color=auto")) {
if (color_output_possible()) {
// Output is redirected, so color output must be forced.
args_add(stripped_args, "-fdiagnostics-color=always");
cc_log("Automatically forcing colors");
} else {
args_add(stripped_args, argv[i]);
}
found_color_diagnostics = true;
continue;
}
// GCC
if (str_eq(argv[i], "-fdirectives-only")) {
found_directives_only = true;
continue;
}
// Clang
if (str_eq(argv[i], "-frewrite-includes")) {
found_rewrite_includes = true;
continue;
}
// Options taking an argument that we may want to rewrite to relative paths
// to get better hit rate. A secondary effect is that paths in the standard
// error output produced by the compiler will be normalized.
if (compopt_takes_path(argv[i])) {
if (i == argc - 1) {
cc_log("Missing argument to %s", argv[i]);
stats_update(STATS_ARGS);
result = false;
goto out;
}
if (!detect_pch(argv[i], argv[i+1], &found_pch)) {
result = false;
goto out;
}
char *relpath = make_relative_path(x_strdup(argv[i+1]));
if (compopt_affects_cpp(argv[i])) {
args_add(cpp_args, argv[i]);
args_add(cpp_args, relpath);
} else {
args_add(stripped_args, argv[i]);
args_add(stripped_args, relpath);
}
free(relpath);
i++;
continue;
}
// Same as above but options with concatenated argument beginning with a
// slash.
if (argv[i][0] == '-') {
char *slash_pos = strchr(argv[i], '/');
if (slash_pos) {
char *option = x_strndup(argv[i], slash_pos - argv[i]);
if (compopt_takes_concat_arg(option) && compopt_takes_path(option)) {
char *relpath = make_relative_path(x_strdup(slash_pos));
char *new_option = format("%s%s", option, relpath);
if (compopt_affects_cpp(option)) {
args_add(cpp_args, new_option);
} else {
args_add(stripped_args, new_option);
}
free(new_option);
free(relpath);
free(option);
continue;
} else {
free(option);
}
}
}
// Options that take an argument.
if (compopt_takes_arg(argv[i])) {
if (i == argc - 1) {
cc_log("Missing argument to %s", argv[i]);
stats_update(STATS_ARGS);
result = false;
goto out;
}
if (compopt_affects_cpp(argv[i])) {
args_add(cpp_args, argv[i]);
args_add(cpp_args, argv[i+1]);
} else {
args_add(stripped_args, argv[i]);
args_add(stripped_args, argv[i+1]);
}
i++;
continue;
}
// Other options.
if (argv[i][0] == '-') {
if (compopt_affects_cpp(argv[i])
|| compopt_prefix_affects_cpp(argv[i])) {
args_add(cpp_args, argv[i]);
} else {
args_add(stripped_args, argv[i]);
}
continue;
}
// If an argument isn't a plain file then assume its an option, not an
// input file. This allows us to cope better with unusual compiler options.
struct stat st;
if (stat(argv[i], &st) != 0 || !S_ISREG(st.st_mode)) {
cc_log("%s is not a regular file, not considering as input file",
argv[i]);
args_add(stripped_args, argv[i]);
continue;
}
if (input_file) {
if (language_for_file(argv[i])) {
cc_log("Multiple input files: %s and %s", input_file, argv[i]);
stats_update(STATS_MULTIPLE);
} else if (!found_c_opt) {
cc_log("Called for link with %s", argv[i]);
if (strstr(argv[i], "conftest.")) {
stats_update(STATS_CONFTEST);
} else {
stats_update(STATS_LINK);
}
} else {
cc_log("Unsupported source extension: %s", argv[i]);
stats_update(STATS_SOURCELANG);
}
result = false;
goto out;
}
// The source code file path gets put into the notes.
if (generating_coverage) {
input_file = x_strdup(argv[i]);
continue;
}
if (is_symlink(argv[i])) {
// Don't rewrite source file path if it's a symlink since
// make_relative_path resolves symlinks using realpath(3) and this leads
// to potentially choosing incorrect relative header files. See the
// "symlink to source file" test.
input_file = x_strdup(argv[i]);
} else {
// Rewrite to relative to increase hit rate.
input_file = make_relative_path(x_strdup(argv[i]));
}
} // for
if (found_S_opt) {
// Even if -gsplit-dwarf is given, the .dwo file is not generated when -S
// is also given.
using_split_dwarf = false;
cc_log("Disabling caching of dwarf files since -S is used");
}
if (!input_file) {
cc_log("No input file found");
stats_update(STATS_NOINPUT);
result = false;
goto out;
}
if (found_pch || found_fpch_preprocess) {
using_precompiled_header = true;
if (!(conf->sloppiness & SLOPPY_TIME_MACROS)) {
cc_log("You have to specify \"time_macros\" sloppiness when using"
" precompiled headers to get direct hits");
cc_log("Disabling direct mode");
stats_update(STATS_CANTUSEPCH);
result = false;
goto out;
}
}
if (explicit_language && str_eq(explicit_language, "none")) {
explicit_language = NULL;
}
file_language = language_for_file(input_file);
if (explicit_language) {
if (!language_is_supported(explicit_language)) {
cc_log("Unsupported language: %s", explicit_language);
stats_update(STATS_SOURCELANG);
result = false;
goto out;
}
actual_language = x_strdup(explicit_language);
} else {
actual_language = file_language;
}
output_is_precompiled_header =
actual_language && strstr(actual_language, "-header");
if (output_is_precompiled_header
&& !(conf->sloppiness & SLOPPY_PCH_DEFINES)) {
cc_log("You have to specify \"pch_defines,time_macros\" sloppiness when"
" creating precompiled headers");
stats_update(STATS_CANTUSEPCH);
result = false;
goto out;
}
if (!found_c_opt && !found_S_opt) {
if (output_is_precompiled_header) {
args_add(stripped_args, "-c");
} else {
cc_log("No -c option found");
// I find that having a separate statistic for autoconf tests is useful,
// as they are the dominant form of "called for link" in many cases.
if (strstr(input_file, "conftest.")) {
stats_update(STATS_CONFTEST);
} else {
stats_update(STATS_LINK);
}
result = false;
goto out;
}
}
if (!actual_language) {
cc_log("Unsupported source extension: %s", input_file);
stats_update(STATS_SOURCELANG);
result = false;
goto out;
}
if (!conf->run_second_cpp && str_eq(actual_language, "cuda")) {
cc_log("Using CUDA compiler; not compiling preprocessed code");
conf->run_second_cpp = true;
}
direct_i_file = language_is_preprocessed(actual_language);
if (output_is_precompiled_header && !conf->run_second_cpp) {
// It doesn't work to create the .gch from preprocessed source.
cc_log("Creating precompiled header; not compiling preprocessed code");
conf->run_second_cpp = true;
}
if (str_eq(conf->cpp_extension, "")) {
const char *p_language = p_language_for_language(actual_language);
free(conf->cpp_extension);
conf->cpp_extension = x_strdup(extension_for_language(p_language) + 1);
}
// Don't try to second guess the compilers heuristics for stdout handling.
if (output_obj && str_eq(output_obj, "-")) {
stats_update(STATS_OUTSTDOUT);
cc_log("Output file is -");
result = false;
goto out;
}
if (!output_obj) {
if (output_is_precompiled_header) {
output_obj = format("%s.gch", input_file);
} else {
output_obj = basename(input_file);
char *p = strrchr(output_obj, '.');
if (!p || !p[1]) {
cc_log("Badly formed object filename");
stats_update(STATS_ARGS);
result = false;
goto out;
}
p[1] = found_S_opt ? 's' : 'o';
p[2] = 0;
}
}
if (using_split_dwarf) {
char *p = strrchr(output_obj, '.');
if (!p || !p[1]) {
cc_log("Badly formed object filename");
stats_update(STATS_ARGS);
result = false;
goto out;
}
char *base_name = remove_extension(output_obj);
output_dwo = format("%s.dwo", base_name);
free(base_name);
}
// Cope with -o /dev/null.
struct stat st;
if (!str_eq(output_obj, "/dev/null")
&& stat(output_obj, &st) == 0
&& !S_ISREG(st.st_mode)) {
cc_log("Not a regular file: %s", output_obj);
stats_update(STATS_DEVICE);
result = false;
goto out;
}
// Some options shouldn't be passed to the real compiler when it compiles
// preprocessed code:
//
// -finput-charset=XXX (otherwise conversion happens twice)
// -x XXX (otherwise the wrong language is selected)
if (input_charset) {
args_add(cpp_args, input_charset);
}
if (found_pch) {
args_add(cpp_args, "-fpch-preprocess");
}
if (explicit_language) {
args_add(cpp_args, "-x");
args_add(cpp_args, explicit_language);
}
// Since output is redirected, compilers will not color their output by
// default, so force it explicitly if it would be otherwise done.
if (!found_color_diagnostics && color_output_possible()) {
if (guessed_compiler == GUESSED_CLANG) {
if (!str_eq(actual_language, "assembler")) {
args_add(stripped_args, "-fcolor-diagnostics");
cc_log("Automatically enabling colors");
}
} else if (guessed_compiler == GUESSED_GCC) {
// GCC has it since 4.9, but that'd require detecting what GCC version is
// used for the actual compile. However it requires also GCC_COLORS to be
// set (and not empty), so use that for detecting if GCC would use
// colors.
if (getenv("GCC_COLORS") && getenv("GCC_COLORS")[0] != '\0') {
args_add(stripped_args, "-fdiagnostics-color");
cc_log("Automatically enabling colors");
}
}
}
// Add flags for dependency generation only to the preprocessor command line.
if (generating_dependencies) {
if (!dependency_filename_specified) {
char *base_name = remove_extension(output_obj);
char *default_depfile_name = format("%s.d", base_name);
free(base_name);
args_add(dep_args, "-MF");
args_add(dep_args, default_depfile_name);
output_dep = make_relative_path(x_strdup(default_depfile_name));
}
if (!dependency_target_specified
&& !str_eq(get_extension(output_dep), ".o")) {
args_add(dep_args, "-MQ");
args_add(dep_args, output_obj);
}
}
if (generating_coverage) {
char *base_name = remove_extension(output_obj);
char *default_covfile_name = format("%s.gcno", base_name);
free(base_name);
output_cov = make_relative_path(default_covfile_name);
}
if (generating_stackusage) {
char *base_name = remove_extension(output_obj);
char *default_sufile_name = format("%s.su", base_name);
free(base_name);
output_su = make_relative_path(default_sufile_name);
}
*compiler_args = args_copy(stripped_args);
if (conf->run_second_cpp) {
args_extend(*compiler_args, cpp_args);
} else if (found_directives_only || found_rewrite_includes) {
// Need to pass the macros and any other preprocessor directives again.
args_extend(*compiler_args, cpp_args);
if (found_directives_only) {
args_add(cpp_args, "-fdirectives-only");
// The preprocessed source code still needs some more preprocessing.
args_add(*compiler_args, "-fpreprocessed");
args_add(*compiler_args, "-fdirectives-only");
}
if (found_rewrite_includes) {
args_add(cpp_args, "-frewrite-includes");
// The preprocessed source code still needs some more preprocessing.
args_add(*compiler_args, "-x");
args_add(*compiler_args, actual_language);
}
} else if (explicit_language) {
// Workaround for a bug in Apple's patched distcc -- it doesn't properly
// reset the language specified with -x, so if -x is given, we have to
// specify the preprocessed language explicitly.
args_add(*compiler_args, "-x");
args_add(*compiler_args, p_language_for_language(explicit_language));
}
if (found_c_opt) {
args_add(*compiler_args, "-c");
}
for (size_t i = 0; i < arch_args_size; ++i) {
args_add(*compiler_args, "-arch");
args_add(*compiler_args, arch_args[i]);
}
// Only pass dependency arguments to the preprocesor since Intel's C++
// compiler doesn't produce a correct .d file when compiling preprocessed
// source.
args_extend(cpp_args, dep_args);
*preprocessor_args = args_copy(stripped_args);
args_extend(*preprocessor_args, cpp_args);
out:
args_free(expanded_args);
args_free(stripped_args);
args_free(dep_args);
args_free(cpp_args);
return result;
}
static void
create_initial_config_file(struct conf *conf, const char *path)
{
if (create_parent_dirs(path) != 0) {
return;
}
unsigned max_files;
uint64_t max_size;
char *stats_dir = format("%s/0", conf->cache_dir);
struct stat st;
if (stat(stats_dir, &st) == 0) {
stats_get_obsolete_limits(stats_dir, &max_files, &max_size);
// STATS_MAXFILES and STATS_MAXSIZE was stored for each top directory.
max_files *= 16;
max_size *= 16;
} else {
max_files = 0;
max_size = conf->max_size;
}
free(stats_dir);
FILE *f = fopen(path, "w");
if (!f) {
return;
}
if (max_files != 0) {
fprintf(f, "max_files = %u\n", max_files);
conf->max_files = max_files;
}
if (max_size != 0) {
char *size = format_parsable_size_with_suffix(max_size);
fprintf(f, "max_size = %s\n", size);
free(size);
conf->max_size = max_size;
}
fclose(f);
}
// Read config file(s), populate variables, create configuration file in cache
// directory if missing, etc.
static void
initialize(void)
{
conf_free(conf);
conf = conf_create();
char *errmsg;
char *p = getenv("CCACHE_CONFIGPATH");
if (p) {
primary_config_path = x_strdup(p);
} else {
secondary_config_path = format("%s/ccache.conf", TO_STRING(SYSCONFDIR));
if (!conf_read(conf, secondary_config_path, &errmsg)) {
if (errno == 0) {
// We could read the file but it contained errors.
fatal("%s", errmsg);
}
// A missing config file in SYSCONFDIR is OK.
free(errmsg);
}
if (str_eq(conf->cache_dir, "")) {
fatal("configuration setting \"cache_dir\" must not be the empty string");
}
if ((p = getenv("CCACHE_DIR"))) {
free(conf->cache_dir);
conf->cache_dir = strdup(p);
}
if (str_eq(conf->cache_dir, "")) {
fatal("CCACHE_DIR must not be the empty string");
}
primary_config_path = format("%s/ccache.conf", conf->cache_dir);
}
bool should_create_initial_config = false;
if (!conf_read(conf, primary_config_path, &errmsg)) {
if (errno == 0) {
// We could read the file but it contained errors.
fatal("%s", errmsg);
}
if (!conf->disable) {
should_create_initial_config = true;
}
}
if (!conf_update_from_environment(conf, &errmsg)) {
fatal("%s", errmsg);
}
if (should_create_initial_config) {
create_initial_config_file(conf, primary_config_path);
}
exitfn_init();
exitfn_add_nullary(stats_flush);
exitfn_add_nullary(clean_up_pending_tmp_files);
cc_log("=== CCACHE %s STARTED =========================================",
CCACHE_VERSION);
if (conf->umask != UINT_MAX) {
umask(conf->umask);
}
}
// Reset the global state. Used by the test suite.
void
cc_reset(void)
{
conf_free(conf); conf = NULL;
free(primary_config_path); primary_config_path = NULL;
free(secondary_config_path); secondary_config_path = NULL;
free(current_working_dir); current_working_dir = NULL;
for (size_t i = 0; i < debug_prefix_maps_len; i++) {
free(debug_prefix_maps[i]);
debug_prefix_maps[i] = NULL;
}
free(debug_prefix_maps); debug_prefix_maps = NULL;
debug_prefix_maps_len = 0;
free(profile_dir); profile_dir = NULL;
free(sanitize_blacklist); sanitize_blacklist = NULL;
free(included_pch_file); included_pch_file = NULL;
args_free(orig_args); orig_args = NULL;
free(input_file); input_file = NULL;
free(output_obj); output_obj = NULL;
free(output_dep); output_dep = NULL;
free(output_cov); output_cov = NULL;
free(output_su); output_su = NULL;
free(output_dia); output_dia = NULL;
free(output_dwo); output_dwo = NULL;
free(cached_obj_hash); cached_obj_hash = NULL;
free(cached_stderr); cached_stderr = NULL;
free(cached_obj); cached_obj = NULL;
free(cached_dep); cached_dep = NULL;
free(cached_cov); cached_cov = NULL;
free(cached_su); cached_su = NULL;
free(cached_dia); cached_dia = NULL;
free(cached_dwo); cached_dwo = NULL;
free(manifest_path); manifest_path = NULL;
time_of_compilation = 0;
for (size_t i = 0; i < ignore_headers_len; i++) {
free(ignore_headers[i]);
ignore_headers[i] = NULL;
}
free(ignore_headers); ignore_headers = NULL;
ignore_headers_len = 0;
if (included_files) {
hashtable_destroy(included_files, 1); included_files = NULL;
}
has_absolute_include_headers = false;
generating_debuginfo = false;
generating_dependencies = false;
generating_coverage = false;
generating_stackusage = false;
profile_arcs = false;
free(profile_dir); profile_dir = NULL;
i_tmpfile = NULL;
direct_i_file = false;
free(cpp_stderr); cpp_stderr = NULL;
free(stats_file); stats_file = NULL;
output_is_precompiled_header = false;
conf = conf_create();
using_split_dwarf = false;
}
// Make a copy of stderr that will not be cached, so things like distcc can
// send networking errors to it.
static void
set_up_uncached_err(void)
{
int uncached_fd = dup(2); // The file descriptor is intentionally leaked.
if (uncached_fd == -1) {
cc_log("dup(2) failed: %s", strerror(errno));
failed();
}
// Leak a pointer to the environment.
char *buf = format("UNCACHED_ERR_FD=%d", uncached_fd);
if (putenv(buf) == -1) {
cc_log("putenv failed: %s", strerror(errno));
failed();
}
}
static void
configuration_logger(const char *descr, const char *origin, void *context)
{
(void)context;
cc_bulklog("Config: (%s) %s", origin, descr);
}
// The main ccache driver function.
static void
ccache(int argc, char *argv[])
{
#ifndef _WIN32
set_up_signal_handlers();
#endif
orig_args = args_init(argc, argv);
initialize();
find_compiler(argv);
if (str_eq(conf->temporary_dir, "")) {
clean_up_internal_tempdir();
}
if (!str_eq(conf->log_file, "")) {
conf_print_items(conf, configuration_logger, NULL);
}
if (conf->disable) {
cc_log("ccache is disabled");
failed();
}
set_up_uncached_err();
cc_log_argv("Command line: ", argv);
cc_log("Hostname: %s", get_hostname());
cc_log("Working directory: %s", get_current_working_dir());
conf->limit_multiple = MIN(MAX(conf->limit_multiple, 0.0), 1.0);
guessed_compiler = guess_compiler(orig_args->argv[0]);
// Arguments (except -E) to send to the preprocessor.
struct args *preprocessor_args;
// Arguments to send to the real compiler.
struct args *compiler_args;
if (!cc_process_args(orig_args, &preprocessor_args, &compiler_args)) {
failed();
}
cc_log("Source file: %s", input_file);
if (generating_dependencies) {
cc_log("Dependency file: %s", output_dep);
}
if (generating_coverage) {
cc_log("Coverage file: %s", output_cov);
}
if (generating_stackusage) {
cc_log("Stack usage file: %s", output_su);
}
if (generating_diagnostics) {
cc_log("Diagnostics file: %s", output_dia);
}
if (output_dwo) {
cc_log("Split dwarf file: %s", output_dwo);
}
cc_log("Object file: %s", output_obj);
struct mdfour common_hash;
hash_start(&common_hash);
calculate_common_hash(preprocessor_args, &common_hash);
// Try to find the hash using the manifest.
struct mdfour direct_hash = common_hash;
bool put_object_in_manifest = false;
struct file_hash *object_hash = NULL;
struct file_hash *object_hash_from_manifest = NULL;
if (conf->direct_mode) {
cc_log("Trying direct lookup");
object_hash = calculate_object_hash(preprocessor_args, &direct_hash, 1);
if (object_hash) {
update_cached_result_globals(object_hash);
// If we can return from cache at this point then do so.
from_cache(FROMCACHE_DIRECT_MODE, 0);
// Wasn't able to return from cache at this point. However, the object
// was already found in manifest, so don't readd it later.
put_object_in_manifest = false;
object_hash_from_manifest = object_hash;
} else {
// Add object to manifest later.
put_object_in_manifest = true;
}
}
if (conf->read_only_direct) {
cc_log("Read-only direct mode; running real compiler");
failed();
}
// Find the hash using the preprocessed output. Also updates included_files.
struct mdfour cpp_hash = common_hash;
object_hash = calculate_object_hash(preprocessor_args, &cpp_hash, 0);
if (!object_hash) {
fatal("internal error: object hash from cpp returned NULL");
}
update_cached_result_globals(object_hash);
if (object_hash_from_manifest
&& !file_hashes_equal(object_hash_from_manifest, object_hash)) {
// The hash from manifest differs from the hash of the preprocessor output.
// This could be because:
//
// - The preprocessor produces different output for the same input (not
// likely).
// - There's a bug in ccache (maybe incorrect handling of compiler
// arguments).
// - The user has used a different CCACHE_BASEDIR (most likely).
//
// The best thing here would probably be to remove the hash entry from the
// manifest. For now, we use a simpler method: just remove the manifest
// file.
cc_log("Hash from manifest doesn't match preprocessor output");
cc_log("Likely reason: different CCACHE_BASEDIRs used");
cc_log("Removing manifest as a safety measure");
x_unlink(manifest_path);
put_object_in_manifest = true;
}
// If we can return from cache at this point then do.
from_cache(FROMCACHE_CPP_MODE, put_object_in_manifest);
if (conf->read_only) {
cc_log("Read-only mode; running real compiler");
failed();
}
add_prefix(compiler_args, conf->prefix_command);
// Run real compiler, sending output to cache.
to_cache(compiler_args);
x_exit(0);
}
static void
configuration_printer(const char *descr, const char *origin, void *context)
{
assert(context);
fprintf(context, "(%s) %s\n", origin, descr);
}
// The main program when not doing a compile.
static int
ccache_main_options(int argc, char *argv[])
{
enum longopts {
DUMP_MANIFEST
};
static const struct option options[] = {
{"cleanup", no_argument, 0, 'c'},
{"clear", no_argument, 0, 'C'},
{"dump-manifest", required_argument, 0, DUMP_MANIFEST},
{"help", no_argument, 0, 'h'},
{"max-files", required_argument, 0, 'F'},
{"max-size", required_argument, 0, 'M'},
{"set-config", required_argument, 0, 'o'},
{"print-config", no_argument, 0, 'p'},
{"show-stats", no_argument, 0, 's'},
{"version", no_argument, 0, 'V'},
{"zero-stats", no_argument, 0, 'z'},
{0, 0, 0, 0}
};
int c;
while ((c = getopt_long(argc, argv, "cChF:M:o:psVz", options, NULL)) != -1) {
switch (c) {
case DUMP_MANIFEST:
manifest_dump(optarg, stdout);
break;
case 'c': // --cleanup
initialize();
clean_up_all(conf);
printf("Cleaned cache\n");
break;
case 'C': // --clear
initialize();
wipe_all(conf);
printf("Cleared cache\n");
break;
case 'h': // --help
fputs(USAGE_TEXT, stdout);
x_exit(0);
case 'F': // --max-files
{
initialize();
char *errmsg;
if (conf_set_value_in_file(primary_config_path, "max_files", optarg,
&errmsg)) {
unsigned files = atoi(optarg);
if (files == 0) {
printf("Unset cache file limit\n");
} else {
printf("Set cache file limit to %u\n", files);
}
} else {
fatal("could not set cache file limit: %s", errmsg);
}
}
break;
case 'M': // --max-size
{
initialize();
uint64_t size;
if (!parse_size_with_suffix(optarg, &size)) {
fatal("invalid size: %s", optarg);
}
char *errmsg;
if (conf_set_value_in_file(primary_config_path, "max_size", optarg,
&errmsg)) {
if (size == 0) {
printf("Unset cache size limit\n");
} else {
char *s = format_human_readable_size(size);
printf("Set cache size limit to %s\n", s);
free(s);
}
} else {
fatal("could not set cache size limit: %s", errmsg);
}
}
break;
case 'o': // --set-config
{
initialize();
char *p = strchr(optarg, '=');
if (!p) {
fatal("missing equal sign in \"%s\"", optarg);
}
char *key = x_strndup(optarg, p - optarg);
char *value = p + 1;
char *errmsg;
if (!conf_set_value_in_file(primary_config_path, key, value, &errmsg)) {
fatal("%s", errmsg);
}
free(key);
}
break;
case 'p': // --print-config
initialize();
conf_print_items(conf, configuration_printer, stdout);
break;
case 's': // --show-stats
initialize();
stats_summary(conf);
break;
case 'V': // --version
fprintf(stdout, VERSION_TEXT, CCACHE_VERSION);
x_exit(0);
case 'z': // --zero-stats
initialize();
stats_zero();
printf("Statistics zeroed\n");
break;
default:
fputs(USAGE_TEXT, stderr);
x_exit(1);
}
}
return 0;
}
int
ccache_main(int argc, char *argv[])
{
// Check if we are being invoked as "ccache".
char *program_name = basename(argv[0]);
if (same_executable_name(program_name, MYNAME)) {
if (argc < 2) {
fputs(USAGE_TEXT, stderr);
x_exit(1);
}
// If the first argument isn't an option, then assume we are being passed a
// compiler name and options.
if (argv[1][0] == '-') {
return ccache_main_options(argc, argv);
}
}
free(program_name);
ccache(argc, argv);
return 1;
}
|