How to refer to the source directory in qmake?

后端 未结 5 1873
长发绾君心
长发绾君心 2020-12-30 10:57

I added

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

QMAKE_EXTRA_TARGETS += version

PRE_TARGETDEPS += version.h
<
相关标签:
5条回答
  • 2020-12-30 11:01

    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.

    0 讨论(0)
  • 2020-12-30 11:11

    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.

    0 讨论(0)
  • 2020-12-30 11:18

    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
    
    0 讨论(0)
  • 2020-12-30 11:23

    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.

    0 讨论(0)
  • 2020-12-30 11:26

    This works and is easy to understand.

    version.commands = ( cd $${PWD}; generate-version.sh )
    
    0 讨论(0)
提交回复
热议问题