cmake_format
cmake_format copied to clipboard
Automatically remove header files from add_library / add_executable SOURCES
In most cases, there is no need to add header files to your source list in add_library
and add_executable
SOURCES
list.
I would love a feature that could automatically remove them.
Great idea! Though as a matter of project preference there is actually a reason. For some IDE build systems (visual studio, code blocks, qt creator IIRC) the headers won't show up in the file explorer if they are not listed as sources for a target.
I could help with this if pointed in a good direction to start.
Here's a rough sketch of how I would implement it:
- First add a flag to
parser.TreeNode
foris_file_list
... set to try by parsers or optionally by a tag like# cmf:files
. Still thinking about the whole tagging thing so maybe I'm not 100% on what I would do for that. The fundamental issue here is for statements likeset()
which are totally unstructured and whether or not their content is a file list will have to be inferred. Perhaps by "every element in the list matches a "^\S+.\S{0,3}+$" pattern or something. - Second, add a configuration option
remove_headers_from_filelists
toconfiguration.py
- Third, in
formatter.PargGroupNode
underlock()
, do the actual filtering if the nodeis_file_list
and the config option istrue
.
You're welcome to take a stab at it, but it's a pretty straight forward ask. Honestly the only reason I haven't done it yet is because I'm still noodling the whole tagging and is_file_list
idea. It get's a bit tricky and edge-casey as I learned while implementing autosort
.
Edit: this comment is as much a note-to-self as it is for you :wink:
By the way, half of my team uses vim/emacs, and another half IDE.
We compromised on enumerating headers in separate call to
target_sources(${PROJECT_NAME} PRIVATE ...)
.
@amerlyq that's a pretty good compromise. Sounds like a handy warning to output from a linter:
WXXX: headers should be enumerated in a separate call to target_sources
.
Also, seems like it is the kind of thing cmake-format could do for you in some cases. I like the idea.