生成代码的 qmake 规则

发布于 2024-08-17 01:07:38 字数 552 浏览 3 评论 0原文

我意识到我之前的问题对规则和依赖关系有点困惑。以下 .pro 文件生成一个 makefile,如果 qmake 运行时“生成”目录中的源文件存在,则该 makefile 可以正常工作。

idl.target   = generated/qmtest.h
idl.commands = code_generator 
idl.config   = no_link
idl.depends  = $$SOURCES $$HEADERS $$FORMS

TEMPLATE       = app
INCLUDEPATH    += generated
SOURCES        += generated/*.cpp
PRE_TARGETDEPS += generated/qmtest.h
QMAKE_EXTRA_UNIX_TARGETS += idl

但是当qmake运行时,它只生成一个makefile,以及PRE_TARGETDEPS & QMAKE_EXTRA_UNIX_TARGETS 对我没有帮助。如何让 qmake 生成一个 makefile,将 generated/ 的内容添加到 SOURCES?

I realized my earlier question was a little confused about the rules and dependencies. The following .pro file generates a makefile which works correctly IF the source files in the directory 'generated' exist at the time qmake runs.

idl.target   = generated/qmtest.h
idl.commands = code_generator 
idl.config   = no_link
idl.depends  = $SOURCES $HEADERS $FORMS

TEMPLATE       = app
INCLUDEPATH    += generated
SOURCES        += generated/*.cpp
PRE_TARGETDEPS += generated/qmtest.h
QMAKE_EXTRA_UNIX_TARGETS += idl

But when qmake runs, its only generating a makefile, and PRE_TARGETDEPS & QMAKE_EXTRA_UNIX_TARGETS don't help me. How can I get qmake to generate a makefile which will add the contents of generated/ to SOURCES?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

如歌彻婉言 2024-08-24 01:07:38

您可能需要分两遍执行此操作。

在 qmake 文件中,添加以下行:

include( generated/generated.pri )

,在 code_generator 脚本的末尾,将源添加到 generated.pri 文件(以 bash 为例,但几乎所有语言的想法都是相同的):

rm generated/generated.pri
for file in $( ls generated/*.cpp ); do
    echo "SOURCES += ${file}" >> generated/generated.pri
done

然后 当你运行 qmake 文件时, generated/ generated.pri 可能是空的。当您运行 make 时,它​​将填充 generated.pri 文件。第二次,它将重新创建 make 文件(因为源 .pri 文件已更改),然后再次编译。您也许可以摆弄其他命令来为您完成第二阶段。

You may need to do this in two passes.

In your qmake file, add the following line:

include( generated/generated.pri )

Then, at the end of your code_generator script, add the sources to the generated.pri file (using bash for the example, but the idea is the same for almost all languages):

rm generated/generated.pri
for file in $( ls generated/*.cpp ); do
    echo "SOURCES += ${file}" >> generated/generated.pri
done

The first time you run the qmake file, generated/generated.pri will presumably be empty. When you run make, it will populate the generated.pri file. The second time, it will recreate the make file (as a source .pri file changed), then compile again. You might be able to fiddle around with other commands which would do the second stage for you.

一生独一 2024-08-24 01:07:38

我刚才遇到了同样的问题,但是对于仅生成一个文件的更简单的用例。为此,我找到了一种更简单的方法来实现这一点,即使用 GENERATED_SOURCES 而不是 SOURCES:

dummyfile.target = dummy.cpp
dummyfile.commands = touch $dummyfile.target
QMAKE_EXTRA_TARGETS += dummyfile
GENERATED_SOURCES += $dummyfile.target

也许可以将其推入 qmake 循环并为多个文件生成正确的目标。

I had the same issue just now, but for a simpler usecase of just a single generated file. For that, I found a much simpler way to achieve this by using GENERATED_SOURCES instead of SOURCES:

dummyfile.target = dummy.cpp
dummyfile.commands = touch $dummyfile.target
QMAKE_EXTRA_TARGETS += dummyfile
GENERATED_SOURCES += $dummyfile.target

Probably one could push that into a qmake loop and generate the proper targets for multiple files as well.

年华零落成诗 2024-08-24 01:07:38

我提出了一个我认为稳健且通用的解决方案,它依赖于 qmake 生成 GNU Makefile。

假设我们在名为 Makefile.pri 的文件中获得了所有额外的 SOURCESHEADERS 分配,该文件是通过执行脚本 生成的generate_Makefile.pri.sh 还生成 Makefile.pri 中提到的源代码和/或标头。

我们希望这个文件由构建系统本身自动可靠地生成,因此要包含在 .pro 文件中,以便 qmake 可以考虑其内容并生成具有适当依赖关系的 Makefile。

这就是我们必须放入 .pro 文件中的内容。

Makefile_pri.target   = Makefile.pri
Makefile_pri.commands = generate_Makefile.pri.sh
Makefile_pri.depends  = FORCE

Makefile.target  = $MAKEFILE
Makefile.depends = Makefile_pri

include($Makefile_pri.target)

QMAKE_EXTRA_TARGETS = Makefile Makefile_pri

这样:

  1. 如果没有 Makefile.pri,那么它就会被创建;
  2. 如果 Makefile.pri 被创建或更新,则 qmake 再次运行并考虑其内容,重新生成 Makefile

如前所述,此 确实依赖于qmake生成GNU Makefile,但我相信同样的方法也可以适用于其他目标构建系统。

I've come up with a solution that I believe to be robust and general, that relies on qmake generating a GNU Makefile.

Say we've got all extra SOURCES and HEADERS assignments in a file named Makefile.pri, which gets generated by executing the script generate_Makefile.pri.sh that also generates the sources and/or headers mentioned in Makefile.pri.

We want this file automatically and reliably generated by the build system itself, so to be included in the .pro file, so that qmake can take into account its content and generate the Makefile with the proper dependencies.

This is then what we have to put in our .pro file.

Makefile_pri.target   = Makefile.pri
Makefile_pri.commands = generate_Makefile.pri.sh
Makefile_pri.depends  = FORCE

Makefile.target  = $MAKEFILE
Makefile.depends = Makefile_pri

include($Makefile_pri.target)

QMAKE_EXTRA_TARGETS = Makefile Makefile_pri

This way:

  1. if there's no Makefile.pri, then it gets created;
  2. If Makefile.pri gets created or updated, then qmake runs again and takes into account its content, regenerating the Makefile

As said, this does depend on qmake generating a GNU Makefile, but the same approach I believe could be adapted to work also with other target build systems.

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