从 QMake 运行程序/脚本
我们有相当大的代码库。绝大多数代码是使用 qmake 编译以生成 makefile。但是,有一些子项目是通过运行批处理文件或运行其他程序生成的。
我希望能够使用 qmake 编译所有内容,但我不知道如何让 qmake 简单地运行脚本。
我尝试过的一件事是在我的 pro 文件中使用 QMAKE_EXTRA_TARGETS ,如下所示:
TEMPLATE = lib
SOURCES = placeholder.cpp
CONFIG += no_link staticlib
batch_runner.target = placeholder.cpp
batch_runner.commands = my_batch_file.bat
QMAKE_EXTRA_TARGETS = batch_runner
然后我必须让批处理文件生成 placeholder.cpp ,如下所示:
# do the real work here
# ...
# create placeholder.cpp so qmake and nmake are happy
echo // dummy >> placeholder.cpp
这似乎工作正常。问题是它有点做作。如果我不指定batch_runner.target(即我将其留空)或不将placeholder.cpp放在SOURCES中,则批处理文件永远不会运行。这是因为 qmake 没有使batch_runner.commands 成为Makefile 中任何其他依赖项的操作。
有没有更好的方法让 QMake 构建 Makefile,以便在 Makefile 执行时运行脚本?
We have a fairly large code-base. The vast majority of the code is compiled using qmake to produce the makefiles. However, there are some sub-projects that get produced by running batch files or running other programs.
I'd like to be able to have everything compiled using qmake, but I can't figure out how to get qmake to simply run a script.
One thing that I've tried is using QMAKE_EXTRA_TARGETS in my pro file, like so:
TEMPLATE = lib
SOURCES = placeholder.cpp
CONFIG += no_link staticlib
batch_runner.target = placeholder.cpp
batch_runner.commands = my_batch_file.bat
QMAKE_EXTRA_TARGETS = batch_runner
I then have to have the batch file produce placeholder.cpp like so:
# do the real work here
# ...
# create placeholder.cpp so qmake and nmake are happy
echo // dummy >> placeholder.cpp
This seems to work fine. The trouble is that it is somewhat hokey. If I don't specify batch_runner.target (i.e. I leave it blank) or don't put placeholder.cpp in SOURCES then the batch file never gets run. This is because qmake isn't making batch_runner.commands the action for any other dependency in the Makefile.
Is there any better way to get QMake to construct a Makefile such that a script is run when the Makefile executes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来 QMAKE_POST_LINK 非常适合这类事情。
这似乎完成了工作。 my_batch_file.bat 在 nmake 运行时运行(而不是在 qmake 运行时),并且我不需要对占位符目标或文件做任何有趣的事情。
我很可能不需要“CONFIG”中列出的所有项目。
It looks like QMAKE_POST_LINK works well for this sort of thing.
This seems to get the job done. my_batch_file.bat runs when nmake runs (rather than when qmake runs) and I don't need to do anything funny with placeholder targets or files.
It's quite likely that I don't need all of the items listed in 'CONFIG'.
尝试使用 system() 命令。例如:
Try the system() command. For example:
这是另一个解决方案:
模板 aux 基本上生成一个 makefile,在不指定目标的情况下运行时该文件不执行任何操作。
OBJECTS_DIR
和DESTDIR
变量设置为当前目录,以防止 qmake 创建debug
和release
目录(将它们设置为./
很重要,而不仅仅是.
(至少在 Windows 上);然后,使用QMAKE_EXTRA_TARGETS
,我们first
重新定义目标,以便在没有目标的情况下调用makefile时运行自定义命令。这有点hacky,但它可以完成工作。
添加:
如果你想阻止生成三个makefile(
Makefile
、Makefile.Debug
、Makefile.Release
),你可以添加但是,如果你使用它并根据 makefile 的调用方式(总是手动调用,由父目录的“子目录”*.pro 文件调用,...),可能需要创建假的
debug
和release
目标以避免“没有规则来创建目标...”错误。例如:Here is another solution:
The template
aux
basically produces a makefile which does nothing when run without specifying a target. TheOBJECTS_DIR
andDESTDIR
variables are set to the current directory to prevent that qmake creates thedebug
andrelease
directories (it's important to set them to./
and not just to.
; at least on Windows). Then, usingQMAKE_EXTRA_TARGETS
, we redefine the targetfirst
to run the custom command when the makefile is invoked without target.It's a bit hacky but it gets the job done.
Addition:
If you want to prevent the generation of three makefiles (
Makefile
,Makefile.Debug
,Makefile.Release
), you can addHowever, if you use this and depending on how the makefile is invoked (always invoked manually, invoked by parent directory's "subdirs" *.pro file, ...), it might be necessary to create fake
debug
andrelease
targets to avoid "no rule to make target..." errors. For example:您可以使用 SUBDIRS 配置来运行多个不同的目标,甚至可以从同一个 makefile 运行。这可能特别适合您的额外目标,因为子目录配置可以在 makefile 中指定要运行的特定目标(请参阅 未记录的 qmake 了解详细信息)。在这种情况下,我会将所有“常规”构建命令放入一个 .pro 文件中,将外部构建命令放入另一个文件中,并使用一个子目录 .pro 文件来构建所有这些命令。我还没有测试过类似的东西,但它应该有效。
常规.pro:
外部.pro:
do_it_all.pro:
You could use the SUBDIRS configuration to run multiple different targets, even from the same makefile. This might work especially well with your extra targets, as a subdir configuration can specific a specific target in the makefile to run (see undocumented qmake for details). In this case, I would put all of the "regular" build commands in one .pro file, the external build commands in another, and a subdirs .pro file to build all of them. I haven't tested anything quite like this, but it should work.
regular.pro:
external.pro:
do_it_all.pro: