Retrieve revision number in VS with qmake
-
20-09-2019 - |
Question
My current workflow:
hg update
(or whatever one uses to check out a revision)MyProject.pro
→qmake
→MyProject.vcproj
- Open Visual Studio, edit files
- Build project
During the build step, how can I update my config.h
header file with information from version control system (e.g. hg id
)?
MyProject.vcproj
is generated by qmake
, so I shouldn't edit it by hand.
Solution
You can execute external commands from inside qmake. The easiest way to make the information available in your sources would be to use a define:
HGID = $$system(hg id)
DEFINES += HGID=\\\"$$HGID\\\"
I'm not sure if you can edit an external file from qmake. You could use an external tool, but on Windows you normally don't have things like sed
, so it might be a little more problematic.
OTHER TIPS
You can accomplish that using a custom build target and the PRE_TARGETDEPS
keyword. Assuming config.h.in
has the folowing format:
#define HGID $HGID
You can define a custom build target that will process hgid.h.in
and output to hgid.h
prior to building your main target as follows:
hgid.target = hgid
hgid.commands = sed s/\\\$$HGID/`hg id`/ hgid.h.in > hgid.h
QMAKE_EXTRA_TARGETS += hgid
PRE_TARGETDEPS += hgid
One opton is to enable the Keyword Extension. Put something like this in your hgrc (or Mercurial.ini if that's your thing):
[extensions]
hgext.keyword=
[keyword]
config.h =
[keywordmaps]
HGREV = {node}
Then in config.h put:
#define HGREV "$HGREV$"
You might need to parse the hex value out of the "$HGREV: deadbeefdeadbeef $" that you'll get, but that's easily done by whatever code is accessing the HGREV define.
In addition to Lukáš Lalinský and goodrone's comment, I'd like to mention that qmake can link directly to the script, not only to it's output. So one can say
DEFINES += REPO_ID=\\\"`./setlocalversion.sh`\\\"
and the script will be freshly executed for every single target.