10

I want the compiled application to have the commit number, source files checksums and other things to be available during the compilation.

In plain Makefiles I do like this:

prog: VERSION source.c
    gcc -DVERSION=\"$(shell cat VERSION)\" source.c -o prog 

VERSION: .git
    git describe > VERSION

How to use something similar with qmake?

2 Answers 2

19

If you were to pass the version information as an included file (let's say "version.h") instead of a #define, then you could add the following to your qmake file

# Define how to create version.h
version.target = version.h
version.commands = <PUT_YOUR_COMMANDS_HERE>
version.depends = .git

QMAKE_EXTRA_TARGETS += version

PRE_TARGETDEPS += version.h

The first 3 lines tell how to make a new target object called "version" that generates "version.h". It is made by executing the commands "<PUT_YOUR_COMMANDS_HERE>". The target is dependent on ".git"

The "QMAKE_EXTRA_TARGETS" says there is a new target known as "version".

The "PRE_TARGETDEPS" indicates that "version.h" needs to exist before anything else can be done (which forces it to be made if it isn't already made).

2
  • Trying to get this to work, having other issue: stackoverflow.com/questions/5192714/…
    – Vi.
    Mar 4, 2011 at 12:22
  • Unfortunately PRE_TARGETDEPS only puts the target at the front of the dependency list of the final target. Thus as soon as you compile in parallel runing make -j this is likely to fail.
    – Morty
    Aug 19, 2021 at 14:51
0

A simpler solution even if @jwernemy as nice way to solve it:

VERSION = $$system(-git-dir=$PWD/.git <PUT_YOUR_GIT_COMMANDS_HERE>)
4
  • In what directory will the command run? In source directory or in build directory?
    – Vi.
    Apr 21, 2015 at 16:22
  • And the .git is in the source directory.
    – Vi.
    Apr 22, 2015 at 14:57
  • 1
    you can use git --git-dir to specify the .git folder Apr 22, 2015 at 16:10
  • like that git --git-dir=$PWD/.git <YOU_COMMANDS_HERE Apr 22, 2015 at 16:11

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.