Hum, it’s a little difficult to explain, especially in the title…
Let’s say I have the following files: main.cpp
, domain.cpp
, domain.hpp
, generated.hpp
and generator.cpp
. domain.cpp
includes domain.hpp
, which in turn includes generated.hpp
. I want to build two executables:
generator
, consisting ofgenerator.cpp
anddomain.cpp
main
, consisting ofmain.cpp
anddomain.cpp
Here’s the twist. Initially, generated.hpp
is empty. Before building main
, I want to build generator
(if needed), and always run it before building main
. generator
calculates stuff related to domains.cpp
, and rewrites generated.hpp
– but only if its content would be different. Thus, running generator
for the first time will rewrite the empty generated.hpp
; running it again immediately afterwards will not touch generated.hpp
; and running generator
after making changes to the domain.?pp
files may or may not rewrite generated.hpp
.
Thus, there is a cycle because generator
depends on its output, but it will be traversed only once.
Moreover, generator
has to run before ninja/make/whatever starts examining the dependencies of main
.
Can it be achieved in plain cmake
, without playing tricks with scripts?
It would be nice if the generated generated.hpp
goes in the build area.
The files are in C++17, thus solutions that involve __has_include
are acceptable. But I doubt it can help.
PS In case you wonder about the context, it’s for my YOMM2 open multi-methods library. generator
calculates offsets in v-tables, and writes them as constexpr
offsets. If present, they are used to speed up method dispatch.