IAR 工程中使用 PowerShell 脚本实现 HEX 文件自动版本重命名
IAR 工程中使用 PowerShell 脚本实现 HEX 文件自动版本重命名
概述
本方案介绍如何在 IAR Embedded Workbench 中使用 PowerShell 脚本实现编译后自动为 HEX 文件添加版本信息的功能。通过配置 Post-build 命令和自定义变量,可以自动为生成的 HEX 文件添加包含年、月、日和小版本号的后缀,并在 C/C++ 代码中使用这些版本号定义。
功能特点
- 自动为 HEX 文件添加版本信息
- 支持十六进制和十进制版本号输入
- 在 C/C++ 代码中可使用版本号宏定义
- 完整的错误处理和验证机制
- 与 IAR 工程无缝集成
PowerShell 脚本
创建 post-build.ps1 文件并保存到工程目录:
param(
[Parameter(Mandatory=$true)]
[string]$HexDir,
[Parameter(Mandatory=$true)]
[string]$HexName,
[int]$Year = 0x25,
[int]$Month = 0x11,
[int]$Day = 0x21,
[int]$Revision = 0x01
)
function Format-HexValue {
param([int]$Value)
return "{0:X2}" -f $Value
}
Write-Host "=== PowerShell Hex File Processor ==="
Write-Host ""
Write-Host "Input parameters:"
Write-Host "Hex Directory: '$HexDir'"
Write-Host "Hex File Name: '$HexName'"
Write-Host "Year version (SE_SOFTRELEASE0): 0x$(Format-HexValue $Year) ($Year)"
Write-Host "Month version (SE_SOFTRELEASE1): 0x$(Format-HexValue $Month) ($Month)"
Write-Host "Day version (SE_SOFTRELEASE2): 0x$(Format-HexValue $Day) ($Day)"
Write-Host "Revision version (SE_SOFTRELEASEDOT): 0x$(Format-HexValue $Revision) ($Revision)"
Write-Host ""
Write-Host "Defined constants:"
Write-Host "#define SE_SOFTRELEASE0 0x$(Format-HexValue $Year)"
Write-Host "#define SE_SOFTRELEASE1 0x$(Format-HexValue $Month)"
Write-Host "#define SE_SOFTRELEASE2 0x$(Format-HexValue $Day)"
Write-Host "#define SE_SOFTRELEASEDOT 0x$(Format-HexValue $Revision)"
Write-Host ""
Write-Host "File processing:"
try {
# Build full source path
$HexFilePath = Join-Path $HexDir $HexName
Write-Host "Source file path: '$HexFilePath'"
# Check if source file exists
if (-not (Test-Path $HexFilePath)) {
Write-Host "ERROR: Source file does not exist!" -ForegroundColor Red
exit 1
}
# Get file information
$FileInfo = Get-Item $HexFilePath
$Name = $FileInfo.BaseName
$Ext = $FileInfo.Extension
# Format version components
$YearStr = Format-HexValue $Year
$MonthStr = Format-HexValue $Month
$DayStr = Format-HexValue $Day
$RevisionStr = Format-HexValue $Revision
# Build new filename
$NewFilename = "${Name}_${YearStr}_${MonthStr}_${DayStr}_${RevisionStr}${Ext}"
$NewFilePath = Join-Path $HexDir $NewFilename
Write-Host "Target file name: '$NewFilename'"
Write-Host "Target file path: '$NewFilePath'"
# Copy file with new name
Write-Host "Copying file..." -NoNewline
Copy-Item -Path $HexFilePath -Destination $NewFilePath
Write-Host " DONE" -ForegroundColor Green
# Verify the copy was successful
if (Test-Path $NewFilePath) {
$SourceSize = (Get-Item $HexFilePath).Length
$TargetSize = (Get-Item $NewFilePath).Length
Write-Host "Copy verification:" -ForegroundColor Green
Write-Host " Source file size: $SourceSize bytes"
Write-Host " Target file size: $TargetSize bytes"
if ($SourceSize -eq $TargetSize) {
Write-Host " SUCCESS: Files are identical in size" -ForegroundColor Green
} else {
Write-Host " WARNING: File sizes differ!" -ForegroundColor Yellow
}
} else {
Write-Host "ERROR: Copy operation may have failed - target file not found!" -ForegroundColor Red
exit 1
}
Write-Host ""
Write-Host "Operation completed successfully!" -ForegroundColor Green
}
catch {
Write-Host "ERROR: $($_.Exception.Message)" -ForegroundColor Red
exit 1
}
IAR 工程配置
1. 配置自定义变量
在 IAR 项目中,打开 Project > Options > Customize,点击 Configure Custom Argument Variables 按钮,添加以下自定义变量:

| 变量名 | 值 | 描述 |
|---|---|---|
| SE_YEAR | 0x25 | 年份版本号 (十六进制) |
| SE_MONTH | 0x11 | 月份版本号 (十六进制) |
| SE_DAY | 0x27 | 日期版本号 (十六进制) |
| SE_VERSION | 0x01 | 小版本号 (十六进制) |
| SE_SOFTRELEASE0 | $SE_YEAR$ | 年份版本号宏定义 |
| SE_SOFTRELEASE1 | $SE_MONTH$ | 月份版本号宏定义 |
| SE_SOFTRELEASE2 | $SE_DAY$ | 日期版本号宏定义 |
| SE_SOFTRELEASEDOT | $SE_VERSION$ | 小版本号宏定义 |
2. 配置全局宏定义
在 Project > Options > C/C++ Compiler > Preprocessor 中的 Defined symbols 字段中添加以下全局宏定义:

SE_SOFTRELEASE0=$SE_SOFTRELEASE0$
SE_SOFTRELEASE1=$SE_SOFTRELEASE1$
SE_SOFTRELEASE2=$SE_SOFTRELEASE2$
SE_SOFTRELEASEDOT=$SE_SOFTRELEASEDOT$
VERSION=$SE_SOFTRELEASE0$
3. 配置 Post-build 命令
在 Project > Options > Build Actions 中的 Post-build command line 字段中输入:

powershell -ExecutionPolicy Bypass -File "$PROJ_DIR$\post-build.ps1" -HexDir "$EXE_DIR$" -HexName "LED_Demo.hex" -Year $SE_YEAR$ -Month $SE_MONTH$ -Day $SE_DAY$ -Revision $SE_VERSION$
4. 变量说明
$PROJ_DIR$- IAR 工程文件所在目录$EXE_DIR$- 输出文件目录$SE_YEAR$,$SE_MONTH$,$SE_DAY$,$SE_VERSION$- 自定义版本变量$SE_SOFTRELEASE0$,$SE_SOFTRELEASE1$,$SE_SOFTRELEASE2$,$SE_SOFTRELEASEDOT$- 宏定义变量
在 C/C++ 代码中使用版本号
配置完成后,您可以在 C/C++ 代码中直接使用这些宏定义:
#include <stdio.h>
// 这些宏定义已经在 IAR 的预处理器中定义
// SE_SOFTRELEASE0, SE_SOFTRELEASE1, SE_SOFTRELEASE2, SE_SOFTRELEASEDOT
// VERSION (等于 SE_SOFTRELEASE0)
void print_version_info(void)
{
printf("Firmware Version: %d.%d.%d.%d\n",
SE_SOFTRELEASE0, SE_SOFTRELEASE1, SE_SOFTRELEASE2, SE_SOFTRELEASEDOT);
printf("Major Version: %d\n", VERSION);
// 十六进制格式输出
printf("Version (Hex): 0x%02X.0x%02X.0x%02X.0x%02X\n",
SE_SOFTRELEASE0, SE_SOFTRELEASE1, SE_SOFTRELEASE2, SE_SOFTRELEASEDOT);
}
// 在代码中根据版本号执行不同逻辑
void version_specific_function(void)
{
#if (VERSION >= 0x25)
// 版本 0x25 及以上特有的功能
printf("This feature is available in version 0x25 and above\n");
#else
// 旧版本的功能
printf("Using legacy feature for versions below 0x25\n");
#endif
}
使用说明
1. 版本号格式
- 版本号支持十六进制(0x前缀)和十进制格式
- 脚本会自动将版本号格式化为两位十六进制数字
- 例如:0x25 会格式化为 "25",0xB 会格式化为 "0B"
2. 文件命名规则
原始文件:LED_Demo.hex
重命名后:LED_Demo_25_11_27_01.hex
格式:原文件名_年_月_日_小版本号.hex
3. 对应常量定义
脚本中定义的常量与版本参数对应关系:
SE_SOFTRELEASE0- 年份 (Year)SE_SOFTRELEASE1- 月份 (Month)SE_SOFTRELEASE2- 日期 (Day)SE_SOFTRELEASEDOT- 小版本号 (Revision)VERSION- 主要版本号 (等于 SE_SOFTRELEASE0)
注意事项
-
PowerShell 执行策略:确保系统允许执行 PowerShell 脚本,或在命令中使用
-ExecutionPolicy Bypass参数 -
文件编码:确保 PowerShell 脚本保存为 ANSI 或 UTF-8 without BOM 编码
-
路径问题:如果路径包含空格,确保使用双引号括起来
-
文件验证:脚本包含文件大小验证,确保复制操作成功
-
宏定义更新:修改自定义变量后,需要重新编译才能使新的宏定义生效
扩展应用
此方案可以扩展用于:
- 自动生成带版本号的固件文件
- 在代码中根据版本号执行不同逻辑
- 批量处理多个 HEX 文件
- 集成到持续集成/持续部署流程中
- 与其他构建工具链集成
通过此方案,可以有效地管理和追踪不同版本的固件文件,并在代码中使用版本信息,提高开发效率。
浙公网安备 33010602011771号