从 QMake 运行程序/脚本

发布于 2024-09-16 09:53:58 字数 816 浏览 6 评论 0原文

我们有相当大的代码库。绝大多数代码是使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薔薇婲 2024-09-23 09:53:58

看起来 QMAKE_POST_LINK 非常适合这类事情。

这似乎完成了工作。 my_batch_file.bat 在 nmake 运行时运行(而不是在 qmake 运行时),并且我不需要对占位符目标或文件做任何有趣的事情。

我很可能不需要“CONFIG”中列出的所有项目。

TEMPLATE = lib
TARGET   = 
CONFIG  += no_link target_predeps staticlib

QMAKE_POST_LINK  = my_batch_file.bat
QMAKE_CLEAN     += batch_output.obj

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'.

TEMPLATE = lib
TARGET   = 
CONFIG  += no_link target_predeps staticlib

QMAKE_POST_LINK  = my_batch_file.bat
QMAKE_CLEAN     += batch_output.obj
放赐 2024-09-23 09:53:58

尝试使用 system() 命令。例如:

system(pwd)

Try the system() command. For example:

system(pwd)
乖乖公主 2024-09-23 09:53:58

这是另一个解决方案:

TEMPLATE = aux
OBJECTS_DIR = ./
DESTDIR = ./

first.commands = my_batch_file.bat
QMAKE_EXTRA_TARGETS += first
QMAKE_CLEAN += batch_output.obj

模板 aux 基本上生成一个 makefile,在不指定目标的情况下运行时该文件不执行任何操作。 OBJECTS_DIRDESTDIR 变量设置为当前目录,以防止 qmake 创建 debugrelease 目录(将它们设置为 ./ 很重要,而不仅仅是 .(至少在 Windows 上);然后,使用QMAKE_EXTRA_TARGETS,我们first重新定义目标,以便在没有目标的情况下调用makefile时运行自定义命令。

这有点hacky,但它可以完成工作。

添加:
如果你想阻止生成三个makefile(MakefileMakefile.DebugMakefile.Release),你可以添加

CONFIG -= debug_and_release

但是,如果你使用它并根据 makefile 的调用方式(总是手动调用,由父目录的“子目录”*.pro 文件调用,...),可能需要创建假的 debugrelease 目标以避免“没有规则来创建目标...”错误。例如:

release.target = release
release-clean.target = release-clean
release-install.target = release-install
[...]
debug.target = debug
debug-clean.target = debug-clean
debug-install.target = debug-install
[...]
QMAKE_EXTRA_TARGETS += release release-clean release-install [...]
QMAKE_EXTRA_TARGETS += debug debug-clean debug-install [...]

Here is another solution:

TEMPLATE = aux
OBJECTS_DIR = ./
DESTDIR = ./

first.commands = my_batch_file.bat
QMAKE_EXTRA_TARGETS += first
QMAKE_CLEAN += batch_output.obj

The template aux basically produces a makefile which does nothing when run without specifying a target. The OBJECTS_DIR and DESTDIR variables are set to the current directory to prevent that qmake creates the debug and release directories (it's important to set them to ./ and not just to .; at least on Windows). Then, using QMAKE_EXTRA_TARGETS, we redefine the target first 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 add

CONFIG -= debug_and_release

However, 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 and release targets to avoid "no rule to make target..." errors. For example:

release.target = release
release-clean.target = release-clean
release-install.target = release-install
[...]
debug.target = debug
debug-clean.target = debug-clean
debug-install.target = debug-install
[...]
QMAKE_EXTRA_TARGETS += release release-clean release-install [...]
QMAKE_EXTRA_TARGETS += debug debug-clean debug-install [...]
淡淡的优雅 2024-09-23 09:53:58

您可以使用 SUBDIRS 配置来运行多个不同的目标,甚至可以从同一个 makefile 运行。这可能特别适合您的额外目标,因为子目录配置可以在 makefile 中指定要运行的特定目标(请参阅 未记录的 qmake 了解详细信息)。在这种情况下,我会将所有“常规”构建命令放入一个 .pro 文件中,将外部构建命令放入另一个文件中,并使用一个子目录 .pro 文件来构建所有这些命令。我还没有测试过类似的东西,但它应该有效。

常规.pro:

SOURCES += main.cpp
TARGET = regular.exe

外部.pro:

batch_runner.commands = my_batch_file.bat
QMAKE_EXTRA_TARGETS   += batch_runner

other_runner.commands = other_batch_file.bat
QMAKE_EXTRA_TARGETS   += other_runner

do_it_all.pro:

TEMPLATE = subdirs
CONFIG += ordered

regular.file = regular.pro
SUBDIRS += regular

batch.file = external.pro
batch.target = batch_runner
SUBDIRS += batch

other.file = external.pro
other.target = other_runner
SUBDIRS += other

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:

SOURCES += main.cpp
TARGET = regular.exe

external.pro:

batch_runner.commands = my_batch_file.bat
QMAKE_EXTRA_TARGETS   += batch_runner

other_runner.commands = other_batch_file.bat
QMAKE_EXTRA_TARGETS   += other_runner

do_it_all.pro:

TEMPLATE = subdirs
CONFIG += ordered

regular.file = regular.pro
SUBDIRS += regular

batch.file = external.pro
batch.target = batch_runner
SUBDIRS += batch

other.file = external.pro
other.target = other_runner
SUBDIRS += other
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文