import os
def check_error(log_path):
with open(log_path, 'r', encoding='utf-8') as temp_f:
datafile = temp_f.readlines()
for line in datafile:
if '.h: No such file or directory' in line:
return True
return False
# 在make命令下报缺少头文件的头文件依次添加进去,直到没有报缺头文件的错误
def add_header():
while 1:
res = os.system("make -j20 SRC_DIR=../../src/ipbase/hrs/src/common/session_lib > ./temp_file/log.txt 2>&1") # make命令以及需要重定向的文件,2>&1是把错误信息也打印进去
print(res)
log_path = './temp_file/log.txt'
if not check_error(log_path): #判断跳出条件
break
os.system("cat ./temp_file/log.txt | grep \#include > ./temp_file/log1.txt") #
fname = './temp_file/log1.txt'
with open(fname, 'r', encoding='utf-8') as f: # 打开文件
first_line = f.readline()
header =first_line.split("\"")[-2]
print(header)
cmd = f'find /usr3/code/VRP/ -name {header} > ./temp_file/header_path.txt' # 拼接命令
res1 = os.system(cmd)
print(res1)
path_file = './temp_file/header_path.txt'
with open(path_file, 'r', encoding='utf-8') as f1: # 打开文件
first_line1 = f1.readline().strip()
copy_cmd = f'cp {first_line1} ./../../src/ipbase/hrs/src/common/session_lib'
res2 = os.system(copy_cmd)
print(res2)
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
# print_hi('PyCharm')
add_header()
自动添加所缺头文件脚本
最新推荐文章于 2022-11-30 17:35:34 发布
该脚本通过读取make命令的错误输出,检测缺失的头文件,并自动从/usr3/code/VRP目录中查找并复制到指定源代码目录,以解决编译错误。它使用grep和正则表达式定位头文件,然后使用find命令找到文件路径并进行复制。
摘要由CSDN通过智能技术生成