@@ -81,7 +81,7 @@ def get_build_artifacts(self, target:str):
8181 buildlog = os .path .join (builddir , "build.log" )
8282 return builddir , cmakelog , buildlog
8383
84- def build (self , target :str , target_alias :list , config :dict , installdir :str , parallel = "-j" , norebuild = False ):
84+ def build (self , target :str , target_alias :list , config :dict , installdir :str , parallel = "-j" , norebuild = False , cmake_extra_args = None ):
8585 if isinstance (config , dict ) == False :
8686 return False
8787 cmakefile = os .path .join (self .nl_src , "CMakeLists.txt" )
@@ -92,6 +92,10 @@ def build(self, target:str, target_alias:list, config:dict, installdir:str, para
9292 abs_nlsrc = os .path .abspath (self .nl_src )
9393 for key in config :
9494 cmakeopts += "-D%s=%s " % (key , config [key ])
95+
96+ if cmake_extra_args :
97+ cmakeopts += ' ' .join (cmake_extra_args ) + ' '
98+
9599 nl_buildir , nl_cmakelog , nl_buildlog = self .get_build_artifacts (target )
96100 makefile = os .path .join (nl_buildir , "Makefile" )
97101 genmake = False
@@ -194,7 +198,7 @@ def strip_library(libroot):
194198 run_command (strip_cmd )
195199 pass
196200
197- def install_library (libsrc , buildcfgs :dict , aliascfgs :dict , libprefix , libroot , target :str = "all" , strip = True , parallel = "-j" , ignore_fail = False , norebuild = False ):
201+ def install_library (libsrc , buildcfgs :dict , aliascfgs :dict , libprefix , libroot , target :str = "all" , strip = True , parallel = "-j" , ignore_fail = False , norebuild = False , cmake_extra_args = None ):
198202 if isinstance (buildcfgs , dict ) == False :
199203 print ("No build configuration found" )
200204 return False
@@ -209,7 +213,7 @@ def install_library(libsrc, buildcfgs:dict, aliascfgs:dict, libprefix, libroot,
209213 print (">>> Build and install %s library for config %s" % (libsrc , key ))
210214 _ , _ , buildlog = nlb .get_build_artifacts (key )
211215 target_alias = aliascfgs .get (key , [])
212- ret = nlb .build (key , target_alias , buildcfgs [key ], libroot , parallel , norebuild )
216+ ret = nlb .build (key , target_alias , buildcfgs [key ], libroot , parallel , norebuild , cmake_extra_args )
213217
214218 cost_time = round (time .time () - start_time , 2 )
215219 rst_table .add_row ([key , ret , cost_time , buildlog ])
@@ -224,7 +228,8 @@ def install_library(libsrc, buildcfgs:dict, aliascfgs:dict, libprefix, libroot,
224228 start_time = time .time ()
225229 print (">>> Build and install %s library for config %s" % (libsrc , target ))
226230 _ , _ , buildlog = nlb .get_build_artifacts (target )
227- ret = nlb .build (target , buildcfgs [target ], libroot , parallel , norebuild )
231+ target_alias = aliascfgs .get (target , [])
232+ ret = nlb .build (target , target_alias , buildcfgs [target ], libroot , parallel , norebuild , cmake_extra_args )
228233 cost_time = round (time .time () - start_time , 2 )
229234 rst_table .add_row ([target , ret , cost_time , buildlog ])
230235 if ret == False :
@@ -253,13 +258,76 @@ def install_library(libsrc, buildcfgs:dict, aliascfgs:dict, libprefix, libroot,
253258 parser .add_argument ('--target' , default = "all" , help = "if target = all, it means run all the targets defined in config" )
254259 parser .add_argument ('--parallel' , default = "-j4" , help = "parallel build library, default -j4" )
255260 parser .add_argument ('--ignore_fail' , action = 'store_true' , help = "If specified, will ignore fail even any build configuration failed" )
261+ parser .add_argument ('--toolchain' , default = "nuclei_gnu" , help = "Select the toolchain profile to use (e.g., nuclei_gnu, nuclei_llvm, terapines)" )
256262
257263 args = parser .parse_args ()
258264
259265 if sys .platform == "win32" :
260266 print ("Windows build is not yet supported!" )
261267 sys .exit (1 )
262268
269+ toolchain_cmake_args = []
270+ toolchain_tools = {}
271+ selected_toolchain = args .toolchain .lower ()
272+
273+ TOOLCHAIN_CONFIGS = {
274+ 'terapines' : {
275+ 'name' : 'Terapines (zcc)' ,
276+ 'cc_name' : 'zcc' ,
277+ 'cxx_name' : 'z++' ,
278+ 'ar_name' : 'llvm-ar'
279+ },
280+ 'nuclei_llvm' : {
281+ 'name' : 'nuclei_llvm' ,
282+ 'cc_name' : 'riscv64-unknown-elf-clang' ,
283+ 'cxx_name' : 'riscv64-unknown-elf-clang++' ,
284+ 'ar_name' : 'riscv64-unknown-elf-ar'
285+ },
286+ 'nuclei_gnu' : {
287+ 'name' : 'default GCC' ,
288+ 'cc_name' : 'riscv64-unknown-elf-gcc' ,
289+ 'cxx_name' : 'riscv64-unknown-elf-g++' ,
290+ 'ar_name' : 'riscv64-unknown-elf-ar'
291+ }
292+ }
293+
294+ # Gets the selected toolchain configuration
295+ config = TOOLCHAIN_CONFIGS .get (selected_toolchain )
296+
297+ if not config :
298+ print (f"Warning: Unknown toolchain '{ args .toolchain } '. Using GCC default." )
299+ print (">>> Using default GCC toolchain profile." )
300+ config = TOOLCHAIN_CONFIGS ['nuclei_gnu' ]
301+ else :
302+ print (f">>> Using { config ['name' ]} toolchain profile." )
303+
304+ cc_name = config ['cc_name' ]
305+ cc_path = shutil .which (cc_name )
306+
307+ if not cc_path :
308+ print (f"!!! ERROR: Toolchain C compiler '{ cc_name } ' not found in your system PATH." )
309+ print (f"Please ensure the { config ['name' ]} toolchain is installed and its 'bin' directory is added to PATH." )
310+ sys .exit (1 )
311+
312+ print (f" Found CC: { cc_path } (Using absolute path)" )
313+ toolchain_bindir = os .path .dirname (cc_path )
314+
315+ toolchain_tools = {
316+ 'CC' : os .path .abspath (cc_path ),
317+ 'CXX' : os .path .abspath (os .path .join (toolchain_bindir , config ['cxx_name' ])),
318+ 'AR' : os .path .abspath (os .path .join (toolchain_bindir , config ['ar_name' ])),
319+ }
320+ print (f" Using CXX: { toolchain_tools ['CXX' ]} (Using absolute path)" )
321+ print (f" Using AR: { toolchain_tools ['AR' ]} (Using absolute path)" )
322+
323+ toolchain_cmake_args .extend ([
324+ '-D' , f"CMAKE_C_COMPILER={ toolchain_tools ['CC' ]} " ,
325+ '-D' , f"CMAKE_CXX_COMPILER={ toolchain_tools ['CXX' ]} " ,
326+ '-D' , f"CMAKE_AR={ toolchain_tools ['AR' ]} " ,
327+ ])
328+
329+ all_cmake_extra_args = toolchain_cmake_args
330+
263331 valid , jsoncfg = load_json (args .config )
264332
265333 if valid != JSON_OK :
@@ -272,7 +340,7 @@ def install_library(libsrc, buildcfgs:dict, aliascfgs:dict, libprefix, libroot,
272340
273341 buildcfgs = get_buildcfgs (jsoncfg )
274342 aliascfgs = get_aliascfgs (jsoncfg )
275- runrst = install_library (args .lib_src , buildcfgs , aliascfgs , args .lib_prefix , args .lib_root , args .target , args .strip , args .parallel , args .ignore_fail , args .norebuild )
343+ runrst = install_library (args .lib_src , buildcfgs , aliascfgs , args .lib_prefix , args .lib_root , args .target , args .strip , args .parallel , args .ignore_fail , args .norebuild , all_cmake_extra_args )
276344 print ("Build Library %s with config %s, generated into %s status: %s" % (args .lib_src , args .config , args .lib_root , runrst ))
277345 if args .norebuild :
278346 print ("!!!Use Caution: This build is not fully rebuilt, please take care!!!!" )
0 commit comments