1# Copyright (c) 2005 Reece H. Dunn.
2# Copyright 2006 Ilya Sokolov
3#
4# Use, modification and distribution is subject to the Boost Software
5# License Version 1.0. (See accompanying file LICENSE_1_0.txt or
6# http://www.boost.org/LICENSE_1_0.txt)
7
8##### Using Precompiled Headers (Quick Guide) #####
9#
10# Make precompiled mypch.hpp:
11#
12#    import pch ;
13#
14#    cpp-pch mypch
15#      : # sources
16#        mypch.hpp
17#      : # requiremnts
18#        <toolset>msvc:<source>mypch.cpp
19#      ;
20#
21# Add cpp-pch to sources:
22#
23#    exe hello
24#      : main.cpp hello.cpp mypch
25#      ;
26
27import "class" : new ;
28import type ;
29import feature ;
30import generators ;
31
32type.register     PCH : pch ;
33
34type.register   C_PCH : : PCH ;
35type.register CPP_PCH : : PCH ;
36
37# Control precompiled header (PCH) generation.
38feature.feature pch :
39      on
40      off
41    : propagated ;
42
43
44feature.feature pch-header : : free dependency ;
45feature.feature pch-file   : : free dependency ;
46
47# Base PCH generator. The 'run' method has the logic to prevent this generator
48# from being run unless it's being used for a top-level PCH target.
49class pch-generator : generator
50{
51    import property-set ;
52
53    rule action-class ( )
54    {
55        return compile-action ;
56    }
57
58    rule run ( project name ? : property-set : sources + )
59    {
60        if ! $(name)
61        {
62            # Unless this generator is invoked as the top-most generator for a
63            # main target, fail. This allows using 'H' type as input type for
64            # this generator, while preventing Boost.Build to try this generator
65            # when not explicitly asked for.
66            #
67            # One bad example is msvc, where pch generator produces both PCH
68            # target and OBJ target, so if there's any header generated (like by
69            # bison, or by msidl), we'd try to use pch generator to get OBJ from
70            # that H, which is completely wrong. By restricting this generator
71            # only to pch main target, such problem is solved.
72        }
73        else
74        {
75            local r = [ run-pch $(project) $(name)
76              : [ $(property-set).add-raw <define>BOOST_BUILD_PCH_ENABLED ]
77              : $(sources) ] ;
78            return [ generators.add-usage-requirements $(r)
79              : <define>BOOST_BUILD_PCH_ENABLED ] ;
80        }
81    }
82
83    # This rule must be overridden by the derived classes.
84    rule run-pch ( project name ? : property-set : sources + )
85    {
86    }
87}
88
89
90# NOTE: requirements are empty, default pch generator can be applied when
91# pch=off.
92generators.register
93    [ new dummy-generator pch.default-c-pch-generator   : :   C_PCH ] ;
94generators.register
95    [ new dummy-generator pch.default-cpp-pch-generator : : CPP_PCH ] ;
96