Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cmake: read and compile dynamically-generated list of cpp files

Tags:

c++

cmake

idl

I have a custom tool that processes a given list of IDL files and produces a number of .cpp and .h files as output. I want to add those files to the list of things to compile in my CMakeLists, and also model the dependencies those files have on the IDL. To keep things simple, I will state that any change to any of the IDL files should trigger a regeneration of all cpp/h.

I have a custom command that takes care of running the generator tool and listing all the IDL files as dependencies.

My issue is getting the subsequent list of cpp/h files into cmake at build-time. It is not possible to infer from the name of the IDL files what cpp files will be generated. My generator tool will, however, output the list of generated files to a text file.

So my question is: how do I instruct cmake to "read from this text file and add the contents as extra source and header files to be compiled", also bearing in mind that the said text file only exists during a certain point of the build?

like image 473
gimmeamilk Avatar asked Feb 03 '14 14:02

gimmeamilk


3 Answers

CMake needs to be able to infer the names of all .cpp files participating in the build at configure time. It is not possible to add files afterwards without re-running CMake.

One possible approach would be to use a two-phase CMake build: Instead of building the generated source files directly from your main project, you create a separate CMake project for building just the generated sources.

Then in your main CMake project you add a custom target that runs after the code generation and invokes CMake to both configure and build the generated files project.

The disadvantage here is that the generated files no longer appear as part of the main project. Also some trickery is required if you don't want to rebuild the generated sources every time - custom targets are always considered out-of-date, so you might want to use a script here that only runs CMake on the subproject if the generated files changed.

like image 182
ComicSansMS Avatar answered Nov 20 '22 06:11

ComicSansMS


This is a few years late but this works just fine:

#run whatever tool that generates the cpp files
execute_process(COMMAND "./your_tool.sh")

#read files from files.txt and make a cmake 'list' out of them
file(READ "files.txt" SOURCES)

#found this technique to build the cmake list here:
#http://public.kitware.com/pipermail/cmake/2007-May/014236.html
#maybe there is a better way...
STRING(REGEX REPLACE ";" "\\\\;" SOURCES "${SOURCES}")
STRING(REGEX REPLACE "\n" ";" SOURCES "${SOURCES}")

#at this point you have your source files inside ${SOURCES}
#build a static library...?
add_library(mylib STATIC ${SOURCES})
like image 21
skelliam Avatar answered Nov 20 '22 05:11

skelliam


There is a function that build the list directly from file:

file(STRINGS <filename> <variable> [<options>...])

source: https://cmake.org/cmake/help/v3.11/command/file.html

like image 2
chepseskaf Avatar answered Nov 20 '22 06:11

chepseskaf