I have a grammar file grammar.peg
that needs to be turned into grammar.inc
where the grammar is wrapped into a C++ string of the form
namespace mynamespace
{
constexpr const char* headerSectionGrammar = R"(
here comes the grammar.peg contents
)";
}
At the moment, I do this during the CMake configuration with something like this
file(READ "grammar.peg" GRAMMAR_PEG)
string(PREPEND GRAMMAR_PEG "namespace mynamespae\n{\nconstexpr const char* headerSectionGrammar = R\"(\n")
string(APPEND GRAMMAR_PEG "\n)\"\;\n}\n")
file(WRITE "grammar.inc" ${GRAMMAR_PEG})
which works fine. The only downside is that when I change something in grammar.peg
, I must not forget to re-run CMake to recreate grammar.inc
.
Question: Is there a way to make this dependent on changes in grammar.peg
so that it is re-run automatically while building the project? There are two critical aspects:
- There cannot be any additional thing in
grammar.peg
like a${var}
that gets replaced, because I'm using the peg file with a linter that expects a correct grammar file. Therefore, CMake'sconfigure_file
is afaik not an option - I don't want to use any OS-specific commands (like
awk
) because we compile this under Linux, Mac OS, and Windows. When I rely on CMake's capabilities only, there much less trouble.
I've not used CMake generator expressions much, but they look promising. Maybe someone can give me a pointer in the right direction.