Files
kunlun/praper_vscode.py

103 lines
2.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json
import os
import sys
import time
'''
这个文件用于编译时输出编译符号在makefile.cfg中调用
自动更新vscode的c/c++配置
2024.9.28
根据脚本位置来确定位置文件路径
使用非冗余的路径
使用宏定义字符转义
'''
def unescape(line:str):
table=[("\\\"","\""),("\\<","<"),("\\>",">"),("\\$","$"),("\\(","("),("\\)",")")]
for item in table:
line=line.replace(item[0],item[1])
# 删除多余的 $ 符号
line=line.replace('$','')
return line
def main(flag_file:str,target:str):
make_flags=flag_file
top_path=os.path.split(sys.argv[0])[0]
current_path=os.path.abspath('.')
if(make_flags!='clear'):
with open(make_flags,encoding='utf-8') as f:
flags=f.read()
print(time.strftime("%Y-%m-%d %H:%M:%S"),"append vscode setting.")
print('\ttarget: ',target)
else:
flags=""
print(time.strftime("%Y-%m-%d %H:%M:%S"),"clear vscode setting.")
cfgs_file_path=f'{top_path}/.vscode/c_cpp_properties.json'
with open(cfgs_file_path,encoding='utf-8') as f:
cfgs=f.read()
flags_list=flags.split()
flags_list=list(set(flags_list))
inc_list=[]
def_list=[]
for item in flags_list:
if(item[:2]=='-D'):
def_list.append(unescape(item[2:]))
elif(item[:2]=='-I'):
p=item[2:]
if(p[0]!='/'):
p=os.path.join(current_path,p)
p=os.path.normpath(p)
if(os.path.exists(p)):
inc_list.append(p)
else:
print("\tinvalid inc path: ",p)
cfg=json.loads(cfgs)
predef_info=cfg["configurations"][0]
if(make_flags!='clear'):
predef_info["name"]="myname"
for item in inc_list:
# 只有路径列表中不存在时才添加
if(item not in predef_info["includePath"]):
predef_info["includePath"].append(item)
for item in def_list:
if(item not in predef_info["defines"]):
predef_info["defines"].append(item)
else:
predef_info["includePath"]=[]
predef_info["defines"]=[]
with open(cfgs_file_path,mode='w+', encoding='utf-8') as f:
f.write(json.dumps(cfg,sort_keys=True, indent=2, separators=(',', ': ')))
if __name__=="__main__":
if(len(sys.argv)>=2):
if(len(sys.argv)>=3):
target=sys.argv[2]
else:
target="null"
main(sys.argv[1],target)
else:
print("kill me.")