高级会员

- 积分
- 907
- 金钱
- 907
- 注册时间
- 2011-10-19
- 在线时间
- 196 小时
|
发表于 2020-12-2 21:39:15
|
显示全部楼层
用时间做软件版本其实作用不大。
真正的软件版本控制是要用git之类的软件版本控制系统,并且软件代码的版本号要有两个特性:
- 软件版本号能够要编译时自动生成;
- 软件版本号能够跟版本控制系统挂钩。
git版本控制系统在软件发布时,会对代码仓库用git tag命令打一个标签,如:V0.3,而用命令git describe可以获取代码仓库的直观的版本标记,如:V0.3-15-ga39d965。
于是,就可以要写一个脚本基于git describe命令生成软件版本号头文件,如:version.h,而在IDE的编译步骤中设置在编译工程前执行这个脚本,即可实现这两个特性。
获取生成软件版本号的脚本如下:
- #!/usr/bin/python
- import sys
- import os
- import time
- version_file_fmt = '''#ifndef VERSION_H_
- #define VERSION_H_
- #define VERSION "{version}"
- #define BUILD "{build}"
- #define CONFIG "{config}"
- #endif /* VERSION_H_ */
- '''
- filePath = sys.argv[1]
- print("Generating: {}".format(filePath))
- if len(sys.argv) > 2:
- config = sys.argv[2]
- else:
- config = ""
- cmd = "git describe --always --dirty"
- output = os.popen(cmd)
- describe = output.read()
- lt = time.localtime()
- if 'dirty' in describe:
- dirty = time.strftime("%Y%m%d%H%M", lt)
- version = describe.replace('dirty', dirty)
- else:
- version = describe
- version = version.replace('\n', '')
- build = time.strftime("%Y-%m-%d %H:%M:%S", lt)
- version_file_string = version_file_fmt.format(version=version, build=build, config=config)
- print("version: {}\nbuild : {}\nconfig : {}".format(version, build, config))
- with open(filePath, 'w') as version_file:
- version_file.write(version_file_string)
复制代码 在STM32CubeIDE的编译设置为:工程属性 -> C/C++ Bulid -> Settings -> Build Steps -> Pre-build steps 选项中运行如下命令:
- python ../../version.py ../Core/Inc/version.h ${config_name:USB_Camera}
复制代码
其生成的头文件 version.h 如下:
- #ifndef VERSION_H_
- #define VERSION_H_
- #define VERSION "V0.3-15-ga39d965-202011290001"
- #define BUILD "2020-11-29 00:01:41"
- #define CONFIG "Release"
- #endif /* VERSION_H_ */
复制代码
|
|