硬汉嵌入式论坛

 找回密码
 立即注册
查看: 933|回复: 8
收起左侧

[MDK] 使用PY和MDK支持多目标文件生成

[复制链接]

12

主题

60

回帖

96

积分

初级会员

积分
96
发表于 2024-10-16 20:01:24 | 显示全部楼层 |阅读模式
在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)


回复

使用道具 举报

0

主题

12

回帖

12

积分

新手上路

积分
12
发表于 2024-10-16 23:06:26 | 显示全部楼层
不知所云,在干啥
回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
116230
QQ
发表于 2024-10-17 09:51:11 | 显示全部楼层
谢谢楼主分享。
回复

使用道具 举报

97

主题

537

回帖

843

积分

金牌会员

积分
843
发表于 2024-10-17 10:14:57 | 显示全部楼层

用.h里面的宏定义来构建输出的文件名和时间。
共产主义一定胜利!
回复

使用道具 举报

97

主题

537

回帖

843

积分

金牌会员

积分
843
发表于 2024-10-17 10:46:50 | 显示全部楼层

demo.7z (6.68 MB, 下载次数: 11)
你可以参考一下这个工程,在硬汉哥的例程基础上改的。
我和楼主的有点不一样。我一部分用了python一部分用了bat。
楼主是全部都是python。

实际上全部使用bat脚本来实现的也可以。这样子就不用装python环境了。
我有个同事他的脚本就是全用bat写的。

然后我还提供了一个代码格式化的脚本,在clang-format文件夹里面。
共产主义一定胜利!
回复

使用道具 举报

12

主题

60

回帖

96

积分

初级会员

积分
96
 楼主| 发表于 2024-10-17 17:03:22 | 显示全部楼层
会飞的猪_2020 发表于 2024-10-17 10:14
用.h里面的宏定义来构建输出的文件名和时间。

I have two main implementation plans:
1. Read .config.h. If there is hardware_name in it, only use this to generate the hex file.
2. If hardware_name, use the name of the generated .hex as hardware_name

为什么有第二个需求
因为MDK是有给功能是多目标编译,可以选择性编译
例如A项目是带7寸液晶屏,B项目是带10寸液晶屏,其他都一样的时候
MDK可以通过配置,直接生成不同的HEX文件
而由于文件名在AC6的时候不会生成预编译文件,只能使用output的名称作为对应配置的文件名了
这样就能直接一键编译生成生产文件

上面的英文为翻译软件突然帮我转换了,懒得码字了,将就看吧
懂的都懂,不懂的也不会用到
回复

使用道具 举报

11

主题

694

回帖

727

积分

金牌会员

积分
727
发表于 2024-10-18 09:29:03 | 显示全部楼层
需要安装PYTHON吧?要求版本是多少?谢谢。
回复

使用道具 举报

12

主题

60

回帖

96

积分

初级会员

积分
96
 楼主| 发表于 2024-10-18 11:18:32 | 显示全部楼层
hqgboy 发表于 2024-10-18 09:29
需要安装PYTHON吧?要求版本是多少?谢谢。

需要安装win的python
用的是3.13.0版本

修改后,可以自行生成EXE,就不用安装环境了
回复

使用道具 举报

11

主题

694

回帖

727

积分

金牌会员

积分
727
发表于 2024-10-18 16:12:30 | 显示全部楼层
flying1104 发表于 2024-10-18 11:18
需要安装win的python
用的是3.13.0版本

谢谢。。。。。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2025-6-11 06:33 , Processed in 0.633745 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表