2525import os
2626import sys
2727import string
28+ import shutil
2829
2930import xml .etree .ElementTree as etree
3031from xml .etree .ElementTree import SubElement
@@ -337,7 +338,26 @@ def MDK4Project(target, script):
337338 if os .path .exists ('template.uvopt' ):
338339 import shutil
339340 shutil .copy2 ('template.uvopt' , '{}.uvopt' .format (os .path .splitext (target )[0 ]))
340-
341+ import threading
342+ import time
343+ def monitor_log_file (log_file_path ):
344+ if not os .path .exists (log_file_path ):
345+ open (log_file_path , 'w' ).close ()
346+ empty_line_count = 0
347+ with open (log_file_path , 'r' ) as log_file :
348+ while True :
349+ line = log_file .readline ()
350+ if line :
351+ print (line .strip ())
352+ if 'Build Time Elapsed' in line :
353+ break
354+ empty_line_count = 0
355+ else :
356+ empty_line_count += 1
357+ time .sleep (1 )
358+ if empty_line_count > 30 :
359+ print ("Timeout reached or too many empty lines, exiting log monitoring thread." )
360+ break
341361def MDK5Project (target , script ):
342362
343363 if os .path .isfile ('template.uvprojx' ) is False :
@@ -356,6 +376,22 @@ def MDK5Project(target, script):
356376 if os .path .exists ('template.uvoptx' ):
357377 import shutil
358378 shutil .copy2 ('template.uvoptx' , '{}.uvoptx' .format (os .path .splitext (target )[0 ]))
379+ # build with UV4.exe
380+
381+ if shutil .which ('UV4.exe' ) is not None :
382+ target_name = template_tree .find ('Targets/Target/TargetName' )
383+ print ('target_name:' , target_name .text )
384+ log_file_path = 'keil.log'
385+ if os .path .exists (log_file_path ):
386+ os .remove (log_file_path )
387+ log_thread = threading .Thread (target = monitor_log_file , args = (log_file_path ,))
388+ log_thread .start ()
389+ cmd = 'UV4.exe -b project.uvprojx -q -j0 -t ' + target_name .text + ' -o ' + log_file_path
390+ print ('Start to build keil project' )
391+ print (cmd )
392+ os .system (cmd )
393+ else :
394+ print ('UV4.exe is not available, please check your keil installation' )
359395
360396def MDK2Project (target , script ):
361397 template = open ('template.Uv2' , "r" )
0 commit comments