This file is indexed.

/usr/share/boost-build/build/targets.jam is in libboost1.55-tools-dev 1.55.0+dfsg-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
# Copyright Vladimir Prus 2002.
# Copyright Rene Rivera 2006.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)

# Supports 'abstract' targets, which are targets explicitly defined in a
# Jamfile.
#
# Abstract targets are represented by classes derived from 'abstract-target'
# class. The first abstract target is 'project-target', which is created for
# each Jamfile, and can be obtained by the 'target' rule in the Jamfile's module
# (see project.jam).
#
# Project targets keep a list of 'main-target' instances. A main target is what
# the user explicitly defines in a Jamfile. It is possible to have several
# definitions for a main target, for example to have different lists of sources
# for different platforms. So, main targets keep a list of alternatives.
#
# Each alternative is an instance of 'abstract-target'. When a main target
# subvariant is defined by some rule, that rule will decide what class to use,
# create an instance of that class and add it to the list of alternatives for
# the main target.
#
# Rules supplied by the build system will use only targets derived from the
# 'basic-target' class, which will provide some default behaviour. There will be
# different classes derived from it such as 'make-target', created by the 'make'
# rule, and 'typed-target', created by rules such as 'exe' and 'lib'.
#
#                  +--------------------------+
#                  | abstract-target          |
#                  +==========================+
#                  | name                     |
#                  | project                  |
#                  |                          |
#                  | generate(properties) = 0 |
#                  +-------------+------------+
#                                |
#                                ^
#                               / \
#                              +-+-+
#                                |
#                                |
#             +------------------+-----+-------------------------------+
#             |                        |                               |
#             |                        |                               |
# +-----------+----------+      +------+------+                +-------+------+
# | project-target       |      | main-target |                | basic-target |
# +======================+ 1  * +=============+  alternatives  +==============+
# | generate(properties) |o-----+ generate    |<>------------->| generate     |
# | main-target          |      +-------------+                | construct = 0|
# +----------------------+                                     +-------+------+
#                                                                      |
#                                                                      ^
#                                                                     / \
#                                                                    +-+-+
#                                                                      |
#                                                                      |
#          ...--+-----------------+-----------------+------------------+
#               |                 |                 |                  |
#               |                 |                 |                  |
#        ... ---+-----+   +-------+------+   +------+------+   +-------+------+
#                     |   | typed-target |   | make-target |   | stage-target |
#                     .   +==============+   +=============+   +==============+
#                     .   | construct    |   | construct   |   | construct    |
#                         +--------------+   +-------------+   +--------------+

import assert ;
import build-request ;
import "class" : new ;
import feature ;
import indirect ;
import path ;
import property ;
import property-set ;
import sequence ;
import set ;
import toolset ;


# Base class for all abstract targets.
#
class abstract-target
{
    import assert ;
    import "class" ;
    import errors ;
    import project ;

    rule __init__ ( name  # Name of the target in Jamfile.
        : project-target  # The project target to which this one belongs.
    )
    {
        # Note: it might seem that we don't need either name or project at all.
        # However, there are places where we really need it. One example is
        # error messages which should name problematic targets. Another is
        # setting correct paths for sources and generated files.

        self.name = $(name) ;
        self.project = $(project-target) ;
        self.location = [ errors.nearest-user-location ] ;
    }

    # Returns the name of this target.
    rule name ( )
    {
        return $(self.name) ;
    }

    # Returns the project for this target.
    rule project ( )
    {
        return $(self.project) ;
    }

    # Return the location where the target was declared.
    rule location ( )
    {
        return $(self.location) ;
    }

    # Returns a user-readable name for this target.
    rule full-name ( )
    {
        local location = [ $(self.project).get location ] ;
        return $(location)/$(self.name) ;
    }

    # Generates virtual targets for this abstract target using the specified
    # properties, unless a different value of some feature is required by the
    # target.
    # On success, returns:
    # - a property-set with the usage requirements to be applied to dependants
    # - a list of produced virtual targets, which may be empty.
    # If 'property-set' is empty, performs the default build of this target, in
    # a way specific to the derived class.
    #
    rule generate ( property-set )
    {
        errors.error "method should be defined in derived classes" ;
    }

    rule rename ( new-name )
    {
        self.name = $(new-name) ;
    }
}


if --debug-building in [ modules.peek : ARGV ]
{
    modules.poke : .debug-building : true ;
}


rule indent ( )
{
    return $(.indent:J="") ;
}


rule increase-indent ( )
{
    .indent += "    " ;
}


rule decrease-indent ( )
{
    .indent = $(.indent[2-]) ;
}


#  Project target class (derived from 'abstract-target').
#
#  This class has the following responsibilities:
#  - Maintaining a list of main targets in this project and building them.
#
#  Main targets are constructed in two stages:
#  - When Jamfile is read, a number of calls to 'add-alternative' is made. At
#    that time, alternatives can also be renamed to account for inline targets.
#  - The first time 'main-target' or 'has-main-target' rule is called, all
#    alternatives are enumerated and main targets are created.
#
class project-target : abstract-target
{
    import project ;
    import targets ;
    import path ;
    import print ;
    import property-set ;
    import set ;
    import sequence ;
    import "class" : new ;

    rule __init__ ( name : project-module parent-project ?
        : requirements * : default-build * )
    {
        abstract-target.__init__ $(name) : $(__name__) ;

        self.project-module = $(project-module) ;
        self.location = [ project.attribute $(project-module) location ] ;
        self.requirements = $(requirements) ;
        self.default-build = $(default-build) ;

        if $(parent-project)
        {
            inherit $(parent-project) ;
        }
    }

    # This is needed only by the 'make' rule. Need to find a way to make 'make'
    # work without this method.
    #
    rule project-module ( )
    {
        return $(self.project-module) ;
    }

    rule get ( attribute )
    {
        return [ project.attribute $(self.project-module) $(attribute) ] ;
    }

    rule build-dir ( )
    {
        if ! $(self.build-dir)
        {
            self.build-dir = [ get build-dir ] ;
            if ! $(self.build-dir)
            {
                local location = [ $(self.project).get location ] ;
                if $(location)
                {
                    self.build-dir = [ path.join $(location) bin ] ;
                }
                else
                {
                    local id = [ get id  ] ;
                    if $(id)
                    {
                        local rid = [ MATCH ^/(.*) : $(id) ] ;
                        self.build-dir = [ path.join [ project.standalone-build-dir ] $(rid) ] ;
                    }
                    else
                    {
                        errors.error "Could not create build-dir for standalone project $(self.project-module:E=)."
                                   : "Missing project id" ;
                    }
                }
            }
        }
        return $(self.build-dir) ;
    }

    # Generates all possible targets contained in this project.
    #
    rule generate ( property-set * )
    {
        if [ modules.peek : .debug-building ]
        {
            ECHO [ targets.indent ] "building project" [ name ]
                " ('$(__name__)') with" [ $(property-set).raw ] ;
            targets.increase-indent ;
        }

        local usage-requirements = [ property-set.empty ] ;
        local targets ;

        for local t in [ targets-to-build ]
        {
            local g = [ $(t).generate $(property-set) ] ;
            usage-requirements = [ $(usage-requirements).add $(g[1]) ] ;
            targets += $(g[2-]) ;
        }
        targets.decrease-indent ;
        return $(usage-requirements) [ sequence.unique $(targets) ] ;
    }

    # Computes and returns a list of abstract-target instances which must be
    # built when this project is built.
    #
    rule targets-to-build ( )
    {
        local result ;

        if ! $(self.built-main-targets)
        {
            build-main-targets ;
        }

        # Collect all main targets here, except for "explicit" ones.
        for local t in $(self.main-targets)
        {
            if ! [ $(t).name ] in $(self.explicit-targets)
            {
                result += $(t) ;
            }
        }

        # Collect all projects referenced via "projects-to-build" attribute.
        local self-location = [ get location ] ;
        for local pn in [ get projects-to-build ]
        {
            result += [ find $(pn)/ ] ;
        }

        return $(result) ;
    }

    # Add 'target' to the list of targets in this project that should be build
    # only by explicit request
    #
    rule mark-target-as-explicit ( target-name * )
    {
        # Record the name of the target, not instance, since this rule is called
        # before main target instances are created.
        self.explicit-targets += $(target-name) ;
    }

    rule mark-target-as-always ( target-name * )
    {
        # Record the name of the target, not instance, since this rule is called
        # before main target instances are created.
        self.always-targets += $(target-name) ;
    }

    # Add new target alternative
    #
    rule add-alternative ( target-instance )
    {
        if $(self.built-main-targets)
        {
            import errors : error : errors.error ;
            errors.error add-alternative called when main targets are already
                created. : in project [ full-name ] ;
        }
        self.alternatives += $(target-instance) ;
    }

    # Returns a 'main-target' class instance corresponding to 'name'.
    #
    rule main-target ( name )
    {
        if ! $(self.built-main-targets)
        {
            build-main-targets ;
        }
        return $(self.main-target.$(name)) ;
    }

    # Returns whether a main target with the specified name exists.
    #
    rule has-main-target ( name )
    {
        if ! $(self.built-main-targets)
        {
            build-main-targets ;
        }

        if $(self.main-target.$(name))
        {
            return true ;
        }
    }

    # Worker function for the find rule not implementing any caching and simply
    # returning nothing in case the target can not be found.
    #
    rule find-really ( id )
    {
        local result ;
        local current-location = [ get location ] ;

        local split = [ MATCH ^(.*)//(.*)$ : $(id) ] ;
        local project-part = $(split[1]) ;
        local target-part = $(split[2]) ;

        local extra-error-message ;
        if $(project-part)
        {
            # There is an explicitly specified project part in id. Looks up the
            # project and passes the request to it.
            local pm = [ project.find $(project-part) : $(current-location) ] ;
            if $(pm)
            {
                project-target = [ project.target $(pm) ] ;
                result = [ $(project-target).find $(target-part) : no-error ] ;
            }
            else
            {
                extra-error-message = could not resolve project reference
                    '$(project-part)' ;
                if ! [ path.is-rooted $(project-part) ]
                {
                    local rooted = [ path.root $(project-part) / ] ;
                    if [ project.is-registered-id $(rooted) ]
                    {
                        extra-error-message += - possibly missing a leading
                            slash ('/') character. ;
                    }
                }
            }
        }
        else
        {
            # Interpret target-name as name of main target. Need to do this
            # before checking for file. Consider the following scenario with a
            # toolset not modifying its executable's names, e.g. gcc on
            # Unix-like platforms:
            #
            #  exe test : test.cpp ;
            #  install s : test : <location>. ;
            #
            # After the first build we would have a target named 'test' in the
            # Jamfile and a file named 'test' on the disk. We need the target to
            # override the file.
            result = [ main-target $(id) ] ;

            # Interpret id as an existing file reference.
            if ! $(result)
            {
                result = [ new file-reference [ path.make $(id) ] :
                    $(self.project) ] ;
                if ! [ $(result).exists ]
                {
                    result = ;
                }
            }

            # Interpret id as project-id.
            if ! $(result)
            {
                local project-module = [ project.find $(id) :
                    $(current-location) ] ;
                if $(project-module)
                {
                    result = [ project.target $(project-module) ] ;
                }
            }
        }

        return $(result:E="") $(extra-error-message) ;
    }

    # Find and return the target with the specified id, treated relative to
    # self. Id may specify either a target or a file name with the target taking
    # priority. May report an error or return nothing if the target is not found
    # depending on the 'no-error' parameter.
    #
    rule find ( id : no-error ? )
    {
        local v = $(.id.$(id)) ;
        local extra-error-message ;
        if ! $(v)
        {
            local r = [ find-really $(id) ] ;
            v = $(r[1]) ;
            extra-error-message = $(r[2-]) ;
            if ! $(v)
            {
                v = none ;
            }
            .id.$(id) = $(v) ;
        }

        if $(v) != none
        {
            return $(v) ;
        }
        else if ! $(no-error)
        {
            local current-location = [ get location ] ;
            import errors : user-error : errors.user-error ;
            errors.user-error Unable to find file or target named
                : "   " '$(id)'
                : referred to from project at
                : "   " '$(current-location)'
                : $(extra-error-message) ;
        }
    }

    rule build-main-targets ( )
    {
        self.built-main-targets = true ;
        for local a in $(self.alternatives)
        {
            local name = [ $(a).name ] ;
            local target = $(self.main-target.$(name)) ;
            if ! $(target)
            {
                local t = [ new main-target $(name) : $(self.project) ] ;
                self.main-target.$(name) = $(t) ;
                self.main-targets += $(t) ;
                target = $(self.main-target.$(name)) ;
            }

            if $(name) in $(self.always-targets)
            {
                $(a).always ;
            }

            $(target).add-alternative $(a) ;
        }
    }

    # Accessor, add a constant.
    #
    rule add-constant (
        name       # Variable name of the constant.
        : value +  # Value of the constant.
        : type ?   # Optional type of value.
        )
    {
        switch $(type)
        {
            case path :
              local r ;
              for local v in $(value)
              {
                local l = $(self.location) ;
                if ! $(l)
                {
                    # Project corresponding to config files do not have
                    # 'location' attribute, but do have source location. It
                    # might be more reasonable to make every project have a
                    # location and use some other approach to prevent buildable
                    # targets in config files, but that has been left for later.
                    l = [ get source-location ] ;
                }
                v = [ path.root [ path.make $(v) ] $(l) ] ;
                # Now make the value absolute path.
                v = [ path.root $(v) [ path.pwd ] ] ;
                # Constants should be in platform-native form.
                v = [ path.native $(v) ] ;
                r += $(v) ;
              }
              value = $(r) ;
        }
        if ! $(name) in $(self.constants)
        {
            self.constants += $(name) ;
        }
        self.constant.$(name) = $(value) ;
        # Inject the constant in the scope of the Jamroot module.
        modules.poke $(self.project-module) : $(name) : $(value) ;
    }

    rule inherit ( parent )
    {
        for local c in [ modules.peek $(parent) : self.constants ]
        {
            # No need to pass the type. Path constants were converted to
            # absolute paths already by parent.
            add-constant $(c) : [ modules.peek $(parent) : self.constant.$(c) ]
                ;
        }

        # Import rules from parent.
        local this-module = [ project-module ] ;
        local parent-module = [ $(parent).project-module ] ;
        # Do not import rules coming from 'project-rules' as they must be
        # imported localized.
        local user-rules = [ set.difference
            [ RULENAMES $(parent-module) ] :
            [ RULENAMES project-rules ] ] ;
        IMPORT $(parent-module) : $(user-rules) : $(this-module) : $(user-rules)
            ;
        EXPORT $(this-module) : $(user-rules) ;
    }
}


# Helper rules to detect cycles in main target references.
#
local rule start-building ( main-target-instance )
{
    if $(main-target-instance) in $(.targets-being-built)
    {
        local names ;
        for local t in $(.targets-being-built) $(main-target-instance)
        {
            names += [ $(t).full-name ] ;
        }

        import errors ;
        errors.error "Recursion in main target references"
          : "the following target are being built currently:"
          : $(names) ;
    }
    .targets-being-built += $(main-target-instance) ;
}


local rule end-building ( main-target-instance )
{
    .targets-being-built = $(.targets-being-built[1--2]) ;
}


# A named top-level target in Jamfile.
#
class main-target : abstract-target
{
    import assert ;
    import feature ;
    import print ;
    import property-set ;
    import sequence ;
    import targets : start-building end-building ;

    rule __init__ ( name : project )
    {
        abstract-target.__init__ $(name) : $(project) ;
    }

    # Add a new alternative for this target
    rule add-alternative ( target )
    {
        local d = [ $(target).default-build ] ;
        if $(self.alternatives) && ( $(self.default-build) != $(d) )
        {
            import errors : error : errors.error ;
            errors.error "default build must be identical in all alternatives"
                : "main target is" [ full-name ]
                : "with" [ $(d).raw ]
                : "differing from previous default build"
                    [ $(self.default-build).raw ] ;
        }
        else
        {
            self.default-build = $(d) ;
        }
        self.alternatives += $(target) ;
    }

    # Returns the best viable alternative for this property-set. See the
    # documentation for selection rules.
    #
    local rule select-alternatives ( property-set debug ? )
    {
        # When selecting alternatives we have to consider defaults, for example:
        #    lib l : l.cpp : <variant>debug ;
        #    lib l : l_opt.cpp : <variant>release ;
        # will not work unless we add default value <variant>debug.
        property-set = [ $(p).add-defaults ] ;

        # The algorithm: we keep the current best viable alternative. When we
        # encounter a new best viable alternative, we compare it with the
        # current one.

        local best ;
        local best-properties ;

        if $(self.alternatives[2-])
        {
            local bad ;
            local worklist = $(self.alternatives) ;
            while $(worklist) && ! $(bad)
            {
                local v = $(worklist[1]) ;
                local properties = [ $(v).match $(property-set) $(debug) ] ;

                if $(properties) != no-match
                {
                    if ! $(best)
                    {
                        best = $(v) ;
                        best-properties = $(properties) ;
                    }
                    else
                    {
                        if $(properties) = $(best-properties)
                        {
                            bad = true ;
                        }
                        else if $(properties) in $(best-properties)
                        {
                            # Do nothing, this alternative is worse
                        }
                        else if $(best-properties) in $(properties)
                        {
                            best = $(v) ;
                            best-properties = $(properties) ;
                        }
                        else
                        {
                            bad = true ;
                        }
                    }
                }
                worklist = $(worklist[2-]) ;
            }
            if ! $(bad)
            {
                return $(best) ;
            }
        }
        else
        {
            return $(self.alternatives) ;
        }
    }

    rule apply-default-build ( property-set )
    {
        return [ targets.apply-default-build $(property-set) :
            $(self.default-build) ] ;
    }

    # Select an alternative for this main target, by finding all alternatives
    # whose requirements are satisfied by 'properties' and picking the one with
    # the longest requirements set. Returns the result of calling 'generate' on
    # that alternative.
    #
    rule generate ( property-set )
    {
        start-building $(__name__) ;

        # We want composite properties in the build request to act as if all the
        # properties they expand to have been explicitly specified.
        property-set = [ $(property-set).expand ] ;

        local all-property-sets = [ apply-default-build $(property-set) ] ;
        local usage-requirements = [ property-set.empty ] ;
        local result ;
        for local p in $(all-property-sets)
        {
            local r = [ generate-really $(p) ] ;
            if $(r)
            {
                usage-requirements = [ $(usage-requirements).add $(r[1]) ] ;
                result += $(r[2-]) ;
            }
        }
        end-building $(__name__) ;
        return $(usage-requirements) [ sequence.unique $(result) ] ;
    }

    # Generates the main target with the given property set and returns a list
    # which first element is property-set object containing usage-requirements
    # of generated target and with generated virtual target in other elements.
    # It is possible that no targets are generated.
    #
    local rule generate-really ( property-set )
    {
        local best-alternatives = [ select-alternatives $(property-set) ] ;
        if ! $(best-alternatives)
        {
            ECHO "error: No best alternative for" [ full-name ] ;
            select-alternatives $(property-set) debug ;
            return [ property-set.empty ] ;
        }
        else
        {
            # Now return virtual targets for the only alternative.
            return [ $(best-alternatives).generate $(property-set) ] ;
        }
    }

    rule rename ( new-name )
    {
        abstract-target.rename $(new-name) ;
        for local a in $(self.alternatives)
        {
            $(a).rename $(new-name) ;
        }
    }
}


# Abstract target referring to a source file. This is an artificial entity
# allowing sources to a target to be represented using a list of abstract target
# instances.
#
class file-reference : abstract-target
{
    import virtual-target ;
    import property-set ;
    import path ;

    rule __init__ ( file : project )
    {
        abstract-target.__init__ $(file) : $(project) ;
    }

    rule generate ( properties )
    {
        return [ property-set.empty ] [ virtual-target.from-file $(self.name) :
            [ location ] : $(self.project) ] ;
    }

    # Returns true if the referred file really exists.
    rule exists ( )
    {
        location ;
        return $(self.file-path) ;
    }

    # Returns the location of target. Needed by 'testing.jam'.
    rule location ( )
    {
        if ! $(self.file-location)
        {
            local source-location = [ $(self.project).get source-location ] ;
            for local src-dir in $(source-location)
            {
                if ! $(self.file-location)
                {
                    local location = [ path.root $(self.name) $(src-dir) ] ;
                    if [ CHECK_IF_FILE [ path.native $(location) ] ]
                    {
                         self.file-location = $(src-dir) ;
                         self.file-path = $(location) ;
                    }
                }
            }
        }
        return $(self.file-location) ;
    }
}


# Given a target-reference, made in context of 'project', returns the
# abstract-target instance that is referred to, as well as properties explicitly
# specified for this reference.
#
rule resolve-reference ( target-reference : project )
{
    # Separate target name from properties override.
    local split = [ MATCH "^([^<]*)(/(<.*))?$" : $(target-reference) ] ;
    local id = $(split[1]) ;
    local sproperties = ;
    if $(split[3])
    {
        sproperties = [ property.make [ feature.split $(split[3]) ] ] ;
        sproperties = [ feature.expand-composites $(sproperties) ] ;
    }

    # Find the target.
    local target = [ $(project).find $(id) ] ;

    return $(target) [ property-set.create $(sproperties) ] ;
}


# Attempts to generate the target given by target reference, which can refer
# both to a main target or to a file. Returns a list consisting of
# - usage requirements
# - generated virtual targets, if any
#
rule generate-from-reference (
    target-reference  # Target reference.
    : project         # Project where the reference is made.
    : property-set    # Properties of the main target that makes the reference.
)
{
    local r = [ resolve-reference $(target-reference) : $(project) ] ;
    local target = $(r[1]) ;
    local sproperties = $(r[2]) ;

    # Take properties which should be propagated and refine them with
    # source-specific requirements.
    local propagated = [ $(property-set).propagated ] ;
    local rproperties = [ $(propagated).refine $(sproperties) ] ;
    if $(rproperties[1]) = "@error"
    {
        import errors ;
        errors.error
            "When building" [ full-name ] " with properties " $(properties) :
            "Invalid properties specified for " $(source) ":"
            $(rproperties[2-]) ;
    }
    return [ $(target).generate $(rproperties) ] ;
}


rule apply-default-build ( property-set : default-build )
{
    # 1. First, see what properties from default-build are already present in
    # property-set.

    local raw = [ $(property-set).raw ] ;
    local specified-features = $(raw:G) ;

    local defaults-to-apply ;
    for local d in [ $(default-build).raw ]
    {
        if ! $(d:G) in $(specified-features)
        {
            defaults-to-apply += $(d) ;
        }
    }

    # 2. If there are any defaults to be applied, form a new build request. Pass
    # it through to 'expand-no-defaults' since default-build might contain
    # "release debug" resulting in two property-sets.
    local result ;
    if $(defaults-to-apply)
    {
        # We have to compress subproperties here to prevent property lists like:
        #    <toolset>msvc <toolset-msvc:version>7.1 <threading>multi
        #
        # from being expanded into:
        #    <toolset-msvc:version>7.1/<threading>multi
        #    <toolset>msvc/<toolset-msvc:version>7.1/<threading>multi
        #
        # due to a cross-product property combination. That may be an indication
        # that build-request.expand-no-defaults is the wrong rule to use here.
        properties = [ build-request.expand-no-defaults
            [ feature.compress-subproperties $(raw) ] $(defaults-to-apply) ] ;

        if $(properties)
        {
            for local p in $(properties)
            {
                result += [ property-set.create
                    [ feature.expand [ feature.split $(p) ] ] ] ;
            }
        }
        else
        {
            result = [ property-set.empty ] ;
        }
    }
    else
    {
        result = $(property-set) ;
    }
    return $(result) ;
}


# Given a build request and requirements, return properties common to dependency
# build request and target requirements.
#
# TODO: Document exactly what 'common properties' are, whether they should
# include default property values, whether they should contain any conditional
# properties or should those be already processed, etc. See whether there are
# any differences between use cases with empty and non-empty build-request as
# well as with requirements containing and those not containing any non-free
# features.
#
rule common-properties ( build-request requirements )
{
    # For optimization, we add free requirements directly, without using a
    # complex algorithm. This gives the complex algorithm a better chance of
    # caching results.
    local free = [ $(requirements).free ] ;
    local non-free = [ property-set.create [ $(requirements).base ]
        [ $(requirements).incidental ] ] ;

    local key = .rp.$(build-request)-$(non-free) ;
    if ! $($(key))
    {
        $(key) = [ common-properties2 $(build-request) $(non-free) ] ;
    }
    return [ $($(key)).add-raw $(free) ] ;
}


# Given a 'context' -- a set of already present properties, and 'requirements',
# decide which extra properties should be applied to 'context'. For conditional
# requirements, this means evaluating the condition. For indirect conditional
# requirements, this means calling a rule. Ordinary requirements are always
# applied.
#
# Handles the situation where evaluating one conditional requirement affects
# conditions of another conditional requirements, such as:
#     <toolset>gcc:<variant>release <variant>release:<define>RELEASE
#
# If 'what' is 'refined' returns context refined with new requirements. If
# 'what' is 'added' returns just the requirements to be applied.
#
rule evaluate-requirements ( requirements : context : what )
{
    # Apply non-conditional requirements. It is possible that further
    # conditional requirement change a value set by non-conditional
    # requirements. For example:
    #
    #    exe a : a.cpp : <threading>single <toolset>foo:<threading>multi ;
    #
    # I am not sure if this should be an error, or not, especially given that
    #
    #    <threading>single
    #
    # might come from project's requirements.

    local unconditional = [ feature.expand [ $(requirements).non-conditional ] ]
        ;

    local raw = [ $(context).raw ] ;
    raw = [ property.refine $(raw) : $(unconditional) ] ;

    # We have collected properties that surely must be present in common
    # properties. We now try to figure out what other properties should be added
    # in order to satisfy rules (4)-(6) from the docs.

    local conditionals = [ $(requirements).conditional ] ;
    # The 'count' variable has one element for each conditional feature and for
    # each occurrence of '<indirect-conditional>' feature. It is used as a loop
    # counter: for each iteration of the loop before we remove one element and
    # the property set should stabilize before we are done. It is assumed that
    # #conditionals iterations should be enough for properties to propagate
    # along conditions in any direction.
    local count = $(conditionals) [ $(requirements).get <conditional> ]
        and-once-more ;

    local added-requirements ;

    local current = $(raw) ;

    # It is assumed that ordinary conditional requirements can not add
    # <conditional> properties (a.k.a. indirect conditional properties), and
    # that rules referred to by <conditional> properties can not add new
    # <conditional> properties. So the list of indirect conditionals does not
    # change.
    local indirect = [ $(requirements).get <conditional> ] ;
    indirect = [ MATCH ^@(.*) : $(indirect) ] ;

    local ok ;
    while $(count)
    {
        # Evaluate conditionals in context of current properties.
        local e = [ property.evaluate-conditionals-in-context $(conditionals) :
            $(current) ] ;

        # Evaluate indirect conditionals.
        for local i in $(indirect)
        {
            local t = [ current ] ;
            local p = [ $(t).project ] ;
            local new = [ indirect.call $(i) $(current) ] ;
            e += [ property.translate-paths $(new) : [ $(p).location ] ] ;
        }

        if $(e) = $(added-requirements)
        {
            # If we got the same result, we have found the final properties.
            count = ;
            ok = true ;
        }
        else
        {
            # Oops, conditional evaluation results have changed. Also 'current'
            # contains leftovers from a previous evaluation. Recompute 'current'
            # using initial properties and conditional requirements.
            added-requirements = $(e) ;
            current = [ property.refine $(raw) : [ feature.expand $(e) ] ] ;
        }
        count = $(count[2-]) ;
    }
    if ! $(ok)
    {
        import errors ;
        errors.error Can not evaluate conditional properties $(conditionals) ;
    }

    if $(what) = added
    {
        return [ property-set.create $(unconditional) $(added-requirements) ] ;
    }
    else if $(what) = refined
    {
        return [ property-set.create $(current) ] ;
    }
    else
    {
        import errors ;
        errors.error "Invalid value of the 'what' parameter." ;
    }
}


rule common-properties2 ( build-request requirements )
{
    # This guarantees that default properties are present in the result, unless
    # they are overriden by some requirement. FIXME: There is a possibility that
    # we have added <foo>bar, which is composite and expands to <foo2>bar2, but
    # default value of <foo2> is not bar2, in which case it is not clear what to
    # do.
    #
    build-request = [ $(build-request).add-defaults ] ;
    # Features added by 'add-defaults' can be composite and expand to features
    # without default values -- which therefore have not been added yet. It
    # could be clearer/faster to expand only newly added properties but that is
    # not critical.
    build-request = [ $(build-request).expand ] ;

    return [ evaluate-requirements $(requirements) : $(build-request) :
        refined ] ;
}


rule push-target ( target )
{
    .targets = $(target) $(.targets) ;
}

rule pop-target ( )
{
    .targets = $(.targets[2-]) ;
}

# Return the metatarget that is currently being generated.
rule current ( )
{
    return $(.targets[1]) ;
}


# Implements the most standard way of constructing main target alternative from
# sources. Allows sources to be either file or other main target and handles
# generation of those dependency targets.
#
class basic-target : abstract-target
{
    import build-request ;
    import build-system ;
    import "class" : new ;
    import feature ;
    import property ;
    import property-set ;
    import sequence ;
    import set ;
    import targets ;
    import virtual-target ;

    rule __init__ ( name : project : sources * : requirements * :
        default-build * : usage-requirements * )
    {
        abstract-target.__init__ $(name) : $(project) ;

        self.sources = $(sources) ;
        if ! $(requirements)
        {
            requirements = [ property-set.empty ] ;
        }
        self.requirements = $(requirements) ;
        if ! $(default-build)
        {
            default-build = [ property-set.empty ] ;
        }
        self.default-build = $(default-build) ;
        if ! $(usage-requirements)
        {
            usage-requirements = [ property-set.empty ] ;
        }
        self.usage-requirements = $(usage-requirements) ;

        if $(sources:G)
        {
            import errors : user-error : errors.user-error ;
            errors.user-error properties found "in" the 'sources' parameter
                "for" [ full-name ] ;
        }
    }

    rule always ( )
    {
        self.always = 1 ;
    }

    # Returns the list of abstract-targets which are used as sources. The extra
    # properties specified for sources are not represented. The only user for
    # this rule at the moment is the "--dump-tests" feature of the test system.
    #
    rule sources ( )
    {
        if ! $(self.source-targets)
        {
            for local s in $(self.sources)
            {
                self.source-targets += [ targets.resolve-reference $(s) :
                    $(self.project) ] ;
            }
        }
        return $(self.source-targets) ;
    }

    rule requirements ( )
    {
        return $(self.requirements) ;
    }

    rule default-build ( )
    {
        return $(self.default-build) ;
    }

    # Returns the alternative condition for this alternative, if the condition
    # is satisfied by 'property-set'.
    #
    rule match ( property-set debug ? )
    {
        # The condition is composed of all base non-conditional properties. It
        # is not clear if we should expand 'self.requirements' or not. For one
        # thing, it would be nice to be able to put
        #    <toolset>msvc-6.0
        # in requirements. On the other hand, if we have <variant>release as a
        # condition it does not make sense to require <optimization>full to be
        # in the build request just to select this variant.
        local bcondition = [ $(self.requirements).base ] ;
        local ccondition = [ $(self.requirements).conditional ] ;
        local condition = [ set.difference $(bcondition) : $(ccondition) ] ;
        if $(debug)
        {
            ECHO "    next alternative: required properties:"
                $(condition:E=(empty)) ;
        }

        if $(condition) in [ $(property-set).raw ]
        {
            if $(debug)
            {
                ECHO "        matched" ;
            }
            return $(condition) ;
        }
        else
        {
            if $(debug)
            {
                ECHO "        not matched" ;
            }
            return no-match ;
        }
    }

    # Takes a target reference, which might be either target id or a dependency
    # property, and generates that target using 'property-set' as a build
    # request.
    #
    # The results are added to the variable called 'result-var'. Usage
    # requirements are added to the variable called 'usage-requirements-var'.
    #
    rule generate-dependencies ( dependencies * : property-set : result-var
        usage-requirements-var )
    {
        for local dependency in $(dependencies)
        {
            local grist = $(dependency:G) ;
            local id = $(dependency:G=) ;
            local result = [ targets.generate-from-reference $(id) :
                $(self.project) : $(property-set) ] ;

            $(result-var) += $(result[2-]:G=$(grist)) ;
            $(usage-requirements-var) += [ $(result[1]).raw ] ;
        }
    }

    # Determines final build properties, generates sources, and calls
    # 'construct'. This method should not be overridden.
    #
    rule generate ( property-set )
    {
        if [ modules.peek : .debug-building ]
        {
            ECHO ;
            local fn = [ full-name ] ;
            ECHO [ targets.indent ] "Building target '$(fn)'" ;
            targets.increase-indent ;
            ECHO [ targets.indent ] Build request: $(property-set)
                [ $(property-set).raw ] ;
            local cf = [ build-system.command-line-free-features ] ;
            ECHO [ targets.indent ] Command line free features: [ $(cf).raw ] ;
            ECHO [ targets.indent ] Target requirements:
                [ $(self.requirements).raw ] ;
        }
        targets.push-target $(__name__) ;

        # Apply free features from the command line. If user said
        #   define=FOO
        # he most likely wants this define to be set for all compiles.
        # Make it before check for already built.
        property-set = [ $(property-set).refine
            [ build-system.command-line-free-features ] ] ;

        if ! $(self.generated.$(property-set))
        {
            local rproperties = [ targets.common-properties $(property-set)
                $(self.requirements) ] ;

            if [ modules.peek : .debug-building ]
            {
                ECHO ;
                ECHO [ targets.indent ] "Common properties: "
                    [ $(rproperties).raw ] ;
            }

            if ( $(rproperties[1]) != "@error" ) && ( [ $(rproperties).get
                <build> ] != no )
            {
                local source-targets ;
                local properties = [ $(rproperties).non-dependency ] ;
                local usage-requirements ;

                generate-dependencies [ $(rproperties).dependency ] :
                    $(rproperties) : properties usage-requirements ;

                generate-dependencies $(self.sources) : $(rproperties) :
                    source-targets usage-requirements ;

                if [ modules.peek : .debug-building ]
                {
                    ECHO ;
                    ECHO [ targets.indent ] "Usage requirements for"
                        $(self.name)": " $(usage-requirements) ;
                }

                rproperties = [ property-set.create $(properties)
                    $(usage-requirements) ] ;
                usage-requirements = [ property-set.create $(usage-requirements)
                    ] ;

                if [ modules.peek : .debug-building ]
                {
                    ECHO [ targets.indent ] "Build properties: "
                        [ $(rproperties).raw ] ;
                }

                local extra = [ $(rproperties).get <source> ] ;
                source-targets += $(extra:G=) ;
                # We might get duplicate sources, for example if we link to two
                # libraries having the same <library> usage requirement. Use
                # stable sort, since for some targets the order is important,
                # e.g. RUN_PY targets need a python source to come first.
                source-targets = [ sequence.unique $(source-targets) : stable ]
                    ;

                local result = [ construct $(self.name) : $(source-targets) :
                    $(rproperties) ] ;

                if $(result)
                {
                    local gur = $(result[1]) ;
                    result = $(result[2-]) ;

                    if $(self.always)
                    {
                        for local t in $(result)
                        {
                            $(t).always ;
                        }
                    }

                    local s = [ create-subvariant $(result)
                        : [ virtual-target.recent-targets ]
                        : $(property-set) : $(source-targets)
                        : $(rproperties) : $(usage-requirements) ] ;
                    virtual-target.clear-recent-targets ;

                    local ur = [ compute-usage-requirements $(s) ] ;
                    ur = [ $(ur).add $(gur) ] ;
                    $(s).set-usage-requirements $(ur) ;
                    if [ modules.peek : .debug-building ]
                    {
                        ECHO [ targets.indent ] "Usage requirements from"
                            $(self.name)": " [ $(ur).raw ] ;
                    }

                    self.generated.$(property-set) = $(ur) $(result) ;
                }
            }
            else
            {
                if $(rproperties[1]) = "@error"
                {
                    ECHO [ targets.indent ] "Skipping build of:" [ full-name ]
                        "cannot compute common properties" ;
                }
                else if [ $(rproperties).get <build> ] = no
                {
                    # If we just see <build>no, we cannot produce any reasonable
                    # diagnostics. The code that adds this property is expected
                    # to explain why a target is not built, for example using
                    # the configure.log-component-configuration function.
                }
                else
                {
                    ECHO [ targets.indent ] "Skipping build of: " [ full-name ]
                        " unknown reason" ;
                }

                # We are here either because there has been an error computing
                # properties or there is <build>no in properties. In the latter
                # case we do not want any diagnostic. In the former case, we
                # need diagnostics. FIXME

                # If this target fails to build, add <build>no to properties to
                # cause any parent target to fail to build. Except that it
                # - does not work now, since we check for <build>no only in
                #   common properties, but not in properties that came from
                #   dependencies
                # - it is not clear if that is a good idea anyway. The alias
                #   target, for example, should not fail to build if a
                #   dependency fails.
                self.generated.$(property-set) = [ property-set.create <build>no
                    ] ;
            }
        }
        else
        {
            if [ modules.peek : .debug-building ]
            {
                ECHO [ targets.indent ] "Already built" ;
                local ur = $(self.generated.$(property-set)) ;
                ur = $(ur[0]) ;
                targets.increase-indent ;
                ECHO [ targets.indent ] "Usage requirements from"
                    $(self.name)": " [ $(ur).raw ] ;
                targets.decrease-indent ;
            }
        }

        targets.pop-target ;
        targets.decrease-indent ;
        return $(self.generated.$(property-set)) ;
    }

    # Given the set of generated targets, and refined build properties,
    # determines and sets appropriate usage requirements on those targets.
    #
    rule compute-usage-requirements ( subvariant )
    {
        local rproperties = [ $(subvariant).build-properties ] ;
        xusage-requirements = [ targets.evaluate-requirements
            $(self.usage-requirements) : $(rproperties) : added ] ;

        # We generate all dependency properties and add them, as well as their
        # usage requirements, to the result.
        local extra ;
        generate-dependencies [ $(xusage-requirements).dependency ] :
            $(rproperties) : extra extra ;

        local result = [ property-set.create
            [ $(xusage-requirements).non-dependency ] $(extra) ] ;

        # Propagate usage requirements we got from sources, except for the
        # <pch-header> and <pch-file> features.
        #
        # That feature specifies which pch file to use, and should apply only to
        # direct dependents. Consider:
        #
        #   pch pch1 : ...
        #   lib lib1 : ..... pch1 ;
        #   pch pch2 :
        #   lib lib2 : pch2 lib1 ;
        #
        # Here, lib2 should not get <pch-header> property from pch1.
        #
        # Essentially, when those two features are in usage requirements, they
        # are propagated only to direct dependents. We might need a more general
        # mechanism, but for now, only those two features are special.
        #
        # TODO - Actually there are more possible candidates like for instance
        # when listing static library X as a source for another static library.
        # Then static library X will be added as a <source> property to the
        # second library's usage requirements but those requirements should last
        # only up to the first executable or shared library that actually links
        # to it.
        local raw = [ $(subvariant).sources-usage-requirements ] ;
        raw = [ $(raw).raw ] ;
        raw = [ property.change $(raw) : <pch-header> ] ;
        raw = [ property.change $(raw) : <pch-file> ] ;
        return [ $(result).add [ property-set.create $(raw) ] ] ;
    }

    # Creates new subvariant instances for 'targets'.
    # 'root-targets'  - virtual targets to be returned to dependants
    # 'all-targets'   - virtual targets created while building this main target
    # 'build-request' - property-set instance with requested build properties
    #
    local rule create-subvariant ( root-targets * : all-targets * :
        build-request : sources * : rproperties : usage-requirements )
    {
        for local e in $(root-targets)
        {
            $(e).root true ;
        }

        # Process all virtual targets that will be created if this main target
        # is created.
        local s = [ new subvariant $(__name__) : $(build-request) : $(sources) :
            $(rproperties) : $(usage-requirements) : $(all-targets) ] ;
        for local v in $(all-targets)
        {
            if ! [ $(v).creating-subvariant ]
            {
                $(v).creating-subvariant $(s) ;
            }
        }
        return $(s) ;
    }

    # Constructs virtual targets for this abstract target and the dependency
    # graph. Returns a usage-requirements property-set and a list of virtual
    # targets. Should be overriden in derived classes.
    #
    rule construct ( name : source-targets * : properties * )
    {
        import errors : error : errors.error ;
        errors.error "method should be defined in derived classes" ;
    }
}


class typed-target : basic-target
{
    import generators ;

    rule __init__ ( name : project : type : sources * : requirements * :
         default-build * : usage-requirements * )
    {
        basic-target.__init__ $(name) : $(project) : $(sources) :
            $(requirements) : $(default-build) : $(usage-requirements) ;

        self.type = $(type) ;
    }

    rule type ( )
    {
        return $(self.type) ;
    }

    rule construct ( name : source-targets * : property-set )
    {
        local r = [ generators.construct $(self.project) $(name:S=)
            : $(self.type)
            : [ property-set.create [ $(property-set).raw ]
                <main-target-type>$(self.type) ]
            : $(source-targets) : true ] ;
        if ! $(r)
        {
            ECHO "warn: Unable to construct" [ full-name ] ;

            # Are there any top-level generators for this type/property set.
            if ! [ generators.find-viable-generators $(self.type) :
                $(property-set) ]
            {
                ECHO "error: no generators were found for type '$(self.type)'" ;
                ECHO "error: and the requested properties" ;
                ECHO "error: make sure you've configured the needed tools" ;
                ECHO "See http://boost.org/boost-build2/doc/html/bbv2/advanced/configuration.html" ;
                EXIT "To debug this problem, try the --debug-generators option."
                    ;
            }
        }
        return $(r) ;
    }
}


# Return the list of sources to use, if main target rule is invoked with
# 'sources'. If there are any objects in 'sources', they are treated as main
# target instances, and the name of such targets are adjusted to be
# '<name_of_this_target>__<name_of_source_target>'. Such renaming is disabled if
# a non-empty value is passed as the 'no-renaming' parameter.
#
rule main-target-sources ( sources * : main-target-name : no-renaming ? )
{
    local result ;
    for local t in $(sources)
    {
        if [ class.is-instance $(t) ]
        {
            local name = [ $(t).name ] ;
            if ! $(no-renaming)
            {
                name = $(main-target-name)__$(name) ;
                $(t).rename $(name) ;
            }
            # Inline targets are not built by default.
            local p = [ $(t).project ] ;
            $(p).mark-target-as-explicit $(name) ;
            result += $(name) ;
        }
        else
        {
            result += $(t) ;
        }
    }
    return $(result) ;
}


# Returns the requirements to use when declaring a main target, obtained by
# translating all specified property paths and refining project requirements
# with the ones specified for the target.
#
rule main-target-requirements (
    specification *  # Properties explicitly specified for the main target.
    : project        # Project where the main target is to be declared.
)
{
    local requirements = [ property-set.refine-from-user-input
        [ $(project).get requirements ] : $(specification) :
        [ $(project).project-module ] : [ $(project).get location ] ] ;
    if $(requirements[1]) = "@error"
    {
        import errors ;
        errors.error "Conflicting requirements for target:" $(requirements) ;
    }
    return [ $(requirements).add [ toolset.requirements ] ] ;
}


# Returns the usage requirements to use when declaring a main target, which are
# obtained by translating all specified property paths and adding project's
# usage requirements.
#
rule main-target-usage-requirements (
    specification *  # Use-properties explicitly specified for a main target.
    : project        # Project where the main target is to be declared.
)
{
    local project-usage-requirements = [ $(project).get usage-requirements ] ;

    # We do not use 'refine-from-user-input' because:
    # - I am not sure if removing parent's usage requirements makes sense
    # - refining usage requirements is not needed, since usage requirements are
    #   always free.
    local usage-requirements = [ property-set.create-from-user-input
        $(specification)
        : [ $(project).project-module ] [ $(project).get location ] ] ;

    return [ $(project-usage-requirements).add $(usage-requirements) ] ;
}


# Return the default build value to use when declaring a main target, which is
# obtained by using the specified value if not empty and parent's default build
# attribute otherwise.
#
rule main-target-default-build (
    specification *  # Default build explicitly specified for a main target.
    : project        # Project where the main target is to be declared.
)
{
    local result ;
    if $(specification)
    {
        result = $(specification) ;
    }
    else
    {
        result = [ $(project).get default-build ] ;
    }
    return [ property-set.create-with-validation $(result) ] ;
}


# Registers the specified target as a main target alternative and returns it.
#
rule main-target-alternative ( target )
{
    local ptarget = [ $(target).project ] ;
    $(ptarget).add-alternative $(target) ;
    return $(target) ;
}


# Creates a metatarget with the specified properties, using 'klass' as the
# class. The 'name', 'sources', 'requirements', 'default-build' and
# 'usage-requirements' are assumed to be in the form specified by the user in
# the Jamfile corresponding to 'project'.
#
rule create-metatarget ( klass : project : name : sources * : requirements * :
    default-build * : usage-requirements * )
{
    return [ targets.main-target-alternative [ new $(klass) $(name) : $(project)
        : [ targets.main-target-sources $(sources) : $(name) ]
        : [ targets.main-target-requirements $(requirements) : $(project) ]
        : [ targets.main-target-default-build $(default-build) : $(project) ]
        : [ targets.main-target-usage-requirements $(usage-requirements) :
            $(project) ] ] ] ;
}


# Creates a typed-target with the specified properties. The 'name', 'sources',
# 'requirements', 'default-build' and 'usage-requirements' are assumed to be in
# the form specified by the user in the Jamfile corresponding to 'project'.
#
rule create-typed-target ( type : project : name : sources * : requirements * :
    default-build * : usage-requirements * )
{
    return [ targets.main-target-alternative [ new typed-target $(name) :
        $(project) : $(type)
        : [ targets.main-target-sources $(sources) : $(name) ]
        : [ targets.main-target-requirements $(requirements) : $(project) ]
        : [ targets.main-target-default-build $(default-build) : $(project) ]
        : [ targets.main-target-usage-requirements $(usage-requirements) :
            $(project) ] ] ] ;
}