1# -*- coding: utf-8 -*-
2"""
3Next Generation building.py with minimal modifications.
4
5This file shows how to integrate the new OOP system with minimal changes to building.py.
6The actual implementation would modify the original building.py file.
7"""
8
9# Import everything from original building.py
10import sys
11import os
12
13# Add parent directory to path to import original building
14sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
15from building import *
16
17# Import new OOP modules
18from ng.adapter import (
19    init_build_context,
20    inject_environment_methods,
21    load_rtconfig as ng_load_rtconfig,
22    GenerateProject as ng_GenerateProject
23)
24
25
26# Override PrepareBuilding to integrate new system
27_original_PrepareBuilding = PrepareBuilding
28
29def PrepareBuilding(env, root_directory, has_libcpu=False, remove_components=[]):
30    """
31    Enhanced PrepareBuilding that integrates the new OOP system.
32
33    This function wraps the original PrepareBuilding and adds OOP functionality.
34    """
35    # Initialize new build context
36    context = init_build_context(root_directory)
37
38    # Call original PrepareBuilding
39    result = _original_PrepareBuilding(env, root_directory, has_libcpu, remove_components)
40
41    # Inject new methods into environment
42    inject_environment_methods(env)
43
44    # Load configuration into new system
45    ng_load_rtconfig('rtconfig.h')
46
47    # Store context in environment for access
48    env['_BuildContext'] = context
49
50    return result
51
52
53# Override DefineGroup to use new implementation
54_original_DefineGroup = DefineGroup
55
56def DefineGroup(name, src, depend, **parameters):
57    """
58    Enhanced DefineGroup that uses the new OOP implementation.
59
60    This maintains backward compatibility while using the new system internally.
61    """
62    # Get environment from global Env
63    global Env
64    if Env and hasattr(Env, 'DefineGroup'):
65        # Use new method if available
66        return Env.DefineGroup(name, src, depend, **parameters)
67    else:
68        # Fallback to original
69        return _original_DefineGroup(name, src, depend, **parameters)
70
71
72# Override GetDepend to use new implementation
73_original_GetDepend = GetDepend
74
75def GetDepend(depend):
76    """
77    Enhanced GetDepend that uses the new OOP implementation.
78    """
79    global Env
80    if Env and hasattr(Env, 'GetDepend'):
81        # Use new method if available
82        return Env.GetDepend(depend)
83    else:
84        # Fallback to original
85        return _original_GetDepend(depend)
86
87
88# Override DoBuilding to integrate project generation
89_original_DoBuilding = DoBuilding
90
91def DoBuilding(target, objects):
92    """
93    Enhanced DoBuilding that integrates new project generation.
94    """
95    # Call original DoBuilding
96    _original_DoBuilding(target, objects)
97
98    # Handle project generation with new system
99    if GetOption('target'):
100        target_name = GetOption('target')
101        global Env, Projects
102
103        # Use new generator if available
104        try:
105            ng_GenerateProject(target_name, Env, Projects)
106        except Exception as e:
107            print(f"Falling back to original generator: {e}")
108            # Call original GenTargetProject
109            from building import GenTargetProject
110            GenTargetProject(Projects, program=target)
111
112
113# Export enhanced functions
114__all__ = ['PrepareBuilding', 'DefineGroup', 'GetDepend', 'DoBuilding'] + \
115          [name for name in dir(sys.modules['building']) if not name.startswith('_')]