I'm working with ANTLR and want to generate the parser files during the generation process and only want to generate them if they are not only there or are out of date. This works and will only add the generation to the build if necessary:
add_custom_command(
OUTPUT(ExpressionsParser.cpp)
COMMAND java -jar ${ANTLR} -Dlanguage=Cpp -o ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/Expressions.g4
)
function(must_generate)
message(">>> Start must_generate <<<")
if (
${CMAKE_CURRENT_SOURCE_DIR}/Expressions.g4 IS_NEWER_THAN ${CMAKE_CURRENT_SOURCE_DIR}/ExpressionsParser.cpp)
message(">>> Adding the custome generation command")
add_library(
parser_src OBJECT ExpressionsParser.cpp
)
endif()
message(">>> End must_generate <<<")
endfunction()
must_generate()
However, what I really want is to determine whether the files need to be generated when I execute cmake --build build
. The above solution only works if I rerun the configuration. Is there a way to make the decision during build?