How to refer to the source directory in qmake?
Asked Answered
A

5

12

I added

version.target = version.h
version.commands = bash generate-version.sh

QMAKE_EXTRA_TARGETS += version

PRE_TARGETDEPS += version.h

to the project, but it attempts to run "generate-version.sh" in destination directory:

make: Leaving directory `.../qqq-build-desktop'
make: Entering directory `.../qqq-build-desktop'
Makefile:236: warning: overriding commands for target `version.h'
Makefile:233: warning: ignoring old commands for target `version.h'
bash generate-version.sh
bash: generate-version.sh: No such file or directory
make: Leaving directory `.../qqq-build-desktop'

There is $$DESTDIR, but I don't see $$SRCDIR. How to refer to the project directory in qmake (or how to rewrite this)?

Annecy answered 4/3, 2011 at 10:46 Comment(0)
L
15

My first thought is to try to rewrite

version.commands = bash generate-version.sh

so as not to have to invoke a shell script. Perhaps you can combine all of the statements into one line:

version.commands = echo \'char VERSION[]=\"1.0\";\' > version.h && ls && echo Done

If you are stuck with invoking the script, probably PWD or OUT_PWD are what you are looking for. From the qmake Variable Reference

PWD

This variable contains the full path leading to the directory where the qmake project file (project.pro) is located.

OUT_PWD

This variable contains the full path leading to the directory where qmake places the generated Makefile.

The one caveat that is not mentioned in the documentation is that if you are doing a recursive qmake, PWD refers to where the top level .pro file was read from. Thus if you run qmake -r from {proj-root}, when sub/sub/sub/dir-proj.pro is finally read in, PWD will still point to {proj-root}.

Assuming that generate-version.sh is in the same directory as your top level .pro file, you might try:

version.commands = bash $$PWD/generate-version.sh
Lopez answered 4/3, 2011 at 15:0 Comment(2)
It is anyway needed to access ".git" or ".svn" directories to get version, so we need $$PWD in any case.Annecy
If used within included .pri file, PWD directs to directory where .pri resides, not original directory of .pro file. Qt least int Qt 5.6. Is there way to get original .pro directory in .pri file..?Avatar
T
1

I found a better and cleaner solution

version.target = version.h
version.commands = bash ${QMAKE_VAR__PRO_FILE_PWD_}/generate-version.sh
QMAKE_EXTRA_TARGETS += version

The variable _PRO_FILE_PWD_ is documented since qt 4.5 and contains the path to the directory containing the project file in use (Contains the .pro file)

But to access this variable for QMAKE_EXTRA_TARGETS, QMAKE_VAR_ must be appended.

Thievery answered 9/6, 2012 at 15:46 Comment(3)
Seems not to work. When I replace $$PWD to ${QMAKE_VAR__PRO_FILE_PWD_} it fails to compile. Makefile contains literal ${QMAKE_VAR__PRO_FILE_PWD_}.Annecy
What is your version of qtcreatorThievery
I was checking with plain "qmake-qt4". QMake version 2.01a now (I don't know the version at the time I was testing it).Annecy
D
1

PWD

Specifies the full path leading to the directory containing the current file being parsed. This can be useful to refer to files within the source tree when writing project files to support shadow builds.

Dupre answered 15/6, 2014 at 20:46 Comment(0)
R
0

I use (Linux and g++)

DEFINES += SVN_VERSION=\\\"\""`svnversion $$PWD`\""\\\"
DEFINES += COMPILE_DATE=\\\"\""`date`\""\\\"
DEFINES += SW_VERSION=\\\"\"0.5\"\\\"

which defines the macro SVNVERSON to be the svn version. To access it from C++:

QString svnVersion = SVN_VERSION;
QString swVersion  = SW_VERSION;

Explanation: On the shell I want to see this call:

-DSVN_VERSION=\""`svnversion /path/to/my/source`"\"

As you see some escapes are necessary on shell level. In the .pro-file it then has to be escaped twice.

Rubie answered 29/1, 2015 at 7:49 Comment(1)
If the version changes this will trigger a recompilation of all files - not only those including the version-header. - Not a big issue with SVN, but when utilizing the full power of git, this happens often.Choplogic
C
0

This works and is easy to understand.

version.commands = ( cd $${PWD}; generate-version.sh )
Chema answered 29/6, 2016 at 14:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.