1# -*- coding: utf-8 -*- 2""" 3Adapter module to integrate new OOP implementation with existing building.py. 4 5This module provides the bridge between the legacy function-based API and the new 6object-oriented implementation, ensuring backward compatibility. 7""" 8 9import os 10from typing import List, Dict, Any, Optional 11 12from .core import BuildContext 13from .environment import RTEnv 14from .generator import GeneratorConfig, GeneratorRegistry 15 16 17# Global variables for compatibility 18_context: Optional[BuildContext] = None 19 20 21def init_build_context(root_directory: str) -> BuildContext: 22 """ 23 Initialize the build context. 24 25 This function should be called early in PrepareBuilding. 26 27 Args: 28 root_directory: RT-Thread root directory 29 30 Returns: 31 BuildContext instance 32 """ 33 global _context 34 _context = BuildContext(root_directory) 35 return _context 36 37 38def get_build_context() -> Optional[BuildContext]: 39 """Get the current build context.""" 40 return _context 41 42 43def inject_environment_methods(env) -> None: 44 """ 45 Inject RT-Thread methods into SCons Environment. 46 47 This should be called in PrepareBuilding after environment setup. 48 49 Args: 50 env: SCons Environment object 51 """ 52 RTEnv.inject_methods(env) 53 54 # Also set the environment in context 55 if _context: 56 _context.prepare_environment(env) 57 58 59def load_rtconfig(config_file: str = 'rtconfig.h') -> Dict[str, Any]: 60 """ 61 Load configuration from rtconfig.h. 62 63 Args: 64 config_file: Configuration file name 65 66 Returns: 67 Dictionary of build options 68 """ 69 if _context: 70 _context.load_configuration(config_file) 71 return _context.build_options 72 return {} 73 74 75def DefineGroup(name: str, src: List[str], depend: Any = None, **kwargs) -> List: 76 """ 77 Legacy DefineGroup function for backward compatibility. 78 79 This function delegates to the environment method. 80 81 Args: 82 name: Group name 83 src: Source files 84 depend: Dependencies 85 **kwargs: Additional parameters 86 87 Returns: 88 List of build objects 89 """ 90 if _context and _context.environment: 91 return _context.environment.DefineGroup(name, src, depend, **kwargs) 92 else: 93 # Fallback behavior 94 print(f"Warning: DefineGroup called before environment setup for group '{name}'") 95 return [] 96 97 98def GetDepend(depend: Any) -> bool: 99 """ 100 Legacy GetDepend function for backward compatibility. 101 102 Args: 103 depend: Dependency to check 104 105 Returns: 106 True if dependency is satisfied 107 """ 108 if _context: 109 return _context.get_dependency(depend) 110 return False 111 112 113def GetCurrentDir() -> str: 114 """ 115 Get current directory. 116 117 Returns: 118 Current directory path 119 """ 120 return os.path.abspath('.') 121 122 123def SrcRemove(src: List[str], remove: List[str]) -> None: 124 """ 125 Remove files from source list. 126 127 Args: 128 src: Source list (modified in place) 129 remove: Files to remove 130 """ 131 if not isinstance(remove, list): 132 remove = [remove] 133 134 for item in remove: 135 if item in src: 136 src.remove(item) 137 138 139def GetBuildOptions() -> Dict[str, Any]: 140 """ 141 Get build options. 142 143 Returns: 144 Dictionary of build options 145 """ 146 if _context: 147 return _context.build_options 148 return {} 149 150 151def MergeGroups() -> List: 152 """ 153 Merge all registered groups. 154 155 Returns: 156 List of all build objects 157 """ 158 if _context: 159 return _context.merge_groups() 160 return [] 161 162 163def GenerateProject(target: str, env, projects: List) -> None: 164 """ 165 Generate IDE project files. 166 167 Args: 168 target: Target type (mdk5, iar, vscode, etc.) 169 env: SCons Environment 170 projects: Project list 171 """ 172 if not _context: 173 print("Error: Build context not initialized") 174 return 175 176 # Get project info from registry 177 project_info = _context.project_registry.get_project_info() 178 179 # Create generator config 180 config = GeneratorConfig( 181 output_dir=os.getcwd(), 182 project_name=os.path.basename(os.getcwd()), 183 target_name="rtthread.elf" 184 ) 185 186 # Create and run generator 187 try: 188 generator = _context.generator_registry.create_generator(target, config) 189 if generator.generate(_context, project_info): 190 print(f"Successfully generated {target} project files") 191 else: 192 print(f"Failed to generate {target} project files") 193 except Exception as e: 194 print(f"Error generating {target} project: {e}") 195 196 197def PrepareModuleBuilding(env, root_directory, bsp_directory) -> None: 198 """ 199 Prepare for building a module. 200 201 This is a simplified version of PrepareBuilding for module compilation. 202 203 Args: 204 env: SCons Environment 205 root_directory: RT-Thread root directory 206 bsp_directory: BSP directory 207 """ 208 # Initialize context 209 context = init_build_context(root_directory) 210 context.bsp_directory = bsp_directory 211 212 # Inject methods 213 inject_environment_methods(env) 214 215 # Load configuration 216 config_path = os.path.join(bsp_directory, 'rtconfig.h') 217 if os.path.exists(config_path): 218 load_rtconfig(config_path)