在编译之前进行预构建

时间:2022-08-05 23:07:49

There are several questions on SO regarding how to create a pre-build step for qmake, I can do that with this in my .pro file:

关于如何为qmake创建预构建步骤,我有几个问题,我可以在我的。pro文件中这样做:

versionTarget.target = ../VersionData/versioning.h
versionTarget.depends = FORCE
win32: versionTarget.commands = cd $$PWD; python.exe ./version_getter.py -p $$TARGET
else:  versionTarget.commands = cd $$PWD; python ./version_getter.py -p $$TARGET

PRE_TARGETDEPS += ../VersionData/versioning.h
QMAKE_EXTRA_TARGETS += versionTarget

Now, the problem is that this approach is not a build step per se but just another build target, so if I have the -j flag configured for make it runs my script in parallel with the other build jobs. This is very bad, because my script creates/updates a header file - having it change part way through the compilation is not acceptable.

现在的问题是,这种方法本身不是构建步骤,而是另一个构建目标,所以如果我配置了-j标志,使它与其他构建作业并行运行我的脚本。这很糟糕,因为我的脚本创建/更新一个头文件——让它在编译过程中进行部分更改是不可接受的。

So, is there anyway I can have this script executed before any compilation is ran? I know I can create another script and call the version_getter.py and qmake in sequence from that, but this is not desirable as I would have to compile from the command line rather than from within Qt Creator.

那么,我是否可以在编译之前执行这个脚本呢?我知道我可以创建另一个脚本并调用version_getter。py和qmake按顺序排列,但这是不可取的,因为我必须从命令行编译,而不是从Qt创建者中编译。


Update

The complete .pri file that is included by every one of my sub-projects is below:

我的每一个子项目都包含的.pri文件如下:

CONFIG += thread
QT += core \
      gui

versionTarget.target = ../VersionData/versioning.h
versionTarget.depends = FORCE
win32: versionTarget.commands = cd $$PWD; python.exe ./version_getter.py -p $$TARGET
else:  versionTarget.commands = cd $$PWD; python ./version_getter.py -p $$TARGET

PRE_TARGETDEPS += ../VersionData/versioning.h
QMAKE_EXTRA_TARGETS += versionTarget

DEPENDPATH += ../VersionData
INCLUDEPATH += ../VersionData
HEADERS += ../VersionData/versioning.h

UI_HEADERS_DIR = $${_PRO_FILE_PWD_}/include/Qui
DESTDIR = $(SYREN_PATH)

!win32-msvc {
    QMAKE_CXXFLAGS += -std=c++0x
}

But this still results in the same parallel behaviour. I thought it may have been due to my use of ccache, but turning it off made no difference (other than being much slower of course).

但这仍然会导致相同的并行行为。我认为这可能是由于我使用了ccache,但是关掉它并没有什么区别(当然除了要慢得多)。

2 个解决方案

#1


11  

Another option would be to start with the project file snippet in your original question, and also ensure that qmake is aware that versioning.h is a dependency for the other build targets in your project file —

另一种选择是从原始问题中的项目文件片段开始,并确保qmake知道版本控制。h是项目文件中其他构建目标的依赖项

  • Add the full path to versioning.h to your HEADERS variable.
  • 向版本控制添加完整路径。h的header变量。
  • Add the folder in which versioning.h resides to your DEPENDPATH variable.
  • 添加版本控制的文件夹。h位于所依赖路径变量。

(Caveat: if you run qmake when versioning.h doesn't exist, it will emit "WARNING: Failure to find: versioning.h" — the only workaround for that warning is to use the system() command, as I described in my other answer.)

(注意:如果您在版本控制时运行qmake。h不存在,它会发出“警告:找不到:版本控制”。h“-唯一的解决方法是使用system()命令,正如我在其他答案中所描述的那样。)

Example

Create test.pro containing the following:

创建测试。箴包含以下几点:

versionTarget.target = ../versioning.h
versionTarget.depends = FORCE
versionTarget.commands = sleep 5s ; touch ../versioning.h
PRE_TARGETDEPS += ../versioning.h
QMAKE_EXTRA_TARGETS += versionTarget

SOURCES = test.c
HEADERS = ../versioning.h
DEPENDPATH = ..

Create test.c containing the following:

创建测试。c包含以下:

#include "../versioning.h"

Run qmake. It will output WARNING: Failure to find: ../versioning.h.

qmake运行。它将输出警告:没有找到:../版本。

Run make -j9. It will run versionTarget.commands (which sleeps for 5 seconds to exaggerate any multiprocessing problems), and, after that is done, run the command to compile test.c.

运行make j9。它将运行versiontarget .命令(它休眠5秒,以夸大任何多处理问题),并且在完成之后,运行该命令来编译test.c。

(And if you examine the generated Makefile, you'll see that test.o depends on both test.c and ../versioning.h, so Make should correctly figure out that it can't run the command to compile test.c before the command to create/update ../versioning.h.)

(如果您检查生成的Makefile,您将看到这个测试。o取决于两个测试。c和. . /版本控制。h,所以Make应该正确地指出它不能运行编译测试命令。c在创建/更新./版本.h命令之前)

#2


4  

Use the system() qmake command — it runs when you run qmake, which happens before make runs any build commands.

使用system() qmake命令——它在运行qmake时运行,这发生在make运行任何构建命令之前。

win32: PYTHON=python.exe
else:  PYTHON=python
system(cd $$PWD; $$PYTHON ./version_getter.py -p ../VersionData/versioning.h)

#1


11  

Another option would be to start with the project file snippet in your original question, and also ensure that qmake is aware that versioning.h is a dependency for the other build targets in your project file —

另一种选择是从原始问题中的项目文件片段开始,并确保qmake知道版本控制。h是项目文件中其他构建目标的依赖项

  • Add the full path to versioning.h to your HEADERS variable.
  • 向版本控制添加完整路径。h的header变量。
  • Add the folder in which versioning.h resides to your DEPENDPATH variable.
  • 添加版本控制的文件夹。h位于所依赖路径变量。

(Caveat: if you run qmake when versioning.h doesn't exist, it will emit "WARNING: Failure to find: versioning.h" — the only workaround for that warning is to use the system() command, as I described in my other answer.)

(注意:如果您在版本控制时运行qmake。h不存在,它会发出“警告:找不到:版本控制”。h“-唯一的解决方法是使用system()命令,正如我在其他答案中所描述的那样。)

Example

Create test.pro containing the following:

创建测试。箴包含以下几点:

versionTarget.target = ../versioning.h
versionTarget.depends = FORCE
versionTarget.commands = sleep 5s ; touch ../versioning.h
PRE_TARGETDEPS += ../versioning.h
QMAKE_EXTRA_TARGETS += versionTarget

SOURCES = test.c
HEADERS = ../versioning.h
DEPENDPATH = ..

Create test.c containing the following:

创建测试。c包含以下:

#include "../versioning.h"

Run qmake. It will output WARNING: Failure to find: ../versioning.h.

qmake运行。它将输出警告:没有找到:../版本。

Run make -j9. It will run versionTarget.commands (which sleeps for 5 seconds to exaggerate any multiprocessing problems), and, after that is done, run the command to compile test.c.

运行make j9。它将运行versiontarget .命令(它休眠5秒,以夸大任何多处理问题),并且在完成之后,运行该命令来编译test.c。

(And if you examine the generated Makefile, you'll see that test.o depends on both test.c and ../versioning.h, so Make should correctly figure out that it can't run the command to compile test.c before the command to create/update ../versioning.h.)

(如果您检查生成的Makefile,您将看到这个测试。o取决于两个测试。c和. . /版本控制。h,所以Make应该正确地指出它不能运行编译测试命令。c在创建/更新./版本.h命令之前)

#2


4  

Use the system() qmake command — it runs when you run qmake, which happens before make runs any build commands.

使用system() qmake命令——它在运行qmake时运行,这发生在make运行任何构建命令之前。

win32: PYTHON=python.exe
else:  PYTHON=python
system(cd $$PWD; $$PYTHON ./version_getter.py -p ../VersionData/versioning.h)