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