summaryrefslogtreecommitdiff
path: root/tools/preprocessor.py
blob: 483c89c75bd5f5760e38cdc4adf6fbd41450b2a8 (plain)
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
# -*- coding: utf-8 -*-
#
# File      : preprocessor.py
# This file is part of RT-Thread RTOS
# COPYRIGHT (C) 2006 - 2025, RT-Thread Development Team
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Change Logs:
# Date           Author       Notes
# 2025-01-05     Assistant    Extract SCons PreProcessor patch to independent class

from SCons.Script import *

class SConsPreProcessorPatch:
    """
    SCons PreProcessor patch class
    
    This class provides methods to patch the SCons PreProcessor
    to handle conditional compilation directives properly.
    """
    
    def __init__(self):
        """Initialize the PreProcessor patch"""
        self._patched_preprocessor = None
        self._apply_patch()
    
    def _apply_patch(self):
        """
        Apply the patch to SCons PreProcessor
        
        This method patches the SCons.cpp.PreProcessor class with
        custom methods for handling includes during conditional compilation.
        """
        from SCons import cpp
        
        # Store reference to original PreProcessor
        self._patched_preprocessor = cpp.PreProcessor
        
        # Create bound methods for the patch
        def start_handling_includes(preprocessor_self, t=None):
            d = preprocessor_self.dispatch_table
            p = preprocessor_self.stack[-1] if preprocessor_self.stack else preprocessor_self.default_table
            for k in ('import', 'include', 'include_next', 'define'):
                d[k] = p[k]
        
        def stop_handling_includes(preprocessor_self, t=None):
            d = preprocessor_self.dispatch_table
            d['import'] = preprocessor_self.do_nothing
            d['include'] = preprocessor_self.do_nothing
            d['include_next'] = preprocessor_self.do_nothing
            d['define'] = preprocessor_self.do_nothing
        
        # Apply the patch methods
        self._patched_preprocessor.start_handling_includes = start_handling_includes
        self._patched_preprocessor.stop_handling_includes = stop_handling_includes
    
    def get_patched_preprocessor(self):
        return self._patched_preprocessor
    
    def create_preprocessor_instance(self):
        return self._patched_preprocessor()

# Global instance for easy access
_preprocessor_patch = None

def get_patched_preprocessor():
    global _preprocessor_patch
    if _preprocessor_patch is None:
        _preprocessor_patch = SConsPreProcessorPatch()
    return _preprocessor_patch.get_patched_preprocessor()

def create_preprocessor_instance():
    global _preprocessor_patch
    if _preprocessor_patch is None:
        _preprocessor_patch = SConsPreProcessorPatch()
    return _preprocessor_patch.create_preprocessor_instance()