@@ -170,62 +170,56 @@ def BuildPackage(env, package_path: str = None) -> List:
170170
171171 Args:
172172 env: SCons Environment
173- package_path: Path to package.json or directory containing it
173+ package_path: Path to package.json. If None, looks for package.json in current directory.
174174
175175 Returns:
176176 List of build objects
177177 """
178- import json
178+ # Import the existing package module
179+ import sys
180+ import os
179181
180- # Find package.json
181- if package_path is None :
182- package_path = 'package.json'
183- elif os .path .isdir (package_path ):
184- package_path = os .path .join (package_path , 'package.json' )
185-
186- if not os .path .exists (package_path ):
187- env .GetContext ().logger .error (f"Package file not found: { package_path } " )
188- return []
189-
190- # Load package definition
191- with open (package_path , 'r' ) as f :
192- package = json .load (f )
193-
194- # Process package
195- name = package .get ('name' , 'unnamed' )
196- dependencies = package .get ('dependencies' , {})
182+ # Get the building module path
183+ building_path = os .path .dirname (os .path .abspath (__file__ ))
184+ tools_path = os .path .dirname (building_path )
197185
198- # Check main dependency
199- if 'RT_USING_' + name .upper () not in dependencies :
200- main_depend = 'RT_USING_' + name .upper ().replace ('-' , '_' )
201- else :
202- main_depend = list (dependencies .keys ())[0 ]
186+ # Add to path if not already there
187+ if tools_path not in sys .path :
188+ sys .path .insert (0 , tools_path )
189+
190+ # Import and use the existing BuildPackage
191+ try :
192+ from package import BuildPackage as build_package_func
203193
204- if not env .GetDepend (main_depend ):
205- return []
194+ # BuildPackage uses global functions, so we need to set up the context
195+ # Save current directory
196+ current_dir = os .getcwd ()
206197
207- # Collect sources
208- src = []
209- include_paths = []
210-
211- sources = package .get ('sources' , {})
212- for category , config in sources .items ():
213- # Check condition
214- condition = config .get ('condition' )
215- if condition and not eval (condition , {'env' : env , 'GetDepend' : env .GetDepend }):
216- continue
198+ # Change to the directory where we want to build
199+ if package_path is None :
200+ work_dir = env .GetCurrentDir ()
201+ elif os .path .isdir (package_path ):
202+ work_dir = package_path
203+ else :
204+ work_dir = os .path .dirname (package_path )
217205
218- # Add source files
219- source_files = config .get ('source_files' , [])
220- for pattern in source_files :
221- src .extend (env .Glob (pattern ))
206+ os .chdir (work_dir )
207+
208+ try :
209+ # Call the original BuildPackage
210+ result = build_package_func (package_path )
211+ finally :
212+ # Restore directory
213+ os .chdir (current_dir )
222214
223- # Add header paths
224- header_path = config .get ('header_path' , [])
225- include_paths .extend (header_path )
215+ return result
226216
227- # Create group
228- return env .DefineGroup (name , src , depend = main_depend , CPPPATH = include_paths )
217+ except ImportError :
218+ # Fallback if import fails
219+ context = BuildContext .get_current ()
220+ if context :
221+ context .logger .error ("Failed to import package module" )
222+ return []
229223
230224 @staticmethod
231225 def Glob (env , pattern : str ) -> List [str ]:
0 commit comments