生成代码的 qmake 规则
我意识到我之前的问题对规则和依赖关系有点困惑。以下 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能需要分两遍执行此操作。
在 qmake 文件中,添加以下行:
,在 code_generator 脚本的末尾,将源添加到 generated.pri 文件(以 bash 为例,但几乎所有语言的想法都是相同的):
然后 当你运行 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:
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):
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.
我刚才遇到了同样的问题,但是对于仅生成一个文件的更简单的用例。为此,我找到了一种更简单的方法来实现这一点,即使用 GENERATED_SOURCES 而不是 SOURCES:
也许可以将其推入 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:
Probably one could push that into a qmake loop and generate the proper targets for multiple files as well.
我提出了一个我认为稳健且通用的解决方案,它依赖于
qmake
生成 GNU Makefile。假设我们在名为
Makefile.pri
的文件中获得了所有额外的SOURCES
和HEADERS
分配,该文件是通过执行脚本生成的generate_Makefile.pri.sh
还生成Makefile.pri
中提到的源代码和/或标头。我们希望这个文件由构建系统本身自动可靠地生成,因此要包含在
.pro
文件中,以便qmake
可以考虑其内容并生成具有适当依赖关系的 Makefile。这就是我们必须放入
.pro
文件中的内容。这样:
Makefile.pri
,那么它就会被创建;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
andHEADERS
assignments in a file namedMakefile.pri
, which gets generated by executing the scriptgenerate_Makefile.pri.sh
that also generates the sources and/or headers mentioned inMakefile.pri
.We want this file automatically and reliably generated by the build system itself, so to be included in the
.pro
file, so thatqmake
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.This way:
Makefile.pri
, then it gets created;Makefile.pri
gets created or updated, thenqmake
runs again and takes into account its content, regenerating theMakefile
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.