0

In a cmake project on ubuntu, I am trying to use CPPClean to find unused/unnecessary headers for all my targets. However I'm lost how to integrate this in my project.

The usage of CPPClean is

cppclean --include-path=directory1 --include-path=directory2 <path>

I would like to have one target that I can build that does runs cppclean on every target that I mark. Something like:

add_library(foo_target
        src/foo.cpp     include/foo.hpp)

target_include_directories(foo_target PUBLIC include)

target_link_libraries(foo_target PUBLIC bar)

cppclean(foo_target) #this function adds the target to some kind of global list?

Then the build target that does the cppclean would have to do the following

For all targets that are tagged:

  • get all the include dirs of the target
  • get all the include dirs of the libs that are linked to
  • run the cppclean commmand (which we can assume is installed on the system) on the target main dir with the include dirs that we just determined

Any ideas if this is a descent way to do this? And how would I implement this last part? Especially how do I have one target execute a (unknown) number of commands?

Frank
  • 2,446
  • 7
  • 33
  • 67

1 Answers1

1

Any ideas if this is a descent way to do this?

Sure.

how would I implement this last part?

Well, just like you specified, you would run cppclean command on the target "main dir" with the include dirs from previous point.

Especially how do I have one target execute a (unknown) number of commands?

A "target" represents things to do. foo_target builds target, it does not do anything else. Define another target with add_custom_target().

function(cppclean name target)
   # get all the include dirs of the target
   get_target_properties....
   # get all the include dirs of the libs that are linked to
   recursively, get target_properties for each library target links to
   See appriopriate stackoverflow thread.
   #  (which we can assume is installed on the system)
   find_program(cppclean NAMES cppclean REQUIRED)
   # run the cppclean commmand on the target main dir with the include dirs that we just determined
   add_custom_target(${name}
      COMMAND ${cppclean} args constructed from above
   )
endfunction()

how to use cppclean in cmake

You can also go with generating compile_commands.json and from it extracting the include paths for every executable.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • It seems like there is a new target created every time I call the function. Or are the commands added to the same target 'name' every time I call the function? If not, how would I run cppclean on all the targets I added, in one command? – Frank Dec 15 '21 at 06:37
  • `It seems like there is a new target created every time I call the function` Yes `Or are the commands added to the same target 'name' every time I call the function?` no `If not, how would I run cppclean on all the targets I added, in one command?` Add all the commands to one target. (??) Or do `add_custom_target(supertarget) add_dependencies(supertarget this that)` – KamilCuk Dec 15 '21 at 08:15