How to get git-commit ID during build and display it as version information
-
Hi,
Are you using qmake or cmake ?
In either case, what you can do is generate a header file from a template that contains that information.
You will have to execute git and retrieve the result that you will then use as input for said template.
-
@ynatynat As SGaist indicated,
cmake
has "magic" to do this for you.You can also "roll your own" fairly simply. I use this bash script:
Which is invoked (for example, on github CI Actions) like so:
tools/update_version_strings.sh \ /home/runner/work/qt-qml-project-template-with-ci/qt-qml-project-template-with-ci/src/version.genfiles.input/version.h \ /home/runner/work/qt-qml-project-template-with-ci/qt-qml-project-template-with-ci/build/generated_files/autogenerated/version.h
Then you have to make sure that the path to "build/generated_files/autogenerated/version.h" is part of INCLUDEPATH for anything that needs to
#include
version.h -
Thank you for your reply.
The shell script is nice to get version.I am using qmake.
How to contain the version in qmake?
.pro file, build setting on qtcreator ,etc ... -
@ynatynat I don't know that I can be of any further help here. I do not use Qt Creator. I do use qmake, but I have an outer bash script that drives the entire build, and the outermost bash script first invokes my versioning script and then invokes
qmake
. So in my solution,qmake
has no idea about where the version comes from (it has already been produced by the timeqmake
gets involved). The only part that needs to be "known" to thepro
files is what I mentioned earlier about INCLUDEPATH:src/app/app.pro:15:INCLUDEPATH += $${top_srcdir}/build/generated_files # for version.h
You can probably look at 50 different projects and see 50 variations on how to make a version header. I absolutely do not claim that mine is the "best" solution. But this has been one area of my projects where I have applied the mindset of "put in place a working, good-enough, adequate, well-understood solution and then just move on to solving bigger issues."
-
@ynatynat
I guess you can get the current number with
git rev-list --count HEAD
this command can be added to pro file asGIT_PATH=$$system(which git) !isEmpty(GIT_PATH) { BUILD_VERSION=$$system($$GIT_PATH rev-list --count HEAD) } isEmpty(BUILD_VERSION) { BUILD_VERSION=0 } DEFINES += "BUILD_SERIES_NUMBER=\"\\\"$$BUILD_VERSION\\\"\""
BUILD_SERIES_NUMBER can be accessed in qt gui code.
-
QMAKE_SUBSTITUTE as described here will avoid the need to fiddle with all the backslash escaping changing from one OS to another.
In the example linked they used it with define but you can create a QString with it or even a basic char array.
-