[CMake] Technique for generating projects that depend on generated sources

David.Karr at L-3COM.COM David.Karr at L-3COM.COM
Sun Mar 1 23:19:26 EST 2009


From: Eric Noulard <eric.noulard at gmail.com>
>> Can you run the source generator at cmake time ?
>
> Yes you are right and I miss that,
> if the generator is launched at CMake time it may help a lot.
> Moreover I would say that the "source file list" generated by the
> generator may be some kind of CMake file, let's say
>
> generated_source.cmake
>
> then one may
>
> INCLUDE(generated_source.cmake OPTIONAL) in the CMakeLists.txt
>[...]
> Thus you would have the usual CMake + build step with an extra
> CMake run triggered by the build step itself.

I have a similar INCLUDE (but not OPTIONAL) in my largest CMake project
due to the requests of other developers.  They were accustomed to adding
new files directly in the Visual Studio IDE and some of them resisted
the idea that they would even have to look at a CMakeLists.txt file let
alone add the names of their new files there, simple though it might be.

The compromise we finally reached was that in each directory where
there's an executable or library to be built, CMakeLists.txt uses
EXEC_PROGRAM to run a program that searches the directory for all source
files and writes a temporary file containing a CMake command that
defines the variable LOCAL_SOURCE_FILES to be that list of files.  Then
CMakeLists.txt does a similar thing to define LOCAL_HEADER_FILES to be
the list of all header files in that directory.  There are similar
tricks for files in subdirectories, though each subdirectory ends up in
its own pair of source groups.  Then CMakeLists.txt does an INCLUDE on
each temporary file, hence defining the lists of files so that they can
be made into source groups.

The drawbacks to this approach have already been discussed, but two
obvious problems are: (1) CMake has no way to know when it needs to
regenerate the project files, so you have to remember to run it whenever
you add, remove, or move source files, and (2) this technique forces a
rigid structure onto your source code tree that not everyone would like.
But in this case it has kept the programmers happy for several years, so
we've continued to use it.

Your situation is different.  But the part that is similar is that I
think you may be able to find a way to run your generator AND use its
output (via INCLUDE) in the same run of CMake, so that you only have to
run CMake once to generate everything.  (You may also suffer some of the
same drawbacks, but I suspect that some "ugliness" of that sort is
inevitable when you have a generator such as yours.)

David Karr



More information about the CMake mailing list