方法的根本原理是在ccs项目中配置编译前预处理,通过脚本自动生成代码的节点信息,然后编译的时候将这些信息编入项目生成的.out文件中。

Step1:首先我们的项目工程必须要支持git进行软件的版本管理。新建DSP工程项目

项目工程版本管理我已经配置好了。

目前最新的信息如下:

c2b3283ef53043d489fa0f950495d613.png

Step2:在项目的根目录下创建如下工程路径(注意:需要将version排除项目的编译,否则报错):

d6463c129bb64dc784a8d9391c1a1f99.png

2a765f7a185c44068959f902f46663ea.png

其中git_script.c的功能是获取项目的git节点和时间信息生成对应的信息文件,其内容如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void CreatVersion_dot_c(char* commit_hash, char* commit_time);
void CreatVersionGitInfo(char* commit_hash, char* commit_time);

// 获取 Git 信息的函数
void get_git_info() {
    FILE *fp;
    char commit_hash[41];  // commit hash 长度为 40 个字符 + 1 个空终止符
    char commit_time[256]; // 用于存储 commit 时间的缓冲区

    // 获取当前的 commit hash
    fp = popen("git rev-parse HEAD", "r");
    if (fp == NULL) {
        perror("无法运行 git 命令");
        exit(1);
    }
    if (fgets(commit_hash, sizeof(commit_hash), fp) != NULL) {
        // 去掉字符串末尾的换行符
        commit_hash[strcspn(commit_hash, "\n")] = 0;
    }
    pclose(fp);

    // 获取 commit 时间
    fp = popen("git show -s --format=%ci HEAD", "r");
    if (fp == NULL) {
        perror("无法运行 git 命令");
        exit(1);
    }
    if (fgets(commit_time, sizeof(commit_time), fp) != NULL) {
        // 去掉字符串末尾的换行符
        commit_time[strcspn(commit_time, "\n")] = 0;
    }
    pclose(fp);

    // 将 commit hash 和时间写入到一个文件中
    CreatVersionGitInfo(commit_hash, commit_time);
    // 将 commit hash 和时间写入到一个文件中, 生成.c文件
//    CreatVersion_dot_c(commit_hash, commit_time);

    // 打印输出以便调试
    printf("\n当前编译版本信息:\n");
    printf("Commit hash: %s\n", commit_hash);
    printf("Commit time: %s\n", commit_time);
    printf("\n");

    return;
}

int main() {
    get_git_info();
    return 0;
}

// 生成version.c文件
void CreatVersion_dot_c(char* commit_hash, char* commit_time)
{
    FILE *output_file = fopen("../version.c", "w");
    if (output_file == NULL) {
        perror("无法打开文件");
        exit(1);
    }

    fprintf(output_file, "/*\n * 当前 commit hash: %s\n * 提交时间: %s\n\
 * 注:本文件每次编译前由脚本自动生成。(本地需安装Git)\n */\n", commit_hash, commit_time);

    fprintf(output_file, "static char s_gitVer[] = \"git:%s--avic609\";\n\n", commit_hash);
    fprintf(output_file, "static char s_gitTime[] = \"%s--avic609\";\n\n", commit_time);

    fprintf(output_file,"\
//\n\
// 获取软件的git节点\n\
char *GetVersion()\n\
{\n\
	return s_gitVer;\n\
}\n\n"							);

    fprintf(output_file,"\
//\n\
// 获取软件的git提交时间\n\
char *GetVersionTime()\n\
{\n\
	return s_gitTime;\n\
}\n\n"							);

    fclose(output_file);
    return;
}


// 生成git节点信息文件
void CreatVersionGitInfo(char* commit_hash, char* commit_time)
{
    FILE *output_file = fopen("../git_node.txt", "w");
    if (output_file == NULL) {
        perror("无法打开文件");
        exit(1);
    }

    fprintf(output_file, "/*\n * 当前 commit hash: %s\n * 提交时间: %s\n\
 * 注:本.c文件每次编译前由脚本自动生成。(本地需安装Git)\n */\n", commit_hash, commit_time);

    fprintf(output_file, "\"git:%s\"\n", commit_hash);
    fclose(output_file);

    output_file = fopen("../git_time.txt", "w");
    if (output_file == NULL) {
        perror("无法打开文件");
        exit(1);
    }
    fprintf(output_file, "\"%s\"\n", commit_time);
    fclose(output_file);

    return;
}

Script.bat脚本的作用是编译git_script.c并执行它,script.bat中的内容为:

:: 到本文件所在目录
cd /d "%~dp0"

:: 生成git节点信息
gcc git_script.c -o git_script.exe
git_script.exe

:: bat文件正常退出
exit /b 0

Step3:

配置CCS项目,在项目编译前执行我们的脚本

内容是: cmd /c "cd ${PROJECT_LOC}/version/script/ && script.bat"

点击Apply应用。

effe2bfbfbbd41aca6659a82cc866c01.png

Step4:

在项目创建version.c的文件

40708b4b94184ba2856dae49320d2686.png

内容如下:
代码中其他地方需要读取版本节点和版本编译时间信息,并怎么用(上报,在界面显示)看自己需求了。

/*
 * version.c
 *
 *  Created on: 2024-11-1
 *      Author:yl
 */


static char s_gitVer[] = {
	#include "../version/git_node.txt"
};

static char s_gitTime[] = {
	#include "../version/git_time.txt"
};


//
// 获取软件的git节点
char *GetVersion()
{
	return s_gitVer;
}

//
// 获取软件的git提交时间
char *GetVersionTime()
{
	return s_gitTime;
}

我们编译项目的时候就会额外生成git_script.exe和git_node.txt,git_time.txt文件。

c51643555fd44ab88e08bffec79ebf36.png

我们打开生成的文件查看,可以和git信息对应:

最后在进行项目管理的时候,在.gitignore文件中需要添加如下内容。因为下述的文件都是编译时自动生成的。

Debug/

.settings/

.launches/

.vscode/

version/script/git_script.exe

version/git_node.txt

version/git_time.txt

.project

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐