aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/target.hxx
blob: 1b7b755cb6de80021253db7d177e619b1272af91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
// file      : libbuild2/target.hxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef LIBBUILD2_TARGET_HXX
#define LIBBUILD2_TARGET_HXX

#include <cstddef>      // max_align_t
#include <iterator>     // tags, etc.
#include <type_traits>  // is_*
#include <unordered_map>

#include <libbutl/multi-index.hxx> // map_iterator_adapter

#include <libbuild2/types.hxx>
#include <libbuild2/forward.hxx>
#include <libbuild2/utility.hxx>

#include <libbuild2/scope.hxx>
#include <libbuild2/action.hxx>
#include <libbuild2/recipe.hxx>
#include <libbuild2/context.hxx>
#include <libbuild2/variable.hxx>
#include <libbuild2/target-key.hxx>
#include <libbuild2/target-type.hxx>
#include <libbuild2/target-state.hxx>
#include <libbuild2/prerequisite.hxx>

#include <libbuild2/export.hxx>

namespace build2
{
  // From <libbuild2/algorithm.hxx>.
  //
  LIBBUILD2_SYMEXPORT const target&
  search (const target&, const prerequisite&);

  LIBBUILD2_SYMEXPORT const target*
  search_existing (const prerequisite&);

  // Prerequisite inclusion/exclusion (see include() function below).
  //
  // Note that posthoc is handled internally and should normally be treated by
  // the rules the same as excluded.
  //
  class include_type
  {
  public:
    enum value {excluded, posthoc, adhoc, normal};

    include_type (value v): v_ (v) {}
    include_type (bool  v): v_ (v ? normal : excluded) {}

    operator         value () const {return v_;}
    explicit operator bool () const {return v_ == normal || v_ == adhoc;}

  private:
    value v_;
  };

  // A view of target group members.
  //
  // Note that the members array may have "holes" (NULL pointers) and count is
  // the number of elements in this array (and not necessarily the number of
  // group members).
  //
  // Note that members being NULL and count being 0 have different meaning:
  // the former means group members are not known while the latter means it's
  // an empty group.
  //
  struct group_view
  {
    const target* const* members; // NULL means not yet known.
    size_t count;
  };

  // List of prerequisites resolved to targets. Unless additional storage is
  // needed, it can be treated as just vector<const target*> (which is what we
  // used to have initially).
  //
  // The include member normally just indicates (in the first bit) whether
  // this prerequisite is ad hoc. But it can also carry additional information
  // (for example, from operation-specific override) in other bits (see below
  // for details).
  //
  struct prerequisite_target
  {
    using target_type = build2::target;

    prerequisite_target (const target_type* t, bool a = false, uintptr_t d = 0)
        : target (t), include (a ? include_adhoc : 0), data (d) {}

    prerequisite_target (const target_type& t, bool a = false, uintptr_t d = 0)
        : prerequisite_target (&t, a, d) {}

    prerequisite_target (const target_type* t, include_type a, uintptr_t d = 0)
        : prerequisite_target (t, a == include_type::adhoc, d) {}

    prerequisite_target (const target_type& t, include_type a, uintptr_t d = 0)
        : prerequisite_target (&t, a, d) {}

    const target_type* target;

    operator const target_type*&  ()       {return target;}
    operator const target_type*   () const {return target;}
    const target_type* operator-> () const {return target;}

    // The first 8 bits are reserved with the first two having the following
    // semantics:
    //
    // adhoc
    //
    //   This prerequisite is ad hoc.
    //
    // udm
    //
    //   This prerequisite is updated during match. Note that only static
    //   prerequisites that are updated during match should have this bit set
    //   (see dyndep_rule::*_existing_file() for details).
    //
    // target
    //
    //   The data member contains the target pointer that has been "blanked
    //   out" for some reason (updated during match, unmatched, etc). See
    //   dyndep_rule::updated_during_match() for details.
    //
    static const uintptr_t include_adhoc  = 0x01;
    static const uintptr_t include_udm    = 0x02;
    static const uintptr_t include_target = 0x80;

    uintptr_t include;

    bool adhoc () const {return (include & include_adhoc) != 0;}

    // Auxiliary data.
    //
    uintptr_t data;
  };
  using prerequisite_targets = vector<prerequisite_target>;

  // A rule match is an element of name_rule_map.
  //
  using rule_match = pair<const string, reference_wrapper<const rule>>;

  // A map of target type plus operation ids to rule hints (see name_rule_map
  // for details on rule names and hints). The default_id serves as a fallback
  // for update and clean operations.
  //
  // Note that for now hints are tried in the order specified and the first
  // that matches, used.
  //
  struct rule_hints
  {
    // Return empty string if not found.
    //
    const string&
    find (const target_type&, operation_id, bool untyped) const;

    bool
    empty () const {return map.empty ();}

    // Note that insertion of an existing entry overrides the old value.
    //
    void
    insert (const target_type*, operation_id, string);

    struct value_type
    {
      const target_type* type;
      operation_id       operation;
      string             hint;
    };

    vector<value_type> map;
  };

  // Additional information about a rule match (see rule.hxx for details).
  //
  // Note that passing this information to a base rule's match() as-is may or
  // may not be correct. If some changes must be made (for example, the
  // fallback flag must be cleared), then that should be done by modifying
  // (and restoring, if necessary) the passed instance rather than making a
  // copy (which would not survive to apply()).
  //
  struct match_extra
  {
    bool locked;   // Normally true (see adhoc_rule::match() for background).
    bool fallback; // True if matching a fallback rule (see match_rule_impl()).

    // When matching a rule, the caller may wish to request a subset of the
    // full functionality of performing the operation on the target. This is
    // achieved with match options.
    //
    // Since the match caller normally has no control over which rule will be
    // matched, the options are not specific to a particular rule. Rather,
    // options are defined for performing a specific operation on a specific
    // target type and would normally be part of the target type semantics.
    // To put it another way, when a rule matches a target of certain type for
    // certain operation, there is an expectation of certain semantics, some
    // parts of which could be made optional.
    //
    // As a concrete example, consider installing libs{}, which traditionally
    // has two parts: runtime (normally just the versioned shared library) and
    // build-time (non-versioned symlinks, pkg-config files, headers, etc).
    // The option to install only the runtime files is part of the bin::libs{}
    // semantics, not of, say, cc::install_rule.
    //
    // The match options are specified as a uint64_t mask, which means there
    // can be a maximum of 64 options per operation/target type. Options are
    // opt-out rather than opt-in. That is, by default, all the options are
    // enabled unless the match caller explicitly opted out of some
    // functionality. Even if the caller opted out, there is no guarantee that
    // the matching rule will honor this request (for example, because it is a
    // user-provided ad hoc recipe). To put it another way, support for
    // options is a quality of implementation matter.
    //
    // From the rule implementation's point view, match options are handled as
    // follows: On initial match()/apply(), cur_options is initialized to ~0
    // (all options enabled) and the matching rule is expected to override it
    // with new_options in apply() (note that match() should no base any
    // decisions on new_options since they may change between match() and
    // apply()). This way a rule that does not support any match options does
    // not need to do anything. Subsequent match calls may add new options
    // which causes a rematch that manifests in the rule's reapply() call. In
    // reapply(), cur_options are the currently enabled options and
    // new_options are the newly requested options. Here the rule is expected
    // to factor new_options to cur_options as appropriate. Note also that on
    // rematch, if current options already include new options, then no call
    // to reapply() is made. This, in particular, means that a rule that does
    // not adjust cur_options in match() will never get a reapply() call
    // (because all the options are enabled from the start). Note that
    // cur_options should only be modfied in apply() or reapply().
    //
    // If a rematch is triggered after the rule has already been executed, an
    // error is issued.  This means that match options are not usable for
    // operation/target types that could plausibly be executed during
    // match. In particular, using match options for update and clean
    // operations is a bad idea (update of pretty much any target can happen
    // during match as a result of a tool update while clean might have to be
    // performed during match to provide the mirror semantics).
    //
    // Note also that with rematches the assumption that in the match phase
    // after matching the target we can MT-safely examine its state (such as
    // its prerequisite_targets) no longer holds since such state could be
    // modified during a rematch. As a result, if the target type specifies
    // options for a certain operation, then you should not rely on this
    // assumption for targets of this type during this operation.
    //
    // A rule that supports match options must also be prepared to handle the
    // apply() call with new_options set to 0, for example, by using a
    // minimally supported set of options instead. While 0 usually won't be
    // passed by the match caller, this value is passed in the following
    // circumstances:
    //
    //   - match to resolve group (resolve_group())
    //   - match to resolve members (resolve_members())
    //   - match of ad hoc group via one of its ad hoc members
    //
    // Note that the 0 cur_options value is illegal.
    //
    // When it comes to match options specified for group members, the
    // semantics differs between explicit and ad hoc groups. For explicit
    // groups, the standard semantics described above applies and the group's
    // reapply() function will be called both for the group itself as well as
    // for its members and its the responsibility of the rule to decide what
    // to do with the two sets of options (e.g., factor member's options into
    // group's options, etc). For ad hoc groups, members are not matched to a
    // rule but to the group_recipe directly (so there cannot be a call to
    // reapply()). Currently, ad hoc group members cannot have options (more
    // precisely, their options should always be ~0). An alternative semantics
    // where the group rule is called to translate member options to group
    // options may be implemented in the future (see match_impl_impl() for
    // details).
    //
    // Note: match options are currently not exposed in Buildscript ad hoc
    // recipes/rules (but are in C++).
    //
    static constexpr uint64_t all_options = ~uint64_t (0);

    uint64_t cur_options;
    uint64_t new_options;

    atomic<uint64_t> cur_options_; // Implementation detail (see lock_impl()).

    // The list of post hoc prerequisite targets for this target. Only not
    // NULL in rule::apply_posthoc() and rule::reapply() functions and only if
    // there are post hoc prerequisites. Primarily useful for adjusting match
    // options for post hoc prerequisites (but can also be used to blank some
    // of them out).
    //
    vector<context::posthoc_target::prerequisite_target>*
      posthoc_prerequisite_targets;

    // Auxiliary data storage.
    //
    // A rule (whether matches or not) may use this pad to pass data between
    // its match and apply functions (but not the recipe). The rule should
    // static assert that the size of the pad is sufficient for its needs.
    //
    // This facility is complementary to the auxiliary data storage in target:
    // it can store slightly more/extra data without dynamic memory allocation
    // but can only be used during match/apply.
    //
    // Note also that a rule that delegates to another rule may not be able to
    // use this mechanism fully since the delegated-to rule may also need the
    // data storage.
    //
    static constexpr size_t data_size = (sizeof (string) > sizeof (void*) * 4
                                         ? sizeof (string)
                                         : sizeof (void*) * 4);

    alignas (std::max_align_t) unsigned char data_[data_size];
    void (*data_dtor_) (void*) = nullptr;

    template <typename R,
              typename T = typename std::remove_cv<
                typename std::remove_reference<R>::type>::type>
    typename std::enable_if<std::is_trivially_destructible<T>::value,T&>::type
    data (R&& d)
    {
      assert (sizeof (T) <= data_size);
      clear_data ();
      return *new (&data_) T (forward<R> (d));
    }

    template <typename R,
              typename T = typename std::remove_cv<
                typename std::remove_reference<R>::type>::type>
    typename std::enable_if<!std::is_trivially_destructible<T>::value,T&>::type
    data (R&& d)
    {
      assert (sizeof (T) <= data_size);
      clear_data ();
      T& r (*new (&data_) T (forward<R> (d)));
      data_dtor_ = [] (void* p) {static_cast<T*> (p)->~T ();};
      return r;
    }

    template <typename T>
    T&
    data () {return *reinterpret_cast<T*> (&data_);}

    template <typename T>
    const T&
    data () const {return *reinterpret_cast<const T*> (&data_);}

    void
    clear_data ()
    {
      if (data_dtor_ != nullptr)
      {
        data_dtor_ (&data_);
        data_dtor_ = nullptr;
      }
    }

    // Implementation details.
    //
    // NOTE: see match_rule_impl() in algorithms.cxx if changing anything here.
    //
  public:
    explicit
    match_extra (bool l = true, bool f = false)
        : locked (l), fallback (f),
          cur_options (all_options), new_options (0),
          posthoc_prerequisite_targets (nullptr) {}

    void
    reinit (bool fallback);

    // Force freeing of the dynamically-allocated memory.
    //
    void
    free ();

    ~match_extra ()
    {
      clear_data ();
    }
  };

  // Target.
  //

  // A target can be entered for several reasons that are useful to
  // distinguish for diagnostics, when considering as the default target, etc.
  //
  // Note that the order of the enumerators is arranged so that their integral
  // values indicate whether one "overrides" the other.
  //
  // We refer to the targets other than real and implied as dynamically-
  // created or just dynamic.
  //
  // Implied means the target's existence is implied by the presence of
  // another entity in the buildfile or the environment. This can range from a
  // target-specific variable assignment, to a group target (which may imply
  // the presence of member), to targets implied by the presence of installed
  // artifacts.
  //
  // Note that it's often tempting to add another class of declarations for
  // what we refer to as synthesized dependencies (see cc::link_rule for an
  // example). However, this is probably an orthogonal notion (maybe a
  // sub-state) to declaration since we can synthesize a dependency based on
  // any declaration, including real.  And saying that synthesized is stronger
  // than real feels wrong. To put it another way, synthesized dependencies
  // are more about the target-prerequisite relationship rather that the
  // target.
  //
  enum class target_decl: uint8_t
  {
    prereq_new = 1, // Created from prerequisite (create_new_target()).
    prereq_file,    // Created from prerequisite/file (search_existing_file()).
    implied,        // Target-spec variable assignment, implicitly-entered, etc.
    real            // Real dependency declaration.
  };

  inline bool
  operator>= (target_decl l, target_decl r)
  {
    return static_cast<uint8_t> (l) >= static_cast<uint8_t> (r);
  }

  class LIBBUILD2_SYMEXPORT target
  {
  public:
    // Context this target belongs to.
    //
    context& ctx;

    // For targets that are in the src tree of a project we also keep the
    // corresponding out directory. As a result we may end up with multiple
    // targets for the same file if we are building multiple configurations of
    // the same project at once. We do it this way because, in a sense, a
    // target's out directory is its "configuration" (in terms of variables).
    // As an example, consider installing the same README file (src) but for
    // two different project configurations at once. Which installation
    // directory should we use? The answer depends on which configuration you
    // ask.
    //
    // Empty out directory indicates this target is in the out tree (including
    // when src == out). We also treat out of project targets as being in the
    // out tree.
    //
    const dir_path    dir;  // Absolute and normalized.
    const dir_path    out;  // Empty or absolute and normalized.
    const string      name; // Empty for dir{} and fsdir{} targets.
    optional<string>* ext_; // Reference to value in target_key.

    const string* ext () const; // Return NULL if not specified.
    const string& ext (string);

    // As above but assume targets mutex is locked.
    //
    const string* ext_locked () const;

    const dir_path&
    out_dir () const {return out.empty () ? dir : out;}

    // Note that the target declaration should only be upgraded via the MT-
    // safe target_set::insert().
    //
    target_decl decl;

    // Target group to which this target belongs, if any. Note that we assume
    // that the group and all its members are in the same scope (for example,
    // in variable lookup). We also don't support nested groups (with an
    // exception for ad hoc groups; see below).
    //
    // The semantics of the interaction between the group and its members and
    // what it means to, say, update the group, is unspecified and is
    // determined by the group's type. In particular, a group can be created
    // out of member types that have no idea they are part of this group
    // (e.g., cli.cxx{}).
    //
    // Normally, however, there are two kinds of groups: "all" and "choice".
    // In a choice-group, normally one of the members is selected when the
    // group is mentioned as a prerequisite with, perhaps, an exception for
    // special rules, like aliases, where it makes more sense to treat such
    // group prerequisites as a whole. In this case we say that the rule
    // "semantically recognizes" the group and picks some of its members.
    //
    // Updating a choice-group as a whole can mean updating some subset of its
    // members (e.g., lib{}). Or the group may not support this at all (e.g.,
    // obj{}).
    //
    // In an all-group, when a group is updated, normally all its members are
    // updated (and usually with a single command), though there could be some
    // members that are omitted, depending on the configuration (e.g., an
    // inline file not/being generated). When an all-group is mentioned as a
    // prerequisite, the rule is usually interested in the individual members
    // rather than the group target. For example, a C++ compile rule would
    // like to "see" the ?xx{} members when it gets a cli.cxx{} group.
    //
    // Which brings us to the group iteration mode. The target type contains a
    // flag called see_through that indicates whether the default iteration
    // mode for the group should be "see through"; that is, whether we see the
    // members or the group itself. For the iteration support itself, see the
    // *_prerequisite_members() machinery below.
    //
    // In an all-group we usually want the state (and timestamp; see mtime())
    // for members to come from the group. This is achieved with the special
    // target_state::group state. You would normally also use the group_recipe
    // for group members.
    //
    // Note that the group-member link-up can happen anywhere between the
    // member creation and rule matching so reading the group before the
    // member has been matched can be racy. However, once the member is linked
    // up to the group, this relationship is immutable. As a result, one can
    // atomically query the current value to see if already linked up (can be
    // used as an optimization, to avoid deadlocks, etc).
    //
    relaxed_atomic<const target*> group = nullptr;

    // What has been described above is an "explicit" group. That is, there is
    // a dedicated target type that explicitly serves as a group and there is
    // an explicit mechanism for discovering the group's members.
    //
    // However, sometimes, we may want to create a group on the fly out of a
    // normal target type. For example, we have the libs{} target type. But
    // on Windows a shared library consist of (at least) two files: the import
    // library and the DLL itself. So we somehow need to be able to capture
    // that. One approach would be to imply the presence of the second file.
    // However, that means that a lot of generic rules (e.g., clean, install,
    // etc) will need to know about this special semantics on Windows. Also,
    // there would be no convenient way to customize things like extensions,
    // etc (for which we use target-specific variables). In other words, it
    // would be much easier and more consistent to make these extra files
    // proper targets.
    //
    // So to support this requirement we have ad hoc groups. The idea is that
    // any target can be turned either by a user's declaration in a buildfile
    // or by the rule that matches it into an ad hoc group by chaining several
    // targets together.
    //
    // Ad hoc groups have a more restricted semantics compared to the normal
    // groups. In particular:
    //
    // - The ad hoc group itself is in a sense its first/primary target.
    //
    // - Group member's recipes, if set, should be group_recipe. Normally, a
    //   rule-managed member isn't matched by the rule since all that's
    //   usually needed is to derive its path.
    //
    // - Unless declared, members are discovered lazily, they are only known
    //   after the matching rule's apply() call.
    //
    // - Only declared members can be used as prerequisites but all can be
    //   used as targets (e.g., to set variables, etc).
    //
    // - Members don't have prerequisites.
    //
    // - Ad hoc group cannot have sub-groups (of any kind) though an ad hoc
    //   group can be a sub-group of an explicit group.
    //
    // - Member variable lookup skips the ad hoc group (since the group is the
    //   first member, this is normally what we want). But special semantics
    //   could be arranged; see var_backlink, for example.
    //
    // Note that ad hoc groups can be part of explicit groups. In a sense, we
    // have a two-level grouping: an explicit group with its members each of
    // which can be an ad hoc group. For example, lib{} contains libs{} which
    // may have an import stub as its ad hoc member.
    //
    // Use add_adhoc_member(), find_adhoc_member() from algorithms to manage
    // ad hoc members.
    //
    // One conceptual issue we have with our ad hoc group implementation is
    // that the behavior could be sensitive to the order in which the members
    // are specified (due to the primary member logic). For example, unless we
    // specify the header in the header/source group first, it will not be
    // installed. Perhaps the solution is to synthesize a separate group
    // target for the ad hoc members (with a special target type that rules
    // like install could recognize). See also the variable lookup semantics.
    // We could also probably support see_through via an attribute or some
    // such. Or perhaps such cases should be handled through explicit groups
    // and the ad hoc semantics is left to the non-see_through "primary
    // targets with a bunch of subordinates" cases. In other words, if the
    // members are "equal/symmetrical", then perhaps an explicit group is the
    // correct approach.
    //
    const_ptr<target> adhoc_member = nullptr;

    // Return true if this target is an ad hoc group (that is, its primary
    // member).
    //
    bool
    adhoc_group () const
    {
      // An ad hoc group can be a member of a normal group.
      //
      return adhoc_member != nullptr &&
        (group == nullptr || group->adhoc_member == nullptr);
    }

    // Return true if this target is an ad hoc group member (that is, its
    // secondary member).
    //
    bool
    adhoc_group_member () const
    {
      return group != nullptr && group->adhoc_member != nullptr;
    }

  public:
    // Normally you should not call this function directly and rather use
    // resolve_members() from <libbuild2/algorithm.hxx>. Note that action
    // is always inner.
    //
    virtual group_view
    group_members (action) const;

    // Note that the returned key "tracks" the target (except for the
    // extension).
    //
    target_key
    key () const;

    // As above but assume targets mutex is locked.
    //
    target_key
    key_locked () const;

    // Note that the returned name is guaranteed to be "stable" (e.g., for
    // hashing) only if the target has the extension assigned. This happens,
    // for example, when a path is derived for a path-based target (which
    // normally happens when such a target is matched for update).
    //
    names
    as_name () const;

    void
    as_name (names&) const;

    // Scoping.
    //
   public:
    // Most qualified scope that contains this target.
    //
    const scope&
    base_scope () const
    {
      if (ctx.phase != run_phase::load)
      {
        if (const scope* s = base_scope_.load (memory_order_consume))
          return *s;
      }

      return base_scope_impl ();
    }

    // Root scope of a project that contains this target. Note that
    // a target can be out of any (known) project root in which case
    // this function asserts. If you need to detect this situation,
    // then use base_scope().root_scope() expression instead.
    //
    const scope&
    root_scope () const
    {
      return *base_scope ().root_scope ();
    }

    // Root scope of a bundle amalgamation that contains this target. The
    // same notes as to root_scope() apply.
    //
    const scope&
    bundle_scope () const {return *root_scope ().bundle_scope ();}

    // Root scope of a strong amalgamation that contains this target.
    // The same notes as to root_scope() apply.
    //
    const scope&
    strong_scope () const {return *root_scope ().strong_scope ();}

    // Root scope of the outermost amalgamation that contains this target.
    // The same notes as to root_scope() apply.
    //
    const scope&
    weak_scope () const {return *root_scope ().weak_scope ();}

    bool
    in (const scope& s) const
    {
      return out_dir ().sub (s.out_path ());
    }

    // Implementation details (use above functions instead).
    //
    // Base scope cached value. Invalidated every time we switch to the load
    // phase (which is the only phase where we may insert new scopes).
    //
    mutable atomic<const scope*> base_scope_ {nullptr};

    const scope&
    base_scope_impl () const;

    // Prerequisites.
    //
    // We use an atomic-empty semantics that allows one to "swap in" a set of
    // prerequisites if none were specified. This is used to implement
    // "synthesized" dependencies.
    //
  public:
    using prerequisites_type = build2::prerequisites;

    const prerequisites_type&
    prerequisites () const;

    // Swap-in a list of prerequisites. Return false if unsuccessful (i.e.,
    // someone beat us to it), in which case the passed prerequisites are
    // not moved. Note that it can be called on const target.
    //
    bool
    prerequisites (prerequisites_type&&) const;

    // Check if there are any prerequisites. Note that the group version may
    // be racy (see target::group).
    //
    bool
    has_prerequisites () const;

    bool
    has_group_prerequisites () const;

  private:
    friend class parser;

    // Note that the state is also used to synchronize the prerequisites
    // value so we use the release-acquire ordering.
    //
    // 0 - absent
    // 1 - being set
    // 2 - present
    //
    atomic<uint8_t> prerequisites_state_ {0};
    prerequisites_type prerequisites_;

    // Target-specific variables.
    //
    // See also rule-specific variables below.
    //
  public:
    variable_map vars;

    // Lookup, including in groups to which this target belongs and then in
    // outer scopes (including target type/pattern-specific variables). If you
    // only want to lookup in this target, do it on the variable map directly
    // (and note that there will be no overrides).
    //
    using lookup_type = build2::lookup;

    lookup_type
    operator[] (const variable& var) const
    {
      return lookup (var).first;
    }

    lookup_type
    operator[] (const variable* var) const // For cached variables.
    {
      assert (var != nullptr);
      return operator[] (*var);
    }

    lookup_type
    operator[] (const string& name) const
    {
      const scope& bs (base_scope ());
      const variable* var (bs.var_pool ().find (name));
      return var != nullptr ? lookup (*var, &bs).first : lookup_type ();
    }

    // As above but also return the depth at which the value is found. The
    // depth is calculated by adding 1 for each test performed. So a value
    // that is from the target will have depth 1. That from the group -- 2.
    // From the innermost scope's target type/patter-specific variables for
    // the target -- 3. From the innermost scope's target type/patter-specific
    // variables for the group -- 4. From the innermost scope's variables --
    // 5. And so on. Note that if a target type/patter-specific append/prepend
    // was applied, then the returned depth is of the innermost such
    // append/prepend. The idea is that given two lookups from the same
    // target, we can say which one came earlier. If no value is found, then
    // the depth is set to ~0.
    //
    pair<lookup_type, size_t>
    lookup (const variable& var, const scope* bs = nullptr) const
    {
      auto p (lookup_original (var, bs));
      return var.overrides == nullptr
        ? p
        : (bs != nullptr
           ? *bs
           : base_scope ()).lookup_override (var, move (p), true);
    }

    // If limit is target, then only look in target and its target group
    // without continuing in scopes. If it is target_type, then additionally
    // look in target type/pattern-specific variables in scopes (see
    // scope::lookup_original() for the exact semantics).
    //
    // As an optimization, the caller can also pass the base scope of the
    // target, if already known.
    //
    // If locked is true, assume the targets mutex is locked (not relevant if
    // limit is target).
    //
    pair<lookup_type, size_t>
    lookup_original (const variable&,
                     lookup_limit = lookup_limit::none,
                     const scope* bs = nullptr,
                     bool locked = false) const;

    pair<lookup_type, size_t>
    lookup_original (const variable& var,
                     const scope* bs,
                     bool locked = false) const
    {
      return lookup_original (var, lookup_limit::none, bs, locked);
    }

    // Return a value suitable for assignment. See scope for details.
    //
    value&
    assign (const variable& var) {return vars.assign (var);}

    value&
    assign (const variable* var) {return vars.assign (var);} // For cached.

    // Note: variable must already be entered.
    //
    value&
    assign (const string& name)
    {
      return vars.assign (base_scope ().var_pool ().find (name));
    }

    // Return a value suitable for appending. See scope for details.
    //
    value&
    append (const variable&, const scope* bs = nullptr);

    // Note: variable must already be entered.
    //
    value&
    append (const string& name)
    {
      const scope& bs (base_scope ());
      return append (*bs.var_pool ().find (name), &bs);
    }

    // As above but assume the targets mutex is locked.
    //
    value&
    append_locked (const variable&, const scope* bs = nullptr);

    // Note: variable must already be entered.
    //
    value&
    append_locked (const string& name)
    {
      const scope& bs (base_scope ());
      return append_locked (*bs.var_pool ().find (name), &bs);
    }

    // Rule hints.
    //
  public:
    build2::rule_hints rule_hints;

    // Find the rule hint for the specified operation taking into account the
    // target type/group. Note: racy with regards to the group link-up and
    // should only be called when safe.
    //
    const string&
    find_hint (operation_id) const;

    // Ad hoc recipes.
    //
  public:
    vector<shared_ptr<adhoc_rule>> adhoc_recipes;

    // Target operation state.
    //
  public:
    // Atomic task count that is used during match and execution to track the
    // target's "meta-state" as well as the number of its sub-tasks (e.g.,
    // busy+1, busy+2, and so on, for instance, number of prerequisites
    // being matched or executed).
    //
    // For each operation in a meta-operation batch (current_on) we have a
    // "band" of counts, [touched, executed], that represent the target
    // meta-state. Once the next operation is started, this band "moves" thus
    // automatically resetting the target to "not yet touched" state for this
    // operation.
    //
    // The target is said to be synchronized (in this thread) if we have
    // either observed the task count to reach applied or executed or we have
    // successfully changed it (via compare_exchange) to locked or busy. If
    // the target is synchronized, then we can access and modify (second case)
    // its state etc.
    //
    // NOTE: see also the corresponding count_*() fuctions in context (must be
    //       kept in sync).
    //
    static const size_t offset_touched  = 1; // Target has been locked.
    static const size_t offset_tried    = 2; // Rule match has been tried.
    static const size_t offset_matched  = 3; // Rule has been matched.
    static const size_t offset_applied  = 4; // Rule has been applied.
    static const size_t offset_executed = 5; // Recipe has been executed.
    static const size_t offset_busy     = 6; // Match/execute in progress.

    // @@ PERF There is a lot of data below that is only needed for "output"
    //    as opposed to "source" targets (data pads, prerequisite_targets,
    //    etc). Maybe we should move this stuff to an optional extra (like we
    //    have for the root scope). Maybe we could even allocate it as part of
    //    the target's memory block or some such?

    // Inner/outer operation state. See <libbuild2/action.hxx> for details.
    //
    class LIBBUILD2_SYMEXPORT opstate
    {
    public:
      mutable atomic_count task_count {0}; // Start offset_touched - 1.

      // Number of direct targets that depend on this target in the current
      // operation. It is incremented during match and then decremented during
      // execution, before running the recipe. As a result, the recipe can
      // detect the last chance (i.e., last dependent) to execute the command
      // (see also the first/last execution modes in <libbuild2/operation.hxx>).
      //
      mutable atomic_count dependents {0};

      // Match state storage between the match() and apply() calls with only
      // the *_options members extended to reapply().
      //
      // Note: in reality, cur_options are used beyong (re)apply() as an
      // implementation detail.
      //
      build2::match_extra match_extra;

      // Matched rule (pointer to name_rule_map element). Note that in case of
      // a direct recipe assignment we may not have a rule (NULL).
      //
      const rule_match* rule;

      // Applied recipe.
      //
      // Note: also used as the auxiliary data storage during match, which is
      //       why mutable (see the target::data() API below for details). The
      //       default recipe_keep value is set by clear_target().
      //
      mutable build2::recipe recipe;
      mutable bool           recipe_keep;         // Keep after execution.
      bool                   recipe_group_action; // Recipe is group_action.

      // Target state for this operation.
      //
      // Note that it is undetermined until a rule is matched and recipe
      // applied (see set_recipe()). However, we need it to be not postponed
      // for ad hoc members that are not matched (see group_state()).
      //
      target_state state = target_state::uninitialized;

      // Set to true (only for the inner action) if this target has been
      // matched but not executed as a result of the resolve_members() call.
      // See also context::resolve_count.
      //
      bool resolve_counted;

      // Rule-specific variables.
      //
      // The rule (for this action) has to be matched before these variables
      // can be accessed and only the rule being matched can modify them (so
      // no iffy modifications of the group's variables by member's rules).
      //
      // They are also automatically cleared before another rule is matched,
      // similar to the auxiliary data storage. In other words, rule-specific
      // variables are only valid for this match-execute phase.
      //
      variable_map vars;

      // Lookup, continuing in the target-specific variables, etc. Note that
      // the group's rule-specific variables are not included. If you only
      // want to lookup in this target, do it on the variable map directly
      // (and note that there will be no overrides).
      //
      using lookup_type = build2::lookup;

      lookup_type
      operator[] (const variable& var) const
      {
        return lookup (var).first;
      }

      lookup_type
      operator[] (const variable* var) const // For cached variables.
      {
        assert (var != nullptr);
        return operator[] (*var);
      }

      // As above but also return the depth at which the value is found. The
      // depth is calculated by adding 1 for each test performed. So a value
      // that is from the rule will have depth 1. That from the target - 2,
      // and so on, similar to target-specific variables.
      //
      pair<lookup_type, size_t>
      lookup (const variable& var) const
      {
        auto p (lookup_original (var));
        return var.overrides == nullptr
          ? p
          : target_->base_scope ().lookup_override (var, move (p), true, true);
      }

      // The limit semantics is the same as in target::lookup_original().
      //
      pair<lookup_type, size_t>
      lookup_original (const variable&,
                       lookup_limit = lookup_limit::none) const;

      // Return a value suitable for assignment. See target for details.
      //
      value&
      assign (const variable& var) {return vars.assign (var);}

      value&
      assign (const variable* var) {return vars.assign (var);} // For cached.

      // Implementation details.
      //
    public:
      explicit
      opstate (context& c): vars (variable_map::owner::target, &c) {}

    private:
      friend class target_set;

      // Back-pointer, set by target_set along with vars.target_.
      //
      const target* target_ = nullptr;
    };

    action_state<opstate> state;

    opstate&       operator[] (action a)       {return state[a];}
    const opstate& operator[] (action a) const {return state[a];}

    // Return true if the target has been matched for the specified action.
    // This function can only be called during the match or execute phases.
    //
    // If you need to observe something in the matched target (e.g., the
    // matched rule or recipe), use memory_order_acquire.
    //
    bool
    matched (action, memory_order mo = memory_order_relaxed) const;

    // This function can only be called during match if we have observed
    // (synchronization-wise) that this target has been matched (i.e., the
    // rule has been applied) for this action.
    //
    target_state
    matched_state (action, bool fail = true) const;

    // See try_match_sync().
    //
    pair<bool, target_state>
    try_matched_state (action, bool fail = true) const;

    // After the target has been matched and synchronized, check if the target
    // is known to be unchanged. Used for optimizations during search & match.
    //
    bool
    unchanged (action) const;

    // This function can only be called during execution if we have observed
    // (synchronization-wise) that this target has been executed.
    //
    // It can also be called during the serial load phase (but make sure you
    // understand what you are doing).
    //
    target_state
    executed_state (action, bool fail = true) const;

    // Return true if the state comes from the group. Target must be at least
    // matched except for ad hoc group members during the execute phase.
    //
    bool
    group_state (action) const;

  protected:
    // Version that should be used during match after the target has been
    // matched for this action.
    //
    // Indicate whether there is a rule match with the first half of the
    // result (see try_match_sync()).
    //
    pair<bool, target_state>
    matched_state_impl (action) const;

    // Return fail-untranslated (but group-translated) state assuming the
    // target is executed and synchronized.
    //
    target_state
    executed_state_impl (action) const;

  public:
    // Targets to which prerequisites resolve for this action. Note that
    // unlike prerequisite::target, these can be resolved to group members.
    // NULL means the target should be skipped (or the rule may simply not add
    // such a target to the list).
    //
    // A rule should make sure that the target's prerequisite_targets are in
    // the "canonical" form (that is, all the prerequisites that need to be
    // executed are present with prerequisite_target::target pointing to the
    // corresponding target). This is relied upon in a number of places,
    // including in dump and to be able to pretend-execute the operation on
    // this target without actually calling the recipe (see perform_execute(),
    // resolve_members_impl() for background). Note that a rule should not
    // store targets that are semantically prerequisites in an ad hoc manner
    // (e.g., in match data) with a few well-known execeptions (see
    // group_recipe and inner_recipe).
    //
    // Note that the recipe may modify this list during execute. Normally this
    // would be just blanking out of ad hoc prerequisites, in which case check
    // for ad hoc first and for not NULL second if accessing prerequisites of
    // targets that you did not execute (see the library metadata protocol in
    // cc for an example).
    //
    mutable action_state<build2::prerequisite_targets> prerequisite_targets;

    // Auxiliary data storage.
    //
    // A rule that matches (i.e., returns true from its match() function) may
    // use this facility to pass data between its match and apply functions as
    // well as the recipe. Specifically, between match() and apply() the data
    // is stored in the recipe member (which is std::move_only_function-like).
    // If the data needs to be passed on to the recipe, then it must become
    // the recipe itself. Here is a typical arrangement:
    //
    // class compile_rule
    // {
    //   struct match_data
    //   {
    //     ... // Data.
    //
    //     const compile_rule& rule;
    //
    //     target_state
    //     operator() (action a, const target& t)
    //     {
    //       return rule.perform_update (a, t, this);
    //     }
    //   };
    //
    //   virtual bool
    //   match (action a, const target& t)
    //   {
    //     ... // Determine if matching.
    //
    //     t.data (a, match_data {..., *this});
    //     return true;
    //   }
    //
    //   virtual bool
    //   apply (action a, target& t)
    //   {
    //     match_data& md (t.data (a));
    //
    //     ... // Match prerequisites, etc.
    //
    //     return move (md); // Data becomes the recipe.
    //   }
    //
    //   target_state
    //   perform_update (action a, const target& t, match_data& md) const
    //   {
    //     ... // Access data (also available as t.data<match_data> (a)).
    //   }
    // };
    //
    // Note: see also similar facility in match_extra.
    //
    // After the recipe is executed, the recipe/data is destroyed, unless
    // explicitly requested not to (see below). The rule may static assert
    // that the small size of the storage (which doesn't require dynamic
    // memory allocation) is sufficient for its needs.
    //
    // Note also that a rule that delegates to another rule may need to store
    // the base rule's data/recipe in its own data/recipe.

    // Provide the small object optimization size for the common compilers
    // (see recipe.hxx for details) in case a rule wants to make sure its data
    // won't require a dynamic memory allocation. Note that using a minimum
    // generally available (2 pointers) is not always possible because the
    // data size may depend on sizes of other compiler-specific types (e.g.,
    // std::string).
    //
    static constexpr size_t small_data_size =
#if defined(__GLIBCXX__)
      sizeof (void*) * 2
#elif defined(_LIBCPP_VERSION)
      sizeof (void*) * 3
#elif defined(_MSC_VER)
      sizeof (void*) * 6
#else
      sizeof (void*) * 2 // Assume at least 2 pointers.
#endif
      ;

    template <typename T>
    struct data_wrapper
    {
      T d;

      target_state
      operator() (action, const target&) const // Never called.
      {
        return target_state::unknown;
      }
    };

    // Avoid wrapping the data if it is already a recipe.
    //
    // Note that this techniques requires a fix for LWG issue 2132 (which all
    // our minimum supported compiler versions appear to have).
    //
    template <typename T>
    struct data_invocable: std::is_constructible<
      std::function<recipe_function>,
      std::reference_wrapper<typename std::remove_reference<T>::type>> {};

    template <typename T>
    typename std::enable_if<!data_invocable<T>::value, void>::type
    data (action a, T&& d) const
    {
      using V = typename std::remove_cv<
        typename std::remove_reference<T>::type>::type;

      const opstate& s (state[a]);
      s.recipe = data_wrapper<V> {forward<T> (d)};
      s.recipe_keep = false; // Can't keep non-recipe data.
    }

    template <typename T>
    typename std::enable_if<!data_invocable<T>::value, T&>::type
    data (action a) const
    {
      using V = typename std::remove_cv<T>::type;
      return state[a].recipe.target<data_wrapper<V>> ()->d;
    }

    // Return NULL if there is no data or the data is of a different type.
    //
    template <typename T>
    typename std::enable_if<!data_invocable<T>::value, T*>::type
    try_data (action a) const
    {
      using V = typename std::remove_cv<T>::type;

      if (auto& r = state[a].recipe)
        if (auto* t = r.target<data_wrapper<V>> ())
          return &t->d;

      return nullptr;
    }

    // Note that in this case we don't strip const (the expectation is that we
    // move the recipe in/out of data).
    //
    // If keep is true, then keep the recipe as data after execution. In
    // particular, this can be used to communicate between inner/outer rules
    // (see cc::install_rule for an example).
    //
    //
    template <typename T>
    typename std::enable_if<data_invocable<T>::value, void>::type
    data (action a, T&& d, bool keep = false) const
    {
      const opstate& s (state[a]);
      s.recipe = forward<T> (d);
      s.recipe_keep = keep;
    }

    void
    keep_data (action a, bool keep = true) const
    {
      state[a].recipe_keep = keep;
    }

    template <typename T>
    typename std::enable_if<data_invocable<T>::value, T&>::type
    data (action a) const
    {
      return *state[a].recipe.target<T> ();
    }

    template <typename T>
    typename std::enable_if<data_invocable<T>::value, T*>::type
    try_data (action a) const
    {
      auto& r = state[a].recipe;
      return r ? r.target<T> () : nullptr;
    }

    // Target type info and casting.
    //
  public:
    const target*
    is_a (const target_type& tt) const
    {
      return type ().is_a (tt) ? this : nullptr;
    }

    template <typename T>
    T*
    is_a ()
    {
      // At least with GCC we see slightly better and more consistent
      // performance with our own type information.
      //
#if 0
      return dynamic_cast<T*> (this);
#else
      // We can skip dynamically-derived type here (derived_type).
      //
      return dynamic_type->is_a<T> () ? static_cast<T*> (this) : nullptr;
#endif
    }

    template <typename T>
    const T*
    is_a () const
    {
#if 0
      return dynamic_cast<const T*> (this);
#else
      return dynamic_type->is_a<T> () ? static_cast<const T*> (this) : nullptr;
#endif
    }

    const target*
    is_a (const char* n) const
    {
      return type ().is_a (n) ? this : nullptr;
    }

    // Unchecked cast.
    //
    template <typename T>
    T&
    as () {return static_cast<T&> (*this);}

    template <typename T>
    const T&
    as () const {return static_cast<const T&> (*this);}

    // Target type information.
    //
    // A derived target is expected to set dynamic_type to its static_type in
    // its constructor body.
    //
    // We also have dynamic "derivation" support (e.g., via define in
    // buildfile).
    //
    const target_type&
    type () const
    {
      return derived_type != nullptr ? *derived_type : *dynamic_type;
    }

    static const target_type static_type;
    const target_type* dynamic_type;
    const target_type* derived_type = nullptr;

    // RW access.
    //
    target&
    rw () const
    {
      assert (ctx.phase == run_phase::load);
      return const_cast<target&> (*this);
    }

  public:
    // Split the name (not necessarily a simple path) into target name (in
    // place) and extension (returned).
    //
    static optional<string>
    split_name (string&, const location&);

    // Combine the target name (not necessarily a simple path) and
    // extension.
    //
    // If the target type has the default extension, then "escape" the
    // existing extension if any.
    //
    static void
    combine_name (string&, const optional<string>&, bool default_extension);

    // Targets should be created via the targets set below.
    //
  protected:
    friend class target_set;

    target (context& c, dir_path d, dir_path o, string n)
        : ctx (c),
          dir (move (d)), out (move (o)), name (move (n)),
          vars (*this, false /* shared */),
          state (c)
    {
      dynamic_type = &static_type;
    }

  public:
    target (target&&) = delete;
    target& operator= (target&&) = delete;

    target (const target&) = delete;
    target& operator= (const target&) = delete;

    virtual
    ~target ();
  };

  // All targets are from the targets set below.
  //
  inline bool
  operator== (const target& x, const target& y) {return &x == &y;}

  inline bool
  operator!= (const target& x, const target& y) {return !(x == y);}

  // Note that if the targets mutex is locked, then calling this operator
  // will lead to a deadlock. Instead, do:
  //
  // ... << t.key_locked () << ...
  //
  ostream&
  operator<< (ostream&, const target&);

  // Sometimes it is handy to "mark" a pointer to a target (for example, in
  // prerequisite_targets). We use the last 2 bits in a pointer for that (aka
  // the "bit stealing" technique). Note that the pointer needs to be unmarked
  // before it can be usable so care must be taken in the face of exceptions,
  // etc.
  //
  void
  mark (const target*&, uint8_t = 1);

  uint8_t
  marked (const target*); // Can be used as a predicate or to get the mark.

  uint8_t
  unmark (const target*&);

  // Helper for dealing with the prerequisite inclusion/exclusion (see
  // var_include in context.hxx).
  //
  // If the lookup argument is not NULL, then it will be set to the operation-
  // specific override, if present. Note that in this case the caller is
  // expected to validate that the override value is valid (note: use the same
  // diagnostics as in include() for consistency).
  //
  // Note that the include(prerequisite_member) overload is also provided.
  //
  include_type
  include (action, const target&, const prerequisite&, lookup* = nullptr);

  // A "range" that presents the prerequisites of a group and one of
  // its members as one continuous sequence, or, in other words, as
  // if they were in a single container. The group's prerequisites
  // come first followed by the member's. If you need to see them
  // in the other direction, iterate in reverse, for example:
  //
  // for (prerequisite& p: group_prerequisites (t))
  //
  // for (prerequisite& p: reverse_iterate (group_prerequisites (t))
  //
  // Note that in this case the individual elements of each list will
  // also be traversed in reverse, but that's what you usually want,
  // anyway.
  //
  // Note that you either should be iterating over a locked target (e.g., in
  // rule's match() or apply()) or you should call resolve_group().
  //
  class group_prerequisites
  {
  public:
    explicit
    group_prerequisites (const target&);

    group_prerequisites (const target&, const target* group);

    using prerequisites_type = target::prerequisites_type;
    using base_iterator      = prerequisites_type::const_iterator;

    struct iterator
    {
      using value_type        = base_iterator::value_type;
      using pointer           = base_iterator::pointer;
      using reference         = base_iterator::reference;
      using difference_type   = base_iterator::difference_type;
      using iterator_category = std::bidirectional_iterator_tag;

      iterator () {}
      iterator (const prerequisites_type* t,
                const prerequisites_type* g,
                const prerequisites_type* c,
                base_iterator i): t_ (t), g_ (g), c_ (c), i_ (i) {}

      iterator&
      operator++ ();

      iterator
      operator++ (int) {iterator r (*this); operator++ (); return r;}

      iterator&
      operator-- ();

      iterator
      operator-- (int) {iterator r (*this); operator-- (); return r;}

      reference operator* () const {return *i_;}
      pointer operator-> () const {return i_.operator -> ();}

      friend bool
      operator== (const iterator& x, const iterator& y)
      {
        return x.t_ == y.t_ && x.g_ == y.g_ && x.c_ == y.c_ && x.i_ == y.i_;
      }

      friend bool
      operator!= (const iterator& x, const iterator& y) {return !(x == y);}

    private:
      const prerequisites_type* t_ = nullptr;
      const prerequisites_type* g_ = nullptr;
      const prerequisites_type* c_ = nullptr;
      base_iterator             i_;
    };

    using reverse_iterator = std::reverse_iterator<iterator>;

    iterator
    begin () const;

    iterator
    end () const;

    reverse_iterator
    rbegin () const {return reverse_iterator (end ());}

    reverse_iterator
    rend () const {return reverse_iterator (begin ());}

    size_t
    size () const;

  private:
    const prerequisites_type* t_; // NULL if empty.
    const prerequisites_type* g_; // NULL if no group or empty.
  };

  // A member of a prerequisite. If 'member' is NULL, then this is the
  // prerequisite itself. Otherwise, it is its member. In this case
  // 'prerequisite' still refers to the prerequisite.
  //
  struct prerequisite_member
  {
    using scope_type = build2::scope;
    using target_type = build2::target;
    using prerequisite_type = build2::prerequisite;
    using target_type_type = build2::target_type;

    const prerequisite_type& prerequisite;
    const target_type* member;

    template <typename T>
    bool
    is_a () const
    {
      return member != nullptr
        ? member->is_a<T> () != nullptr
        : prerequisite.is_a<T> ();
    }

    bool
    is_a (const target_type_type& tt) const
    {
      return member != nullptr
        ? member->is_a (tt) != nullptr
        : prerequisite.is_a (tt);
    }

    prerequisite_key
    key () const;

    const target_type_type&
    type () const
    {
      return member != nullptr ? member->type () : prerequisite.type;
    }

    const string&
    name () const
    {
      return member != nullptr ? member->name : prerequisite.name;
    }

    const dir_path&
    dir () const
    {
      return member != nullptr ? member->dir : prerequisite.dir;
    }

    const optional<project_name>&
    proj () const
    {
      // Member cannot be project-qualified.
      //
      return member != nullptr ? nullopt_project_name : prerequisite.proj;
    }

    const scope_type&
    scope () const
    {
      return member != nullptr ? member->base_scope () : prerequisite.scope;
    }

    const target_type&
    search (const target_type& t) const
    {
      return member != nullptr ? *member : build2::search (t, prerequisite);
    }

    const target_type*
    search_existing () const
    {
      return member != nullptr
        ? member
        : build2::search_existing (prerequisite);
    }

    const target_type*
    load (memory_order mo = memory_order_consume)
    {
      return member != nullptr ? member : prerequisite.target.load (mo);
    }

    // Return as a new prerequisite instance. Note that it includes a copy
    // of prerequisite-specific variables.
    //
    prerequisite_type
    as_prerequisite () const;
  };

  // It is often stored as the target's auxiliary data so make sure there is
  // no destructor overhead.
  //
  static_assert (std::is_trivially_destructible<prerequisite_member>::value,
                 "prerequisite_member is not trivially destructible");

  inline bool
  operator== (const prerequisite_member& x, const prerequisite_member& y)
  {
    return &x.prerequisite == &y.prerequisite && x.member == y.member;
  }

  inline bool
  operator!= (const prerequisite_member& x, const prerequisite_member& y)
  {
    return !(x == y);
  }

  inline ostream&
  operator<< (ostream& os, const prerequisite_member& pm)
  {
    return os << pm.key ();
  }

  include_type
  include (action, const target&, const prerequisite_member&, lookup* = nullptr);

  // A "range" that presents a sequence of prerequisites (e.g., from
  // group_prerequisites()) as a sequence of prerequisite_member's. For each
  // group prerequisite you will "see" either the prerequisite itself or all
  // its members, depending on the default iteration mode of the target group
  // type (ad hoc groups are never implicitly see through since one can only
  // safely access members after a synchronous match). You can skip the
  // rest of the group members with leave_group() and you can force iteration
  // over the members with enter_group(). Usage:
  //
  // for (prerequisite_member pm: prerequisite_members (a, ...))
  //
  // Where ... can be:
  //
  //   t.prerequisites
  //   reverse_iterate(t.prerequisites)
  //   group_prerequisites (t)
  //   reverse_iterate (group_prerequisites (t))
  //
  // But use shortcuts instead:
  //
  // prerequisite_members (a, t)
  // reverse_prerequisite_members (a, t)
  // group_prerequisite_members (a, t)
  // reverse_group_prerequisite_members (a, t)
  //
  template <typename R>
  class prerequisite_members_range;

  // See-through group members iteration mode. Ad hoc members must always
  // be entered explicitly.
  //
  // Note that if the group is empty, then we see the group itself (rather
  // than nothing). Failed that, an empty group would never be executed (e.g.,
  // during clean) since there is no member to trigger the group execution.
  // Other than that, it feels like seeing the group in this cases should be
  // harmless (i.e., rules are generally prepared to see prerequisites they
  // don't recognize).
  //
  enum class members_mode
  {
    always, // Iterate over members if not empty, group if empty, assert if
            // not resolvable.
    maybe,  // Iterate over members if resolvable and not empty, group
            // otherwise.
    never   // Iterate over group (can still use enter_group()).
  };

  template <typename R>
  inline prerequisite_members_range<R>
  prerequisite_members (action a, const target& t,
                        R&& r,
                        members_mode m = members_mode::always)
  {
    return prerequisite_members_range<R> (a, t, forward<R> (r), m);
  }

  template <typename R>
  class prerequisite_members_range
  {
  public:
    prerequisite_members_range (action a, const target& t,
                                R&& r,
                                members_mode m)
        : a_ (a), t_ (t), mode_ (m), r_ (forward<R> (r)), e_ (r_.end ()) {}

    using base_iterator = decltype (declval<R> ().begin ());

    struct iterator
    {
      using value_type        = prerequisite_member;
      using pointer           = const value_type*;
      using reference         = const value_type&;
      using difference_type   = typename base_iterator::difference_type;
      using iterator_category = std::forward_iterator_tag;

      iterator (): r_ (nullptr) {}
      iterator (const prerequisite_members_range* r, const base_iterator& i)
          : r_ (r), i_ (i), g_ {nullptr, 0}, k_ (nullptr)
      {
        if (r_->mode_ != members_mode::never &&
            i_ != r_->e_                     &&
            i_->type.see_through ())
          switch_mode ();
      }

      iterator& operator++ ();
      iterator operator++ (int) {iterator r (*this); operator++ (); return r;}

      // Skip iterating over the rest of this group's members, if any. Note
      // that the only valid operation after this call is to increment the
      // iterator.
      //
      void
      leave_group ();

      // Iterate over this group's members. Return false if the member
      // information is not available (note: return true if the group is
      // empty). Similar to leave_group(), you should increment the iterator
      // after calling this function provided group() returns true (see
      // below).
      //
      bool
      enter_group ();

      // Return true if the next element is this group's members. Normally
      // used to iterate over group members only, for example:
      //
      // for (...; ++i)
      // {
      //   if (i->prerequisite.type.see_through ())
      //   {
      //     for (i.enter_group (); i.group (); )
      //     {
      //       ++i;
      //       ...
      //     }
      //   }
      // }
      //
      bool
      group () const;

      value_type operator* () const
      {
        const target* t (k_ != nullptr ? k_:
                         g_.count != 0 ? g_.members[j_ - 1] : nullptr);

        return value_type {*i_, t};
      }

      pointer operator-> () const
      {
        static_assert (
          std::is_trivially_destructible<value_type>::value,
          "prerequisite_member is not trivially destructible");

        const target* t (k_ != nullptr ? k_:
                         g_.count != 0 ? g_.members[j_ - 1] : nullptr);

        return new (&m_) value_type {*i_, t};
      }

      friend bool
      operator== (const iterator& x, const iterator& y)
      {
        return x.i_ == y.i_ &&
          x.g_.count == y.g_.count &&
          (x.g_.count == 0 || x.j_ == y.j_) &&
          x.k_ == y.k_;
      }

      friend bool
      operator!= (const iterator& x, const iterator& y) {return !(x == y);}

      // What we have here is a state for three nested iteration modes (and
      // no, I am not proud of it). The innermost mode is iteration over an ad
      // hoc group (k_). Then we have iteration over a normal group (g_ and
      // j_). Finally, at the outer level, we have the range itself (i_).
      //
      // Also, the enter/leave group support is full of ugly, special cases.
      //
    private:
      void
      switch_mode ();

      group_view
      resolve_members (const prerequisite&);

    private:
      const prerequisite_members_range* r_;
      base_iterator i_;
      group_view g_;
      size_t j_;        // 1-based index, to support enter_group().
      const target* k_; // Current member of ad hoc group or NULL.
      alignas (value_type) mutable unsigned char m_[sizeof (value_type)];
    };

    iterator
    begin () const {return iterator (this, r_.begin ());}

    iterator
    end () const {return iterator (this, e_);}

  private:
    action a_;
    const target& t_;
    members_mode mode_;
    R r_;
    base_iterator e_;
  };

  // prerequisite_members(t.prerequisites ())
  //
  auto
  prerequisite_members (action a, const target& t,
                        members_mode m = members_mode::always);

  // prerequisite_members(reverse_iterate(t.prerequisites ()))
  //
  auto
  reverse_prerequisite_members (action a, const target& t,
                                members_mode m = members_mode::always);

  // prerequisite_members(group_prerequisites (t))
  //
  inline auto
  group_prerequisite_members (action a, target& t,
                              members_mode m = members_mode::always)
  {
    return prerequisite_members (a, t, group_prerequisites (t), m);
  }

  inline auto
  group_prerequisite_members (action a, const target& t,
                              members_mode m = members_mode::always)
  {
    return prerequisite_members (a, t, group_prerequisites (t), m);
  }

  // prerequisite_members(reverse_iterate (group_prerequisites (t)))
  //
  inline auto
  reverse_group_prerequisite_members (action a, target& t,
                                      members_mode m = members_mode::always)
  {
    return prerequisite_members (
      a, t, reverse_iterate (group_prerequisites (t)), m);
  }

  inline auto
  reverse_group_prerequisite_members (action a, const target& t,
                                      members_mode m = members_mode::always)
  {
    return prerequisite_members (
      a, t, reverse_iterate (group_prerequisites (t)), m);
  }

  // A target with an unspecified extension is considered equal to the one
  // with the specified one. And when we find a target with an unspecified
  // extension via a key with the specified one, we update the extension,
  // essentially modifying the map's key. To make this work we use a hash
  // map. The key's hash ignores the extension, so the hash will stay stable
  // across extension updates.
  //
  // @@ TODO: we currently do not detect ambiguity if there are multiple merge
  //    candidates for a no-extension key. We could probably do it using the
  //    unordered_map::bucket() API.
  //
  // Note also that once the extension is specified, it becomes immutable.
  //
  class LIBBUILD2_SYMEXPORT target_set
  {
  public:
    using map_type = std::unordered_map<target_key, unique_ptr<target>>;

    // Return existing target or NULL.
    //
    const target*
    find (const target_key& k, tracer& trace) const;

    const target*
    find (const target_type& type,
          const dir_path& dir,
          const dir_path& out,
          const string& name,
          const optional<string>& ext,
          tracer& trace) const
    {
      return find (target_key {&type, &dir, &out, &name, ext}, trace);
    }

    template <typename T>
    const T*
    find (const target_type& type,
          const dir_path& dir,
          const dir_path& out,
          const string& name,
          const optional<string>& ext,
          tracer& trace) const
    {
      return static_cast<const T*> (find (type, dir, out, name, ext, trace));
    }

    // As above but ignore the extension.
    //
    const target*
    find (const target_type& type,
          const dir_path& dir,
          const dir_path& out,
          const string& name) const
    {
      slock l (mutex_, defer_lock); if (ctx.phase != run_phase::load) l.lock ();
      auto i (map_.find (target_key {&type, &dir, &out, &name, nullopt}));
      return i != map_.end () ? i->second.get () : nullptr;
    }

    template <typename T>
    const T*
    find (const dir_path& dir, const dir_path& out, const string& name) const
    {
      return static_cast<const T*> (find (T::static_type, dir, out, name));
    }

    // If the target was inserted, keep the map exclusive-locked and return
    // the lock. In this case, the target is effectively still being created
    // since nobody can see it until the lock is released. Note that there
    // is normally quite a bit of contention around this map so make sure to
    // not hold the lock longer than absolutely necessary.
    //
    // If skip_find is true, then don't first try to find an existing target
    // with a shared lock, instead going directly for the unique lock and
    // insert. It's a good idea to pass true as this argument if you know the
    // target is unlikely to be there.
    //
    // If need_lock is false, then release the lock (the target insertion is
    // indicated by the presence of the associated mutex).
    //
    pair<target&, ulock>
    insert_locked (const target_type&,
                   dir_path dir,
                   dir_path out,
                   string name,
                   optional<string> ext,
                   target_decl,
                   tracer&,
                   bool skip_find = false,
                   bool need_lock = true);

    // As above but instead of the lock return an indication of whether the
    // target was inserted.
    //
    pair<target&, bool>
    insert (const target_type& tt,
            dir_path dir,
            dir_path out,
            string name,
            optional<string> ext,
            target_decl decl,
            tracer& t,
            bool skip_find = false)
    {
      auto p (insert_locked (tt,
                             move (dir),
                             move (out),
                             move (name),
                             move (ext),
                             decl,
                             t,
                             skip_find,
                             false));

      return pair<target&, bool> (p.first, p.second.mutex () != nullptr);
    }

    // The following versions always enter implied targets and are primarily
    // meant for entering members when there is a group or vice versa.
    //
    template <typename T>
    T&
    insert_implied (const target_type& tt,
                    dir_path dir,
                    dir_path out,
                    string name,
                    optional<string> ext,
                    tracer& t,
                    bool skip_find = false)
    {
      return insert (tt,
                     move (dir),
                     move (out),
                     move (name),
                     move (ext),
                     target_decl::implied,
                     t,
                     skip_find).first.template as<T> ();
    }

    template <typename T>
    T&
    insert_implied (const dir_path& dir,
                    const dir_path& out,
                    const string& name,
                    const optional<string>& ext,
                    tracer& t,
                    bool skip_find = false)
    {
      return insert_implied<T> (
        T::static_type, dir, out, name, ext, t, skip_find);
    }

    template <typename T>
    T&
    insert_implied (const dir_path& dir,
                    const dir_path& out,
                    const string& name,
                    tracer& t,
                    bool skip_find = false)
    {
      return insert_implied<T> (dir, out, name, nullopt, t, skip_find);
    }

    // Note: not MT-safe so can only be used during serial execution.
    //
  public:
    using iterator = butl::map_iterator_adapter<map_type::iterator>;
    using const_iterator = butl::map_iterator_adapter<map_type::const_iterator>;

    iterator begin () {return map_.begin ();}
    iterator end ()   {return map_.end ();}

    const_iterator begin () const {return map_.begin ();}
    const_iterator end ()   const {return map_.end ();}

    size_t
    size () const {return map_.size ();}

    void
    clear () {map_.clear ();}

  private:
    friend class target; // Access to mutex.
    friend class context;

    explicit
    target_set (context& c): ctx (c) {}

    context& ctx;

    mutable shared_mutex mutex_;
    map_type map_;

#if 0
    size_t buckets_ = 0;
#endif
  };

  // Modification time-based target.
  //
  class LIBBUILD2_SYMEXPORT mtime_target: public target
  {
  public:
    mtime_target (context& c, dir_path d, dir_path o, string n)
      : target (c, move (d), move (o), move (n))
    {
      dynamic_type = &static_type;
    }

    // Modification time is an "atomic cache". That is, it can be set at any
    // time (including on a const instance) and we assume everything will be
    // ok regardless of the order in which racing updates happen because we do
    // not modify the external state (which is the source of timestemps) while
    // updating the internal.
    //
    // The modification time is reserved for the inner operation thus there is
    // no action argument.
    //
    // The rule for groups that utilize target_state::group is as follows: if
    // it has any members that are mtime_targets, then the group should be
    // mtime_target and the members get the mtime from it. During match and
    // execute the target should be synchronized.
    //
    // Note that this function can be called before the target is matched in
    // which case the value always comes from the target itself. In other
    // words, that group logic only kicks in once the target is matched.
    //
    timestamp
    mtime () const;

    // Note also that while we can cache the mtime, it may be ignored if the
    // target state is set to group (see above).
    //
    // NOTE: if setting both path and mtime (typically during match), then use
    // the path_target::path_mtime() function to do it in the correct order.
    //
    void
    mtime (timestamp) const;

    // If the mtime is unknown, then load it from the filesystem also caching
    // the result.
    //
    // Note: must not be used if the target state is group.
    //
    timestamp
    load_mtime (const path&) const;

    // Return true if this target is newer than the specified timestamp.
    //
    // Note: can only be called during execute on a synchronized target.
    //
    bool
    newer (timestamp) const;

    // As above but for cases where the state is already queried.
    //
    bool
    newer (timestamp, target_state) const;

  public:
    static const target_type static_type;

  protected:

    // Complain if timestamp is not lock-free unless we were told non-lock-
    // free is ok.
    //
#ifndef LIBBUILD2_ATOMIC_NON_LOCK_FREE
    // C++17:
    //
    // static_assert (atomic<timestamp::rep>::is_always_lock_free,
    //               "timestamp is not lock-free on this architecture");
    //
#if !defined(ATOMIC_LLONG_LOCK_FREE) || ATOMIC_LLONG_LOCK_FREE != 2
#  error timestamp is not lock-free on this architecture
#endif
#endif

    // Note that the value is not used to synchronize any other state so we
    // use the release-consume ordering (i.e., we are only interested in the
    // mtime value being synchronized).
    //
    // Store it as an underlying representation (normally int64_t) since
    // timestamp is not usable with atomic (non-noexcept default ctor).
    //
    mutable atomic<timestamp::rep> mtime_ {timestamp_unknown_rep};
  };

  // Filesystem path-based target.
  //
  class LIBBUILD2_SYMEXPORT path_target: public mtime_target
  {
  public:
    path_target (context& c, dir_path d, dir_path o, string n)
      : mtime_target (c, move (d), move (o), move (n))
    {
      dynamic_type = &static_type;
    }

    using path_type = build2::path;

    // Target path. Must be absolute and normalized.
    //
    // Target path is an "atomic consistent cache". That is, it can be set at
    // any time (including on a const instance) but any subsequent updates
    // must set the same path. Or, in other words, once the path is set, it
    // never changes.
    //
    // An empty path may signify special unknown/undetermined/unreal location
    // (for example, a binless library or an installed import library -- we
    // know the DLL is there, just not exactly where). In this case you could
    // also set its mtime to timestamp_unreal (but don't have to, if a real
    // timestamp can be derived, for example, from the import library in the
    // DLL case above).
    //
    // Note, however, that a target with timestamp_unreal does not have to
    // have an empty path. One consequence of this arrangement (assigned path
    // with unreal timestamp) is that the timestamp of such a target when used
    // as a prerequisite won't affect the dependent's target out-of-date-ness.
    //
    // We used to return a pointer to properly distinguish between not set and
    // empty but that proved too tedious to work with. So now we return empty
    // path both when not set (which will be empty_path so you can distinguish
    // the two case if you really want to) and when set to empty. Note that
    // this means there could be a race between path and mtime (unless you
    // lock the target in some other way; see file_rule) so in this case it
    // makes sense to set the timestamp first.
    //
    // NOTE: if setting both path and mtime (typically during match), then use
    // the path_mtime() function to do it in the correct order.
    //
    const path_type&
    path (memory_order = memory_order_acquire) const;

    const path_type&
    path (path_type) const;

    // Set both path and mtime and in the correct order.
    //
    const path_type&
    path_mtime (path_type, timestamp) const;

    // Load mtime using the cached path.
    //
    timestamp
    load_mtime () const;

    // Derive a path from target's dir, name, and, if set, ext. If ext is not
    // set, try to derive it using the target type extension function and
    // fallback to default_ext, if specified. In both cases also update the
    // target's extension (this becomes important if later we need to reliably
    // determine whether this file has an extension; think hxx{foo.bar.} and
    // hxx{*}:extension is empty).
    //
    // If name_prefix is not NULL, add it before the name part and after the
    // directory. Similarly, if name_suffix is not NULL, add it after the name
    // part and before the extension. And if extra_ext is not NULL, then add
    // it as an extra extension (think libfoo.so.1.2.3).
    //
    // Finally, if the path was already assigned to this target, then this
    // function verifies that the two are the same.
    //
    const path_type&
    derive_path (const char* default_ext = nullptr,
                 const char* name_prefix = nullptr,
                 const char* name_suffix = nullptr,
                 const char* extra_ext = nullptr);

    // As above but with the already derived (by calling derive_extension())
    // extension.
    //
    const path_type&
    derive_path_with_extension (const string& ext,
                                const char* name_prefix = nullptr,
                                const char* name_suffix = nullptr,
                                const char* extra_ext = nullptr);

    // This version can be used to derive the path from another target's path
    // by adding another extension.
    //
    const path_type&
    derive_path (path_type base,
                 const char* default_ext = nullptr,
                 const char* extra_ext = nullptr);

    // As above but with the already derived (by calling derive_extension())
    // extension.
    //
    const path_type&
    derive_path_with_extension (path_type base,
                                const string& ext,
                                const char* extra_ext = nullptr);

    // As above but only derives (and returns) the extension (empty means no
    // extension used).
    //
    const string&
    derive_extension (const char* default_ext = nullptr)
    {
      return *derive_extension (false, default_ext);
    }

    // As above but if search is true then look for the extension as if it was
    // a prerequisite, not a target. In this case, if no extension can be
    // derived, return NULL instead of failing (like search_existing_file()).
    //
    const string*
    derive_extension (bool search, const char* default_ext = nullptr);

    // Const versions of the above that can be used on unlocked targets. Note
    // that here we don't allow providing any defaults since you probably
    // should only use this version if everything comes from the target itself
    // (and is therefore atomic).
    //
    const path_type&
    derive_path () const
    {
      return const_cast<path_target*> (this)->derive_path (); // MT-aware.
    }

    const string&
    derive_extension () const
    {
      return const_cast<path_target*> (this)->derive_extension (); // MT-aware.
    }

  public:
    static const target_type static_type;

  private:
    // Note that the state is also used to synchronize the path value so
    // we use the release-acquire ordering.
    //
    // 0 - absent
    // 1 - being set
    // 2 - present
    //
    mutable atomic<uint8_t> path_state_ {0};
    mutable path_type path_;
  };

  // File target.
  //
  class LIBBUILD2_SYMEXPORT file: public path_target
  {
  public:
    file (context& c, dir_path d, dir_path o, string n)
      : path_target (c, move (d), move (o), move (n))
    {
      dynamic_type = &static_type;
    }

  public:
    static const target_type static_type;
  };

  // Mtime-based group target.
  //
  // Used to support explicit groups in buildfiles: can be derived from,
  // populated with static members using the group{foo}<...> syntax, and
  // matched with an ad hoc recipe/rule, including dynamic member extraction.
  // Note that it is not see-through but a derived group can be made see-
  // through via the [see_through] attribute.
  //
  // Note also that you shouldn't use it as a base for a custom group defined
  // in C++, instead deriving from mtime_target directly and using a custom
  // members layout more appropriate for the group's semantics. To put it
  // another way, a group-based target should only be matched by an ad hoc
  // recipe/rule (see match_rule_impl() in algorithms.cxx for details).
  //
  class LIBBUILD2_SYMEXPORT group: public mtime_target
  {
  public:
    vector<reference_wrapper<const target>> static_members;

    // Note: we expect no NULL entries in members.
    //
    vector<const target*> members; // Layout compatible with group_view.
    action members_action; // Action on which members were resolved.
    size_t members_on = 0; // Operation number on which members were resolved.
    size_t members_static; // Number of static ones in members (always first).

    void
    reset_members (action a)
    {
      members.clear ();
      members_action = a;
      members_on = ctx.current_on;
      members_static = 0;
    }

    virtual group_view
    group_members (action) const override;

    group (context& c, dir_path d, dir_path o, string n)
      : mtime_target (c, move (d), move (o), move (n))
    {
      dynamic_type = &static_type;
    }

  public:
    static const target_type static_type;
  };

  // Alias target. It represents a list of targets (its prerequisites)
  // as a single "name".
  //
  class LIBBUILD2_SYMEXPORT alias: public target
  {
  public:
    alias (context& c, dir_path d, dir_path o, string n)
      : target (c, move (d), move (o), move (n))
    {
      dynamic_type = &static_type;
    }

  public:
    static const target_type static_type;
  };

  // Directory target. Note that this is not a filesystem directory
  // but rather an alias target with the directory name. For actual
  // filesystem directory (creation), see fsdir.
  //
  class LIBBUILD2_SYMEXPORT dir: public alias
  {
  public:
    dir (context& c, dir_path d, dir_path o, string n)
      : alias (c, move (d), move (o), move (n))
    {
      dynamic_type = &static_type;
    }

  public:
    static const target_type static_type;

  public:
    template <typename K>
    static const target*
    search_implied (const scope&, const K&, tracer&);

    // Return true if the implied buildfile is plausible for the specified
    // subdirectory of a project with the specified root scope. That is, there
    // is a buildfile in at least one of its subdirectories. Note that the
    // directory must exist.
    //
    static bool
    check_implied (const scope& root, const dir_path&);

  private:
    static prerequisites_type
    collect_implied (const scope&);
  };

  // While a filesystem directory is mtime-based, the semantics is not very
  // useful in our case. In particular, if another target depends on fsdir{},
  // then all that's desired is the creation of the directory if it doesn't
  // already exist. In particular, we don't want to update the target just
  // because some unrelated entry was created in that directory.
  //
  class LIBBUILD2_SYMEXPORT fsdir: public target
  {
  public:
    fsdir (context& c, dir_path d, dir_path o, string n)
      : target (c, move (d), move (o), move (n))
    {
      dynamic_type = &static_type;
    }

  public:
    static const target_type static_type;
  };

  // Executable file (not necessarily binary, though we do fallback to the
  // host machine executable extension in certain cases; see the default
  // extension derivation for details).
  //
  class LIBBUILD2_SYMEXPORT exe: public file
  {
  public:
    exe (context& c, dir_path d, dir_path o, string n)
      : file (c, move (d), move (o), move (n))
    {
      dynamic_type = &static_type;
    }

    using process_path_type = build2::process_path;

    // Return the process path of this executable target. Normally it will be
    // the absolute path returned by path() but can also be something custom
    // if, for example, the target was found via a PATH search (see import for
    // details). The idea is to use this path if we need to execute the target
    // in which case, for the above example, we will see a short (recall) path
    // instead of the absolute one in diagnostics.
    //
    process_path_type
    process_path () const;

    // Note that setting the custom process path is not MT-safe and must be
    // done while holding the insertion lock.
    //
    void
    process_path (process_path_type);

    // Lookup metadata variable (see {import,export}.metadata).
    //
    template <typename T>
    const T*
    lookup_metadata (const char* var) const;

  public:
    static const target_type static_type;

  private:
    process_path_type process_path_;
  };

  class LIBBUILD2_SYMEXPORT buildfile: public file
  {
  public:
    buildfile (context& c, dir_path d, dir_path o, string n)
      : file (c, move (d), move (o), move (n))
    {
      dynamic_type = &static_type;
    }

  public:
    static const target_type static_type;
  };

  // This target type is primarily used for files mentioned in the `recipe`
  // directive.
  //
  class LIBBUILD2_SYMEXPORT buildscript: public file
  {
  public:
    buildscript (context& c, dir_path d, dir_path o, string n)
      : file (c, move (d), move (o), move (n))
    {
      dynamic_type = &static_type;
    }

  public:
    static const target_type static_type;
  };

  // Common documentation file target.
  //
  class LIBBUILD2_SYMEXPORT doc: public file
  {
  public:
    doc (context& c, dir_path d, dir_path o, string n)
      : file (c, move (d), move (o), move (n))
    {
      dynamic_type = &static_type;
    }

  public:
    static const target_type static_type;
  };

  // Legal files (LICENSE, AUTHORS, COPYRIGHT, etc).
  //
  class LIBBUILD2_SYMEXPORT legal: public doc
  {
  public:
    legal (context& c, dir_path d, dir_path o, string n)
      : doc (c, move (d), move (o), move (n))
    {
      dynamic_type = &static_type;
    }

  public:
    static const target_type static_type;
  };

  // The problem with man pages is this: different platforms have
  // different sets of sections. What seems to be the "sane" set
  // is 1-9 (Linux and BSDs). SysV (e.g., Solaris) instead maps
  // 8 to 1M (system administration). The section determines two
  // things: the directory where the page is installed (e.g.,
  // /usr/share/man/man1) as well as the extension of the file
  // (e.g., test.1). Note also that there could be sub-sections,
  // e.g., 1p (for POSIX). Such a page would still go into man1
  // but will have the .1p extension (at least that's what happens
  // on Linux). The challenge is to somehow handle this in a
  // portable manner. So here is the plan:
  //
  // First of all, we have the man{} target type which can be used
  // for a custom man page. That is, you can have any extension and
  // install it anywhere you please:
  //
  // man{foo.X}: install = man/manX
  //
  // Then we have man1..9{} target types which model the "sane"
  // section set and that would be automatically installed into
  // correct locations on other platforms. In other words, the
  // idea is that you should be able to have the foo.8 file,
  // write man8{foo} and have it installed as man1m/foo.1m on
  // some SysV host.
  //
  // Re-mapping the installation directory is easy: to help with
  // that we have assigned install.man1..9 directory names. The
  // messy part is to change the extension. It seems the only
  // way to do that would be to have special logic for man pages
  // in the generic install rule. @@ This is still a TODO.
  //
  // Note that handling subsections with man1..9{} is easy, we
  // simply specify the extension explicitly, e.g., man1{foo.1p}.
  //
  class LIBBUILD2_SYMEXPORT man: public doc
  {
  public:
    man (context& c, dir_path d, dir_path o, string n)
      : doc (c, move (d), move (o), move (n))
    {
      dynamic_type = &static_type;
    }

  public:
    static const target_type static_type;
  };

  class LIBBUILD2_SYMEXPORT man1: public man
  {
  public:
    man1 (context& c, dir_path d, dir_path o, string n)
      : man (c, move (d), move (o), move (n))
    {
      dynamic_type = &static_type;
    }

  public:
    static const target_type static_type;
  };

  // We derive manifest from doc rather than file so that it get automatically
  // installed into the same place where the rest of the documentation goes.
  // If you think about it, it's kind of a documentation, similar to (but
  // better than) the version file that many projects come with.
  //
  class LIBBUILD2_SYMEXPORT manifest: public doc
  {
  public:
    manifest (context& c, dir_path d, dir_path o, string n)
      : doc (c, move (d), move (o), move (n))
    {
      dynamic_type = &static_type;
    }

  public:
    static const target_type static_type;
  };

  // Common implementation of the target factory, extension, and search
  // functions.
  //
  template <typename T>
  target*
  target_factory (context& c,
                  const target_type&, dir_path d, dir_path o, string n)
  {
    return new T (c, move (d), move (o), move (n));
  }

  // Return fixed target extension unless one was specified.
  //
  template <const char* ext>
  const char*
  target_extension_fix (const target_key&, const scope*);

  template <const char* ext>
  bool
  target_pattern_fix (const target_type&, const scope&,
                      string&, optional<string>&, const location&,
                      bool);

  LIBBUILD2_SYMEXPORT const char*
  target_extension_none (const target_key&, const scope*);

  LIBBUILD2_SYMEXPORT const char*
  target_extension_must (const target_key&, const scope*);

  // Get the extension from the `extension` variable or use the default if
  // none set. If the default is NULL, then return NULL.
  //
  template <const char* def>
  optional<string>
  target_extension_var (const target_key&, const scope&, const char*, bool);

  template <const char* def>
  bool
  target_pattern_var (const target_type&, const scope&,
                      string&, optional<string>&, const location&,
                      bool);

  // Target print functions (target_type::print).
  //

  // Target type uses the extension but it is fixed and there is no use
  // printing it (e.g., man1{}).
  //
  LIBBUILD2_SYMEXPORT bool
  target_print_0_ext_verb (ostream&, const target_key&, bool);

  // Target type uses the extension and there is normally no default so it
  // should be printed (e.g., file{}).
  //
  LIBBUILD2_SYMEXPORT bool
  target_print_1_ext_verb (ostream&, const target_key&, bool);

  // Target search functions (target_type::search).
  //

  // The default behavior, that is, look for an existing target in the
  // prerequisite's directory scope.
  //
  // Note that this implementation assumes a target can only be found in the
  // out tree (targets that can be in the src tree would normally use
  // file_search() below).
  //
  LIBBUILD2_SYMEXPORT const target*
  target_search (context&, const target*, const prerequisite_key&);

  // First look for an existing target both in out and src. If not found, then
  // look for an existing file in src.
  //
  LIBBUILD2_SYMEXPORT const target*
  file_search (context&, const target*, const prerequisite_key&);
}

#include <libbuild2/target.ixx>
#include <libbuild2/target.txx>

#endif // LIBBUILD2_TARGET_HXX