1

I'm trying to incorporate PC-Lint Plus into my cmake project, mainly per PC-Lint needs a list of all include paths for the files to be scanned. How to get a list of all include paths recursively needed by a target in CMake?.

PC-Lint is getting invoked, but ends up dying because not all the includes are in the PC-Lint invocation.

I dumped the above link into a Lint.cmake, and included that in my top level CMake file. In my project file, I added:

if(COMMAND add_pc_lint)
add_pc_lint(moded ${SRC_FILES})
endif(COMMAND add_pc_lint)

So I'd expect the lines:

function(add_pc_lint target)
    get_directory_property(lint_include_directories INCLUDE_DIRECTORIES)
    # let's get those elephants across the alps
    # prepend each include directory with "-i"; also quotes the directory
    set(lint_include_directories_transformed)
    foreach(include_dir ${lint_include_directories})
        list(APPEND lint_include_directories_transformed -i"${include_dir}")
    endforeach(include_dir)

to pull the include directory list out of INCLUDE_DIRECTORIES.

If I add:

message(STATUS "********************************************")
message(STATUS "Include directories - ${lint_include_directories}")
message(STATUS "********************************************")
message(STATUS "********************************************")
message(STATUS "INCLUDE_DIRECTORIES - ${INCLUDE_DIRECTORIES}")
message(STATUS "********************************************")

directly above the foreach loop, lint_include_directories and INCLUDE_DIRECTORIES both appear to be empty.

What do I need to do to get the full list of include directories, including from target_link_libraries and target_include_directories, from CMake to give to PC-Lint Plus?

Kevin
  • 16,549
  • 8
  • 60
  • 74
bizaff
  • 169
  • 7
  • It sounds like when you include `Lint.cmake` from your top-level file, you lose the directory scope containing your `INCLUDE_DIRECTORIES` directory property. So this property is empty in the new directory scope. – Kevin Feb 27 '20 at 21:50
  • @squareskittles But that casts doubt on the whole idea that this lint file can work, which it appears to be somewhat standard.. I added those message() calls in the project CMakeLists.txt and they just show the single target_include_directories() "main" directory, but none of the include directories implied by libraries in target_link_libraries(). – bizaff Feb 27 '20 at 21:59
  • My earlier comment was incorrect. After taking a closer look, I found what is probably the issue (please see my response). Hope this helps! – Kevin Feb 28 '20 at 13:36

1 Answers1

0

EDIT: I have updated the CMake PC-Lint script to account for the issue described below in my response here.


The documentation on PC-Lint usage appears to be catered for old CMake, not the target-based modern CMake. The add_pc_lint function grabs the include directories and compile definitions from CMake directory properties. These directories properties will be populated if your project has calls such as:

  • include_directories()
  • add_definitions()

Without these commands (which really should not be used in modern CMake), the add_pc_lint function likely won't find all the include directories and compile definitions you expect.

In modern CMake, the include directories and compile definitions are stored as properties of a target instead, when you use commands such as these:

  • target_include_directories()
  • target_compile_definitions()

In this case, you have to modify the add_pc_lint function to grab the properties from the target, not the directory. So, change the top of the add_pc_lint function from this:

function(add_pc_lint target)
    get_directory_property(lint_include_directories INCLUDE_DIRECTORIES)
    get_directory_property(lint_defines COMPILE_DEFINITIONS)

to this:

function(add_pc_lint target)
    get_target_property(lint_include_directories ${target} INCLUDE_DIRECTORIES)
    get_target_property(lint_defines ${target} COMPILE_DEFINITIONS)
Kevin
  • 16,549
  • 8
  • 60
  • 74
  • Ok, this is closer.. I see the one directory specified by a "target_include_directories" line in the project's CMakeLists.txt - but where do I get the full list of directories implied by pulling in "target_link_libraries"? This project compiles, so it can clearly see the list of directories, but I don't know how to get that list to give to lint. If I look in the generated Makefile, CXX_INCLUDES contains the list of includes - how do I get that out of CMake? – bizaff Feb 28 '20 at 16:45
  • @bizaff You likely have to call `add_pc_lint` on each target that you link to in `target_link_libraries()`. It becomes even more complicated, as some include directories may only be populated in the `INTERFACE_INCLUDE_DIRECTORIES` property, instead of the `INCLUDE_DIRECTORIES` property. So it is probably best to check both properties. – Kevin Feb 28 '20 at 17:03