I have a huge legacy code project that I want to clean up a little. I know for a fact a couple of files are not actually being used. The project is based on CMake. But there are more than 15 targets with lots of dynamic variables containing source file paths, so reading the CMakeLists.txt manually is tricky.
So can I produce a list of unused files automatically after a CMake build?
We should be able to get a list of the used .cpp source files after the build, and I bet we can deduct this from an e.g. find ... command.
Note: here I refer to unused files as source files that aren't compiled into a target. Ideally I would also want the unused .h header files, but I bet that's outside the CMake scope.
Related
I am trying to use MIDL compiler with cmake and that's what I came to.
In the similar questions people sujjest to use add_custom_command, but I suppose it is outdated since simply adding the IDL file to the list of files does the trick.
However there are problem with this approach:
Output files are not added to the list of files, so the compiler doesn't compile them.
1.1. Adding files manually doesn't work because cmake can't find them (since they don't exist)
Output files are created in the intermediate directory, where cmake doesn't really gives access to.
It changes default names of the output files in the project settings (e.g. %(Filename).h instead of %(Filename)_i.h for header files).
So my question is did somebody encountered these issues and if so how to fix them?
Using add_custom_command is undesirable because Visual Studio doesn't treat it as a normal IDL file, so you have to setup everything manually.
I'm trying to extend an open source project with my own source files, so that the executable being produced includes my code as well. Probably it's very easy, but somehow I'm overlooking it.
The build process is using cmake and the open source project contains a well written CMakeLists.txt. Instead of adding my source files in the directories of the open source project (and possibly changing CMakeLists.txt of the open source project), I prefer to separate my code in its own directory as follows:
src/opensourceproject/src/...
src/opensourceproject/include/...
...
src/myaddition/include/...
src/myaddition/src/...
Is there a way to wrap the build process and create a CMakeLists.txt that builds opensourceproject with my sources added to the opensource build? The reason why I want it, is that I'm more or less writing a plugin and I want my code to be present in the produced executable.
Overview:
I am wanting to allow visual studio 15 to automatically add an existing source file after I generate them.
Full explanation:
I am currently generating a lot of new .h and .cpp files with other tools outside of the IDE using bat scripts and such but needing to use them within the IDE to integrate with the project. As such I would be great if there was a a way to allow VS to add these source files automatically after they are generated without having to add them manually.
The files are always generated into the same given folder path if that helps.
You could generate the project with a CMake script. When you run your batch file, you could have it re-invoke CMake to update the project with the new sources. If you are generating files, especially if you can't predict the names in advance, you will likely want to use the CMake's file(GLOB ...) command.
Suppose I have a.cpp as a source and I add it to a cmake target:
add_executable(MY_TARG a.cpp)
If a.cpp includes a.h it will be added as a dependency for the target and when I change the header, everything will get rebuilt properly, but a.h won't show up in the list of sources for my project (in visual studio for instance).
Is there a way to add it there?
And can I distinguish between system headers (like <vector>) and headers from a folder from the same directory structure? I might wish to add only headers included with quotes and not with brackets ("header.h" and not <header.h>)
I am looking for something automatic - I already have a lot of CMakeLists.txt files with only .cpp files listed there and going through all of them seems impractical.
It's fairly simple - if you want a file to be listed as part of a project in an IDE, list it as part of the target in CMake:
add_executable(MY_TARG a.cpp a.h)
CMake knows enough to recognize it as a header (and thus not attempt to compile it), but it will list it in the generated IDE project.
EDIT
Based on the added information of looking for an automatic change to an existing system, I don't think that's easy. But it should still be doable. Compilers usually have a way of listing headers included by a file they compile (e.g. gcc has -MM). You should be able to build a one-time conversion script based using this output and your favourite text-processing language.
I am converting a C++ project created using Visual Studio 2005 to CMake and have stumbled upon a bit of a problem with resource files that are included in the project.
The project includes a .rc file, a bunch of .ico files and a .rc2 file.
The regular .rc file works fine in the generated project and uses the resource compiler. The .ico and .rc2 files however are causing problems when they are just being included, because in the generated project Visual Studio attempts to compile them using the C/C++ compiler.
I assume that these files are included by the .rc file, so it would probably work to just not include them in the CMakeLists.txt file, but since it is obviously possible to list them in the project (they are visible in the original project) I would like to do so, so that the user of the generated project can see that these files are being used.
What is the correct way to handle these extra VS resource files in CMake?
Try to set_source_files_properties(your.ico your.rc2 PROPERTIES LANGUAGE RC).
By default it shouldn't do anything with those files. The source file property LANGUAGE should be empty and thus the action for the file should be checked by the file type. Which shouldn't be anything since it's not something it should compile.
Check your CMakeLists.txt that is doesn't contain a set_source_files_properties command that would mess with that property.
If you want to do something with the files, here are two ways to do things:
With add_custom_target you can add them and run custom commands for them when you build the project. Granted that the files have changed.
With configure_file you can easily copy them to a build directory if needed. With the COPYONLY flag.