title: Generate parser files conditionally in cmake build
body: <p>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:</p>
  <pre><code>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(&quot;&gt;&gt;&gt; Start must_generate &lt;&lt;&lt;&quot;)
    if (
      ${CMAKE_CURRENT_SOURCE_DIR}/Expressions.g4 IS_NEWER_THAN ${CMAKE_CURRENT_SOURCE_DIR}/ExpressionsParser.cpp)
        message(&quot;&gt;&gt;&gt; Adding the custome generation command&quot;)
        add_library(
          parser_src OBJECT ExpressionsParser.cpp
        )
    endif()
    message(&quot;&gt;&gt;&gt; End must_generate &lt;&lt;&lt;&quot;)
  endfunction()
  
  must_generate()
  </code></pre>
  <p>However, what I really want is to determine whether the files need to be generated when I execute <code>cmake --build build</code>. The above solution only works if I rerun the configuration. Is there a way to make the decision during build?</p>