1+ import subprocess
2+ import threading
3+ import time
4+ import logging
5+ import sys
16import os
27import shutil
38import re
@@ -36,7 +41,7 @@ def run_cmd(cmd, output_info=True):
3641 return output_str_list , res
3742
3843
39- def build_bsp (bsp , scons_args = '' ,name = 'default' ):
44+ def build_bsp (bsp , scons_args = '' ,name = 'default' , pre_build_commands = None , post_build_command = None , build_check_result = None , bsp_build_env = None ):
4045 """
4146 build bsp.
4247
@@ -56,21 +61,41 @@ def build_bsp(bsp, scons_args='',name='default'):
5661
5762 """
5863 success = True
64+ # 设置环境变量
65+ if bsp_build_env is not None :
66+ print ("Setting environment variables:" )
67+ for key , value in bsp_build_env .items ():
68+ print (f"{ key } ={ value } " )
69+ os .environ [key ] = value # 设置环境变量
5970 os .chdir (rtt_root )
6071 os .makedirs (f'{ rtt_root } /output/bsp/{ bsp } ' , exist_ok = True )
6172 if os .path .exists (f"{ rtt_root } /bsp/{ bsp } /Kconfig" ):
6273 os .chdir (rtt_root )
63- run_cmd (f'scons -C bsp/{ bsp } --pyconfig-silent' , output_info = False )
74+ run_cmd (f'scons -C bsp/{ bsp } --pyconfig-silent' , output_info = True )
6475
6576 os .chdir (f'{ rtt_root } /bsp/{ bsp } ' )
66- run_cmd ('pkgs --update-force' , output_info = False )
77+ run_cmd ('pkgs --update-force' , output_info = True )
6778 run_cmd ('pkgs --list' )
6879
6980 nproc = multiprocessing .cpu_count ()
81+ if pre_build_commands is not None :
82+ print ("Pre-build commands:" )
83+ print (pre_build_commands )
84+ for command in pre_build_commands :
85+ print (command )
86+ output , returncode = run_cmd (command , output_info = True )
87+ print (output )
88+ if returncode != 0 :
89+ print (f"Pre-build command failed: { command } " )
90+ print (output )
7091 os .chdir (rtt_root )
92+ # scons 编译命令
7193 cmd = f'scons -C bsp/{ bsp } -j{ nproc } { scons_args } ' # --debug=time for debug time
72- __ , res = run_cmd (cmd , output_info = True )
73-
94+ output , res = run_cmd (cmd , output_info = True )
95+ if build_check_result is not None :
96+ if res != 0 or not check_output (output , build_check_result ):
97+ print ("Build failed or build check result not found" )
98+ print (output )
7499 if res != 0 :
75100 success = False
76101 else :
@@ -83,6 +108,13 @@ def build_bsp(bsp, scons_args='',name='default'):
83108 shutil .copy (file , f'{ rtt_root } /output/bsp/{ bsp } /{ name .replace ("/" , "_" )} .{ file_type [2 :]} ' )
84109
85110 os .chdir (f'{ rtt_root } /bsp/{ bsp } ' )
111+ if post_build_command is not None :
112+ for command in post_build_command :
113+ output , returncode = run_cmd (command , output_info = True )
114+ print (output )
115+ if returncode != 0 :
116+ print (f"Post-build command failed: { command } " )
117+ print (output )
86118 run_cmd ('scons -c' , output_info = False )
87119
88120 return success
@@ -158,7 +190,17 @@ def build_bsp_attachconfig(bsp, attach_file):
158190
159191 return res
160192
193+ def check_output (output , check_string ):
194+ """检查输出中是否包含指定字符串"""
195+ output_str = '' .join (output ) if isinstance (output , list ) else str (output )
196+ flag = check_string in output_str
197+ if flag == True :
198+ print ('Success: find string ' + check_string )
199+ else :
200+ print (output )
201+ print (f"::error:: can not find string { check_string } output: { output_str } " )
161202
203+ return flag
162204if __name__ == "__main__" :
163205 """
164206 build all bsp and attach config.
@@ -169,10 +211,12 @@ def build_bsp_attachconfig(bsp, attach_file):
169211 """
170212 failed = 0
171213 count = 0
214+ ci_build_run_flag = False
215+ qemu_timeout_second = 50
172216
173217 rtt_root = os .getcwd ()
174218 srtt_bsp = os .getenv ('SRTT_BSP' ).split (',' )
175-
219+ print ( srtt_bsp )
176220 for bsp in srtt_bsp :
177221 count += 1
178222 print (f"::group::Compiling BSP: =={ count } === { bsp } ====" )
@@ -207,26 +251,80 @@ def build_bsp_attachconfig(bsp, attach_file):
207251 continue
208252
209253 config_file = os .path .join (rtt_root , 'bsp' , bsp , '.config' )
210-
254+ # 在使用 pre_build_commands 之前,确保它被定义
255+ pre_build_commands = None
256+ build_command = None
257+ post_build_command = None
258+ qemu_command = None
259+ build_check_result = None
260+ commands = None
261+ check_result = None
262+ bsp_build_env = None
211263 for projects in yml_files_content :
212264 for name , details in projects .items ():
265+ # 如果是bsp_board_info,读取基本的信息
266+ if (name == 'bsp_board_info' ):
267+ print (details )
268+ pre_build_commands = details .get ("pre_build" ).splitlines ()
269+ build_command = details .get ("build_cmd" ).splitlines ()
270+ post_build_command = details .get ("post_build" ).splitlines ()
271+ qemu_command = details .get ("run_cmd" )
272+
273+ if details .get ("kconfig" ) is not None :
274+ if details .get ("buildcheckresult" ) is not None :
275+ build_check_result = details .get ("buildcheckresult" )
276+ else :
277+ build_check_result = None
278+ if details .get ("msh_cmd" ) is not None :
279+ commands = details .get ("msh_cmd" ).splitlines ()
280+ else :
281+ msh_cmd = None
282+ if details .get ("checkresult" ) is not None :
283+ check_result = details .get ("checkresult" )
284+ else :
285+ check_result = None
286+ if details .get ("ci_build_run_flag" ) is not None :
287+ ci_build_run_flag = details .get ("ci_build_run_flag" )
288+ else :
289+ ci_build_run_flag = None
290+ if details .get ("pre_build" ) is not None :
291+ pre_build_commands = details .get ("pre_build" ).splitlines ()
292+ if details .get ("env" ) is not None :
293+ bsp_build_env = details .get ("env" )
294+ else :
295+ bsp_build_env = None
296+ if details .get ("build_cmd" ) is not None :
297+ build_command = details .get ("build_cmd" ).splitlines ()
298+ else :
299+ build_command = None
300+ if details .get ("post_build" ) is not None :
301+ post_build_command = details .get ("post_build" ).splitlines ()
302+ if details .get ("run_cmd" ) is not None :
303+ qemu_command = details .get ("run_cmd" )
304+ else :
305+ qemu_command = None
213306 count += 1
214307 config_bacakup = config_file + '.origin'
215308 shutil .copyfile (config_file , config_bacakup )
309+ #加载yml中的配置放到.config文件
216310 with open (config_file , 'a' ) as destination :
217311 if details .get ("kconfig" ) is None :
312+ #如果没有Kconfig,读取下一个配置
218313 continue
219314 if (projects .get (name ) is not None ):
315+ # 获取Kconfig中所有的信息
220316 detail_list = get_details_and_dependencies ([name ],projects )
221317 for line in detail_list :
222318 destination .write (line + '\n ' )
223319 scons_arg = []
320+ #如果配置中有scons_arg
224321 if details .get ('scons_arg' ) is not None :
225322 for line in details .get ('scons_arg' ):
226323 scons_arg .append (line )
227324 scons_arg_str = ' ' .join (scons_arg ) if scons_arg else ' '
228325 print (f"::group::\t Compiling yml project: =={ count } ==={ name } =scons_arg={ scons_arg_str } ==" )
229- res = build_bsp (bsp , scons_arg_str ,name = name )
326+ # #开始编译bsp
327+ res = build_bsp (bsp , scons_arg_str ,name = name ,pre_build_commands = pre_build_commands ,post_build_command = post_build_command ,build_check_result = build_check_result ,bsp_build_env = bsp_build_env )
230328 if not res :
231329 print (f"::error::build { bsp } { name } failed." )
232330 add_summary (f'\t - ❌ build { bsp } { name } failed.' )
@@ -235,11 +333,15 @@ def build_bsp_attachconfig(bsp, attach_file):
235333 add_summary (f'\t - ✅ build { bsp } { name } success.' )
236334 print ("::endgroup::" )
237335
336+
238337 shutil .copyfile (config_bacakup , config_file )
239338 os .remove (config_bacakup )
240339
340+
341+
241342 attach_dir = os .path .join (rtt_root , 'bsp' , bsp , '.ci/attachconfig' )
242343 attach_list = []
344+ #这里是旧的文件方式
243345 for root , dirs , files in os .walk (attach_dir ):
244346 for file in files :
245347 if file .endswith ('attach' ):
0 commit comments