1

I figured out how to generate define macro containing git hash at compile time:

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

The problem is that when the git hash is changed, the files (mainwindow.cpp which is adding text to about dialog) which are using the GIT_CURRENT_SHA1 are not automatically rebuilt (obviously)..

I would like to generate header file at compile time containing git hash like in const char *git_hash="git-hash-etc-etc";

And this header file I would include in mainwindow.cpp and hope it would be recompiled when the header changes..

My question is mainly what to add into .pro file to generate such file from Makefile?

Thanks.

nayana
  • 3,787
  • 3
  • 20
  • 51
  • Looks similar to [this question](http://stackoverflow.com/questions/15864689/qmake-pre-build-step-before-any-compilation) – sjdowling Nov 27 '14 at 09:38
  • This answer looks so similar to our implementation... But it still compiles some parts twice. It compiles everything, then forces the regen of the version file, then compiles everything the header is included into. Probably a solution would be to first delete the version file, but we are currently fine with it. – Sebastian Lange Nov 27 '14 at 10:23
  • Use Git smudge/clean filters: http://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes#Keyword-Expansion – echristopherson Nov 28 '14 at 04:39
  • @Sebastian I ve added check with grep if the new git hash matches.. maybe there is something similiar in Windows .bat too.. If the git header is changed then those files using it are recompiled.. everything works fine :) – nayana Nov 28 '14 at 12:23

2 Answers2

3

Just for the evidence this script I used for git version on Linux:

#!/bin/bash

echo "Generating header for git hash" $1 $2
GIT_HEADER="$1/$2"
if [ -z "$2" ]; then
        GIT_HEADER="$1/git_version.h"
fi

GIT_VERSION="`git -C \"$1\" describe`"
if grep --quiet $GIT_VERSION $GIT_HEADER; then
        echo "No need to generate new $GIT_HEADER - git hash is unchanged"
        exit 0;
fi

echo "git version is:" $GIT_VERSION

echo "#ifndef GIT_VERSION_H" > $GIT_HEADER
echo "#define GIT_VERSION_H" >> $GIT_HEADER
echo "" >> $GIT_HEADER
echo "#define GIT_CURRENT_SHA1 \"$GIT_VERSION\"" >> $GIT_HEADER
echo "#endif //GIT_VERSION_H" >> $GIT_HEADER

echo "file is generated into" $GIT_HEADER

And in .pro:

GITVERSION = git_version.h
versiontarget.target =  $$GITVERSION
versiontarget.commands = '$$PWD/git_version.sh \"$$PWD\" $$GITVERSION'
versiontarget.depends = FORCE
PRE_TARGETDEPS += $$GITVERSION
QMAKE_EXTRA_TARGETS += versiontarget
nayana
  • 3,787
  • 3
  • 20
  • 51
  • i changed this line, now it is perferct for me: `GIT_VERSION="\`git --work-tree \"$1\" describe --long --tags\`"` – jsaak Mar 03 '15 at 10:11
1

We added commands to the .pro file for calling a batch-file creating a header file with current svn-version:

.pro:

SVNVERSION = Main/svnversion.h
versiontarget.target = $$SVNVERSION
versiontarget.commands = '$$PWD/svnversion.bat $$PWD'
versiontarget.depends = FORCE

PRE_TARGETDEPS += $$SVNVERSION
QMAKE_EXTRA_TARGETS += versiontarget

SOURCES += ... \
           Main/svnversion.h

headerfile created:

#ifndef SVNVERSION_H 
#define SVNVERSION_H 

#define SVN_VERSION 2763 

#endif // SVNVERSION_H 

batch file:

@echo off
set OLDDIR=%CD%
cd %1
echo old %OLDDIR% arg %1
echo SVN Version: Begin
svn.exe info > svnversion.tmp
if %ERRORLEVEL% == 0 (
    echo SVN Version: Insert
    svn info | findstr /B "Revision: " > svnversion.tmp
    FOR /F "tokens=2 delims= " %%G IN (svnversion.tmp) DO (
        echo #ifndef SVNVERSION_H > Main\svnversion.h
        echo #define SVNVERSION_H >> Main\svnversion.h
        echo. >> Main\svnversion.h
        echo #define SVN_VERSION %%G >> Main\svnversion.h
        echo. >> Main\svnversion.h
        echo #endif // SVNVERSION_H >> Main\svnversion.h
    )
) else (
    echo SVN Version: Default
    echo #ifndef SVNVERSION_H > Main\svnversion.h
    echo #define SVNVERSION_H >> Main\svnversion.h
    echo. >> Main\svnversion.h
    echo #define SVN_VERSION -1 >> Main\svnversion.h
    echo. >> Main\svnversion.h
    echo #endif // SVNVERSION_H >> Main\svnversion.h
)
cd %OLDDIR%
echo SVN Version: Finish
Sebastian Lange
  • 3,879
  • 1
  • 19
  • 38