Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print git hash in qt as macro created at compile time

Tags:

c++

git

makefile

qt

I need to get information about git commit (to print it and use in my about dialog) used during compile time (make, not qmake). Its ok to use git describe command for this. I need the solution to be multiplatform(Windows Linux at least), I managed to successfuly get git hash at qnake run on Windows and Linux.

This command is executed at qmake run and works well:

DEFINES += GIT_CURRENT_SHA1=$(shell git describe)

However I need to have git describe executed at compile time, because qmake is not run every time I compile and thus there will be old commit hash.

If I add this code to Makefile generated by qmake it works fine, but of course vanishes after qmake run:

VARIABLE = $(shell cd ../../project/ ; git describe)
DEFINES = -DGIT_CURRENT_SHA1=$(VARIABLE)

And there is problem how to get to correct path due to shadow build which is not inside git repository directory. I need to cd to project and then run git describe from there.

So there are two subquestions:

  • how to add code to Makefile from Qt .pro file
  • how to pass the correct path to git describe command

Or does anybody know of something better?

Thanks

Update 1

Ok I found out how to insert shell code into Makefile define variable, there need to be quotes around it:

DEFINES += GIT_CURRENT_SHA1="$(shell cd ../../project/ ; git describe)"

But the problem is how to pass the project path into it(due to shadow build etc)

Update 2

Ok found out also how to do that..

DEFINES += GIT_CURRENT_SHA1="$(shell git -C \""$$_PRO_FILE_PWD_"\" describe)"

But there is big problem - how to force rebuild files which uses GIT_CURRENT_SHA1 macro ?? I am thinking about some extra header file which I would have to generate every time and include it where I need it.

like image 752
nayana Avatar asked Nov 20 '14 14:11

nayana


3 Answers

Ok so its seems I found out solution after some time. Just add this to defines in Qt .pro file.

DEFINES += GIT_CURRENT_SHA1="\\\"$(shell git -C \""$$_PRO_FILE_PWD_"\" describe)\\\""

This define will be refreshed when building generated Makefile. No need to run qmake every time.

Usage simple:

label->setText(QString("Version: %1").arg(GIT_CURRENT_SHA1));

But there is problem that those files using value from GIT_CURRENT_SHA1 macro will not be automatically rebuilt when git hash changes.

like image 98
nayana Avatar answered Oct 05 '22 22:10

nayana


With Qt 5.14 the following lines in .pro file work for me:

GIT_HASH="\\\"$$system(git -C \""$$_PRO_FILE_PWD_"\" rev-parse --short HEAD)\\\""
GIT_BRANCH="\\\"$$system(git -C \""$$_PRO_FILE_PWD_"\" rev-parse --abbrev-ref HEAD)\\\""
BUILD_TIMESTAMP="\\\"$$system(date -u +\""%Y-%m-%dT%H:%M:%SUTC\"")\\\""
DEFINES += GIT_HASH=$$GIT_HASH GIT_BRANCH=$$GIT_BRANCH BUILD_TIMESTAMP=$$BUILD_TIMESTAMP

In your code you can check the revision like this:

int main(int argc, char *argv[])
{
    QStringList args;
    for (int i = 0; i < argc; i++)
        args << QString(argv[i]);

    if (args.contains("-v") || args.contains("--version")) {
        qDebug() << QString("branch: %1, version: %2, built_at: %3").arg(GIT_BRANCH).arg(GIT_HASH).arg(BUILD_TIMESTAMP);
        return 0;
    }
    // ...
}
like image 41
rightaway717 Avatar answered Oct 05 '22 22:10

rightaway717


I faced the same problem and solved it with a python script that is executed with cmake on compile time: GitHashExtractor

It extracts all the important variables such as gitHash, tag... and saves this data in a version.h file for later access in your main code. The working principle is based on git describe and a additional target in cmake:

set(PROJECT_SOURCES
        [... your files ...]
        GitHashExtractor/firmwareVersion.py
        GitHashExtractor/version.h
)

# START github.com/ni-m/gitHashExtractor
set(VERSION_FILE "./GitHashExtractor/version.h")
set(VERSION_PYTHON "./GitHashExtractor/firmwareVersion.py")
set_property(SOURCE ${VERSION_FILE} PROPERTY SKIP_AUTOGEN ON)
add_custom_command(OUTPUT ${VERSION_FILE}
    COMMAND python ${VERSION_PYTHON} [ARGS] [Qt.h]
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    COMMENT "Creating ${VERSION_FILE}"
)

# target GitHashExtractor is always built
add_custom_target(GitHashExtractor ALL DEPENDS ${VERSION_FILE})
# END github.com/ni-m/gitHashExtractor
like image 41
Marco Avatar answered Oct 05 '22 22:10

Marco