Skip to main content
Javier1
Principal
February 16, 2021
Solved

Git Commit Hash flashed along with my code?

  • February 16, 2021
  • 4 replies
  • 8461 views

Howdy everyone.

I need my MCU to be selfaware of what FW version is currently flashed,

so far im just manually typing the git commit hash inside a const string every time i change the FW version.

0693W000008GHL5QAO.pngIt looks to me like a feature that should be supported somewhere but i couldnt find a thing.

Is there a way to automate this process?

cheers

This topic has been closed for replies.
Best answer by Javier1

I found how:

From this post https://stackoverflow.com/questions/44038428/include-git-commit-hash-and-or-branch-name-in-c-c-source

I added the following command inside Pre-build steps:

git log --pretty=format:'#define GIT_INFO_PRESENT%n static const char* GIT_INFO = "Version Information=[%H,%d]\r\n";' -n 1 > ../Core/Inc/gitcommit.h

0693W00000BaGGpQAN.png 

Creates the following content inside gitcommit.h

#define GIT_INFO_PRESENT
 static const char* GIT_INFO = "Version Information=[abc493ac20blablablae94a692e68, (HEAD -> master, origin/master, origin/HEAD)]\r\n";

4 replies

KnarfB
Super User
February 16, 2021

Here is a nice idea requiring git only:

https://stackoverflow.com/questions/16524225/how-can-i-populate-the-git-commit-id-into-a-file-when-i-commit/38087913#38087913

Instead of the makefile modification, it can be done in STMCubeIDE as a pre-build command.

hth

KnarfB

Javier1
Javier1Author
Principal
February 18, 2021

okay im giving this a try:

So first im looking where to add Pre-Build commands

project>properties>C/C++ build0693W000008GW1UQAW.pngShould i replace there the build command for:

version.h: git log -n 1 --format=format:"#define GIT_COMMIT \"%h\"%n" HEAD > $@; make -j8

If i do i get some path errors, the ide is not finding version.h.

I tried adding the core/Inc folder to the path but no luck, i think my problem is somewhere else.

hit me up in https://www.linkedin.com/in/javiermuñoz/
Javier1
Javier1Author
Principal
February 16, 2021

ill try that out and let you know.

thanks

hit me up in https://www.linkedin.com/in/javiermuñoz/
Uwe Bonnes
Chief
February 16, 2021

This is what we have inth BMP debugger Makefile:

```

include/version.h: FORCE

       $(Q)echo " GIT include/version.h"

       $(Q)echo "#define FIRMWARE_VERSION \"$(shell git describe --always --dirty --tags)\"" > $@

```

Javier1
Javier1Author
Principal
February 17, 2021

sorry but im a bit new with the makefile structure of stm.

What (where) do you mean with BMP debugger makefile?

hit me up in https://www.linkedin.com/in/javiermuñoz/
Uwe Bonnes
Chief
February 17, 2021

BMP is some project for a debugger. But that does not matter for your problem, it should only show the principle

Javier1
Javier1AuthorBest answer
Principal
June 7, 2021

I found how:

From this post https://stackoverflow.com/questions/44038428/include-git-commit-hash-and-or-branch-name-in-c-c-source

I added the following command inside Pre-build steps:

git log --pretty=format:'#define GIT_INFO_PRESENT%n static const char* GIT_INFO = "Version Information=[%H,%d]\r\n";' -n 1 > ../Core/Inc/gitcommit.h

0693W00000BaGGpQAN.png 

Creates the following content inside gitcommit.h

#define GIT_INFO_PRESENT
 static const char* GIT_INFO = "Version Information=[abc493ac20blablablae94a692e68, (HEAD -> master, origin/master, origin/HEAD)]\r\n";

hit me up in https://www.linkedin.com/in/javiermuñoz/