summaryrefslogtreecommitdiff
path: root/tools/ng/environment.py
blob: 34232636831e9c12ac65fb71c765383828798523 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# -*- coding: utf-8 -*-
"""
Environment extensions for RT-Thread build system.

This module provides methods that are injected into the SCons Environment object
to provide RT-Thread-specific functionality.
"""

import os
from typing import List, Union, Dict, Any, Optional
from SCons.Script import *

from .core import BuildContext
from .project import ProjectGroup


class RTEnv:
    """
    RT-Thread environment extensions (RTEnv).
    
    This class provides methods that are added to the SCons Environment object.
    """
    
    @staticmethod
    def inject_methods(env):
        """
        Inject RT-Thread methods into SCons Environment.
        
        Args:
            env: SCons Environment object
        """
        # Core build methods
        env.AddMethod(RTEnv.DefineGroup, 'DefineGroup')
        env.AddMethod(RTEnv.GetDepend, 'GetDepend')
        env.AddMethod(RTEnv.SrcRemove, 'SrcRemove')
        env.AddMethod(RTEnv.GetCurrentDir, 'GetCurrentDir')
        env.AddMethod(RTEnv.BuildPackage, 'BuildPackage')
        
        # Utility methods
        env.AddMethod(RTEnv.Glob, 'GlobFiles')
        env.AddMethod(RTEnv.GetBuildOptions, 'GetBuildOptions')
        env.AddMethod(RTEnv.GetContext, 'GetContext')
        
        # Path utilities
        env.AddMethod(RTEnv.GetRTTRoot, 'GetRTTRoot')
        env.AddMethod(RTEnv.GetBSPRoot, 'GetBSPRoot')
        
    @staticmethod
    def DefineGroup(env, name: str, src: List[str], depend: Any = None, **kwargs) -> List:
        """
        Define a component group.
        
        This method maintains compatibility with the original DefineGroup function
        while using the new object-oriented implementation.
        
        Args:
            env: SCons Environment
            name: Group name
            src: Source file list
            depend: Dependency conditions
            **kwargs: Additional parameters (CPPPATH, CPPDEFINES, etc.)
            
        Returns:
            List of build objects
        """
        context = BuildContext.get_current()
        if not context:
            raise RuntimeError("BuildContext not initialized")
            
        # Check dependencies
        if depend and not env.GetDepend(depend):
            return []
            
        # Process source files
        if isinstance(src, str):
            src = [src]
            
        # Create project group
        group = ProjectGroup(
            name=name,
            sources=src,
            dependencies=depend if isinstance(depend, list) else [depend] if depend else [],
            environment=env
        )
        
        # Process parameters
        group.include_paths = kwargs.get('CPPPATH', [])
        group.defines = kwargs.get('CPPDEFINES', {})
        group.cflags = kwargs.get('CFLAGS', '')
        group.cxxflags = kwargs.get('CXXFLAGS', '')
        group.local_cflags = kwargs.get('LOCAL_CFLAGS', '')
        group.local_cxxflags = kwargs.get('LOCAL_CXXFLAGS', '')
        group.local_include_paths = kwargs.get('LOCAL_CPPPATH', [])
        group.local_defines = kwargs.get('LOCAL_CPPDEFINES', {})
        group.libs = kwargs.get('LIBS', [])
        group.lib_paths = kwargs.get('LIBPATH', [])
        
        # Build objects
        objects = group.build(env)
        
        # Register group
        context.register_project_group(group)
        
        return objects
        
    @staticmethod
    def GetDepend(env, depend: Any) -> bool:
        """
        Check if dependency is satisfied.
        
        Args:
            env: SCons Environment
            depend: Dependency name or list of names
            
        Returns:
            True if dependency is satisfied
        """
        context = BuildContext.get_current()
        if not context:
            # Fallback to checking environment variables
            if isinstance(depend, str):
                return env.get(depend, False)
            elif isinstance(depend, list):
                return all(env.get(d, False) for d in depend)
            return False
            
        return context.get_dependency(depend)
        
    @staticmethod
    def SrcRemove(env, src: List[str], remove: List[str]) -> None:
        """
        Remove files from source list.
        
        Args:
            env: SCons Environment
            src: Source file list (modified in place)
            remove: Files to remove
        """
        if not isinstance(remove, list):
            remove = [remove]
            
        for item in remove:
            # Handle both exact matches and pattern matches
            if item in src:
                src.remove(item)
            else:
                # Try pattern matching
                import fnmatch
                to_remove = [f for f in src if fnmatch.fnmatch(f, item)]
                for f in to_remove:
                    src.remove(f)
                    
    @staticmethod
    def GetCurrentDir(env) -> str:
        """
        Get current directory.
        
        Args:
            env: SCons Environment
            
        Returns:
            Current directory path
        """
        return Dir('.').abspath
        
    @staticmethod
    def BuildPackage(env, package_path: str = None) -> List:
        """
        Build package from package.json.
        
        Args:
            env: SCons Environment
            package_path: Path to package.json. If None, looks for package.json in current directory.
            
        Returns:
            List of build objects
        """
        # Import the existing package module
        import sys
        import os
        
        # Get the building module path
        building_path = os.path.dirname(os.path.abspath(__file__))
        tools_path = os.path.dirname(building_path)
        
        # Add to path if not already there
        if tools_path not in sys.path:
            sys.path.insert(0, tools_path)
        
        # Import and use the existing BuildPackage
        try:
            from package import BuildPackage as build_package_func
            
            # BuildPackage uses global functions, so we need to set up the context
            # Save current directory
            current_dir = os.getcwd()
            
            # Change to the directory where we want to build
            if package_path is None:
                work_dir = env.GetCurrentDir()
            elif os.path.isdir(package_path):
                work_dir = package_path
            else:
                work_dir = os.path.dirname(package_path)
                
            os.chdir(work_dir)
            
            try:
                # Call the original BuildPackage
                result = build_package_func(package_path)
            finally:
                # Restore directory
                os.chdir(current_dir)
                
            return result
            
        except ImportError:
            # Fallback if import fails
            context = BuildContext.get_current()
            if context:
                context.logger.error("Failed to import package module")
            return []
        
    @staticmethod
    def Glob(env, pattern: str) -> List[str]:
        """
        Enhanced glob with better error handling.
        
        Args:
            env: SCons Environment
            pattern: File pattern
            
        Returns:
            List of matching files
        """
        try:
            files = Glob(pattern, strings=True)
            return sorted(files)  # Sort for consistent ordering
        except Exception as e:
            context = BuildContext.get_current()
            if context:
                context.logger.warning(f"Glob pattern '{pattern}' failed: {e}")
            return []
            
    @staticmethod
    def GetBuildOptions(env) -> Dict[str, Any]:
        """
        Get build options.
        
        Args:
            env: SCons Environment
            
        Returns:
            Dictionary of build options
        """
        context = BuildContext.get_current()
        if context:
            return context.build_options
        return {}
        
    @staticmethod
    def GetContext(env) -> Optional[BuildContext]:
        """
        Get current build context.
        
        Args:
            env: SCons Environment
            
        Returns:
            BuildContext instance or None
        """
        return BuildContext.get_current()
        
    @staticmethod
    def GetRTTRoot(env) -> str:
        """
        Get RT-Thread root directory.
        
        Args:
            env: SCons Environment
            
        Returns:
            RT-Thread root path
        """
        return env.get('RTT_ROOT', '')
        
    @staticmethod
    def GetBSPRoot(env) -> str:
        """
        Get BSP root directory.
        
        Args:
            env: SCons Environment
            
        Returns:
            BSP root path
        """
        return env.get('BSP_ROOT', '')