简体   繁体   English

自动将 git 版本(git describe)添加到 Eclipse 中的 C 代码字符串(STM32CubeIDE)

[英]automatically add git version (git describe) to C code string in Eclipse (STM32CubeIDE)

How do I automatically add git version (git describe) to C code string in Eclipse (STM32CubeIDE)?如何自动将 git 版本(git describe)添加到 Eclipse(STM32CubeIDE)中的 C 代码字符串?

This is pretty straightforward when using custom makefiles:这在使用自定义 makefile 时非常简单:

exe:
    @touch ./myMainProgram.c
    @echo -n "#define GIT_VERSION  \""                  >  ./git_ver.h
    @echo -n `git describe --abbrev=7 --dirty --always` >> ./git_ver.h
    @echo "\""                                          >> ./git_ver.h

And in your C code, you include git_ver.h and print the version with something like printf("Version [%s]\n", GIT_VERSION);在您的 C 代码中,您包含git_ver.h并使用类似printf("Version [%s]\n", GIT_VERSION);

WARNING: Do not use git to check in and track git_ver.h because any time it changes, git describe will identify your release as "dirty".警告:不要使用 git 签入和跟踪git_ver.h ,因为任何时候它发生变化, git describe都会将您的版本标识为“脏”。

But in Eclipse (STM32CubeIDE) I can't edit the makefile directly because it is auto-generated.但是在 Eclipse (STM32CubeIDE) 我不能直接编辑 makefile 因为它是自动生成的。 How do I accomplish the same thing?我如何完成同样的事情?

By looking at the auto-generated makefile , you'll notice this section:通过查看自动生成的makefile ,您会注意到以下部分:

-include ../makefile.defs

You can create your own makefile.defs file and add the following您可以创建自己的makefile.defs文件并添加以下内容

OBJS += doGitVersion

doGitVersion:
    @touch ./myMainProgram.c
    @echo -n "#define GIT_VERSION  \""                  >  ./git_ver.h
    @echo -n `git describe --abbrev=7 --dirty --always` >> ./git_ver.h
    @echo "\""                                          >> ./git_ver.h

By the way, the @touch is there to force a re-compile of myMainProgram.c (and thereby refreshing the value of GIT_VERSION in the compiled binary).顺便说一句, @touch可以强制重新编译myMainProgram.c (从而刷新已编译二进制文件中GIT_VERSION的值)。 If you don't do this, often build systems will not actually re-compile code if nothing has changed.如果你不这样做,如果没有任何改变,通常构建系统实际上不会重新编译代码。

A typical situation is that you do a git commit, re-build, and re-test.一个典型的情况是您执行 git 提交、重新构建和重新测试。 Everything looks good, so you tag the release with v1.0.2 and re-build.一切看起来都不错,因此您使用v1.0.2标记发布并重新构建。 If you don't have the @touch line, since the build system detects that nothing has changed, it won't actually re-compile anything and you might have something like v1.0.1-1-gfe093cd left in the compiled binary for the value of GIT_VERSION .如果你没有@touch行,因为构建系统检测到没有任何变化,它实际上不会重新编译任何东西,你可能会在编译的二进制文件中留下类似v1.0.1-1-gfe093cd的东西GIT_VERSION的值。

By using the @touch line, you'll assure that myMainProgram.c gets re-compiled and GIT_VERSION reflects exactly what git describe --abbrev=7 --dirty --always returns.通过使用@touch行,您将确保myMainProgram.c被重新编译,并且GIT_VERSION准确地反映了git describe --abbrev=7 --dirty --always返回的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM