如果指定的文件被更改,如何使 CMake 目标执行? [英] How to make CMake target executed whether specified file was changed?

查看:26
本文介绍了如果指定的文件被更改,如何使 CMake 目标执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 C++ 项目中使用 ANTLR.我为指定语法创建了一个运行 ANTLR 生成器的目标,并使 main prjct 依赖于它.

I'm trying to use ANTLR in my C++ project. I made a target for running ANTLR generator for specified grammar and made main prjct dependent from it.

ADD_CUSTOM_TARGET(GenerateParser
    COMMAND ${ANTLR_COMMAND} ${PROJECT_SOURCE_DIR}/src/MyGrammar.g 
                             -o ${PROJECT_SOURCE_DIR}/src/MyGrammar
)

ADD_LIBRARY(MainProject ${LIBRARY_TYPE} ${TARGET_SOURCES} ${TARGET_OPTIONS})
ADD_DEPENDENCIES(MainProject GenerateParser)

问题是每次我构建项目时ANTLR生成器都在运行并且消耗了足够的时间.我怎样才能让它只在我的语法发生变化时运行?或者有可能让 ANTLR 以某种方式只为过时的语法生成解析器.

The problem is that ANTLR generator running every time I build project and consumes enough time. How can I make it run only whether my grammar has been changed? Or may be it is possible to make ANTLR somehow generate parser only for out of date grammar.

推荐答案

add_custom_command will 解决这个问题,前提是你正确构造了对它的调用.

add_custom_command will do the trick, if you construct the call to it correctly.

这样的事情应该可以工作:

Something like this should work:

ADD_CUSTOM_COMMAND(OUTPUT ${PROJECT_SOURCE_DIR}/src/MyGrammar
  COMMAND ${ANTLR_COMMAND} ${PROJECT_SOURCE_DIR}/src/MyGrammar.g
    -o ${PROJECT_SOURCE_DIR}/src/MyGrammar
  DEPENDS ${PROJECT_SOURCE_DIR}/src/MyGrammar.g
)

ADD_CUSTOM_TARGET(GenerateParser ALL
   DEPENDS ${PROJECT_SOURCE_DIR}/src/MyGrammar
)

ADD_LIBRARY(MainProject ${LIBRARY_TYPE} ${TARGET_SOURCES} ${TARGET_OPTIONS})
ADD_DEPENDENCIES(MainProject GenerateParser)

在这里,自定义目标每次都会构建",但它唯一要做的就是构建它所依赖的输出的自定义命令,但当且仅当自定义命令的输出在相关方面过时到其 DEPENDS 文件.

Here, the custom target will "build" every time, but the only thing it will do is build the custom command on whose output it depends, but if and only if the output of the custom command is out of date with respect to its DEPENDS file(s).

这篇关于如果指定的文件被更改,如何使 CMake 目标执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆