building_ng.py 3.4 KB

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