cmake_format icon indicating copy to clipboard operation
cmake_format copied to clipboard

Automatically remove header files from add_library / add_executable SOURCES

Open peter-moran opened this issue 4 years ago • 5 comments

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.

peter-moran avatar Aug 26 '19 19:08 peter-moran

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.

cheshirekow avatar Aug 26 '19 19:08 cheshirekow

I could help with this if pointed in a good direction to start.

peter-moran avatar Aug 28 '19 15:08 peter-moran

Here's a rough sketch of how I would implement it:

  1. First add a flag to parser.TreeNode for is_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 like set() 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.
  2. Second, add a configuration option remove_headers_from_filelists to configuration.py
  3. Third, in formatter.PargGroupNode under lock(), do the actual filtering if the node is_file_list and the config option is true.

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:

cheshirekow avatar Oct 17 '19 22:10 cheshirekow

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 avatar Oct 26 '19 03:10 amerlyq

@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.

cheshirekow avatar Oct 26 '19 03:10 cheshirekow