Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Running command when building app and using output in a DEFINE



  • I am trying to run a git command to get our build information. I would like to use this in a DEFINE.

    This is what I tried:

    GITVERSIONCOM = $$system("git describe --abbrev=5 --dirty=-t")
    DEFINES += GITVERSION = $${GITVERSIONCOM}
    

    This seems property create the GITVERSION define somewhere as the IDE seems to see it. However, it gives a strange errors:
    "no macro names in #define directive" file not found <command-line>
    "macro names must be identifiers"

    My guess is I am doing this completely wrong from the getgo.


  • Moderators

    Here is a working example of exactly what you need :-) https://github.com/milosolutions/mwizardtemplate/blob/master/version.pri

    MIT-licensed so you are free to use / adapt / expand it. Works on all platforms.

    Your approach is good, you just need to add this insane amount of escape sequences (and remove the spaces around = sign.

    GITVERSIONCOM = $$system("git describe --abbrev=5 --dirty=-t")
    DEFINES += GITVERSION=\\\\\\"$$GITVERSIONCOM\\\\\\"
    


  • @fcarney said in Running command when building app and using output in a DEFINE:

    DEFINES += GITVERSION = $${GITVERSIONCOM}

    Part way there:

    DEFINES += GITVERSION=$${GITVERSIONCOM}
    

    The spacing around the equals was causing issues.


  • Moderators

    Here is a working example of exactly what you need :-) https://github.com/milosolutions/mwizardtemplate/blob/master/version.pri

    MIT-licensed so you are free to use / adapt / expand it. Works on all platforms.

    Your approach is good, you just need to add this insane amount of escape sequences (and remove the spaces around = sign.

    GITVERSIONCOM = $$system("git describe --abbrev=5 --dirty=-t")
    DEFINES += GITVERSION=\\\\\\"$$GITVERSIONCOM\\\\\\"
    


  • @sierdzio I got it working. Interestingly enough, if I used the 6 slashes on each side it would eat the "s out of the Makefile. I ended up doing this:

    GITVERSIONCOM = $$system(git describe --abbrev=5 --dirty=-t)
    COMPILETIMECOM = $$system(date +%s%3N)
    DEFINES += GITVERSION=\\\"$$GITVERSIONCOM\\\"
    DEFINES += COMPILETIME=$$COMPILETIMECOM
    

  • Lifetime Qt Champion

    Hi,

    You might want to consider an alternative, you could generate a header based on a template that you fill with the variables you want. That would avoid these escaping that can change for different platform.



  • @SGaist
    I decided to go that route and have it build based upon platform:

    versiondata.target = buildinfo.h
    unix:versiondata.commands = $$PWD/create_buildinfo
    versiondata.depends = FORCE
    QMAKE_EXTRA_TARGETS += versiondata
    PRE_TARGETDEPS += buildinfo.h
    

    I have a command called create_buildinfo that makes buildinfo.h.
    To add platforms I just add win32: or whatever and create different commands.


Log in to reply