From fc339cf894abe16a1e69a3d7e59ca7a94e4d1e99 Mon Sep 17 00:00:00 2001 From: andy <1414772332@qq.com> Date: Fri, 13 Jun 2025 17:49:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9riscv=E7=9A=84=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- make_riscv.py | 143 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 132 insertions(+), 11 deletions(-) diff --git a/make_riscv.py b/make_riscv.py index 8073d31..2dadee9 100644 --- a/make_riscv.py +++ b/make_riscv.py @@ -27,6 +27,8 @@ CFLAG=[ "-msave-restore" ] +DEF=[] + INC=[ "-I./riscv/rtthread/libcpu/risc-v/common", "-I./riscv/rtthread/include", @@ -79,16 +81,135 @@ TARGET="riscv" OUTPUT="output" -if __name__ == "__main__": + + +def tran_path(path:str): + p=path.replace('\\','/') + p=p.replace('/','_') + if(p[0]=='.'): + return p[1:] + else: + return p + + +# 判断是否需要重新生成 +def check_rebuild(dst:str,src:list): + # print(f"src:{src}") + if(not os.path.exists(dst)): + return True + dst_time=os.path.getmtime(dst) + src_time=[] + for i in src: + src_time.append(os.path.getmtime(i)) + src_time.sort() + if(src_time[-1]>dst_time): + return True + # for item in src_time: + # if(item>dst_time): + # return True + return False + + + +# 读取.d文件,返回依赖文件列表 +def read_depend_files(name:str): + with open(name) as f: + lines=f.readlines() + t='' + for line in lines: + line=line.strip() + if(line[-1]=='\\'): + t+=line[:-1] + else: + t+=line + t=t.split(':')[-1].strip() + t=t.split(' ') + # print(f"依赖列表{t}") + return t + + +# 生成依赖关系 +def build_depend(src:list): + flags=f"{' '.join(INC)} {' '.join(DEF)} {' '.join(CFLAG)}" + for i in src: + name=tran_path(i).split('.')[0] + dst='.'.join([name,'d']) + dst=os.path.join(OUTPUT,dst) + if(check_rebuild(dst,[i])): + cmd=f"{CC} -MM {i} -o {dst} {flags}" + print(cmd) + ret=os.system(cmd) + if(ret): + exit() + else: + # print(f"{i} 没有更新源文件") + pass + +# 生成中间文件 +def build_object(src:list): + flags=f"{' '.join(INC)} {' '.join(DEF)} {' '.join(CFLAG)}" + for i in src: + name_l=tran_path(i).split('.') + name=name_l[0] + file_type=name_l[-1] + dst='.'.join([name,'o']) + dst=os.path.join(OUTPUT,dst) + cd='.'.join([name,'d']) + cmd = '' + if(file_type in ['c','.C']): + cd=os.path.join(OUTPUT,cd) + if(check_rebuild(dst,read_depend_files(cd))): + cmd=f"{CC} -c {i} -o {dst} {flags}" + else: + # print(f"{i} 没有更新依赖关系") + pass + elif(file_type in ['s','S','asm','ASM']): + if(check_rebuild(dst,[i])): + cmd=f"{CC} -c {i} -o {dst} {flags}" + else: + # print(f"{i} 没有更新依赖关系") + pass + if(len(cmd)>0): + print(cmd) + ret=os.system(cmd) + if(ret): + exit() + + +# 生成可执行文件 +def build_target(src:list): + flags=f"{' '.join(INC)} {' '.join(DEF)} {' '.join(CFLAG)}" + obj_list=[] + for i in src: + name=tran_path(i).split('.')[0] + obj_list.append(OUTPUT+'/'+'.'.join([name,'o'])) + dst=os.path.join(OUTPUT,TARGET)+".elf" + if(check_rebuild(dst,obj_list)): + cmd=f"{CC} {' '.join(obj_list)} -o {dst} {flags} -T{LD_FILE} -Wall -Wextra -nostartfiles -Wl,-Map,\"{OUTPUT}/{TARGET}.map\"" + print(cmd) + ret=os.system(cmd) + if(ret): + exit() + else: + # print(f"{dst} 没有更新的链接文件") + pass + + + +def main(): + global SRC + if not os.path.exists(OUTPUT): os.mkdir(OUTPUT) - cmd=f"{CC} {' '.join(CFLAG)} {' '.join(INC)} {' '.join(SRC)} -T{LD_FILE} -Wall -Wextra -nostartfiles -Wl,-Map,\"{TARGET}.map\" -o {TARGET}.elf" - print(cmd) - os.system(cmd) - os.system(f"{OBJCPY} -O binary {TARGET}.elf {TARGET}.bin") - os.system(f"{OBJCPY} -O ihex {TARGET}.elf {TARGET}.hex") - os.system(f"{OBJDUMP} -d {TARGET}.elf > {TARGET}.lst") - tagets_list=[f"{TARGET}.bin",f"{TARGET}.hex",f"{TARGET}.lst",f"{TARGET}.map",f"{TARGET}.elf"] - for item in tagets_list: - if os.path.exists(item): - shutil.move(item,f"{OUTPUT}/{item}") + + build_depend(SRC) + build_object(SRC) + build_target(SRC) + os.system(f"{OBJCPY} -O binary {OUTPUT}/{TARGET}.elf {OUTPUT}/{TARGET}.bin") + os.system(f"{OBJCPY} -O ihex {OUTPUT}/{TARGET}.elf {OUTPUT}/{TARGET}.hex") + os.system(f"{OBJDUMP} -d {OUTPUT}/{TARGET}.elf > {OUTPUT}/{TARGET}.lst") + + + +if __name__ == "__main__": + main()