We’re using CMake to generate solution files for Visual Studio 2008. In addition to regular source projects that build libraries and executables, I’d like to have a project containing just configuration files. Those files are used to configure the behavior of the project at runtime. They are not needed to build the project, but having them included in the solution allows for convenient editing of those files. I was able to accomplish this using add_custom_target like so:
file(GLOB ini_files ${PROJECT_SOURCE_DIR}/../config/ini/*)
source_group(ini FILES ${ini_files})
file(GLOB xml_files ${PROJECT_SOURCE_DIR}/../config/xml/*)
source_group(xml FILES ${xml_files})
add_custom_target(config SOURCES ${ini_files} ${xml_files})
This gives me a “config” project that contains the configuration files as subfolders. However, there’s also a link named “config” to some non-existent file. I assume this is supposed to be the link to the target created by this rule. As there’s nothing to be built, this will remain a dead link. Now my questions are:
- Is add_custom_target the right way to accomplish what I want?
If not, what would be a better solution? - If it is the right way, how can I can I get rid of the link to the target’s name? Also, I’d like to remove the “CMake Rules” folder to make the config project as clean as possible.
Visual Studio projects are essentially made to build targets depending on a set of source files.
Having a project that exists just to list a set of static files is not advised, since as soon as you add a custom target, CMake will blindly try to create an output for the project, in order to remain compatible with other backend systems.
Just imagine if CMake created a Unix Makefile with no actual target, that wouldn’t even be Makefile at all, would it ?
The proper way to do this is to actually add the files to another existing project. Technically speaking, if you are building an executable, and this executable reads configuration files at runtime, these files are legitimately a part of the project.
Also, I can’t recommend using file globing to list source files or configuration files, because if you add files in the folders, CMake will have no way to know that new files have been added, and the Visual Studio project will not be regenerated to include those files. Instead, you should list every file in your CMakeLists.txt files, so that adding the new file there will trigger cmake files regeneration.
For the last point, if you really need “clean” projects, then CMake is not the tool you need, because everything it generates is useful to maintain a coherent build process.