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)
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.
Adds a target with the given name that executes the given commands. The target has no output file and is always considered out of date even if the commands try to create a file with the name of the target. Use the add_custom_command() command to generate a file with dependencies.
This defines a command to generate specified OUTPUT file(s). A target created in the same directory ( CMakeLists.txt file) that specifies any output of the custom command as a source file is given a rule to generate the file using the command at build time.
A CMake-based buildsystem is organized as a set of high-level logical targets. Each target corresponds to an executable or library, or is a custom target containing custom commands.
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)
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With