For a given target it’s pretty easy to get a list of all the source files.
However, I’m not able to easily get all of the -I folders.
I thought it might be the contents of the INCLUDE_DIRECTORIES property, but that doesn’t seem to have the include folders that come from packages we’ve added using find_package() and target_link_libraries(...package)
I checked the INCLUDE_DIRECTORIES property using message()
I added logic to the end of my cmakefile that used get_property() to get the targets INCLUDE_DIRECTORIES value and then print it using message()
The INCLUDE_DIRECTORIES property at configure time is just the set added explicitly via target_include_directories (and maybe include_directories, though that may just be stored on the directory and not gathered until generate time). At generate time, the set of linked libraries is finally known and usage requirements from the targets that are linked may be looked up and expanded (with, e.g., platform detection genexes expanded too). It’s not something you can query at configure time, so your add_custom_command is one way to do it properly.
You might be able to use CMAKE_EXPORT_COMPILE_COMMANDS to generate compile_commands.json and have cppcheck use that instead.