I have a list of files that get generated during the CMake build process. I want to compile these files using "add_library" afterward, but I won't know which files get generated until after they get generated. Is there anyway to build this into a CMake script?
相关问题
- Avoid cmake to add the flags -search_paths_first a
- CMakeList file to generate LLVM bitcode file from
- How to fix NDK build error for HelloCardboard samp
- Linking against GLEW with CMake
- Linking libavcodec in Cmake, find_library won'
相关文章
- Target requires the language dialect “CXX17” (with
- How do I tell cmake not to create a console window
- Do something for all targets
- CMake: Replace compile flags of an INTERFACE targe
- Generate Web Assembly from CMake Project with a Sp
- Integrate ITK (Insight Toolkit) into own project
- Is there a way to disallow “experimental” C++17 in
- CMake properties and expanding generator expressio
If you don't know the name of the files that will be generated, you can "glob" the folders where they reside.
file( GLOB_RECURSE MY_SRC dest_folder/*.cpp )
add_library( libname SHARED ${MY_SRC} )
Now I'm not sure what triggers the generation of these files. The "globbing" will happen only when you manually run cmake: it will not be able to detect automatically that new files are present.
Well, I think it is possible, so I'll share what I've done. My problem was that I had to compile several CORBA idls to use as part of a project's source and I didn't want to manually list every file. I thought it would be better to find the files. So I did it like this:
The GENERATED properties is useful in case one of my idl compilation outputs (*C.cpp, *C.h, *S.cpp and *S.h) is not created, so that the build command doesn't complain that the file doesn't exist.
Treat this as a non-answer, just more info:
I recently had to do something for one case where I had a .cpp file that was auto-generated, but I could not figure out how to get CMake to construct the Visual Studio project file that would then compile it. I had to resort to something quite stinky: I had to
#include <the_generated.cpp>
file from another file that resided under the${CMAKE_CURRENT_SOURCE}
directory. That won't help you much in your case because I suspect you have several .cpp files, so this approach is not scalable.Also, I found that the
GENERATED
source file property, when added to the file, did not help at all.I consider this condition either a bug in Visual Studio (in my case this was VS2008 SP1), or in how CMake generates the .vcproj files, or both.
Well, it is possible to do so with two CMake scripts using CMake's ExternalProject feature.
The simple solution
In the main CMake script you need to add a custom target that generates the source files as follows and a reference to the 2nd (external) CMake project:
Now you can do file globbing in the 2nd (external) CMake script and link all files found into a static library:
In this simple solution your files are generated and built every time you run the build step even if nothing changed. If you want to avoid this you can add a stamp file to your custom target like in the following improved solution:
The stamp file solution
If a change in the input files for your code generator does not necessarily result in a change of all generated files you can improve the solution further by using CMake's
copy_if_different
command in the external project like in the following advanced solution:The advanced solution
In this solution all generated files are copied to another location (
${CMAKE_CURRENT_BINARY_DIR}/generated
) if they have changed or have been added and are built from there. This approach results in a build of changed files only (but requires a clean when files have been removed).