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

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

问题描述

我想在我的C ++项目中使用ANTLR。我为运行ANTLR生成器的指定语法制定了目标,并使主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

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

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天全站免登陆