# Status: Being ported by Steven Watanabe
# Base revision: 47077
#
# Copyright (c) 2005 Reece H. Dunn.
# Copyright 2006 Ilya Sokolov
# Copyright (c) 2008 Steven Watanabe
#
# Use, modification and distribution is subject to the Boost Software
# License Version 1.0. (See accompanying file LICENSE_1_0.txt or
# http://www.boost.org/LICENSE_1_0.txt)

##### Using Precompiled Headers (Quick Guide) #####
#
# Make precompiled mypch.hpp:
#
#    import pch ;
#
#    cpp-pch mypch
#      : # sources
#        mypch.hpp
#      : # requiremnts
#        <toolset>msvc:<source>mypch.cpp
#      ;
#
# Add cpp-pch to sources:
#
#    exe hello
#      : main.cpp hello.cpp mypch
#      ;

from b2.build import type, feature, generators
from b2.tools import builtin

type.register('PCH', ['pch'])
type.register('C_PCH', [], 'PCH')
type.register('CPP_PCH', [], 'PCH')

# Control precompiled header (PCH) generation.
feature.feature('pch',
                ['on', 'off'],
                ['propagated'])

feature.feature('pch-header', [], ['free', 'dependency'])
feature.feature('pch-file', [], ['free', 'dependency'])

class PchGenerator(generators.Generator):
    """
        Base PCH generator. The 'run' method has the logic to prevent this generator
        from being run unless it's being used for a top-level PCH target.
    """
    def action_class(self):
        return builtin.CompileAction

    def run(self, project, name, prop_set, sources):
        if not name:
            # Unless this generator is invoked as the top-most generator for a
            # main target, fail. This allows using 'H' type as input type for
            # this generator, while preventing Boost.Build to try this generator
            # when not explicitly asked for.
            #
            # One bad example is msvc, where pch generator produces both PCH
            # target and OBJ target, so if there's any header generated (like by
            # bison, or by msidl), we'd try to use pch generator to get OBJ from
            # that H, which is completely wrong. By restricting this generator
            # only to pch main target, such problem is solved.
            pass
        else:
            r = self.run_pch(project, name,
                 prop_set.add_raw(['<define>BOOST_BUILD_PCH_ENABLED']),
                 sources)
            return generators.add_usage_requirements(
                r, ['<define>BOOST_BUILD_PCH_ENABLED'])

    # This rule must be overridden by the derived classes.
    def run_pch(self, project, name, prop_set, sources):
        pass

# NOTE: requirements are empty, default pch generator can be applied when
# pch=off.
generators.register(builtin.DummyGenerator(
    "pch.default-c-pch-generator", False, [], ['C_PCH'], []))
generators.register(builtin.DummyGenerator(
    "pch.default-cpp-pch-generator", False, [], ['CPP_PCH'], []))

[((34, 0, 34, 29), 'b2.build.type.register', 'type.register', ({(34, 14, 34, 19): '"""PCH"""', (34, 21, 34, 28): "['pch']"}, {}), "('PCH', ['pch'])", False, 'from b2.build import type, feature, generators\n'), ((35, 0, 35, 33), 'b2.build.type.register', 'type.register', ({(35, 14, 35, 21): '"""C_PCH"""', (35, 23, 35, 25): '[]', (35, 27, 35, 32): '"""PCH"""'}, {}), "('C_PCH', [], 'PCH')", False, 'from b2.build import type, feature, generators\n'), ((36, 0, 36, 35), 'b2.build.type.register', 'type.register', ({(36, 14, 36, 23): '"""CPP_PCH"""', (36, 25, 36, 27): '[]', (36, 29, 36, 34): '"""PCH"""'}, {}), "('CPP_PCH', [], 'PCH')", False, 'from b2.build import type, feature, generators\n'), ((39, 0, 41, 31), 'b2.build.feature.feature', 'feature.feature', ({(39, 16, 39, 21): '"""pch"""', (40, 16, 40, 29): "['on', 'off']", (41, 16, 41, 30): "['propagated']"}, {}), "('pch', ['on', 'off'], ['propagated'])", False, 'from b2.build import type, feature, generators\n'), ((43, 0, 43, 57), 'b2.build.feature.feature', 'feature.feature', ({(43, 16, 43, 28): '"""pch-header"""', (43, 30, 43, 32): '[]', (43, 34, 43, 56): "['free', 'dependency']"}, {}), "('pch-header', [], ['free', 'dependency'])", False, 'from b2.build import type, feature, generators\n'), ((44, 0, 44, 55), 'b2.build.feature.feature', 'feature.feature', ({(44, 16, 44, 26): '"""pch-file"""', (44, 28, 44, 30): '[]', (44, 32, 44, 54): "['free', 'dependency']"}, {}), "('pch-file', [], ['free', 'dependency'])", False, 'from b2.build import type, feature, generators\n'), ((80, 20, 81, 60), 'b2.tools.builtin.DummyGenerator', 'builtin.DummyGenerator', ({(81, 4, 81, 33): '"""pch.default-c-pch-generator"""', (81, 35, 81, 40): '(False)', (81, 42, 81, 44): '[]', (81, 46, 81, 55): "['C_PCH']", (81, 57, 81, 59): '[]'}, {}), "('pch.default-c-pch-generator', False, [], ['C_PCH'], [])", False, 'from b2.tools import builtin\n'), ((82, 20, 83, 64), 'b2.tools.builtin.DummyGenerator', 'builtin.DummyGenerator', ({(83, 4, 83, 35): '"""pch.default-cpp-pch-generator"""', (83, 37, 83, 42): '(False)', (83, 44, 83, 46): '[]', (83, 48, 83, 59): "['CPP_PCH']", (83, 61, 83, 63): '[]'}, {}), "('pch.default-cpp-pch-generator', False, [], [\n    'CPP_PCH'], [])", False, 'from b2.tools import builtin\n'), ((71, 19, 72, 55), 'b2.build.generators.add_usage_requirements', 'generators.add_usage_requirements', ({(72, 16, 72, 17): 'r', (72, 19, 72, 54): "['<define>BOOST_BUILD_PCH_ENABLED']"}, {}), "(r, ['<define>BOOST_BUILD_PCH_ENABLED'])", False, 'from b2.build import type, feature, generators\n')]