adapter.py 5.2 KB

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