芮梦月 发表于 2025-6-8 12:31:16

Keil一键添加.c文件和头文件路径脚本--可遍历添加整个文件夹

最近想移植个LVGL玩玩,发现文件实在是太多了,加的手疼都没搞完,实在不想搞了就去找脚本和工具,基本没找到一个。。。。。。
主要是自己也懒得去研究写脚本,偶然搜到了一个博主写的脚本,原博客地址:https://blog.csdn.net/riyue2044/article/details/139424599
但是有以下问题:
1.这个脚本的.h文件也加在了分组下面,这样一般是不对的,应该加在Target的C/C++的Include path里面
2.脚本没有重复添加检测,导致如果多次添加,会损坏工程文件
3.输入是命令行式的,使用者可能会忘了参数具体设置
 
之前没接触过XML,python也不熟,所以研究了一下,做以下修改
1.把之前的命令行式的输入改为先运行再输入,会提示具体的参数设置,有默认参数,是以我的工程包来写的
2.把.h文件路径直接加在了Target的C/C++的Include path里面
3.加入文件路径检测,重复添加不会导致文件损坏
4.加入更多提示
5.加入三种模式    0:.c文件和.h路径会一起添加 1:只加.c文件 2:只加.h路径
 
使用方法:需要安装python,或者用python打包成exe文件也可,命令参考:pyinstaller -F -i .\icon.ico .\keil_add_file.py,放个百度云的链接,里面有我打包好的,不过注意杀毒软件估计会报毒,请添加信任
链接:https://pan.baidu.com/s/1zC7kVboAtQwHZ2Zy5RFmIw?pwd=arzd
提取码:arzd
脚本需放在keil工程目录,需要添加的目录则以相对路径填充,比如"../../../external/lvgl",需要注意的是分组需要提前在keil里面创建好,这个懒得改了,有需要的朋友可以自行修改
脚本内容如下:
1 import os
2 import glob
3 import xml.etree.ElementTree as ET
4 import argparse
5
6 from multiprocessing import Event
7
8 def indent(elem, level=0):
9   """ Helper function to indent the XML for pretty printing. """
10   i = "\n" + level * "    "
11   if len(elem):
12         if not elem.text or not elem.text.strip():
13             elem.text = i + "    "
14         if not elem.tail or not elem.tail.strip():
15             elem.tail = i
16         for elem in elem:
17             indent(elem, level + 1)
18         if not elem.tail or not elem.tail.strip():
19             elem.tail = i
20   else:
21         if level and (not elem.tail or not elem.tail.strip()):
22             elem.tail = i
23         if not elem.tail:
24             elem.tail = "\n"
25
26 def add_files_to_group(uvprojx_file_path, mode,folder_path, group_name_target):
27   # 改变文件扩展名从 .uvprojx 到 .xml
28   base, ext = os.path.splitext(uvprojx_file_path)
29   if ext != '.uvprojx':
30         print("工程文件扩展名不正确")
31         return
32   
33   xml_path = base + '.xml'
34   os.rename(uvprojx_file_path, xml_path)
35
36   try:
37         #解析XML文件
38         tree = ET.parse(xml_path)
39         #获取根节点
40         root = tree.getroot()
41
42         if mode == 0 or mode == 1:
43             # 找到指定GroupName的Group节点
44             target_group = None
45             for group in root.findall('.//Group'):
46               group_name = group.find('GroupName')
47               if group_name is not None and group_name.text == group_name_target:
48                     target_group = group
49                     break
50
51             if target_group is None:
52               print(f"未发现 '{group_name_target}' 分组,请先创建分组后再尝试")
53               # 将文件扩展名改回 .uvprojx
54               os.rename(xml_path, uvprojx_file_path)
55               return
56
57             # 找到目标 Group 节点下的 Files 节点,如果不存在则创建一个
58             files_node = target_group.find('Files')
59             if files_node is None:
60               files_node = ET.SubElement(target_group, 'Files')
61                                 
62         #寻找头文件分组
63         if mode == 0 or mode == 2:
64             print("寻找头文件分组......")
65             target_header = None
66             heard_inc = None
67             target_header = root.find('.//Cads')
68             if target_header == None:
69               print("未发现头文件分组Cads")
70               return
71             else:
72               heard_inc = target_header.find('VariousControls')
73               if heard_inc == None:
74                     print("未发现头文件分组VariousControls")
75                     return
76               else:
77                     heard_inc = heard_inc.find('IncludePath')
78                     if heard_inc == None:
79                         print("未发现头文件分组IncludePath")
80                         return
81                     else:
82                         print("找到头文件分组")
83                        
84         
85         
86         #下面没有节点
87         if mode == 0 or mode == 1:
88             creat_dot = 0 #是否需要创建节点标志,如果有重复则跳过
89             init_creat = 0
90             file_init = files_node.find('File')
91             if file_init == None:
92               creat_dot = 1
93               init_creat = 1
94               print("初始节点为空需要创建节点")
95   
96         # 遍历指定文件夹,查找所有 .c 文件
97         if mode == 0 or mode == 2:
98             #print(heard_inc.text)
99             heard_data = heard_inc.text + ";"   #末尾需要先加一个分号   
100            
101         for subdir, _, files in os.walk(folder_path):
102             #.h路径
103             if mode == 0 or mode == 2:
104               dir_path = os.path.relpath(subdir, start=os.path.dirname(xml_path))
105               if dir_path in heard_inc.text:
106                     print("需要添加的头文件路径已存在本次跳过")
107               else:
108                     heard_data = heard_data + dir_path + ";"
109                     
110             #.c添加到分组
111             if mode == 0 or mode == 1:
112               for file in files:   
113                     if file.endswith('.c'):
114                         # 计算相对路径
115                         file_path = os.path.relpath(os.path.join(subdir, file), start=os.path.dirname(xml_path))
116                         #print("路径",file_path)
117                         file_check = files_node.findall('File')
118                         if init_creat == 0:
119                           #print("长度",len(file_check))
120                           #遍历当前分组下的节点,检测是否已经包含了该路径,如果有直接跳过
121                           for i in range(len(file_check)):
122                                 if file_path in file_check.find("FilePath").text:
123                                     print("节点已存在本次跳过")
124                                     creat_dot = 0
125                                     break
126                                 else:
127                                     if i == len(file_check) - 1:
128                                       creat_dot = 1
129                                       print("节点不存在,创建节点")
130                                     else:
131                                        creat_dot = 0
132                                     continue   
133                         if creat_dot == 1:
134                           # 创建 File 节点并添加到 Files 节点下
135                           file_node = ET.SubElement(files_node, 'File')
136                           file_name_node = ET.SubElement(file_node, 'FileName')
137                           file_name_node.text = file
138                           file_type_node = ET.SubElement(file_node, 'FileType')
139                           file_type_node.text = '1'# .c 文件类型都为 1
140
141                           file_path_node = ET.SubElement(file_node, 'FilePath')
142                           file_path_node.text = file_path
143                           creat_dot = 0
144                           init_creat = 0
145                           
146         if mode == 0 or mode == 2:
147             heard_data = heard_data.rstrip(";") #移除最后一个多加的;
148             heard_inc.text = heard_data
149             #print(heard_inc.text)                  
150                  
151         # 格式化 XML
152         indent(root)
153
154         # 保存修改后的 XML 文件
155         tree.write(xml_path, encoding='utf-8', xml_declaration=True)
156         print("已完成")
157
158   except ET.ParseError as e:
159         print(f"ParseError: {e}")
160         with open(xml_path, 'r', encoding='utf-8') as file:
161             lines = file.readlines()
162             start = max(0, e.position - 5)
163             end = min(len(lines), e.position + 5)
164             print("Context around the error:")
165             for i in range(start, end):
166               print(f"{i+1}: {lines.strip()}")
167
168   finally:
169         # 将文件扩展名改回 .uvprojx
170         os.rename(xml_path, uvprojx_file_path)
171
172 #寻找工程文件
173 def find_uvprojx_file():
174   uvprojx_files = glob.glob("*.uvprojx")
175   if not uvprojx_files:
176         print("未找到工程文件,请把此文件放在keil工程目录下")
177         return None
178   elif len(uvprojx_files) > 1:
179         print("在当前目录中找到多个.uvprojx文件:")
180         for i, file in enumerate(uvprojx_files, start=1):
181             print(f"{i}. {file}")
182         print("请确保目录中只有一个.uvprojx文件")
183         return None
184   else:
185         return uvprojx_files
186
187 if __name__ == "__main__":
188   print("keil一键添加文件和头文件路径脚本\n\
189   需放在keil工程同级目录下\n\
190   参数格式,参数用空格隔开\n\
191   默认模式:0\n\
192   默认路径:\"../../../external/lvgl\"\n\
193   默认分组:\"lvgl\"\n\
194   1.添加模式 0:全部添加(.c文件全添加到分组.h文件夹加入include路径里) 1:只添加.c文件到分组 2:只添加.h文件夹到include里\n\
195   2.要添加的文件夹路径,请使用相对路径\n\
196   3.要添加的分组名称,如果没有分组需要先去keil手动添加分组\n")
197   
198   param = input("请输入参数:")
199   
200   if param:
201         #print(param)
202         args = param.split()
203         args = int(args)
204         print(args)
205   else:
206         args =
207         print("使用默认参数:",args)
208   uvprojx_file_path = find_uvprojx_file()
209   if uvprojx_file_path:
210         add_files_to_group(uvprojx_file_path, args,args,args)
211
212   event = Event()
213   event.wait() 

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: Keil一键添加.c文件和头文件路径脚本--可遍历添加整个文件夹