在 Qt .pro 文件中添加构建步骤

Adding building steps in Qt .pro file

从QtCreator的“projects - Add Building Step”,可以指定添加的构建步骤。例如,以下设置将在构建项目时回显一条消息。

添加的构建步骤实际出现在对应的.pro.user文件中:

<value type="QString" key="ProjectExplorer.ProcessStep.Arguments">hello. This is my custom process step.</value>
<value type="QString" key="ProjectExplorer.ProcessStep.Command">/usr/bin/echo</value>

我的问题是:有没有办法在 .pro 文件中添加构建步骤而不是使用 GUI(“项目 - 添加构建步骤”)并实际将设置添加到 .pro.user 文件? (对我来说,优点是可以使用 shell 脚本轻松地批处理 .pro 文件。)

我试图将步骤放在 .pro 文件中 QMAKE_EXTRA_TARGETS as

mystep.commands = echo 'hello. This is my custom process step'
QMAKE_EXTRA_TARGETS += mystep

但是,使用命令行 qmake; make 时,只有原始建筑发生。只有在进一步 make mystep 之后,回声才会发生。换句话说,正常的 make 不会发生 mystep 步骤 - 也许我误解了 QMAKE_EXTRA_TARGETS?

经过一段时间的挖掘,我找到了一个解决方案——使用 QMAKE_EXTRA_COMPILERS。

# must use a variable as input
PHONY_DEPS = .
mystep.input = PHONY_DEPS
# use non-existing file here to execute every time
mystep.output = phony.txt
# the system call to the batch file
mystep.commands = echo 'hello. This is my custom process step'
# some name that displays during execution
mystep.name = running mystep...
# "no_link" tells qmake we don’t need to add the output to the object files for linking
# "no_clean" means there is no clean step for them.
# "target_predeps" tells qmake that the output of this needs to exist before we can do the rest of our compilation.
mystep.CONFIG += no_link no_clean target_predeps
# Add the compiler to the list of 'extra compilers'.
QMAKE_EXTRA_COMPILERS += mystep

对于我的实际应用程序,它不仅仅是一个回声,我将以不同的方式指定 output/CONFIG。

参考:

很高兴您找到了适合您的答案。我想澄清一下我在上面所做的评论,因为我已经尝试过并且对我来说效果很好。 (而且看起来比您的解决方案简单得多。)我认为它对您不起作用的原因是因为我想让您将 PRE_TARGETDEPS 与您已经拥有的 QMAKE_EXTRA_TARGETS 结合起来。这应该是您所需要的。

mystep.commands = echo 'hello. This is my custom process step'
QMAKE_EXTRA_TARGETS += mystep
PRE_TARGETDEPS += mystep