1# -*- coding: utf-8 -*-
2"""
3Example of minimal changes needed in building.py to integrate the new OOP system.
4
5This file shows the exact changes that would be made to the original building.py.
6"""
7
8# =============================================================================
9# CHANGES TO ADD AT THE BEGINNING OF building.py
10# =============================================================================
11
12"""
13# Add after the imports section in building.py (around line 45)
14
15# Try to import new OOP system
16try:
17    from ng.adapter import (
18        init_build_context,
19        inject_environment_methods,
20        load_rtconfig as ng_load_rtconfig,
21        MergeGroups as ng_MergeGroups
22    )
23    NG_AVAILABLE = True
24except ImportError:
25    NG_AVAILABLE = False
26"""
27
28# =============================================================================
29# CHANGES IN PrepareBuilding FUNCTION
30# =============================================================================
31
32"""
33# Add these lines in PrepareBuilding function after setting up Env (around line 70)
34
35    # Initialize new OOP system if available
36    if NG_AVAILABLE:
37        # Initialize build context
38        ng_context = init_build_context(Rtt_Root)
39
40        # Inject methods into environment
41        inject_environment_methods(Env)
42
43        # Store context reference
44        Env['__NG_Context'] = ng_context
45"""
46
47# =============================================================================
48# CHANGES AFTER PARSING rtconfig.h
49# =============================================================================
50
51"""
52# Add after parsing rtconfig.h (around line 430)
53
54    # Load configuration into new system
55    if NG_AVAILABLE and 'rtconfig.h' in os.listdir(Bsp_Root):
56        ng_load_rtconfig('rtconfig.h')
57"""
58
59# =============================================================================
60# ENHANCED DefineGroup FUNCTION
61# =============================================================================
62
63"""
64# Replace the original DefineGroup function (around line 565) with:
65
66def DefineGroup(name, src, depend, **parameters):
67    global Env
68    if Env is None:
69        return []
70
71    # Try to use new implementation if available
72    if NG_AVAILABLE and hasattr(Env, 'DefineGroup'):
73        return Env.DefineGroup(name, src, depend, **parameters)
74
75    # Original implementation continues below...
76    # [Keep all the original DefineGroup code here]
77"""
78
79# =============================================================================
80# ENHANCED GetDepend FUNCTION
81# =============================================================================
82
83"""
84# Replace the original GetDepend function (around line 655) with:
85
86def GetDepend(depend):
87    global Env
88
89    # Try to use new implementation if available
90    if NG_AVAILABLE and Env and hasattr(Env, 'GetDepend'):
91        return Env.GetDepend(depend)
92
93    # Original implementation continues below...
94    # [Keep all the original GetDepend code here]
95"""
96
97# =============================================================================
98# ENHANCED MergeGroup FUNCTION
99# =============================================================================
100
101"""
102# Replace the original MergeGroup function (around line 700) with:
103
104def MergeGroup(src_group, group):
105    # Try to use new implementation if available
106    if NG_AVAILABLE and Env and hasattr(Env, '__NG_Context'):
107        context = Env['__NG_Context']
108        if context:
109            # Register groups with new system
110            from ng.project import ProjectGroup
111            for g in group:
112                if 'name' in g:
113                    pg = ProjectGroup(
114                        name=g['name'],
115                        sources=g.get('src', []),
116                        dependencies=[],
117                        environment=Env
118                    )
119                    context.register_project_group(pg)
120
121    # Original implementation continues below...
122    # [Keep all the original MergeGroup code here]
123"""
124
125# =============================================================================
126# EXAMPLE USAGE IN SCONSCRIPT
127# =============================================================================
128
129def example_sconscript():
130    """
131    Example of how to use the new features in a SConscript file.
132    """
133    sconscript_content = '''
134from building import *
135
136# Get environment
137env = GetEnvironment()
138
139# Method 1: Use new environment methods (if available)
140if hasattr(env, 'DefineGroup'):
141    # New OOP style
142    src = env.GlobFiles('*.c')
143    group = env.DefineGroup('MyComponent', src, depend=['RT_USING_XXX'])
144else:
145    # Fallback to traditional style
146    src = Glob('*.c')
147    group = DefineGroup('MyComponent', src, depend=['RT_USING_XXX'])
148
149# Method 2: Always compatible style
150src = Glob('*.c')
151group = DefineGroup('MyComponent', src, depend=['RT_USING_XXX'])
152
153Return('group')
154'''
155    return sconscript_content
156
157# =============================================================================
158# MINIMAL CHANGES SUMMARY
159# =============================================================================
160
161"""
162Summary of changes needed in building.py:
163
1641. Add imports at the beginning (5 lines)
1652. Add initialization in PrepareBuilding (6 lines)
1663. Add config loading after rtconfig.h parsing (3 lines)
1674. Modify DefineGroup to check for new method (3 lines)
1685. Modify GetDepend to check for new method (3 lines)
1696. Enhance MergeGroup to register with new system (15 lines)
170
171Total: ~35 lines of code added/modified in building.py
172
173Benefits:
174- Fully backward compatible
175- Opt-in design (works even if ng module is not present)
176- Gradual migration path
177- No changes needed in existing SConscript files
178"""