[QBS] Retreive git describe result to set cpp.defines value

Joerg Bornemann joerg.bornemann at digia.com
Mon Sep 9 09:30:03 CEST 2013

On 07/09/2013 16:17, Raphael Cotty wrote:

> I am porting a large project using cmake to qbs.
> We use cmake EXECUTE_PROCESS to retrieve the the git tag value and set a
> variable with it.
> The variable is then used to create a #define.

There's the Probe item which can be used for things like that.
The following example adds a GITREV define that contains the latest 
commit id of your repository.

import qbs 1.0
import qbs.Process

Application {
     Probe {
         id: revprobe
         property string revision

         configure: {
             revision = "I don't know";
             var p = new Process();
             if (p.exec("git.exe", ["rev-parse", "HEAD"]) === 0)
                 revision = p.readStdOut().trim();

     Depends { name: "cpp" }
     cpp.defines: ['GITREV="' + revprobe.revision + '"']
     files: ["main.cpp"]

Seems that we should eventually document the Probe items...

I just noticed two small problems: this example will only work correctly 
if qbs is called from the source directory. To make this work correctly, 
we have to tell git where the repository is by either

1) setting the working dir of the Process object - a feature we've 
forgotten to provide (QBS-385).
2) or calling git with the --git-dir opion and pass 
product.sourceDirectory, which is not accessible within Probe items 
unfortunately (QBS-386).

Best Regards,


Joerg Bornemann
Digia, Qt

More information about the Qbs mailing list