在AC6里面不能没有预编译文件,因此多目标文件生成后,永远只会使用.h里面的定义,而不会根据预处理来更换文件名
感谢坛友“会飞的猪_2020”的指引,
采用python的方法,通过读取生成的HEX文件名和获取最新时间来判别,添加版本号,添加编译日期,不用每次都自己修改文件名
具体看代码注释,根据自己的项目修改,文件目录使用的是硬汉的架构,放在MDK目录下
[Python] 纯文本查看 复制代码 #!/usr/bin/python3
# -*- coding: utf-8 -*-
import os
import sys
import re
import datetime
# 相对于MDK-ARM(AC6)工程根目录的路径
h_file_path = '../../User/main/configure.h'
source_dir = r'.\Objects\\'
target_dir = r'..\Hex\\'
# 获取当前日期,格式为:YYYYMMDD
current_date = datetime.datetime.now().strftime('%Y%m%d')
# 获取当前时间,格式为:HHMMSS
current_time = datetime.datetime.now().strftime('%H%M%S')
# 读取version.h文件并提取版本号,更新日期
with open(h_file_path, 'r', encoding='utf-8') as h_file:
content = h_file.read()
version_match = re.search(r'SOFEWARE_VERSION\s+"(V+\d+\.\d+\.\d+)"', content)
if version_match:
version_info = version_match.group(1)
else:
version_info = None
print('Can not find version in configure.h')
# 捕获任何双引号内非双引号的字符
board_name_match = re.search(r'HARDWARE_NAME\s+"([^"]+)"', content)
if board_name_match:
board_name_info = board_name_match.group(1)
else:
board_name_info = None
print('Can not find board name in configure.h')
# 读取source_dir目录下 .hex 后缀对应的文件名,并提取其修改日期在多个hex文件中查找最新的修改日期对应的文件名和修改时间,并返回文件名和修改时间
def get_file_name_and_modify_time():
hex_files = [f for f in os.listdir(source_dir) if f.endswith('.hex')]
modify_time_list = []
for hex_file in hex_files:
hex_file_path = source_dir + hex_file
modify_time = os.path.getmtime(hex_file_path)
modify_time = datetime.datetime.fromtimestamp(modify_time).strftime('%Y%m%d%H%M%S')
print('Hex file: ' + hex_file + ' Modify time: ' + modify_time)
modify_time_list.append((hex_file, modify_time))
modify_time_list.sort(key=lambda x: x[1], reverse=True)
print('New Hex file: ' + modify_time_list[0][0] + ' Modify time: ' + modify_time_list[0][1])
global file_name_info
file_name_info = modify_time_list[0][0].split('.')[0]
global modify_time_info
modify_time_info = modify_time_list[0][1]
# 复制hex文件到指定目录
def copy_hex_file(hex_file_path, new_hex_file_path):
# 检查目标目录是否存在,不存在则创建
if not os.path.exists(target_dir):
os.makedirs(target_dir)
os.system('copy ' + hex_file_path + ' ' + new_hex_file_path)
print('copy ' + hex_file_path + ' ' + new_hex_file_path)
print('Hex file path: ' + hex_file_path)
print('Copy hex file to: ' + new_hex_file_path)
# 运行
if __name__ == '__main__':
print('#' * 10, '调用扩展脚本开始', '#' * 10)
if version_info:
print('Version: ' + version_info)
if board_name_info:
print('Name: ' + board_name_info)
hex_file_path = source_dir + board_name_info + '.hex'
new_hex_file_path = target_dir + board_name_info + '_' + version_info + '(' + current_date + current_time + ')' + '.hex'
copy_hex_file(hex_file_path, new_hex_file_path)
else:
get_file_name_and_modify_time()
if file_name_info and modify_time_info:
hex_file_path = source_dir + file_name_info + '.hex'
new_hex_file_path = target_dir + file_name_info + '_' + version_info + '(' + modify_time_info + ')' + '.hex'
copy_hex_file(hex_file_path, new_hex_file_path)
else:
print('Can not get file name or modify time')
else:
print('Can not get version or name')
print('#' * 10, '调用扩展脚本结束', '#' * 10)
|