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

夙愿已清 提交于 2019-12-19 19:22:11

问题


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?


回答1:


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.




回答2:


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})



回答3:


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



来源:https://stackoverflow.com/questions/21529449/cmake-read-and-compile-dynamically-generated-list-of-cpp-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!