1# Copyright 2005 Vladimir Prus.
2# Distributed under the Boost Software License, Version 1.0. (See
3# accompanying file LICENSE_1_0.txt or copy at
4# http://www.boost.org/LICENSE_1_0.txt)
5
6# Defines main target 'cast', used to change type for target. For example, in Qt
7# library one wants two kinds of CPP files -- those that just compiled and those
8# that are passed via the MOC tool.
9#
10# This is done with:
11#
12#    exe main : main.cpp [ cast _ moccable-cpp : widget.cpp ] ;
13#
14# B2 will assign target type CPP to both main.cpp and widget.cpp. Then,
15# the cast rule will change target type of widget.cpp to MOCCABLE-CPP, and Qt
16# support will run the MOC tool as part of the build process.
17#
18# At the moment, the 'cast' rule only works for non-derived (source) targets.
19#
20# TODO: The following comment is unclear or incorrect. Clean it up.
21# > Another solution would be to add a separate main target 'moc-them' that
22# > would moc all the passed sources, no matter what their type is, but I prefer
23# > cast, as defining a new target type + generator for that type is somewhat
24# > simpler than defining a main target rule.
25
26import "class" : new ;
27import project ;
28import property-set ;
29import targets ;
30import type ;
31
32
33class cast-target-class : typed-target
34{
35    import type ;
36
37    rule __init__ ( name : project : type : sources * : requirements * :
38        default-build * : usage-requirements * )
39    {
40        typed-target.__init__ $(name) : $(project) : $(type) : $(sources) :
41            $(requirements) : $(default-build) : $(usage-requirements) ;
42    }
43
44    rule construct ( name : source-targets * : property-set )
45    {
46        local result ;
47        for local s in $(source-targets)
48        {
49            if ! [ class.is-a $(s) : file-target ]
50            {
51                import errors : user-error : errors.user-error ;
52                errors.user-error Source to the 'cast' rule is not a file! ;
53            }
54            if [ $(s).action ]
55            {
56                import errors : user-error : errors.user-error ;
57                errors.user-error Only non-derived target are allowed for
58                    'cast'. : when building [ full-name ] ;
59            }
60            local r = [ $(s).clone-with-different-type $(self.type) ] ;
61            result += [ virtual-target.register $(r) ] ;
62        }
63        return [ property-set.empty ] $(result) ;
64    }
65}
66
67
68rule cast ( name type : sources * : requirements * : default-build * :
69    usage-requirements * )
70{
71    local project = [ project.current ] ;
72
73    local real-type = [ type.type-from-rule-name $(type) ] ;
74    if ! $(real-type)
75    {
76        import errors ;
77        errors.user-error No type corresponds to the main target rule name
78            '$(type)' : "Hint: try a lowercase name" ;
79    }
80
81    targets.main-target-alternative [ new cast-target-class $(name) : $(project)
82        : $(real-type)
83        : [ targets.main-target-sources $(sources) : $(name) ]
84        : [ targets.main-target-requirements $(requirements) : $(project) ]
85        : [ targets.main-target-default-build $(default-build) : $(project) ]
86        : [ targets.main-target-usage-requirements $(usage-requirements) :
87            $(project) ] ] ;
88}
89
90
91IMPORT $(__name__) : cast : : cast ;
92