integration_example.py 5.6 KB

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